Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we handle this inside isIp() function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have a point. I think this is reasonable to have this inside of isIP. Let me check.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isIP is a public API. It would be a breaking change to do it.

err = new ERR_INVALID_IP_ADDRESS(ip);
process.nextTick(connectErrorNT, self, err);
} else if (addressType !== 4 && addressType !== 6) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
Loading