Full Toolkit for Next-Level Domain Analysis
Show a lame domain's nameservers from its parent delegation 🧭 (#150)
Felix Wotschofsky279e3e3e2ac3
diff --git a/lib/resolvers/authoritative.test.ts b/lib/resolvers/authoritative.test.tsindex 1670793..6e91648 100644--- a/lib/resolvers/authoritative.test.ts+++ b/lib/resolvers/authoritative.test.ts@@ -728,6 +728,102 @@ describe('AuthoritativeResolver transport policy', () => {).toContain('192.0.2.2');});+ it('answers NS from the parent delegation when the zone itself is lame', async () => {+ // The parent delegates to servers that refuse the zone (e.g. NS left at+ // a provider that dropped it): NS still has the parent's answer, other+ // types still fail.+ const resolver = new AuthoritativeResolver({+ udpTransport: async ({ domain, recordType, nameserver }) =>+ nameserver === '192.0.2.1'+ ? ({+ ...response(domain, recordType, 'NOERROR'),+ authorities: [+ {+ name: 'example.eu',+ type: 'NS',+ ttl: 86400,+ data: 'ns1.example.net',+ },+ ],+ additionals: [+ {+ name: 'ns1.example.net',+ type: 'A',+ ttl: 300,+ data: '8.8.8.8',+ },+ ],+ } as DecodedPacket)+ : response(domain, recordType, 'REFUSED'),+ rootServers: async () => ['192.0.2.1'],+ });++ const ns = await resolver.resolveRecordType('example.eu', 'NS');+ expect(ns.records.map((record) => record.data)).toEqual([+ 'ns1.example.net',+ ]);+ expect(ns.trace.at(-1)).toContain('using parent delegation');+ await expect(+ resolver.resolveRecordType('example.eu', 'A'),+ ).rejects.toBeInstanceOf(UserFacingError);+ });++ it('prefers a healthy sibling path over a lame parent delegation', async () => {+ // Two parent servers disagree: one delegates to a server that refuses+ // the zone, the other to a healthy one. The healthy answer must win.+ const referral = (+ domain: string,+ recordType: RecordType,+ zone: string,+ ns: string,+ ip: string,+ ) =>+ ({+ ...response(domain, recordType, 'NOERROR'),+ authorities: [{ name: zone, type: 'NS', ttl: 300, data: ns }],+ additionals: [{ name: ns, type: 'A', ttl: 300, data: ip }],+ }) as DecodedPacket;+ const resolver = new AuthoritativeResolver({+ udpTransport: async ({ domain, recordType, nameserver }) => {+ switch (nameserver) {+ case '192.0.2.1':+ return referral(domain, recordType, 'eu', 'a.eu', '8.8.8.8');+ case '192.0.2.2':+ return referral(domain, recordType, 'eu', 'b.eu', '9.9.9.9');+ case '8.8.8.8':+ return referral(+ domain,+ recordType,+ 'example.eu',+ 'lame.net',+ '1.1.1.1',+ );+ case '9.9.9.9':+ return referral(+ domain,+ recordType,+ 'example.eu',+ 'ok.net',+ '1.0.0.1',+ );+ case '1.0.0.1':+ return {+ ...response(domain, recordType, 'NOERROR', [+ { name: domain, type: 'NS', ttl: 300, data: 'ok.net' },+ ]),+ flag_aa: true,+ } as DecodedPacket;+ default:+ return response(domain, recordType, 'REFUSED');+ }+ },+ rootServers: async () => ['192.0.2.1', '192.0.2.2'],+ });++ const ns = await resolver.resolveRecordType('example.eu', 'NS');+ expect(ns.records.map((record) => record.data)).toEqual(['ok.net']);+ });+it('falls back to a deferred answer when a later referral fails', async () => {// First root: a non-authoritative answer (deferred). Second root: an// in-bailiwick referral that then fails. The deferred answer must windiff --git a/lib/resolvers/authoritative.ts b/lib/resolvers/authoritative.tsindex 94ec796..8f5aa2a 100644--- a/lib/resolvers/authoritative.ts+++ b/lib/resolvers/authoritative.ts@@ -171,6 +171,10 @@ 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.@@ -216,6 +220,16 @@ export class AuthoritativeResolver extends DnsResolver {// 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 = {},) {@@ -559,13 +573,7 @@ export class AuthoritativeResolver extends DnsResolver {deadlineAt = Date.now() +(this.options.fallbackDeadlineMs ??AuthoritativeResolver.FALLBACK_DEADLINE_MS),- budget = {- remaining: AuthoritativeResolver.MAX_CANDIDATES_PER_WALK,- requests: new Map(),- remainingSteps:- AuthoritativeResolver.MAX_CANDIDATES_PER_WALK *- AuthoritativeResolver.MAX_RECURSION_DEPTH,- },+ budget = AuthoritativeResolver.newWalkBudget(),ancestors = new Set<string>(),}: FetchRecordsParams): Promise<WalkResult> {// Cached responses cost no packets, but a crafted or misconfigured graph@@ -603,8 +611,9 @@ export class AuthoritativeResolver extends DnsResolver {// The trace prefix any accepted response shares: the failures skipped to// reach it, the failed attempts retried at its server, plus a note when- // its UDP answer was truncated and retried over TCP. Both the in-loop- // accept and the post-loop lastResort adoption use it.+ // its UDP answer was truncated and retried over TCP. The in-loop accept,+ // the post-loop lastResort adoption and the parent-delegation fallback+ // use it.const buildAcceptedTrace = (candidate: string,{ truncated, retries }: DnsResponse,@@ -733,6 +742,31 @@ export class AuthoritativeResolver extends DnsResolver {failedNameservers.push(`${candidate}: referral failed (${describeError(error)})`,);+ // 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.+ const cut = canonicalDnsName(zone);+ if (+ recordType === 'NS' &&+ !budget.parentDelegation &&+ (!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 = {+ answers: delegation,+ zone,+ trace: [+ ...acceptedTrace,+ `${recordType} ${domain} @ ${candidate} -> referral failed (${describeError(error)}), using parent delegation: ${delegation.map(this.recordToString).join(', ')}`,+ ],+ };+ }continue;}}@@ -1200,11 +1234,17 @@ export class AuthoritativeResolver extends DnsResolver {// callers that need structured rdata (DNSSEC) rather than presentation// strings. With the dnssecOk option, the RRSIGs covering the answer come// back too.- public resolveAnswers(+ public async resolveAnswers(domain: string,recordType: RecordType,): Promise<WalkResult> {- return this.fetchRecordsRaw({ domain, recordType });+ const budget = AuthoritativeResolver.newWalkBudget();+ try {+ return await this.fetchRecordsRaw({ domain, recordType, budget });+ } catch (error) {+ if (budget.parentDelegation) return budget.parentDelegation;+ throw error;+ }}public override async resolveRecordTypes(