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‎

Lines changed: 3 additions & 0 deletions
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
thrownewTypeError('host needs to be a valid IP address');
191191

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

194197
varreq=newGetNameInfoReqWrap();

‎test/parallel/test-dns.js‎

Lines changed: 16 additions & 0 deletions
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+
},/invalidarguments/);
152+
153+
assert.throws(function(){
154+
dns.lookupService('fasdfdsaf',0,noop);
155+
},/hostneedstobeavalidIPaddress/);
156+
157+
assert.throws(function(){
158+
dns.lookupService('0.0.0.0','0',noop);
159+
},/portargumentmustbeanumber,got"0"/);
160+
161+
assert.doesNotThrow(function(){
162+
dns.lookupService('0.0.0.0',0,noop);
163+
});

0 commit comments

Comments
(0)