Skip to content

Commit 26af728

Browse files
oyydaddaleax
authored andcommitted
dns: deprecate passing falsy hostname to dns.lookup
We can `dns.lookup` a falsy `hostname` like `dns.lookup(false)` for the reason of backwards compatibility long before(see #13119 for detail). This behavior is undocumented and seems useless in real world apps. We could also make invalid `hostname` throw in the future and the change might be semver-major. Fixes: #13119 PR-URL: #23173 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent 6f7fd7f commit 26af728

File tree

6 files changed

+61
-7
lines changed

6 files changed

+61
-7
lines changed

doc/api/deprecations.md

+16
Original file line numberDiff line numberDiff line change
@@ -2214,6 +2214,22 @@ the `_handle` property of the `Cipher`, `Decipher`, `DiffieHellman`,
22142214
Using the `_handle` property to access the native object is deprecated because
22152215
improper use of the native object can lead to crashing the application.
22162216
2217+
<a id="DEP0118"></a>
2218+
### DEP0118: dns.lookup() support for a falsy hostname
2219+
<!-- YAML
2220+
changes:
2221+
- version: REPLACEME
2222+
pr-url: https://github.com/nodejs/node/pull/23173
2223+
description: Runtime deprecation.
2224+
-->
2225+
2226+
Type: Runtime
2227+
2228+
Previous versions of Node.js supported `dns.lookup()` with a falsy hostname
2229+
like `dns.lookup(false)` due to backward compatibility.
2230+
This behavior is undocumented and is thought to be unused in real world apps.
2231+
It will become an error in future versions of Node.js.
2232+
22172233
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
22182234
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
22192235
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array

lib/dns.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ const {
3030
getDefaultResolver,
3131
setDefaultResolver,
3232
Resolver,
33-
validateHints
33+
validateHints,
34+
emitInvalidHostnameWarning,
3435
} = require('internal/dns/utils');
3536
const {
3637
ERR_INVALID_ARG_TYPE,
@@ -92,7 +93,7 @@ function lookup(hostname, options, callback) {
9293

9394
// Parse arguments
9495
if (hostname && typeof hostname !== 'string') {
95-
throw new ERR_INVALID_ARG_TYPE('hostname', ['string', 'falsy'], hostname);
96+
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
9697
} else if (typeof options === 'function') {
9798
callback = options;
9899
family = 0;
@@ -113,6 +114,7 @@ function lookup(hostname, options, callback) {
113114
throw new ERR_INVALID_OPT_VALUE('family', family);
114115

115116
if (!hostname) {
117+
emitInvalidHostnameWarning(hostname);
116118
if (all) {
117119
process.nextTick(callback, null, []);
118120
} else {

lib/internal/dns/promises.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
const {
33
bindDefaultResolver,
44
Resolver: CallbackResolver,
5-
validateHints
5+
validateHints,
6+
emitInvalidHostnameWarning,
67
} = require('internal/dns/utils');
78
const { codes, dnsException } = require('internal/errors');
89
const { isIP, isIPv4, isLegalPort } = require('internal/net');
@@ -55,6 +56,7 @@ function onlookupall(err, addresses) {
5556
function createLookupPromise(family, hostname, all, hints, verbatim) {
5657
return new Promise((resolve, reject) => {
5758
if (!hostname) {
59+
emitInvalidHostnameWarning(hostname);
5860
if (all)
5961
resolve([]);
6062
else
@@ -99,7 +101,7 @@ function lookup(hostname, options) {
99101

100102
// Parse arguments
101103
if (hostname && typeof hostname !== 'string') {
102-
throw new ERR_INVALID_ARG_TYPE('hostname', ['string', 'falsy'], hostname);
104+
throw new ERR_INVALID_ARG_TYPE('hostname', 'string', hostname);
103105
} else if (options !== null && typeof options === 'object') {
104106
hints = options.hints >>> 0;
105107
family = options.family >>> 0;

lib/internal/dns/utils.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,26 @@ function validateHints(hints) {
140140
}
141141
}
142142

143+
let invalidHostnameWarningEmitted = false;
144+
145+
function emitInvalidHostnameWarning(hostname) {
146+
if (invalidHostnameWarningEmitted) {
147+
return;
148+
}
149+
invalidHostnameWarningEmitted = true;
150+
process.emitWarning(
151+
`The provided hostname "${hostname}" is not a valid ` +
152+
'hostname, and is supported in the dns module solely for compatibility.',
153+
'DeprecationWarning',
154+
'DEP0118'
155+
);
156+
}
157+
143158
module.exports = {
144159
bindDefaultResolver,
145160
getDefaultResolver,
146161
setDefaultResolver,
147162
validateHints,
148-
Resolver
163+
Resolver,
164+
emitInvalidHostnameWarning,
149165
};

test/parallel/test-dns-lookup.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,31 @@ cares.getaddrinfo = () => internalBinding('uv').UV_ENOENT;
1414
const err = {
1515
code: 'ERR_INVALID_ARG_TYPE',
1616
type: TypeError,
17-
message: /^The "hostname" argument must be one of type string or falsy/
17+
message: /^The "hostname" argument must be of type string\. Received type number/
1818
};
1919

2020
common.expectsError(() => dns.lookup(1, {}), err);
2121
common.expectsError(() => dnsPromises.lookup(1, {}), err);
2222
}
2323

24+
common.expectWarning({
25+
// For 'internal/test/binding' module.
26+
'internal/test/binding': [
27+
'These APIs are exposed only for testing and are not ' +
28+
'tracked by any versioning system or deprecation process.'
29+
],
30+
// For dns.promises.
31+
'ExperimentalWarning': [
32+
'The dns.promises API is experimental'
33+
],
34+
// For calling `dns.lookup` with falsy `hostname`.
35+
'DeprecationWarning': [
36+
'The provided hostname "false" is not a valid ' +
37+
'hostname, and is supported in the dns module solely for compatibility.',
38+
'DEP0118',
39+
],
40+
});
41+
2442
common.expectsError(() => {
2543
dns.lookup(false, 'cb');
2644
}, {

test/parallel/test-dns.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ assert.deepStrictEqual(dns.getServers(), []);
166166
const errorReg = common.expectsError({
167167
code: 'ERR_INVALID_ARG_TYPE',
168168
type: TypeError,
169-
message: /^The "hostname" argument must be one of type string or falsy/
169+
message: /^The "hostname" argument must be of type string\. Received type .*/
170170
}, 10);
171171

172172
assert.throws(() => dns.lookup({}, common.mustNotCall()), errorReg);

0 commit comments

Comments
 (0)