Skip to content

Commit 2b8851c

Browse files
committed
net: validate custom lookup() output
This commit adds validation to the IP address returned by the net module's custom DNS lookup() function.
1 parent 0eca660 commit 2b8851c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/net.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,9 @@ function lookupAndConnect(self, options) {
10391039
// calls net.Socket.connect() on it (that's us). There are no event
10401040
// listeners registered yet so defer the error event to the next tick.
10411041
process.nextTick(connectErrorNT, self, err);
1042+
} else if (!isIP(ip)) {
1043+
err = new ERR_INVALID_IP_ADDRESS(ip);
1044+
process.nextTick(connectErrorNT, self, err);
10421045
} else if (addressType !== 4 && addressType !== 6) {
10431046
err = new ERR_INVALID_ADDRESS_FAMILY(addressType,
10441047
options.host,

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,14 @@ function check(addressType, cb) {
4141
check(4, function() {
4242
common.hasIPv6 && check(6);
4343
});
44+
45+
// Verify that bad lookup() IPs are handled.
46+
{
47+
net.connect({
48+
host: 'localhost',
49+
port: 80,
50+
lookup(host, dnsopts, cb) {
51+
cb(null, undefined, 4);
52+
}
53+
}).on('error', common.expectsError({ code: 'ERR_INVALID_IP_ADDRESS' }));
54+
}

0 commit comments

Comments
 (0)