Skip to content

Commit ad678b9

Browse files
committed
net: throw error when custom lookup resolves a non-string address
1 parent 772c609 commit ad678b9

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

lib/net.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const {
115115
},
116116
genericNodeError,
117117
} = require('internal/errors');
118-
const { isUint8Array } = require('internal/util/types');
118+
const { isStringObject, isUint8Array } = require('internal/util/types');
119119
const { queueMicrotask } = require('internal/process/task_queues');
120120
const {
121121
guessHandleType,
@@ -1416,7 +1416,7 @@ function lookupAndConnect(self, options) {
14161416
// calls net.Socket.connect() on it (that's us). There are no event
14171417
// listeners registered yet so defer the error event to the next tick.
14181418
process.nextTick(connectErrorNT, self, err);
1419-
} else if (!isIP(ip)) {
1419+
} else if ((typeof ip !== 'string' && !isStringObject(ip)) || !isIP(ip)) {
14201420
err = new ERR_INVALID_IP_ADDRESS(ip);
14211421
process.nextTick(connectErrorNT, self, err);
14221422
} else if (addressType !== 4 && addressType !== 6) {

test/parallel/test-net-dns-custom-lookup.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const common = require('../common');
33
const assert = require('assert');
44
const net = require('net');
5+
const { describe, it } = require('node:test');
56

67
function check(addressType, cb) {
78
const server = net.createServer(function(client) {
@@ -65,3 +66,44 @@ check(4, function() {
6566
}
6667
}).on('error', common.expectsError({ code: 'ERR_INVALID_IP_ADDRESS' }));
6768
}
69+
70+
const brokenCustomLookup = (_hostname, options, callback) => {
71+
// Incorrectly return an array of IPs instead of a string.
72+
callback(null, ['127.0.0.1'], options.family);
73+
};
74+
75+
describe('when family is ipv4', () => {
76+
it('net.connect() throws when lookup does not return a string', (t, done) => {
77+
const options = {
78+
host: 'example.com',
79+
port: 80,
80+
lookup: brokenCustomLookup,
81+
family: 4
82+
};
83+
84+
const socket = net.connect(options, common.mustNotCall());
85+
socket.on('error', (err) => {
86+
t.assert.strictEqual(err.code, 'ERR_INVALID_IP_ADDRESS');
87+
88+
done();
89+
});
90+
});
91+
});
92+
93+
describe('when family is ipv6', () => {
94+
it('net.connect() throws when lookup does not return a string', (t, done) => {
95+
const options = {
96+
host: 'example.com',
97+
port: 80,
98+
lookup: brokenCustomLookup,
99+
family: 6
100+
};
101+
102+
const socket = net.connect(options, common.mustNotCall());
103+
socket.on('error', (err) => {
104+
t.assert.strictEqual(err.code, 'ERR_INVALID_IP_ADDRESS');
105+
106+
done();
107+
});
108+
});
109+
});

0 commit comments

Comments
 (0)