Skip to content

Commit 4fcfa5a

Browse files
addaleaxMylesBorins
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 <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Khaidi Chu <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent f17b61e commit 4fcfa5a

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/cares_wrap.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,14 +1263,16 @@ class QueryAnyWrap: public QueryWrap {
12631263
}
12641264

12651265
CHECK_EQ(aaaa_count, naddr6ttls);
1266+
CHECK_EQ(ret->Length(), a_count + aaaa_count);
12661267
for (uint32_t i = a_count; i < ret->Length(); i++) {
12671268
Local<Object> obj = Object::New(env()->isolate());
12681269
obj->Set(context,
12691270
env()->address_string(),
12701271
ret->Get(context, i).ToLocalChecked()).FromJust();
12711272
obj->Set(context,
12721273
env()->ttl_string(),
1273-
Integer::New(env()->isolate(), addr6ttls[i].ttl)).FromJust();
1274+
Integer::New(env()->isolate(), addr6ttls[i - a_count].ttl))
1275+
.FromJust();
12741276
obj->Set(context,
12751277
env()->type_string(),
12761278
env()->dns_aaaa_string()).FromJust();

test/parallel/test-dns-resolveany.js

Lines changed: 9 additions & 4 deletions
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)