net: emit an error when custom lookup resolves to a non-string address

PR-URL: https://github.com/nodejs/node/pull/57192
Fixes: https://github.com/nodejs/node/issues/57112
Reviewed-By: Tim Perry <pimterry@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
Reviewed-By: Jason Zhang <xzha4350@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
Edy Silva 2025-03-01 17:43:41 -03:00 committed by GitHub
parent 60dff4208f
commit 71196c431f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 1 deletions

View File

@ -1416,7 +1416,7 @@ function lookupAndConnect(self, options) {
// calls net.Socket.connect() on it (that's us). There are no event
// listeners registered yet so defer the error event to the next tick.
process.nextTick(connectErrorNT, self, err);
} else if (!isIP(ip)) {
} else if ((typeof ip !== 'string') || !isIP(ip)) {
err = new ERR_INVALID_IP_ADDRESS(ip);
process.nextTick(connectErrorNT, self, err);
} else if (addressType !== 4 && addressType !== 6) {

View File

@ -0,0 +1,44 @@
import * as common from '../common/index.mjs';
import net from 'node:net';
import { describe, it } from 'node:test';
const brokenCustomLookup = (_hostname, options, callback) => {
// Incorrectly return an array of IPs instead of a string.
callback(null, ['127.0.0.1'], options.family);
};
describe('when family is ipv4', () => {
it('socket emits an error when lookup does not return a string', (t, done) => {
const options = {
host: 'example.com',
port: 80,
lookup: brokenCustomLookup,
family: 4
};
const socket = net.connect(options, common.mustNotCall());
socket.on('error', (err) => {
t.assert.strictEqual(err.code, 'ERR_INVALID_IP_ADDRESS');
done();
});
});
});
describe('when family is ipv6', () => {
it('socket emits an error when lookup does not return a string', (t, done) => {
const options = {
host: 'example.com',
port: 80,
lookup: brokenCustomLookup,
family: 6
};
const socket = net.connect(options, common.mustNotCall());
socket.on('error', (err) => {
t.assert.strictEqual(err.code, 'ERR_INVALID_IP_ADDRESS');
done();
});
});
});