Skip to content

Commit

Permalink
dns: throw a TypeError in lookupService with invalid port
Browse files Browse the repository at this point in the history
Previously, port was assumed to be a number and would cause an abort in
cares_wrap. This change throws a TypeError if port is not a number
before we actually hit C++.

Fixes: #4837
PR-URL: #4839
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Roman Klauke <romaaan.git@gmail.com>
Reviewed-By: Brian White <mscdex@mscdex.net>
  • Loading branch information
evanlucas authored and rvagg committed Feb 8, 2016
1 parent a1af6fc commit fa0457e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ exports.lookupService = function(host, port, callback) {
if (cares.isIP(host) === 0)
throw new TypeError('host needs to be a valid IP address');

if (typeof port !== 'number')
throw new TypeError(`port argument must be a number, got "${port}"`);

callback = makeAsync(callback);

var req = new GetNameInfoReqWrap();
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,19 @@ assert.doesNotThrow(function() {
hints: dns.ADDRCONFIG | dns.V4MAPPED
}, noop);
});

assert.throws(function() {
dns.lookupService('0.0.0.0');
}, /invalid arguments/);

assert.throws(function() {
dns.lookupService('fasdfdsaf', 0, noop);
}, /host needs to be a valid IP address/);

assert.throws(function() {
dns.lookupService('0.0.0.0', '0', noop);
}, /port argument must be a number, got "0"/);

assert.doesNotThrow(function() {
dns.lookupService('0.0.0.0', 0, noop);
});

0 comments on commit fa0457e

Please sign in to comment.