From fa0457ed04a7e62e6526d63930ed8c02a530a54e 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 782cdfa3f79df4..6683eacf14008f 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 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..04264907d308d4 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 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); +});