Skip to content

Commit fa0457e

Browse files
evanlucasrvagg
authored andcommitted
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: #4837 PR-URL: #4839 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Roman Klauke <[email protected]> Reviewed-By: Brian White <[email protected]>
1 parent a1af6fc commit fa0457e

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

lib/dns.js

+3
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ exports.lookupService = function(host, port, callback) {
189189
if (cares.isIP(host) === 0)
190190
throw new TypeError('host needs to be a valid IP address');
191191

192+
if (typeof port !== 'number')
193+
throw new TypeError(`port argument must be a number, got "${port}"`);
194+
192195
callback = makeAsync(callback);
193196

194197
var req = new GetNameInfoReqWrap();

test/parallel/test-dns.js

+16
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,19 @@ assert.doesNotThrow(function() {
145145
hints: dns.ADDRCONFIG | dns.V4MAPPED
146146
}, noop);
147147
});
148+
149+
assert.throws(function() {
150+
dns.lookupService('0.0.0.0');
151+
}, /invalid arguments/);
152+
153+
assert.throws(function() {
154+
dns.lookupService('fasdfdsaf', 0, noop);
155+
}, /host needs to be a valid IP address/);
156+
157+
assert.throws(function() {
158+
dns.lookupService('0.0.0.0', '0', noop);
159+
}, /port argument must be a number, got "0"/);
160+
161+
assert.doesNotThrow(function() {
162+
dns.lookupService('0.0.0.0', 0, noop);
163+
});

0 commit comments

Comments
 (0)