From 39ba118a15c7e29f4764f8ee37072a4d0612b0ba Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 18 Aug 2016 17:09:37 -0400 Subject: [PATCH 1/2] dns: lookupService() callback must be a function lookupService() requires a callback function. This commit adds a check to verify that the callback is actually a function. --- lib/dns.js | 5 ++++- test/parallel/test-dns.js | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/dns.js b/lib/dns.js index 8d1541718abf75..3fd5184d83f4ca 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -191,9 +191,12 @@ exports.lookupService = function(host, port, callback) { if (isIP(host) === 0) throw new TypeError('"host" argument needs to be a valid IP address'); - if (port == null || !isLegalPort(port)) + if (!isLegalPort(port)) throw new TypeError(`"port" should be >= 0 and < 65536, got "${port}"`); + if (typeof callback !== 'function') + throw new TypeError('"callback" argument must be a function'); + port = +port; callback = makeAsync(callback); diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index cd5914e026d6a9..6a73371b02f0c2 100644 --- a/test/parallel/test-dns.js +++ b/test/parallel/test-dns.js @@ -177,3 +177,7 @@ assert.throws(function() { assert.throws(function() { dns.lookupService('0.0.0.0', 'test', noop); }, /"port" should be >= 0 and < 65536, got "test"/); + +assert.throws(() => { + dns.lookupService('0.0.0.0', 80, null); +}, /^TypeError: "callback" argument must be a function$/); From 2bebeb1bd5c2bf95dd9cd9712fd684719487ab91 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Thu, 18 Aug 2016 17:15:37 -0400 Subject: [PATCH 2/2] dns: remove makeAsync() function check makeAsync() is an internal method in the dns module. All of the functions that call makeAsync() have already validated that the callback is a function. This commit removes a redundant typeof function check. --- lib/dns.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/dns.js b/lib/dns.js index 3fd5184d83f4ca..7c76aebb97f449 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -56,9 +56,6 @@ function errnoException(err, syscall, hostname) { // callback.immediately = true; // } function makeAsync(callback) { - if (typeof callback !== 'function') { - return callback; - } return function asyncCallback() { if (asyncCallback.immediately) { // The API already returned, we can invoke the callback immediately.