Skip to content

Commit f3be421

Browse files
evanlucasjasnell
authored andcommitted
dns: coerce port to number in lookupService
Previously, port could be any number in dns.lookupService. This change throws a TypeError if port is outside the range of 0-65535. It also coerces the port to a number. PR-URL: #4883 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Brian White <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Minwoo Jung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 089d84f commit f3be421

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

doc/api/dns.markdown

+4
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ on some operating systems (e.g FreeBSD 10.1).
126126
Resolves the given `address` and `port` into a hostname and service using
127127
the operating system's underlying `getnameinfo` implementation.
128128

129+
If `address` is not a valid IP address, a `TypeError` will be thrown.
130+
The `port` will be coerced to a number. If it is not a legal port, a `TypeError`
131+
will be thrown.
132+
129133
The callback has arguments `(err, hostname, service)`. The `hostname` and
130134
`service` arguments are strings (e.g. `'localhost'` and `'http'` respectively).
131135

lib/dns.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ const util = require('util');
55

66
const cares = process.binding('cares_wrap');
77
const uv = process.binding('uv');
8+
const internalNet = require('internal/net');
89

910
const GetAddrInfoReqWrap = cares.GetAddrInfoReqWrap;
1011
const GetNameInfoReqWrap = cares.GetNameInfoReqWrap;
1112
const QueryReqWrap = cares.QueryReqWrap;
1213

1314
const isIp = net.isIP;
15+
const isLegalPort = internalNet.isLegalPort;
1416

1517

1618
function errnoException(err, syscall, hostname) {
@@ -189,9 +191,10 @@ exports.lookupService = function(host, port, callback) {
189191
if (cares.isIP(host) === 0)
190192
throw new TypeError('"host" argument needs to be a valid IP address');
191193

192-
if (typeof port !== 'number')
193-
throw new TypeError(`"port" argument must be a number, got "${port}"`);
194+
if (port == null || !isLegalPort(port))
195+
throw new TypeError(`"port" should be >= 0 and < 65536, got "${port}"`);
194196

197+
port = +port;
195198
callback = makeAsync(callback);
196199

197200
var req = new GetNameInfoReqWrap();

test/parallel/test-dns.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,26 @@ assert.throws(function() {
154154
dns.lookupService('fasdfdsaf', 0, noop);
155155
}, /"host" argument needs to be a valid IP address/);
156156

157-
assert.throws(function() {
157+
assert.doesNotThrow(function() {
158158
dns.lookupService('0.0.0.0', '0', noop);
159-
}, /"port" argument must be a number, got "0"/);
159+
});
160160

161161
assert.doesNotThrow(function() {
162162
dns.lookupService('0.0.0.0', 0, noop);
163163
});
164+
165+
assert.throws(function() {
166+
dns.lookupService('0.0.0.0', null, noop);
167+
}, /"port" should be >= 0 and < 65536, got "null"/);
168+
169+
assert.throws(function() {
170+
dns.lookupService('0.0.0.0', undefined, noop);
171+
}, /"port" should be >= 0 and < 65536, got "undefined"/);
172+
173+
assert.throws(function() {
174+
dns.lookupService('0.0.0.0', 65538, noop);
175+
}, /"port" should be >= 0 and < 65536, got "65538"/);
176+
177+
assert.throws(function() {
178+
dns.lookupService('0.0.0.0', 'test', noop);
179+
}, /"port" should be >= 0 and < 65536, got "test"/);

0 commit comments

Comments
 (0)