Skip to content

Commit 5086d6e

Browse files
cjihrigtrevnorris
authored andcommitted
dns: throw if hostname is not string or falsey
Fix assertion failure from poor argument parsing logic introduced in 6ea5d16. Add tests to make sure arguments are properly parsed. Fixes: 6ea5d16 "dns: always set variable family in lookup()" Reviewed-by: Trevor Norris <[email protected]>
1 parent 437c2f4 commit 5086d6e

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

lib/dns.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ exports.lookup = function lookup(hostname, options, callback) {
105105
var family = -1;
106106

107107
// Parse arguments
108-
if (typeof options === 'function') {
108+
if (hostname && typeof hostname !== 'string') {
109+
throw TypeError('invalid arguments: hostname must be a string or falsey');
110+
} else if (typeof options === 'function') {
109111
callback = options;
110112
family = 0;
111113
} else if (typeof callback !== 'function') {

test/simple/test-dns.js

+41
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,47 @@ assert.throws(function() {
6969
return !(err instanceof TypeError);
7070
}, 'Unexpected error');
7171

72+
// dns.lookup should accept falsey and string values
73+
assert.throws(function() {
74+
dns.lookup({}, noop);
75+
}, 'invalid arguments: hostname must be a string or falsey');
76+
77+
assert.throws(function() {
78+
dns.lookup([], noop);
79+
}, 'invalid arguments: hostname must be a string or falsey');
80+
81+
assert.throws(function() {
82+
dns.lookup(true, noop);
83+
}, 'invalid arguments: hostname must be a string or falsey');
84+
85+
assert.throws(function() {
86+
dns.lookup(1, noop);
87+
}, 'invalid arguments: hostname must be a string or falsey');
88+
89+
assert.throws(function() {
90+
dns.lookup(noop, noop);
91+
}, 'invalid arguments: hostname must be a string or falsey');
92+
93+
assert.doesNotThrow(function() {
94+
dns.lookup('', noop);
95+
});
96+
97+
assert.doesNotThrow(function() {
98+
dns.lookup(null, noop);
99+
});
100+
101+
assert.doesNotThrow(function() {
102+
dns.lookup(undefined, noop);
103+
});
104+
105+
assert.doesNotThrow(function() {
106+
dns.lookup(0, noop);
107+
});
108+
109+
assert.doesNotThrow(function() {
110+
dns.lookup(NaN, noop);
111+
});
112+
72113
/*
73114
* Make sure that dns.lookup throws if hints does not represent a valid flag.
74115
* (dns.V4MAPPED | dns.ADDRCONFIG) + 1 is invalid because:

0 commit comments

Comments
 (0)