Skip to content

Commit 6b5d679

Browse files
cjihrigaddaleax
authored andcommitted
net: validate custom lookup() output
This commit adds validation to the IP address returned by the net module's custom DNS lookup() function. PR-URL: #34813 Fixes: #34812 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Ricky Zhou <0x19951125@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 4661b88 commit 6b5d679

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/net.js

+3
Original file line numberDiff line numberDiff line change
@@ -1053,6 +1053,9 @@ function lookupAndConnect(self, options) {
10531053
// calls net.Socket.connect() on it (that's us). There are no event
10541054
// listeners registered yet so defer the error event to the next tick.
10551055
process.nextTick(connectErrorNT, self, err);
1056+
} else if (!isIP(ip)) {
1057+
err = new ERR_INVALID_IP_ADDRESS(ip);
1058+
process.nextTick(connectErrorNT, self, err);
10561059
} else if (addressType !== 4 && addressType !== 6) {
10571060
err = new ERR_INVALID_ADDRESS_FAMILY(addressType,
10581061
options.host,

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

+11
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)