Skip to content

Commit 5b4fab1

Browse files
addaleaxtargos
authored andcommitted
dns: fix TTL value for AAAA replies to resolveAny()
We were previously reading from the wrong offset, namely the one into the final results array, not the one for the AAAA results itself, which could have lead to reading uninitialized or out-of-bounds data. Also, adjust the test accordingly; TTL values are not modified by c-ares, but are only exposed for a subset of all DNS record types. PR-URL: #25187 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Khaidi Chu <i@2333.moe> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent c13e5be commit 5b4fab1

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/cares_wrap.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -1265,14 +1265,16 @@ class QueryAnyWrap: public QueryWrap {
12651265
}
12661266

12671267
CHECK_EQ(aaaa_count, naddr6ttls);
1268+
CHECK_EQ(ret->Length(), a_count + aaaa_count);
12681269
for (uint32_t i = a_count; i < ret->Length(); i++) {
12691270
Local<Object> obj = Object::New(env()->isolate());
12701271
obj->Set(context,
12711272
env()->address_string(),
12721273
ret->Get(context, i).ToLocalChecked()).FromJust();
12731274
obj->Set(context,
12741275
env()->ttl_string(),
1275-
Integer::New(env()->isolate(), addr6ttls[i].ttl)).FromJust();
1276+
Integer::New(env()->isolate(), addr6ttls[i - a_count].ttl))
1277+
.FromJust();
12761278
obj->Set(context,
12771279
env()->type_string(),
12781280
env()->dns_aaaa_string()).FromJust();

test/parallel/test-dns-resolveany.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ server.bind(0, common.mustCall(async () => {
5353
}));
5454

5555
function validateResults(res) {
56-
// Compare copies with ttl removed, c-ares fiddles with that value.
57-
assert.deepStrictEqual(
58-
res.map((r) => Object.assign({}, r, { ttl: null })),
59-
answers.map((r) => Object.assign({}, r, { ttl: null })));
56+
// TTL values are only provided for A and AAAA entries.
57+
assert.deepStrictEqual(res.map(maybeRedactTTL), answers.map(maybeRedactTTL));
58+
}
59+
60+
function maybeRedactTTL(r) {
61+
const ret = { ...r };
62+
if (!['A', 'AAAA'].includes(r.type))
63+
delete ret.ttl;
64+
return ret;
6065
}

0 commit comments

Comments
 (0)