Skip to content

Commit 1b2749e

Browse files
bnoordhuistreysis
authored andcommitted
dns: default to verbatim=true in dns.lookup()
Switch the default from `ipv4first` to `verbatim` (return them exactly as the resolver sent them to us). PR-URL: #39987 Fixes: #31566 Refs: #6307 Refs: #20710 Refs: #38099 Co-authored-by: treysis <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]>
1 parent b6ac7e6 commit 1b2749e

24 files changed

+70
-54
lines changed

doc/api/dns.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,9 @@ section if a custom port is used.
170170
<!-- YAML
171171
added: v0.1.90
172172
changes:
173+
- version: REPLACEME
174+
pr-url: https://github.com/nodejs/node/pull/39987
175+
description: The `verbatim` options defaults to `true` now.
173176
- version: v8.5.0
174177
pr-url: https://github.com/nodejs/node/pull/14731
175178
description: The `verbatim` option is supported now.
@@ -190,10 +193,9 @@ changes:
190193
* `verbatim` {boolean} When `true`, the callback receives IPv4 and IPv6
191194
addresses in the order the DNS resolver returned them. When `false`,
192195
IPv4 addresses are placed before IPv6 addresses.
193-
**Default:** currently `false` (addresses are reordered) but this is
194-
expected to change in the not too distant future. Default value is
196+
**Default:** `true` (addresses are reordered). Default value is
195197
configurable using [`dns.setDefaultResultOrder()`][] or
196-
[`--dns-result-order`][]. New code should use `{ verbatim: true }`.
198+
[`--dns-result-order`][].
197199
* `callback` {Function}
198200
* `err` {Error}
199201
* `address` {string} A string representation of an IPv4 or IPv6 address.

lib/internal/dns/utils.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,10 @@ function emitInvalidHostnameWarning(hostname) {
193193
);
194194
}
195195

196-
let dnsOrder = getOptionValue('--dns-result-order') || 'ipv4first';
196+
let dnsOrder = getOptionValue('--dns-result-order') || 'verbatim';
197197

198198
function getDefaultVerbatim() {
199-
switch (dnsOrder) {
200-
case 'verbatim':
201-
return true;
202-
case 'ipv4first':
203-
default:
204-
return false;
205-
}
199+
return dnsOrder !== 'ipv4first';
206200
}
207201

208202
function setDefaultResultOrder(value) {

test/common/inspector-helper.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ class NodeInstance extends EventEmitter {
392392
console.log('[test]', `Testing ${path}`);
393393
const headers = hostHeaderValue ? { 'Host': hostHeaderValue } : null;
394394
return this.portPromise.then((port) => new Promise((resolve, reject) => {
395-
const req = http.get({ host, port, path, headers }, (res) => {
395+
const req = http.get({ host, port, family: 4, path, headers }, (res) => {
396396
let response = '';
397397
res.setEncoding('utf8');
398398
res
@@ -418,6 +418,7 @@ class NodeInstance extends EventEmitter {
418418
const port = await this.portPromise;
419419
return http.get({
420420
port,
421+
family: 4,
421422
path: parseURL(devtoolsUrl).path,
422423
headers: {
423424
'Connection': 'Upgrade',

test/parallel/test-cluster-message.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ if (cluster.isWorker) {
6060
maybeReply();
6161
});
6262

63-
server.listen(0, '127.0.0.1');
63+
server.listen(0);
6464
} else if (cluster.isPrimary) {
6565

6666
const checks = {

test/parallel/test-http-localaddress.js

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const server = http.createServer((req, res) => {
4242
server.listen(0, '127.0.0.1', () => {
4343
const options = { host: 'localhost',
4444
port: server.address().port,
45+
family: 4,
4546
path: '/',
4647
method: 'GET',
4748
localAddress: '127.0.0.2' };

test/parallel/test-http-upgrade-client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const server = net.createServer(function(c) {
4949
});
5050
});
5151

52-
server.listen(0, '127.0.0.1', common.mustCall(function() {
52+
server.listen(0, common.mustCall(function() {
5353
const port = this.address().port;
5454
const headers = [
5555
{

test/parallel/test-http2-connect-options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const server = http2.createServer((req, res) => {
2222
});
2323

2424
server.listen(0, '127.0.0.1', common.mustCall(() => {
25-
const options = { localAddress: '127.0.0.2' };
25+
const options = { localAddress: '127.0.0.2', family: 4 };
2626

2727
const client = http2.connect(
2828
'http://localhost:' + server.address().port,

test/parallel/test-https-localaddress.js

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ server.listen(0, '127.0.0.1', function() {
5252
const options = {
5353
host: 'localhost',
5454
port: this.address().port,
55+
family: 4,
5556
path: '/',
5657
method: 'GET',
5758
localAddress: '127.0.0.2',

test/parallel/test-net-connect-options-port.js

+24-12
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ const net = require('net');
8383
}
8484
}, expectedConnections));
8585

86-
server.listen(0, 'localhost', common.mustCall(() => {
86+
server.listen(0, common.localhostIPv4, common.mustCall(() => {
8787
const port = server.address().port;
8888

8989
// Total connections = 3 * 4(canConnect) * 6(doConnect) = 72
@@ -133,28 +133,35 @@ function doConnect(args, getCb) {
133133
}
134134

135135
function syncFailToConnect(port, assertErr, optOnly) {
136+
const family = 4;
136137
if (!optOnly) {
137138
// connect(port, cb) and connect(port)
138-
const portArgFunctions = doConnect([port], () => common.mustNotCall());
139+
const portArgFunctions = doConnect([{ port, family }],
140+
() => common.mustNotCall());
139141
for (const fn of portArgFunctions) {
140142
assert.throws(fn, assertErr, `${fn.name}(${port})`);
141143
}
142144

143145
// connect(port, host, cb) and connect(port, host)
144-
const portHostArgFunctions = doConnect([port, 'localhost'],
146+
const portHostArgFunctions = doConnect([{ port,
147+
host: 'localhost',
148+
family }],
145149
() => common.mustNotCall());
146150
for (const fn of portHostArgFunctions) {
147151
assert.throws(fn, assertErr, `${fn.name}(${port}, 'localhost')`);
148152
}
149153
}
150154
// connect({port}, cb) and connect({port})
151-
const portOptFunctions = doConnect([{ port }], () => common.mustNotCall());
155+
const portOptFunctions = doConnect([{ port, family }],
156+
() => common.mustNotCall());
152157
for (const fn of portOptFunctions) {
153158
assert.throws(fn, assertErr, `${fn.name}({port: ${port}})`);
154159
}
155160

156161
// connect({port, host}, cb) and connect({port, host})
157-
const portHostOptFunctions = doConnect([{ port: port, host: 'localhost' }],
162+
const portHostOptFunctions = doConnect([{ port: port,
163+
host: 'localhost',
164+
family: family }],
158165
() => common.mustNotCall());
159166
for (const fn of portHostOptFunctions) {
160167
assert.throws(fn,
@@ -165,27 +172,30 @@ function syncFailToConnect(port, assertErr, optOnly) {
165172

166173
function canConnect(port) {
167174
const noop = () => common.mustCall();
175+
const family = 4;
168176

169177
// connect(port, cb) and connect(port)
170-
const portArgFunctions = doConnect([port], noop);
178+
const portArgFunctions = doConnect([{ port, family }], noop);
171179
for (const fn of portArgFunctions) {
172180
fn();
173181
}
174182

175183
// connect(port, host, cb) and connect(port, host)
176-
const portHostArgFunctions = doConnect([port, 'localhost'], noop);
184+
const portHostArgFunctions = doConnect([{ port, host: 'localhost', family }],
185+
noop);
177186
for (const fn of portHostArgFunctions) {
178187
fn();
179188
}
180189

181190
// connect({port}, cb) and connect({port})
182-
const portOptFunctions = doConnect([{ port }], noop);
191+
const portOptFunctions = doConnect([{ port, family }], noop);
183192
for (const fn of portOptFunctions) {
184193
fn();
185194
}
186195

187196
// connect({port, host}, cb) and connect({port, host})
188-
const portHostOptFns = doConnect([{ port, host: 'localhost' }], noop);
197+
const portHostOptFns = doConnect([{ port, host: 'localhost', family }],
198+
noop);
189199
for (const fn of portHostOptFns) {
190200
fn();
191201
}
@@ -198,20 +208,22 @@ function asyncFailToConnect(port) {
198208
});
199209

200210
const dont = () => common.mustNotCall();
211+
const family = 4;
201212
// connect(port, cb) and connect(port)
202-
const portArgFunctions = doConnect([port], dont);
213+
const portArgFunctions = doConnect([{ port, family }], dont);
203214
for (const fn of portArgFunctions) {
204215
fn().on('error', onError());
205216
}
206217

207218
// connect({port}, cb) and connect({port})
208-
const portOptFunctions = doConnect([{ port }], dont);
219+
const portOptFunctions = doConnect([{ port, family }], dont);
209220
for (const fn of portOptFunctions) {
210221
fn().on('error', onError());
211222
}
212223

213224
// connect({port, host}, cb) and connect({port, host})
214-
const portHostOptFns = doConnect([{ port, host: 'localhost' }], dont);
225+
const portHostOptFns = doConnect([{ port, host: 'localhost', family }],
226+
dont);
215227
for (const fn of portHostOptFns) {
216228
fn().on('error', onError());
217229
}

test/parallel/test-net-dns-lookup.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ const server = net.createServer(function(client) {
2929
server.close();
3030
});
3131

32-
server.listen(0, '127.0.0.1', common.mustCall(function() {
32+
server.listen(0, common.mustCall(function() {
3333
net.connect(this.address().port, 'localhost')
3434
.on('lookup', common.mustCall(function(err, ip, type, host) {
3535
assert.strictEqual(err, null);
36-
assert.strictEqual(ip, '127.0.0.1');
37-
assert.strictEqual(type, 4);
36+
assert.match(ip, /^(127\.0\.0\.1|::1)$/);
37+
assert.match(type.toString(), /^(4|6)$/);
3838
assert.strictEqual(host, 'localhost');
3939
}));
4040
}));

test/parallel/test-net-pingpong.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,4 @@ const tmpdir = require('../common/tmpdir');
130130
tmpdir.refresh();
131131
pingPongTest(common.PIPE);
132132
pingPongTest(0);
133-
pingPongTest(0, 'localhost');
134-
if (common.hasIPv6)
135-
pingPongTest(0, '::1');
133+
if (common.hasIPv6) pingPongTest(0, '::1'); else pingPongTest(0, '127.0.0.1');

test/parallel/test-net-remote-address-port.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,17 @@ const net = require('net');
2727

2828
let conns_closed = 0;
2929

30-
const remoteAddrCandidates = [ common.localhostIPv4 ];
31-
if (common.hasIPv6) remoteAddrCandidates.push('::ffff:127.0.0.1');
30+
const remoteAddrCandidates = [ common.localhostIPv4,
31+
'::1',
32+
'::ffff:127.0.0.1' ];
3233

33-
const remoteFamilyCandidates = ['IPv4'];
34-
if (common.hasIPv6) remoteFamilyCandidates.push('IPv6');
34+
const remoteFamilyCandidates = ['IPv4', 'IPv6'];
3535

3636
const server = net.createServer(common.mustCall(function(socket) {
37-
assert.ok(remoteAddrCandidates.includes(socket.remoteAddress));
38-
assert.ok(remoteFamilyCandidates.includes(socket.remoteFamily));
37+
assert.ok(remoteAddrCandidates.includes(socket.remoteAddress),
38+
`Invalid remoteAddress: ${socket.remoteAddress}`);
39+
assert.ok(remoteFamilyCandidates.includes(socket.remoteFamily),
40+
`Invalid remoteFamily: ${socket.remoteFamily}`);
3941
assert.ok(socket.remotePort);
4042
assert.notStrictEqual(socket.remotePort, this.address().port);
4143
socket.on('end', function() {
@@ -48,8 +50,8 @@ const server = net.createServer(common.mustCall(function(socket) {
4850
socket.resume();
4951
}, 2));
5052

51-
server.listen(0, 'localhost', function() {
52-
const client = net.createConnection(this.address().port, 'localhost');
53+
server.listen(0, function() {
54+
const client = net.createConnection(this.address().port, '127.0.0.1');
5355
const client2 = net.createConnection(this.address().port);
5456
client.on('connect', function() {
5557
assert.ok(remoteAddrCandidates.includes(client.remoteAddress));

test/parallel/test-net-writable.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const net = require('net');
66
const server = net.createServer(common.mustCall(function(s) {
77
server.close();
88
s.end();
9-
})).listen(0, 'localhost', common.mustCall(function() {
10-
const socket = net.connect(this.address().port, 'localhost');
9+
})).listen(0, '127.0.0.1', common.mustCall(function() {
10+
const socket = net.connect(this.address().port, '127.0.0.1');
1111
socket.on('end', common.mustCall(() => {
1212
assert.strictEqual(socket.writable, true);
1313
socket.write('hello world');

test/parallel/test-tcp-wrap-listen.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ const {
1414

1515
const server = new TCP(TCPConstants.SOCKET);
1616

17-
const r = server.bind('0.0.0.0', 0);
17+
const r = (common.hasIPv6 ?
18+
server.bind6('::', 0) :
19+
server.bind('0.0.0.0', 0));
1820
assert.strictEqual(r, 0);
1921
let port = {};
2022
server.getsockname(port);

test/parallel/test-timers-socket-timeout-removes-other-socket-unref-timer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const server = net.createServer(function onClient(client) {
2828
}
2929
});
3030

31-
server.listen(0, common.localhostIPv4, common.mustCall(() => {
31+
server.listen(0, common.mustCall(() => {
3232
const countdown = new Countdown(2, () => server.close());
3333

3434
{

test/parallel/test-tls-client-getephemeralkeyinfo.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function test(size, type, name, cipher) {
3737

3838
server.on('close', common.mustSucceed());
3939

40-
server.listen(0, '127.0.0.1', common.mustCall(() => {
40+
server.listen(0, common.mustCall(() => {
4141
const client = tls.connect({
4242
port: server.address().port,
4343
rejectUnauthorized: false

test/parallel/test-tls-client-mindhsize.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function test(size, err, next) {
3434
if (next) next();
3535
});
3636

37-
server.listen(0, '127.0.0.1', function() {
37+
server.listen(0, function() {
3838
// Client set minimum DH parameter size to 2048 bits so that
3939
// it fails when it make a connection to the tls server where
4040
// dhparams is 1024 bits

test/parallel/test-tls-wrap-econnreset-localaddress.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const server = net.createServer((c) => {
1616
let errored = false;
1717
tls.connect({
1818
port: port,
19+
family: 4,
1920
localAddress: common.localhostIPv4
2021
}, common.localhostIPv4)
2122
.once('error', common.mustCall((e) => {

test/pummel/test-http-upload-timeout.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ server.on('request', function(req, res) {
4444
req.resume();
4545
});
4646

47-
server.listen(0, '127.0.0.1', function() {
47+
server.listen(0, function() {
4848
for (let i = 0; i < 10; i++) {
4949
connections++;
5050

test/pummel/test-net-pingpong.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ function pingPongTest(host, on_complete) {
3838
if (host === '127.0.0.1') {
3939
assert.strictEqual(address, '127.0.0.1');
4040
} else if (host == null || host === 'localhost') {
41-
assert(address === '127.0.0.1' || address === '::ffff:127.0.0.1');
41+
assert(address === '127.0.0.1' || address === '::ffff:127.0.0.1' ||
42+
address === '::1');
4243
} else {
4344
console.log(`host = ${host}, remoteAddress = ${address}`);
4445
assert.strictEqual(address, '::1');
@@ -110,9 +111,8 @@ function pingPongTest(host, on_complete) {
110111
}
111112

112113
// All are run at once and will run on different ports.
113-
pingPongTest('localhost');
114114
pingPongTest(null);
115-
115+
pingPongTest('127.0.0.1');
116116
if (common.hasIPv6) pingPongTest('::1');
117117

118118
process.on('exit', function() {

test/sequential/test-https-connect-localport.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const assert = require('assert');
1717
res.end();
1818
}));
1919

20-
server.listen(0, 'localhost', common.mustCall(() => {
20+
server.listen(0, '127.0.0.1', common.mustCall(() => {
2121
const port = server.address().port;
2222
const req = https.get({
2323
host: 'localhost',

test/sequential/test-inspector-open.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function reopenAfterClose(msg) {
8080
}
8181

8282
function ping(port, callback) {
83-
net.connect(port)
83+
net.connect({ port, family: 4 })
8484
.on('connect', function() { close(this); })
8585
.on('error', function(err) { close(this, err); });
8686

test/sequential/test-net-better-error-messages-port.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ c.on('connect', common.mustNotCall());
1010
c.on('error', common.mustCall(function(e) {
1111
assert.strictEqual(e.code, 'ECONNREFUSED');
1212
assert.strictEqual(e.port, common.PORT);
13-
assert.strictEqual(e.address, '127.0.0.1');
13+
assert.match(e.address, /^(127\.0\.0\.1|::1)$/);
1414
}));

test/sequential/test-net-connect-local-error.js

+2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ const expectedErrorCodes = ['ECONNREFUSED', 'EADDRINUSE'];
99

1010
const optionsIPv4 = {
1111
port: common.PORT,
12+
family: 4,
1213
localPort: common.PORT + 1,
1314
localAddress: common.localhostIPv4
1415
};
1516

1617
const optionsIPv6 = {
1718
host: '::1',
19+
family: 6,
1820
port: common.PORT + 2,
1921
localPort: common.PORT + 3,
2022
localAddress: '::1',

0 commit comments

Comments
 (0)