Skip to content

Full Toolkit for Next-Level Domain Analysis

Give the authoritative walk one context 🧵🧭 (#151)

Felix Wotschofskyd65c11140c02
diff --git a/lib/resolvers/authoritative.ts b/lib/resolvers/authoritative.ts
index 8f5aa2a..7c4ad94 100644
--- a/lib/resolvers/authoritative.ts
+++ b/lib/resolvers/authoritative.ts
@@ -27,6 +27,7 @@ import {
type ResolverResponse,
} from './base';
import { isPublicIp } from './ip-filter';
+import { WalkContext } from './walk-context';
type RawAnswer = Extract<Answer, { type: RecordType }>;
@@ -127,7 +128,7 @@ export type AuthoritativeResolverOptions = {
tcpTransport?: AuthoritativeTcpTransport;
rootServers?: () => Promise<string[]>;
// Time budget for trying fallback nameservers, shared across the whole
- // walk (see fetchRecordsRaw). Injectable for tests.
+ // walk (see WalkContext). Injectable for tests.
fallbackDeadlineMs?: number;
// Set the EDNS DNSSEC OK (DO) bit on every query of this instance, so
// authoritative servers include RRSIGs.
@@ -162,27 +163,9 @@ type WalkResult = {
coveringRrsigs?: RrsigData[];
};
-// Shared by all glueless branches of one top-level query. Reusing a cached
-// request (including a rejection) consumes neither another slot nor packets.
-// A new top-level query gets a fresh budget and can retry requests that
-// rejected (transport failures); resolved responses, rcode failures
-// included, stay cached for the resolver instance.
-type WalkBudget = {
- remaining: number;
- requests: Map<string, Promise<DnsResponse>>;
- remainingSteps: number;
- // An NS query's fallback when every delegated server fails: the first
- // parent delegation for the queried name seen anywhere in the walk. Adopted
- // only if the whole walk fails, so a sibling path that answers still wins.
- parentDelegation?: WalkResult;
-};
-
-// Name case and a trailing dot don't make a request distinct.
-const requestKey = (
- domain: string,
- recordType: RecordType,
- nameserver: string,
-) => `${canonicalDnsName(domain)} ${recordType} ${nameserver}`;
+// Its fallback result is an NS query's parent delegation, offered when every
+// delegated server fails.
+type Walk = WalkContext<DnsResponse, WalkResult>;
type FetchRecordsParams = {
domain: string;
@@ -191,10 +174,7 @@ type FetchRecordsParams = {
// The zone cut `nameservers` were delegated for.
zone?: string;
trace?: string[];
- depth?: number;
- deadlineAt?: number;
- budget?: WalkBudget;
- ancestors?: ReadonlySet<string>;
+ walk: Walk;
};
const FALLBACK_LOOKUP_ERROR: UserFacingErrorPayload = {
@@ -205,31 +185,6 @@ const FALLBACK_LOOKUP_ERROR: UserFacingErrorPayload = {
};
export class AuthoritativeResolver extends DnsResolver {
- private static readonly MAX_RECURSION_DEPTH = 20;
- // Caps distinct requests (name, type, nameserver) for one walk -- reusing
- // one is free and bounded by the walk's step budget instead -- like BIND's
- // max-recursion-queries: the depth cap alone still lets a crafted zone
- // answer every glueless delegation with 4 fresh NS names and fork
- // 4^depth sub-walks. One attempt costs up to 4 UDP tries plus a TCP
- // fallback each (see requestLoader/sendRequest), so this also caps the
- // walk's packet ceiling at ~8x this value. Far above any honest
- // lookup's needs (a deep all-glueless walk stays under ~30).
- private static readonly MAX_CANDIDATES_PER_WALK = 50;
- // Must exceed one candidate's full retry budget (4 attempts x 3s = ~12s):
- // a single blackholed first server has to leave room to reach a healthy
- // fallback, while all-blackholed candidates stay bounded (~2 budgets).
- private static readonly FALLBACK_DEADLINE_MS = 15_000;
-
- private static newWalkBudget(): WalkBudget {
- return {
- remaining: AuthoritativeResolver.MAX_CANDIDATES_PER_WALK,
- requests: new Map(),
- remainingSteps:
- AuthoritativeResolver.MAX_CANDIDATES_PER_WALK *
- AuthoritativeResolver.MAX_RECURSION_DEPTH,
- };
- }
-
public constructor(
private readonly options: AuthoritativeResolverOptions = {},
) {
@@ -532,21 +487,38 @@ export class AuthoritativeResolver extends DnsResolver {
// dedupe those shared hops because its key includes the final query name.
// First writer wins and only suffixes of the queried name are cached, so a
// deeper (less-trusted) server can't overwrite entries learned from the
- // servers above it.
+ // servers above it. Only the two methods below write it.
private delegationCache = new Map<string, string[]>();
+ // Caches a referral's servers and returns the referral's hold on the entry:
+ // release and replace act only while the entry is still the one this
+ // referral wrote, so it never drops or overwrites another walk's.
private cacheDelegation(zoneCut: string, domain: string, ips: string[]) {
const zone = canonicalDnsName(zoneCut);
const name = canonicalDnsName(domain);
- if (this.delegationCache.has(zone)) return;
- if (name !== zone && !name.endsWith(`.${zone}`)) return;
- this.delegationCache.set(zone, ips);
+ if (
+ !this.delegationCache.has(zone) &&
+ (name === zone || name.endsWith(`.${zone}`))
+ ) {
+ this.delegationCache.set(zone, ips);
+ }
+ const owned = () => this.delegationCache.get(zone) === ips;
+ return {
+ release: () => {
+ if (owned()) this.delegationCache.delete(zone);
+ },
+ replace: (next: string[]) => {
+ if (owned()) this.delegationCache.set(zone, next);
+ },
+ };
}
+ // The deepest cached cut above `domain`. evict drops it whoever wrote it:
+ // its servers all failed, so it may simply be stale.
private getCachedDelegation(
domain: string,
recordType: RecordType,
- ): { zone: string; ips: string[] } | undefined {
+ ): { zone: string; ips: string[]; evict: () => void } | undefined {
const labels = canonicalDnsName(domain).split('.').filter(Boolean);
// DS records live in the parent zone: a child's own servers answer a DS
// query for their apex with NODATA, which would falsely read as "no DS".
@@ -555,7 +527,8 @@ export class AuthoritativeResolver extends DnsResolver {
for (let i = start; i < labels.length; i++) {
const zone = labels.slice(i).join('.');
const ips = this.delegationCache.get(zone);
- if (ips) return { zone, ips };
+ if (ips)
+ return { zone, ips, evict: () => this.delegationCache.delete(zone) };
}
return undefined;
}
@@ -566,29 +539,9 @@ export class AuthoritativeResolver extends DnsResolver {
nameservers,
zone: delegatedZone,
trace = [],
- depth = 0,
- // The deadline and query budget span the whole walk -- referrals and
- // glueless sub-lookups inherit both -- so each delegation level can't
- // stack a fresh allowance.
- deadlineAt = Date.now() +
- (this.options.fallbackDeadlineMs ??
- AuthoritativeResolver.FALLBACK_DEADLINE_MS),
- budget = AuthoritativeResolver.newWalkBudget(),
- ancestors = new Set<string>(),
+ walk,
}: FetchRecordsParams): Promise<WalkResult> {
- // Cached responses cost no packets, but a crafted or misconfigured graph
- // can still fork exponentially through shared requests. Bound that
- // processing separately.
- if (budget.remainingSteps-- <= 0) {
- throw new Error(
- `DNS traversal budget exhausted while resolving ${domain}`,
- );
- }
- if (depth > AuthoritativeResolver.MAX_RECURSION_DEPTH) {
- throw new Error(
- `Max recursion depth exceeded while resolving ${domain} (type ${recordType})`,
- );
- }
+ walk.enter(domain, recordType);
const cached = nameservers?.length
? undefined
@@ -648,7 +601,7 @@ export class AuthoritativeResolver extends DnsResolver {
// candidate, so the ceiling is MAX_CANDIDATES_PER_WALK retry budgets; the
// platform request timeout is the backstop for adversarial chains.
for (const candidate of candidateNameservers) {
- if (failedNameservers.length > 0 && Date.now() >= deadlineAt) {
+ if (failedNameservers.length > 0 && walk.pastDeadline) {
failedNameservers.push(
`${candidate}: skipped, fallback deadline exceeded`,
);
@@ -660,27 +613,18 @@ export class AuthoritativeResolver extends DnsResolver {
nameserver: candidate,
dnssecOk: this.options.dnssecOk ?? false,
};
- const key = requestKey(domain, recordType, candidate);
- // Branch-local ancestry stops a circular glueless dependency, whose
- // replies are cached after a round trip or two and then recurse in
- // microtasks, before it spends the step budget a slower sibling needs.
- // This catches a request repeating itself; followReferralViaNsLookup
- // catches an NS name cycling back through other servers.
- if (ancestors.has(key)) {
+ const pending = walk.send(domain, recordType, candidate, () =>
+ this.requestLoader.load(loaderKey),
+ );
+ if (pending === 'loop') {
failedNameservers.push(`${candidate}: DNS referral loop detected`);
continue;
}
- let pending = budget.requests.get(key);
- if (!pending) {
- if (budget.remaining <= 0) {
- failedNameservers.push(
- `${candidate}: skipped, candidate budget exhausted`,
- );
- break;
- }
- budget.remaining--;
- pending = this.requestLoader.load(loaderKey);
- budget.requests.set(key, pending);
+ if (pending === 'exhausted') {
+ failedNameservers.push(
+ `${candidate}: skipped, candidate budget exhausted`,
+ );
+ break;
}
let result: DnsResponse;
try {
@@ -733,10 +677,7 @@ export class AuthoritativeResolver extends DnsResolver {
wantName,
zone,
trace: acceptedTrace,
- depth,
- deadlineAt,
- budget,
- ancestors: new Set([...ancestors, key]),
+ walk: walk.via(domain, recordType, candidate),
});
} catch (error) {
failedNameservers.push(
@@ -744,28 +685,24 @@ export class AuthoritativeResolver extends DnsResolver {
);
// An NS query whose own zone's servers all fail (a lame delegation,
// e.g. NS at a provider that dropped the zone) still has an answer:
- // the delegation this parent handed out. resolveAnswers adopts it
- // only if the whole walk fails.
+ // the delegation this parent handed out, adopted only if the whole
+ // walk fails.
const cut = canonicalDnsName(zone);
- if (
- recordType === 'NS' &&
- !budget.parentDelegation &&
- (!cut || wantName.endsWith(`.${cut}`))
- ) {
+ if (recordType === 'NS' && (!cut || wantName.endsWith(`.${cut}`))) {
const delegation = result.packet.authorities?.filter(
(answer): answer is RawAnswer =>
answer.type === 'NS' &&
canonicalDnsName(answer.name) === wantName,
);
if (delegation?.length)
- budget.parentDelegation = {
+ walk.offerFallback({
answers: delegation,
zone,
trace: [
...acceptedTrace,
`${recordType} ${domain} @ ${candidate} -> referral failed (${describeError(error)}), using parent delegation: ${delegation.map(this.recordToString).join(', ')}`,
],
- };
+ });
}
continue;
}
@@ -782,8 +719,8 @@ export class AuthoritativeResolver extends DnsResolver {
// the healthy authoritative server the fresh root walk finds. With the
// step budget spent there is no re-walk; the walk ends below, still
// without adopting lastResort.
- this.delegationCache.delete(cached.zone);
- if (budget.remainingSteps > 0)
+ cached.evict();
+ if (walk.hasStepsLeft)
return this.fetchRecordsRaw({
domain,
recordType,
@@ -791,10 +728,7 @@ export class AuthoritativeResolver extends DnsResolver {
...trace,
`${recordType} ${domain} -> cached delegation ${cached.zone} failed (${failedNameservers.join('; ')}), re-walking from root`,
],
- depth: depth + 1,
- deadlineAt,
- budget,
- ancestors,
+ walk: walk.deeper(),
});
} else if (lastResort) {
// No candidate answered authoritatively; the deferred answer is better
@@ -816,10 +750,7 @@ export class AuthoritativeResolver extends DnsResolver {
wantName,
zone,
trace: lastResortTrace,
- depth,
- deadlineAt,
- budget,
- ancestors,
+ walk,
});
}
@@ -868,10 +799,7 @@ export class AuthoritativeResolver extends DnsResolver {
wantName,
zone,
trace,
- depth,
- deadlineAt,
- budget,
- ancestors,
+ walk,
}: {
packet: Packet;
protocol: DnsResponse['protocol'];
@@ -881,10 +809,7 @@ export class AuthoritativeResolver extends DnsResolver {
wantName: string;
zone: string;
trace: string[];
- depth: number;
- deadlineAt: number;
- budget: WalkBudget;
- ancestors: ReadonlySet<string>;
+ walk: Walk;
}): Promise<WalkResult> {
// Only records owned by the queried name (any type, e.g. a CNAME alias)
// make this a terminal answer. An answer section carrying nothing but
@@ -1002,7 +927,11 @@ export class AuthoritativeResolver extends DnsResolver {
const addresses = [
...new Set(aRedirects.map((redirect) => redirect.data)),
].slice(0, 4);
- this.cacheDelegation(nsRedirects[0].name, domain, addresses);
+ const delegation = this.cacheDelegation(
+ nsRedirects[0].name,
+ domain,
+ addresses,
+ );
try {
return await this.fetchRecordsRaw({
domain,
@@ -1013,10 +942,7 @@ export class AuthoritativeResolver extends DnsResolver {
...trace,
`${recordType} ${domain} @ ${candidate} (${protocol}) -> redirect to ${aRedirects.map((r) => r.data).join(', ')}`,
],
- depth: depth + 1,
- deadlineAt,
- budget,
- ancestors,
+ walk: walk.deeper(),
});
} catch (error) {
// Glue can cover only some NS names (whu.edu glues just one, dead,
@@ -1033,9 +959,7 @@ export class AuthoritativeResolver extends DnsResolver {
// costs one more retry budget per zone before failing. Checking what
// the sent glue answered, and skipping servers that already timed
// out under another name, would narrow both.
- const sent = addresses.filter((ip) =>
- budget.requests.has(requestKey(domain, recordType, ip)),
- );
+ const sent = walk.queried(domain, recordType, addresses);
// Grouped once per name: the referral can repeat thousands of
// glue records.
const spent = new Set(
@@ -1053,8 +977,7 @@ export class AuthoritativeResolver extends DnsResolver {
)
.sort((a, b) => Number(isSpent(a)) - Number(isSpent(b)));
// Drop this referral's own entry so the fallback can replace it.
- if (this.delegationCache.get(nextZone) === addresses)
- this.delegationCache.delete(nextZone);
+ delegation.release();
if (!fallbackNs.length) throw error;
return this.followReferralViaNsLookup({
nsRedirects: fallbackNs,
@@ -1067,10 +990,7 @@ export class AuthoritativeResolver extends DnsResolver {
...trace,
`${recordType} ${domain} -> glue for ${nextZone}${sent.length ? ` (${sent.join(', ')})` : ''} failed (${describeError(error)}), resolving NS names instead`,
],
- depth,
- deadlineAt,
- budget,
- ancestors,
+ walk,
});
}
}
@@ -1083,10 +1003,7 @@ export class AuthoritativeResolver extends DnsResolver {
candidate,
protocol,
trace,
- depth,
- deadlineAt,
- budget,
- ancestors,
+ walk,
});
}
@@ -1104,10 +1021,7 @@ export class AuthoritativeResolver extends DnsResolver {
candidate,
protocol,
trace,
- depth,
- deadlineAt,
- budget,
- ancestors,
+ walk,
}: {
nsRedirects: StringAnswer[];
failed?: string[];
@@ -1116,10 +1030,7 @@ export class AuthoritativeResolver extends DnsResolver {
candidate: string;
protocol: DnsResponse['protocol'];
trace: string[];
- depth: number;
- deadlineAt: number;
- budget: WalkBudget;
- ancestors: ReadonlySet<string>;
+ walk: Walk;
}): Promise<WalkResult> {
const zone = canonicalDnsName(nsRedirects[0].name);
const subTrace: string[] = [];
@@ -1134,20 +1045,11 @@ export class AuthoritativeResolver extends DnsResolver {
// as the winner are already collected as fallbacks.
const resolvedSets: string[][] = [];
const lookups = nsRedirects.slice(0, 4).map(async (ns) => {
- // An NS name already being resolved further up this branch is a
- // circular dependency whichever servers it goes through, so it is
- // stopped once rather than once per server ordering.
- const nsKey = `NS ${canonicalDnsName(ns.data)}`;
try {
- if (ancestors.has(nsKey))
- throw new Error(`circular NS dependency (${ns.data})`);
const resolved = await this.fetchRecordsRaw({
domain: ns.data,
recordType: 'A',
- depth: depth + 1,
- deadlineAt,
- budget,
- ancestors: new Set([...ancestors, nsKey]),
+ walk: walk.resolvingNs(ns.data),
});
subTrace.push(...resolved.trace);
// resolved.answers are all A records (filtered by type), so
@@ -1173,7 +1075,7 @@ export class AuthoritativeResolver extends DnsResolver {
}
const tried = [...new Set(resolvedSets.flat())].slice(0, 4);
- this.cacheDelegation(nsRedirects[0].name, domain, tried);
+ const delegation = this.cacheDelegation(nsRedirects[0].name, domain, tried);
const redirectTrace = [
...trace,
`${recordType} ${domain} @ ${candidate} (${protocol}) -> redirect to ${nsRedirects.map((r) => r.data).join(', ')}`,
@@ -1187,10 +1089,7 @@ export class AuthoritativeResolver extends DnsResolver {
nameservers: tried,
zone,
trace: redirectTrace,
- depth: depth + 1,
- deadlineAt,
- budget,
- ancestors,
+ walk: walk.deeper(),
});
} catch (error) {
// Sibling NS lookups are still usable failover: the race above only
@@ -1204,13 +1103,10 @@ export class AuthoritativeResolver extends DnsResolver {
.filter((ip) => !tried.includes(ip))
.slice(0, 4);
if (!late.length) throw error;
- if (this.delegationCache.get(zone) === tried)
- this.delegationCache.set(zone, late);
+ delegation.replace(late);
// The deadline or request cap can stop the walk before every address
// is queried; only the queried ones failed.
- const queried = tried.filter((ip) =>
- budget.requests.has(requestKey(domain, recordType, ip)),
- );
+ const queried = walk.queried(domain, recordType, tried);
return this.fetchRecordsRaw({
domain,
recordType,
@@ -1221,10 +1117,7 @@ export class AuthoritativeResolver extends DnsResolver {
...subTrace.slice(seenSubTrace),
`${recordType} ${domain} -> nameservers${queried.length ? ` ${queried.join(', ')}` : ''} failed (${describeError(error)}), retrying with late sibling nameservers: ${late.join(', ')}`,
],
- depth: depth + 1,
- deadlineAt,
- budget,
- ancestors,
+ walk: walk.deeper(),
});
}
}
@@ -1238,13 +1131,10 @@ export class AuthoritativeResolver extends DnsResolver {
domain: string,
recordType: RecordType,
): Promise<WalkResult> {
- const budget = AuthoritativeResolver.newWalkBudget();
- try {
- return await this.fetchRecordsRaw({ domain, recordType, budget });
- } catch (error) {
- if (budget.parentDelegation) return budget.parentDelegation;
- throw error;
- }
+ return WalkContext.run<DnsResponse, WalkResult>(
+ (walk) => this.fetchRecordsRaw({ domain, recordType, walk }),
+ this.options.fallbackDeadlineMs,
+ );
}
public override async resolveRecordTypes(
diff --git a/lib/resolvers/walk-context.test.ts b/lib/resolvers/walk-context.test.ts
new file mode 100644
index 0000000..313642c
--- /dev/null
+++ b/lib/resolvers/walk-context.test.ts
@@ -0,0 +1,104 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import { WalkContext } from './walk-context';
+
+type Walk = WalkContext<string, string>;
+
+const run = (walk: (context: Walk) => Promise<string>, deadlineMs?: number) =>
+ WalkContext.run<string, string>(walk, deadlineMs);
+
+describe('WalkContext', () => {
+ it('bounds the walk by steps and by depth', async () => {
+ await run(async (walk) => {
+ for (let i = 0; i < 1000; i++) walk.enter('example.com', 'A');
+ expect(walk.hasStepsLeft).toBe(false);
+ expect(() => walk.enter('example.com', 'A')).toThrow(
+ 'DNS traversal budget exhausted while resolving example.com',
+ );
+ return '';
+ });
+
+ await run(async (walk) => {
+ let deep = walk;
+ for (let i = 0; i < 20; i++) deep = deep.deeper();
+ deep.enter('example.com', 'A');
+ expect(() => deep.deeper().enter('example.com', 'A')).toThrow(
+ 'Max recursion depth exceeded while resolving example.com (type A)',
+ );
+ return '';
+ });
+ });
+
+ it('sends each distinct request once, up to the request cap', async () => {
+ await run(async (walk) => {
+ const load = vi.fn(async () => 'response');
+ const first = walk.send('Example.com.', 'A', '192.0.2.1', load);
+ for (let i = 2; i <= 50; i++) {
+ walk.send('example.com', 'A', `192.0.2.${i}`, load);
+ }
+ expect(walk.send('example.com', 'A', '192.0.2.51', load)).toBe(
+ 'exhausted',
+ );
+ // A repeat shares the pending response and costs nothing, even past the
+ // cap and from another branch.
+ expect(walk.deeper().send('example.com', 'A', '192.0.2.1', load)).toBe(
+ first,
+ );
+ expect(load).toHaveBeenCalledTimes(50);
+ expect(
+ walk.queried('example.com', 'A', ['192.0.2.1', '192.0.2.51']),
+ ).toEqual(['192.0.2.1']);
+ return '';
+ });
+ });
+
+ it('detects request and NS-name cycles only within a branch', async () => {
+ await run(async (walk) => {
+ const load = async () => 'response';
+ const branch = walk.via('example.com', 'A', '192.0.2.1').deeper();
+ expect(branch.send('EXAMPLE.com', 'A', '192.0.2.1', load)).toBe('loop');
+ expect(walk.send('example.com', 'A', '192.0.2.1', load)).not.toBe('loop');
+
+ const nsBranch = walk.resolvingNs('ns1.example.net');
+ expect(() => nsBranch.deeper().resolvingNs('NS1.example.net.')).toThrow(
+ 'circular NS dependency (NS1.example.net.)',
+ );
+ expect(() => walk.resolvingNs('ns1.example.net')).not.toThrow();
+ return '';
+ });
+ });
+
+ it('adopts the first fallback offered, only if the walk fails', async () => {
+ await expect(
+ run(async (walk) => {
+ walk.offerFallback('parent delegation');
+ walk.deeper().offerFallback('later delegation');
+ throw new Error('walk failed');
+ }),
+ ).resolves.toBe('parent delegation');
+
+ await expect(
+ run(async (walk) => {
+ walk.offerFallback('parent delegation');
+ return 'answer';
+ }),
+ ).resolves.toBe('answer');
+
+ await expect(
+ run(async () => {
+ throw new Error('walk failed');
+ }),
+ ).rejects.toThrow('walk failed');
+ });
+
+ it('shares one deadline across the walk', async () => {
+ await run(async (walk) => {
+ expect(walk.pastDeadline).toBe(false);
+ return '';
+ });
+ await run(async (walk) => {
+ expect(walk.deeper().pastDeadline).toBe(true);
+ return '';
+ }, 0);
+ });
+});
diff --git a/lib/resolvers/walk-context.ts b/lib/resolvers/walk-context.ts
new file mode 100644
index 0000000..93f5940
--- /dev/null
+++ b/lib/resolvers/walk-context.ts
@@ -0,0 +1,178 @@
+import { canonicalDnsName, type RecordType } from './base';
+
+const MAX_RECURSION_DEPTH = 20;
+// Caps distinct requests (name, type, nameserver) for one walk -- reusing
+// one is free and bounded by the walk's step budget instead -- like BIND's
+// max-recursion-queries: the depth cap alone still lets a crafted zone
+// answer every glueless delegation with 4 fresh NS names and fork
+// 4^depth sub-walks. One attempt costs up to 4 UDP tries plus a TCP
+// fallback each (see the resolver's requestLoader/sendRequest), so this also
+// caps the walk's packet ceiling at ~8x this value. Far above any honest
+// lookup's needs (a deep all-glueless walk stays under ~30).
+const MAX_CANDIDATES_PER_WALK = 50;
+// Must exceed one candidate's full retry budget (4 attempts x 3s = ~12s):
+// a single blackholed first server has to leave room to reach a healthy
+// fallback, while all-blackholed candidates stay bounded (~2 budgets).
+const FALLBACK_DEADLINE_MS = 15_000;
+
+// Name case and a trailing dot don't make a request distinct.
+const requestKey = (
+ domain: string,
+ recordType: RecordType,
+ nameserver: string,
+) => `${canonicalDnsName(domain)} ${recordType} ${nameserver}`;
+
+type SharedState<Response, Result> = {
+ deadlineAt: number;
+ remaining: number;
+ remainingSteps: number;
+ requests: Map<string, Promise<Response>>;
+ fallback?: Result;
+};
+
+// The state of one top-level authoritative walk and its rules. The deadline,
+// request cap, step budget, sent requests and fallback are shared by every
+// branch -- referrals and glueless sub-lookups inherit them -- so no
+// delegation level can stack a fresh allowance. Depth and ancestry belong to
+// one branch: each context is an immutable view of a branch, and deeper
+// steps get derived ones.
+//
+// Reusing a sent request (including a rejection) consumes neither another
+// slot nor packets. A new walk gets a fresh budget and can retry requests
+// that rejected (transport failures); resolved responses, rcode failures
+// included, stay cached by the resolver.
+export class WalkContext<Response, Result> {
+ private constructor(
+ private readonly shared: SharedState<Response, Result>,
+ private readonly depth: number,
+ // Request keys ("name type nameserver") and NS-name keys ("NS name")
+ // this branch descends from.
+ private readonly ancestors: ReadonlySet<string>,
+ ) {}
+
+ // Runs one walk under a fresh context. A recorded fallback is adopted only
+ // if the whole walk fails, so a sibling path that answers still wins.
+ static async run<Response, Result>(
+ walk: (context: WalkContext<Response, Result>) => Promise<Result>,
+ fallbackDeadlineMs = FALLBACK_DEADLINE_MS,
+ ): Promise<Result> {
+ const context = new WalkContext<Response, Result>(
+ {
+ deadlineAt: Date.now() + fallbackDeadlineMs,
+ remaining: MAX_CANDIDATES_PER_WALK,
+ remainingSteps: MAX_CANDIDATES_PER_WALK * MAX_RECURSION_DEPTH,
+ requests: new Map(),
+ },
+ 0,
+ new Set(),
+ );
+ try {
+ return await walk(context);
+ } catch (error) {
+ if (context.shared.fallback) return context.shared.fallback;
+ throw error;
+ }
+ }
+
+ // Counts one step of the walk. Cached responses cost no packets, but a
+ // crafted or misconfigured graph can still fork exponentially through
+ // shared requests, so processing is bounded separately from requests.
+ enter(domain: string, recordType: RecordType): void {
+ if (this.shared.remainingSteps-- <= 0) {
+ throw new Error(
+ `DNS traversal budget exhausted while resolving ${domain}`,
+ );
+ }
+ if (this.depth > MAX_RECURSION_DEPTH) {
+ throw new Error(
+ `Max recursion depth exceeded while resolving ${domain} (type ${recordType})`,
+ );
+ }
+ }
+
+ get hasStepsLeft(): boolean {
+ return this.shared.remainingSteps > 0;
+ }
+
+ // Past the deadline, a step stops trying fallback nameservers.
+ get pastDeadline(): boolean {
+ return Date.now() >= this.shared.deadlineAt;
+ }
+
+ // Sends a request at most once per walk; a repeat shares the pending
+ // response. 'loop' when this branch already descends from the same
+ // request: a circular glueless dependency, whose replies are cached after a
+ // round trip or two and then recurse in microtasks, is stopped before it
+ // spends the step budget a slower sibling needs. 'exhausted' once the
+ // walk's distinct-request cap is spent.
+ send(
+ domain: string,
+ recordType: RecordType,
+ nameserver: string,
+ load: () => Promise<Response>,
+ ): Promise<Response> | 'loop' | 'exhausted' {
+ const key = requestKey(domain, recordType, nameserver);
+ if (this.ancestors.has(key)) return 'loop';
+ let pending = this.shared.requests.get(key);
+ if (!pending) {
+ if (this.shared.remaining <= 0) return 'exhausted';
+ this.shared.remaining--;
+ pending = load();
+ this.shared.requests.set(key, pending);
+ }
+ return pending;
+ }
+
+ // Of `nameservers`, those this walk sent the request to.
+ queried(
+ domain: string,
+ recordType: RecordType,
+ nameservers: string[],
+ ): string[] {
+ return nameservers.filter((nameserver) =>
+ this.shared.requests.has(requestKey(domain, recordType, nameserver)),
+ );
+ }
+
+ // This branch, continuing from a response to the request.
+ via(
+ domain: string,
+ recordType: RecordType,
+ nameserver: string,
+ ): WalkContext<Response, Result> {
+ return this.branch(this.depth, requestKey(domain, recordType, nameserver));
+ }
+
+ // This branch, one delegation level down.
+ deeper(): WalkContext<Response, Result> {
+ return this.branch(this.depth + 1);
+ }
+
+ // A sub-walk resolving an NS hostname, one level down. An NS name already
+ // being resolved further up this branch is a circular dependency whichever
+ // servers it goes through, so it is stopped once rather than once per
+ // server ordering.
+ resolvingNs(name: string): WalkContext<Response, Result> {
+ const key = `NS ${canonicalDnsName(name)}`;
+ if (this.ancestors.has(key)) {
+ throw new Error(`circular NS dependency (${name})`);
+ }
+ return this.branch(this.depth + 1, key);
+ }
+
+ // The first fallback offered anywhere in the walk is kept; see run.
+ // ponytail: callers build a fallback even once one is kept and it is
+ // dropped here; that is a few strings per failed NS referral. Expose a
+ // check first if building one ever gets expensive.
+ offerFallback(result: Result): void {
+ this.shared.fallback ??= result;
+ }
+
+ private branch(depth: number, ancestor?: string) {
+ return new WalkContext(
+ this.shared,
+ depth,
+ ancestor ? new Set([...this.ancestors, ancestor]) : this.ancestors,
+ );
+ }
+}