From 085c5a0b0859b47b509311888dcef672249d3a41 Mon Sep 17 00:00:00 2001 From: Evan Lucas Date: Sun, 24 Jan 2016 08:58:56 -0600 Subject: [PATCH] dns: throw a TypeError in lookupService with invalid port 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: https://github.com/nodejs/node/issues/4837 PR-URL: https://github.com/nodejs/node/pull/4839 Reviewed-By: Colin Ihrig Reviewed-By: Roman Klauke Reviewed-By: Brian White --- lib/dns.js | 3 +++ test/parallel/test-dns.js | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/dns.js b/lib/dns.js index be735f2cfeac24..6c9795384fbf51 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -189,6 +189,9 @@ exports.lookupService = function(host, port, callback) { if (cares.isIP(host) === 0) throw new TypeError('"host" argument 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(); diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index 3362e8c534eaab..84b74e022ab813 100644 --- a/test/parallel/test-dns.js +++ b/test/parallel/test-dns.js @@ -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" argument 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); +});