Skip to content

Commit

Permalink
net: validate custom lookup() output
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cjihrig authored and addaleax committed Sep 22, 2020
1 parent 4661b88 commit 6b5d679
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,9 @@ 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)) {
err = new ERR_INVALID_IP_ADDRESS(ip);
process.nextTick(connectErrorNT, self, err);
} else if (addressType !== 4 && addressType !== 6) {
err = new ERR_INVALID_ADDRESS_FAMILY(addressType,
options.host,
Expand Down
11 changes: 11 additions & 0 deletions test/parallel/test-net-dns-custom-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,14 @@ function check(addressType, cb) {
check(4, function() {
common.hasIPv6 && check(6);
});

// Verify that bad lookup() IPs are handled.
{
net.connect({
host: 'localhost',
port: 80,
lookup(host, dnsopts, cb) {
cb(null, undefined, 4);
}
}).on('error', common.expectsError({ code: 'ERR_INVALID_IP_ADDRESS' }));
}

0 comments on commit 6b5d679

Please sign in to comment.