Skip to content

Commit de1cd38

Browse files
committed
dns: validate both addresses before applying them in setLocalAddress
SetLocalAddress() applied the first address to the c-ares channel with ares_set_local_ip4()/ares_set_local_ip6() before parsing and validating the second one. A call that then threw ERR_INVALID_ARG_VALUE, because both addresses were of the same family or the second address was invalid, left the first address in effect, so later queries were sent from an address the caller had been told was rejected. Parse and validate both arguments first and only then apply them. With a single argument the other family is still reset to "any", as before. Fixes: #67049 Signed-off-by: Martin Mirchev <mmirchev@comp.nus.edu.sg>
1 parent 98dbf60 commit de1cd38

2 files changed

Lines changed: 56 additions & 13 deletions

File tree

‎src/cares_wrap.cc‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2332,16 +2332,16 @@ void SetLocalAddress(const FunctionCallbackInfo<Value>& args) {
23322332
// to 0 (any).
23332333

23342334
if (uv_inet_pton(AF_INET, *ip0, &addr0) == 0) {
2335-
ares_set_local_ip4(channel->cares_channel(), nbytes::ReadUint32BE(addr0));
23362335
type0 = 4;
23372336
} else if (uv_inet_pton(AF_INET6, *ip0, &addr0) == 0) {
2338-
ares_set_local_ip6(channel->cares_channel(), addr0);
23392337
type0 = 6;
23402338
} else {
23412339
THROW_ERR_INVALID_ARG_VALUE(env, "Invalid IP address.");
23422340
return;
23432341
}
23442342

2343+
// Validate the second argument before applying anything, so that a call
2344+
// that throws leaves the channel unchanged.
23452345
if (!args[1]->IsUndefined()) {
23462346
CHECK(args[1]->IsString());
23472347
node::Utf8Value ip1(isolate, args[1]);
@@ -2350,29 +2350,27 @@ void SetLocalAddress(const FunctionCallbackInfo<Value>& args) {
23502350
if (type0 == 4) {
23512351
THROW_ERR_INVALID_ARG_VALUE(env, "Cannot specify two IPv4 addresses.");
23522352
return;
2353-
} else {
2354-
ares_set_local_ip4(channel->cares_channel(),
2355-
nbytes::ReadUint32BE(addr1));
23562353
}
23572354
} else if (uv_inet_pton(AF_INET6, *ip1, &addr1) == 0) {
23582355
if (type0 == 6) {
23592356
THROW_ERR_INVALID_ARG_VALUE(env, "Cannot specify two IPv6 addresses.");
23602357
return;
2361-
} else {
2362-
ares_set_local_ip6(channel->cares_channel(), addr1);
23632358
}
23642359
} else {
23652360
THROW_ERR_INVALID_ARG_VALUE(env, "Invalid IP address.");
23662361
return;
23672362
}
23682363
} else {
23692364
// No second arg specified
2370-
if (type0 == 4) {
2371-
memset(&addr1, 0, sizeof(addr1));
2372-
ares_set_local_ip6(channel->cares_channel(), addr1);
2373-
} else {
2374-
ares_set_local_ip4(channel->cares_channel(), 0);
2375-
}
2365+
memset(&addr1, 0, sizeof(addr1));
2366+
}
2367+
2368+
if (type0 == 4) {
2369+
ares_set_local_ip4(channel->cares_channel(), nbytes::ReadUint32BE(addr0));
2370+
ares_set_local_ip6(channel->cares_channel(), addr1);
2371+
} else {
2372+
ares_set_local_ip6(channel->cares_channel(), addr0);
2373+
ares_set_local_ip4(channel->cares_channel(), nbytes::ReadUint32BE(addr1));
23762374
}
23772375
}
23782376

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// Verifies that a call to resolver.setLocalAddress() that throws leaves the
5+
// previously configured local address in place.
6+
7+
// Binding to 127.0.0.0/8 addresses other than 127.0.0.1 works out of the box
8+
// on Linux only.
9+
if (!common.isLinux)
10+
common.skip('requires the whole 127.0.0.0/8 range on the loopback interface');
11+
12+
const assert = require('assert');
13+
const dgram = require('dgram');
14+
const { Resolver } = require('dns');
15+
16+
const resolver = new Resolver({ timeout: 1000, tries: 1 });
17+
const server = dgram.createSocket('udp4');
18+
19+
server.on('message', common.mustCall((msg, rinfo) => {
20+
assert.strictEqual(rinfo.address, '127.0.0.2');
21+
server.close();
22+
resolver.cancel();
23+
}));
24+
25+
server.bind(0, '127.0.0.1', common.mustCall(() => {
26+
resolver.setServers([`127.0.0.1:${server.address().port}`]);
27+
resolver.setLocalAddress('127.0.0.2');
28+
29+
assert.throws(() => {
30+
resolver.setLocalAddress('127.0.0.3', '127.0.0.4');
31+
}, {
32+
code: 'ERR_INVALID_ARG_VALUE',
33+
message: 'Cannot specify two IPv4 addresses.',
34+
});
35+
assert.throws(() => {
36+
resolver.setLocalAddress('127.0.0.5', 'bad');
37+
}, {
38+
code: 'ERR_INVALID_ARG_VALUE',
39+
message: 'Invalid IP address.',
40+
});
41+
42+
resolver.resolve4('example.test', common.mustCall((err) => {
43+
assert.strictEqual(err.code, 'ECANCELLED');
44+
}));
45+
}));

0 commit comments

Comments
 (0)