Skip to content

Full Toolkit for Next-Level Domain Analysis

Report failed record types from every resolver 🧩🌐 (#154)

Felix Wotschofsky187c706b175d
diff --git a/app/api/internal/resolve/base.test.ts b/app/api/internal/resolve/base.test.ts
new file mode 100644
index 0000000..b8e8903
--- /dev/null
+++ b/app/api/internal/resolve/base.test.ts
@@ -0,0 +1,94 @@
+// @vitest-environment node
+import { log } from 'evlog';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import { INCOMPLETE_LOOKUP_ERROR } from '@/lib/resolvers/base-doh';
+import { InternalDoHResolver } from '@/lib/resolvers/internal';
+import { UserFacingError } from '@/lib/user-facing-error';
+
+import { handler } from './base';
+
+vi.mock('@/env', () => ({
+ env: { SITE_URL: 'https://example.com', INTERNAL_API_SECRET: 'secret' },
+}));
+
+// The DoH answer for each record type, keyed by type name.
+const upstream: Record<string, object> = {};
+
+describe('internal resolve route', () => {
+ beforeEach(() => {
+ vi.spyOn(log, 'warn').mockImplementation(() => {});
+ // Requests to the route reach its handler; the rest go to the DoH resolver.
+ vi.stubGlobal('fetch', async (input: URL, init?: RequestInit) =>
+ input.pathname.startsWith('/api/internal/resolve/')
+ ? handler(new Request(input, { headers: init?.headers }))
+ : Response.json(upstream[input.searchParams.get('type')!]),
+ );
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+ });
+
+ it('passes a failed type through with its error', async () => {
+ upstream.A = {
+ Status: 0,
+ Answer: [{ name: 'example.com', type: 1, TTL: 300, data: '192.0.2.1' }],
+ };
+ upstream.AAAA = { Status: 2 };
+
+ const results = await new InternalDoHResolver(
+ 'fra',
+ 'cloudflare',
+ ).resolveRecordTypes('example.com', ['A', 'AAAA']);
+
+ expect(results.A.records.map((record) => record.data)).toEqual([
+ '192.0.2.1',
+ ]);
+ expect(results.AAAA).toEqual({
+ records: [],
+ trace: [],
+ error: INCOMPLETE_LOOKUP_ERROR,
+ });
+ });
+
+ it('passes an RRSIG lookup the resolver could not answer through as empty', async () => {
+ upstream.A = {
+ Status: 0,
+ Answer: [{ name: 'example.com', type: 1, TTL: 300, data: '192.0.2.1' }],
+ };
+ upstream.RRSIG = { Status: 2 };
+
+ const results = await new InternalDoHResolver(
+ 'fra',
+ 'cloudflare',
+ ).resolveRecordTypes('example.com', ['A', 'RRSIG']);
+
+ expect(results.RRSIG.records).toEqual([]);
+ expect(results.RRSIG.error).toBeUndefined();
+ });
+
+ it('answers a lookup where every type failed with its error, which the caller rethrows', async () => {
+ upstream.A = { Status: 2 };
+ upstream.AAAA = { Status: 2 };
+
+ const response = await handler(
+ new Request(
+ 'https://example.com/api/internal/resolve/fra?resolver=cloudflare&type=A&type=AAAA&domain=example.com',
+ { headers: { Authorization: 'secret' } },
+ ),
+ );
+ expect(response.status).toBe(502);
+ expect(await response.json()).toEqual(INCOMPLETE_LOOKUP_ERROR);
+
+ const lookup = new InternalDoHResolver(
+ 'fra',
+ 'cloudflare',
+ ).resolveRecordTypes('example.com', ['A', 'AAAA']);
+ await expect(lookup).rejects.toBeInstanceOf(UserFacingError);
+ await expect(lookup).rejects.toMatchObject({
+ payload: INCOMPLETE_LOOKUP_ERROR,
+ });
+ });
+});
diff --git a/app/api/internal/resolve/base.ts b/app/api/internal/resolve/base.ts
index 89bc493..1f498f4 100644
--- a/app/api/internal/resolve/base.ts
+++ b/app/api/internal/resolve/base.ts
@@ -6,6 +6,7 @@ import { AlibabaDoHResolver } from '@/lib/resolvers/alibaba';
import { type RecordType } from '@/lib/resolvers/base';
import { CloudflareDoHResolver } from '@/lib/resolvers/cloudflare';
import { GoogleDoHResolver } from '@/lib/resolvers/google';
+import { UserFacingError } from '@/lib/user-facing-error';
if (env.NODE_ENV === 'production' && !env.INTERNAL_API_SECRET) {
throw new Error('INTERNAL_API_SECRET is required in production');
@@ -89,10 +90,16 @@ export const handler = async (request: Request) => {
: resolverName === 'cloudflare'
? new CloudflareDoHResolver()
: new GoogleDoHResolver();
- const response = await resolver.resolveRecordTypes(
- domain,
- types as RecordType[],
- );
-
- return NextResponse.json(response);
+ try {
+ const response = await resolver.resolveRecordTypes(
+ domain,
+ types as RecordType[],
+ );
+ return NextResponse.json(response);
+ } catch (error) {
+ // Every type failed: pass the reason on for InternalDoHResolver to
+ // rethrow, instead of a bare 500 that reads as "resolver unavailable".
+ if (!(error instanceof UserFacingError)) throw error;
+ return NextResponse.json(error.payload, { status: 502 });
+ }
};
diff --git a/app/lookup/[domain]/map/_components/results-globe.tsx b/app/lookup/[domain]/map/_components/results-globe.tsx
index 26294e4..d228a52 100644
--- a/app/lookup/[domain]/map/_components/results-globe.tsx
+++ b/app/lookup/[domain]/map/_components/results-globe.tsx
@@ -8,13 +8,20 @@ import { useRouter } from 'next/navigation';
import { type FC, type RefObject, useEffect, useRef, useState } from 'react';
import type { GlobeMethods } from 'react-globe.gl';
+import type { MapLookupResults } from '@/lib/map';
import { cn } from '@/lib/utils';
import styles from './results-globe.module.css';
const Globe = dynamic(() => import('react-globe.gl'), { ssr: false });
-const createMarkerResults = (results: string[]) => {
+const createMarkerResults = (type: string, results: string[] | null) => {
+ if (results === null) {
+ return h(
+ 'p.text-xs.text-zinc-500.dark:text-zinc-400.italic',
+ `${type} lookup failed`,
+ );
+ }
if (results.length === 0) {
return undefined;
}
@@ -39,11 +46,7 @@ const createMarkerResults = (results: string[]) => {
const createMarker = (
label: string,
- results: {
- A: string[];
- AAAA: string[];
- CNAME: string[];
- },
+ results: MapLookupResults,
href: string,
onClick: () => void,
) =>
@@ -70,13 +73,11 @@ const createMarker = (
h(['h3', 'text-sm', 'font-bold'].join('.'), label),
- createMarkerResults(results.A),
- createMarkerResults(results.AAAA),
- createMarkerResults(results.CNAME),
+ createMarkerResults('A', results.A),
+ createMarkerResults('AAAA', results.AAAA),
+ createMarkerResults('CNAME', results.CNAME),
- results.A.length === 0 &&
- results.AAAA.length === 0 &&
- results.CNAME.length === 0
+ Object.values(results).every((values) => values?.length === 0)
? h(
[
'p',
@@ -113,11 +114,7 @@ type ResultsGlobeProps = {
name: string;
lat: number;
lng: number;
- results: {
- A: string[];
- AAAA: string[];
- CNAME: string[];
- };
+ results: MapLookupResults;
}[];
};
diff --git a/app/lookup/[domain]/map/page.test.tsx b/app/lookup/[domain]/map/page.test.tsx
new file mode 100644
index 0000000..9645fab
--- /dev/null
+++ b/app/lookup/[domain]/map/page.test.tsx
@@ -0,0 +1,84 @@
+// @vitest-environment node
+import { log } from 'evlog';
+import { renderToStaticMarkup } from 'react-dom/server';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import type { RecordType } from '@/lib/resolvers/base';
+
+import MapResultsPage from './page';
+
+// The record types each region fails to resolve, keyed by region code.
+const failing = vi.hoisted(() => new Map<string, RecordType[]>());
+
+vi.mock('next/headers', () => ({
+ headers: async () =>
+ new Headers({
+ 'user-agent':
+ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36',
+ }),
+}));
+
+vi.mock('./_components/results-globe', () => ({ ResultsGlobe: () => null }));
+
+// Lookup analytics needs the server env and request scope.
+vi.mock('@/lib/search', () => ({ recordLookupAfter: async () => {} }));
+
+vi.mock('@/lib/resolvers/internal', async () => {
+ const { DnsResolver } = await import('@/lib/resolvers/base');
+ const { UserFacingError } = await import('@/lib/user-facing-error');
+ const { INCOMPLETE_LOOKUP_ERROR } = await import('@/lib/resolvers/base-doh');
+
+ // Every region answers the same records, except for its failing types.
+ class InternalDoHResolver extends DnsResolver {
+ constructor(private readonly location: string) {
+ super();
+ }
+
+ public async resolveRecordType(domain: string, type: RecordType) {
+ if (failing.get(this.location)?.includes(type)) {
+ throw new UserFacingError(INCOMPLETE_LOOKUP_ERROR);
+ }
+ return {
+ records: [{ name: domain, type, TTL: 300, data: `${type} data` }],
+ trace: [],
+ };
+ }
+ }
+
+ return { InternalDoHResolver };
+});
+
+const renderPage = async () =>
+ renderToStaticMarkup(
+ await MapResultsPage({
+ params: Promise.resolve({ domain: 'example.com' }),
+ }),
+ );
+
+describe('map results page', () => {
+ beforeEach(() => {
+ vi.spyOn(log, 'warn').mockImplementation(() => {});
+ });
+
+ afterEach(() => {
+ failing.clear();
+ vi.restoreAllMocks();
+ });
+
+ it('confirms propagation when every region answered alike', async () => {
+ expect(await renderPage()).toContain('All records are the same!');
+ });
+
+ it.each<[string, RecordType[]]>([
+ ['a whole region failed', ['A', 'AAAA', 'CNAME']],
+ ['one type failed in one region', ['A']],
+ ])('does not confirm propagation when %s', async (_, types) => {
+ failing.set('fra', types);
+
+ const html = await renderPage();
+
+ expect(html).not.toContain('All records are the same');
+ expect(html).not.toContain('propagation was successful');
+ expect(html).toContain('Some lookups failed');
+ });
+});
diff --git a/app/lookup/[domain]/map/page.tsx b/app/lookup/[domain]/map/page.tsx
index 47ff1c9..ae0f153 100644
--- a/app/lookup/[domain]/map/page.tsx
+++ b/app/lookup/[domain]/map/page.tsx
@@ -1,5 +1,10 @@
import { log } from 'evlog';
-import { CheckCircleIcon, InfoIcon, ShieldAlertIcon } from 'lucide-react';
+import {
+ CheckCircleIcon,
+ InfoIcon,
+ ShieldAlertIcon,
+ TriangleAlertIcon,
+} from 'lucide-react';
import type { Metadata } from 'next';
import { headers } from 'next/headers';
import type { FC } from 'react';
@@ -75,6 +80,19 @@ const MapResultsPage: FC<MapResultsPageProps> = async ({ params }) => {
<br /> Keep in mind however, that some providers rotate their IP
addresses, which can also lead to different results.
</IconAlert>
+ ) : lookupResults.some(({ results }) =>
+ // null marks a failed lookup, which can't confirm propagation.
+ Object.values(results).includes(null),
+ ) ? (
+ <IconAlert
+ className="mx-auto mt-12"
+ icon={TriangleAlertIcon}
+ title="Some lookups failed"
+ >
+ The regions that answered all have the same records, but not every
+ region could be checked, so propagation can&apos;t be confirmed.
+ Failed lookups are marked on the map.
+ </IconAlert>
) : (
<IconAlert
className="mx-auto mt-12"
diff --git a/lib/ips.ts b/lib/ips.ts
index f2efaf9..e6d825e 100644
--- a/lib/ips.ts
+++ b/lib/ips.ts
@@ -1,6 +1,7 @@
import DataLoader from 'dataloader';
import isIP from 'validator/lib/isIP';
+import { INCOMPLETE_LOOKUP_ERROR } from './resolvers/base-doh';
import { CloudflareDoHResolver } from './resolvers/cloudflare';
import { UserFacingError } from './user-facing-error';
@@ -118,7 +119,12 @@ export const lookupReverse = async (ip: string): Promise<string[]> => {
);
return records.map((record) => record.data.replace(/\.$/, ''));
} catch (error) {
- if (error instanceof UserFacingError) throw error;
+ if (error instanceof UserFacingError) {
+ // A reverse zone that fails to resolve (often a lame delegation) has
+ // no hostnames to show; the IP's other details are still worth it.
+ if (error.payload === INCOMPLETE_LOOKUP_ERROR) return [];
+ throw error;
+ }
throw new UserFacingError(
{
title: "Couldn't reach Cloudflare DNS",
diff --git a/lib/map.test.ts b/lib/map.test.ts
index 5115960..d5d51fe 100644
--- a/lib/map.test.ts
+++ b/lib/map.test.ts
@@ -1,6 +1,13 @@
+import { log } from 'evlog';
import { describe, expect, it, vi } from 'vitest';
import { getGlobalLookupResults, getHasDifferences } from './map';
+import {
+ DnsResolver,
+ type RecordType,
+ type ResolverResponse,
+} from './resolvers/base';
+import { UserFacingError } from './user-facing-error';
vi.mock('./data', () => ({
REGIONS: {
@@ -61,6 +68,71 @@ describe('getGlobalLookupResults', () => {
});
});
+// Answers every type the same way in each region, except the failing ones,
+// which fail like a SERVFAIL from a DoH resolver.
+class RegionResolver extends DnsResolver {
+ constructor(private readonly failing: readonly RecordType[] = []) {
+ super();
+ }
+
+ public async resolveRecordType(
+ domain: string,
+ type: RecordType,
+ ): Promise<ResolverResponse> {
+ if (this.failing.includes(type)) {
+ throw new UserFacingError({
+ title: 'DNS resolver could not complete the lookup',
+ description: 'SERVFAIL',
+ });
+ }
+ return {
+ records: [{ name: domain, type, TTL: 300, data: `${type} data` }],
+ trace: [],
+ };
+ }
+}
+
+describe('getGlobalLookupResults with failures', () => {
+ vi.spyOn(log, 'warn').mockImplementation(() => {});
+
+ it('treats a failed type as unknown rather than as different records', async () => {
+ const results = await getGlobalLookupResults('example.com', (code) =>
+ code === 'fra' ? new RegionResolver(['A']) : new RegionResolver(),
+ );
+ expect(results.map((region) => region.results)).toEqual([
+ { A: null, AAAA: ['AAAA data'], CNAME: ['CNAME data'] },
+ { A: ['A data'], AAAA: ['AAAA data'], CNAME: ['CNAME data'] },
+ ]);
+ expect(getHasDifferences(results.map((region) => region.results))).toBe(
+ false,
+ );
+ });
+
+ it('keeps the other regions when one region fails entirely', async () => {
+ const results = await getGlobalLookupResults('example.com', (code) =>
+ code === 'fra'
+ ? new RegionResolver(['A', 'AAAA', 'CNAME'])
+ : new RegionResolver(),
+ );
+ expect(results.map((region) => region.results)).toEqual([
+ { A: null, AAAA: null, CNAME: null },
+ { A: ['A data'], AAAA: ['AAAA data'], CNAME: ['CNAME data'] },
+ ]);
+ expect(getHasDifferences(results.map((region) => region.results))).toBe(
+ false,
+ );
+ });
+
+ it('throws when every region fails', async () => {
+ await expect(
+ getGlobalLookupResults(
+ 'example.com',
+ () => new RegionResolver(['A', 'AAAA', 'CNAME']),
+ ),
+ ).rejects.toBeInstanceOf(UserFacingError);
+ });
+});
+
describe('getHasDifferences', () => {
it('should detect differences between DNS record sets', () => {
const markers = [
diff --git a/lib/map.ts b/lib/map.ts
index 9528fc3..c4ba50a 100644
--- a/lib/map.ts
+++ b/lib/map.ts
@@ -1,49 +1,71 @@
+import { log } from 'evlog';
+
import { REGIONS } from '@/lib/data';
import type { DnsResolver } from '@/lib/resolvers/base';
+import { UserFacingError } from '@/lib/user-facing-error';
+import { serializeError } from '@/lib/utils';
const DISPLAYED_RECORD_TYPES = ['A', 'AAAA', 'CNAME'] as const;
-export const getGlobalLookupResults = (
+// null marks a failed lookup: unknown, not a confirmed empty RRset.
+export type MapLookupResults = Record<
+ (typeof DISPLAYED_RECORD_TYPES)[number],
+ string[] | null
+>;
+
+const FAILED_RESULTS: MapLookupResults = { A: null, AAAA: null, CNAME: null };
+
+// One region's failure only marks that region; throws if every region failed.
+export const getGlobalLookupResults = async (
domain: string,
resolverFactory: (regionCode: string) => DnsResolver,
-) =>
- Promise.all(
- Object.entries(REGIONS).map(async ([code, data]) => {
- const resolver = resolverFactory(code);
- const results = await resolver.resolveRecordTypes(
- domain,
- DISPLAYED_RECORD_TYPES,
- );
-
- return {
- ...data,
- code,
- results: {
- A: results.A.records.map((r) => r.data),
- AAAA: results.AAAA.records.map((r) => r.data),
- CNAME: results.CNAME.records.map((r) => r.data),
- },
- };
- }),
+) => {
+ const regions = Object.entries(REGIONS);
+ const settled = await Promise.allSettled(
+ regions.map(([code]) =>
+ resolverFactory(code).resolveRecordTypes(domain, DISPLAYED_RECORD_TYPES),
+ ),
);
+ const failures = settled.filter(
+ (result): result is PromiseRejectedResult => result.status === 'rejected',
+ );
+ if (failures.length === settled.length) throw failures[0].reason;
-export const getHasDifferences = (
- markers: { A: string[]; AAAA: string[]; CNAME: string[] }[],
-) => {
- let hasDifferentRecords = false;
- for (let i = 1; i < markers.length; i++) {
- const previous = markers[i - 1];
- const current = markers[i];
- if (
- previous.A.toSorted().toString() !== current.A.toSorted().toString() ||
- previous.AAAA.toSorted().toString() !==
- current.AAAA.toSorted().toString() ||
- previous.CNAME.toSorted().toString() !==
- current.CNAME.toSorted().toString()
- ) {
- hasDifferentRecords = true;
- break;
+ return regions.map(([code, data], index) => {
+ const result = settled[index];
+ if (result.status === 'rejected') {
+ log[result.reason instanceof UserFacingError ? 'warn' : 'error']({
+ message: 'map_region_lookup_failed',
+ domain,
+ region: code,
+ error: serializeError(result.reason),
+ });
+ return { ...data, code, results: FAILED_RESULTS };
}
- }
- return hasDifferentRecords;
+ const records = (type: (typeof DISPLAYED_RECORD_TYPES)[number]) =>
+ result.value[type].error
+ ? null
+ : result.value[type].records.map((r) => r.data);
+ return {
+ ...data,
+ code,
+ results: {
+ A: records('A'),
+ AAAA: records('AAAA'),
+ CNAME: records('CNAME'),
+ },
+ };
+ });
};
+
+// Failed lookups are unknown, so they never count as a difference.
+export const getHasDifferences = (markers: MapLookupResults[]) =>
+ DISPLAYED_RECORD_TYPES.some(
+ (type) =>
+ new Set(
+ markers.flatMap((marker) => {
+ const values = marker[type];
+ return values ? [values.toSorted().toString()] : [];
+ }),
+ ).size > 1,
+ );
diff --git a/lib/resolvers/authoritative-partial.test.ts b/lib/resolvers/authoritative-partial.test.ts
index 7c21bcf..9d447ad 100644
--- a/lib/resolvers/authoritative-partial.test.ts
+++ b/lib/resolvers/authoritative-partial.test.ts
@@ -3,7 +3,6 @@ import type { Answer, DecodedPacket } from 'dns-packet';
import { log } from 'evlog';
import { afterEach, describe, expect, it, vi } from 'vitest';
-import { UserFacingError } from '../user-facing-error';
import { AuthoritativeResolver } from './authoritative';
import type { RecordType } from './base';
@@ -78,53 +77,4 @@ describe('AuthoritativeResolver partial results', () => {
}),
]);
});
-
- it('does not turn a failed lookup with only empty successful types into no records', async () => {
- const resolver = new AuthoritativeResolver({
- rootServers: async () => ['192.0.2.1'],
- udpTransport: async ({ domain, recordType }) =>
- reply(
- domain,
- recordType,
- [],
- recordType === 'A' ? 'REFUSED' : 'NOERROR',
- ),
- });
- await expect(
- resolver.resolveRecordTypes('example.com', ['A', 'AAAA']),
- ).rejects.toBeInstanceOf(UserFacingError);
- });
-
- it('does not serialize unexpected internal errors into partial results', async () => {
- const logWarn = vi.spyOn(log, 'warn').mockImplementation(() => {});
- const logError = vi.spyOn(log, 'error').mockImplementation(() => {});
- const resolver = new AuthoritativeResolver({
- rootServers: async () => ['192.0.2.1'],
- udpTransport: async ({ domain, recordType }) =>
- reply(domain, recordType, [address(domain)]),
- });
- const singleType = resolver.resolveRecordType.bind(resolver);
- vi.spyOn(resolver, 'resolveRecordType').mockImplementation(
- (domain, type) =>
- type === 'PTR'
- ? Promise.reject(new Error('private transport details'))
- : singleType(domain, type),
- );
- const result = await resolver.resolveRecordTypes('example.com', [
- 'A',
- 'PTR',
- ]);
- expect(result.PTR.error?.title).toBe('DNS lookup failed');
- expect(JSON.stringify(result)).not.toContain('private transport details');
- expect(loggedFailures(logWarn)).toEqual([]);
- expect(loggedFailures(logError)).toEqual([
- expect.objectContaining({
- recordType: 'PTR',
- error: expect.objectContaining({
- message: 'private transport details',
- stack: expect.any(String),
- }),
- }),
- ]);
- });
});
diff --git a/lib/resolvers/authoritative.ts b/lib/resolvers/authoritative.ts
index 7c4ad94..56956d5 100644
--- a/lib/resolvers/authoritative.ts
+++ b/lib/resolvers/authoritative.ts
@@ -10,20 +10,15 @@ import dnsPacket, {
type RrsigData,
type StringAnswer,
} from 'dns-packet';
-import { log } from 'evlog';
-import { retry, serializeError } from '@/lib/utils';
+import { retry } from '@/lib/utils';
-import {
- UserFacingError,
- type UserFacingErrorPayload,
-} from '../user-facing-error';
+import { UserFacingError } from '../user-facing-error';
import {
canonicalDnsName,
DnsResolver,
type RawRecord,
type RecordType,
- type ResolverMultiResponse,
type ResolverResponse,
} from './base';
import { isPublicIp } from './ip-filter';
@@ -177,13 +172,6 @@ type FetchRecordsParams = {
walk: Walk;
};
-const FALLBACK_LOOKUP_ERROR: UserFacingErrorPayload = {
- title: 'DNS lookup failed',
- description:
- 'This record type could not be retrieved. Please try again shortly.',
- retryable: true,
-};
-
export class AuthoritativeResolver extends DnsResolver {
public constructor(
private readonly options: AuthoritativeResolverOptions = {},
@@ -1137,45 +1125,6 @@ export class AuthoritativeResolver extends DnsResolver {
);
}
- public override async resolveRecordTypes(
- domain: string,
- types: readonly RecordType[],
- ): Promise<ResolverMultiResponse> {
- const results = await Promise.allSettled(
- types.map((type) => this.resolveRecordType(domain, type)),
- );
- const firstFailure = results.find((result) => result.status === 'rejected');
- const hasRecords = results.some(
- (result) =>
- result.status === 'fulfilled' && result.value.records.length > 0,
- );
- // Empty successful types must not turn an entirely unusable lookup into
- // a misleading "no records" page (e.g. a broken Route 53 delegation).
- if (firstFailure && !hasRecords) throw firstFailure.reason;
-
- const response: ResolverMultiResponse = {};
- for (const [index, result] of results.entries()) {
- if (result.status === 'fulfilled') {
- response[types[index]] = result.value;
- continue;
- }
- const { reason } = result;
- const expected = reason instanceof UserFacingError;
- // Not rethrown, so onRequestError never sees it. Expected failures
- // (e.g. a refused PTR) only warn, so error alerts stay reserved for
- // bugs.
- log[expected ? 'warn' : 'error']({
- message: 'record_type_lookup_failed',
- domain,
- recordType: types[index],
- error: serializeError(reason),
- });
- const error = expected ? reason.payload : FALLBACK_LOOKUP_ERROR;
- response[types[index]] = { records: [], trace: [], error };
- }
- return response;
- }
-
public async resolveRecordType(
domain: string,
recordType: RecordType,
diff --git a/lib/resolvers/base-doh.test.ts b/lib/resolvers/base-doh.test.ts
index 4bf1353..38eafec 100644
--- a/lib/resolvers/base-doh.test.ts
+++ b/lib/resolvers/base-doh.test.ts
@@ -1,6 +1,11 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { BaseDoHResolver, type DoHAnswer, type DoHResponse } from './base-doh';
+import {
+ BaseDoHResolver,
+ type DoHAnswer,
+ type DoHResponse,
+ INCOMPLETE_LOOKUP_ERROR,
+} from './base-doh';
describe('BaseDoHResolver', () => {
const fetchMock = vi.fn();
@@ -186,13 +191,16 @@ describe('BaseDoHResolver', () => {
await expect(
resolver.resolveAnswers('example.com', 'TXT'),
).rejects.toThrow();
+ await expect(
+ resolver.resolveRecordType('example.com', 'TXT'),
+ ).rejects.toThrow();
});
- it('keeps the other record types when the resolver cannot answer one', async () => {
+ it('marks a type the resolver cannot answer as failed, not empty', async () => {
fetchMock.mockImplementation(async (url: URL) => ({
ok: true,
json: async () =>
- url.searchParams.get('type') === 'RRSIG'
+ url.searchParams.get('type') === 'TXT'
? { Status: 2 }
: mockDoHResponse([{ type: 1 }]),
url: url.href,
@@ -200,15 +208,57 @@ describe('BaseDoHResolver', () => {
const resolver = new TestDoHResolver();
const results = await resolver.resolveRecordTypes('example.com', [
'A',
- 'RRSIG',
+ 'TXT',
]);
expect(results.A.records).toHaveLength(1);
- expect(results.RRSIG).toEqual({
+ expect(results.TXT).toEqual({
records: [],
- trace: [
- 'HTTPS GET https://dns.google/resolve?name=example.com&type=RRSIG -> DNS status 2',
- ],
+ trace: [],
+ error: INCOMPLETE_LOOKUP_ERROR,
+ });
+ });
+
+ it.each([{ Status: 2 }, { Status: 0, TC: true }])(
+ 'reads an RRSIG lookup the resolver cannot answer as empty: %j',
+ async (body) => {
+ fetchMock.mockResolvedValue({
+ ok: true,
+ json: async () => body,
+ url: 'https://dns.google/resolve?name=example.com&type=RRSIG',
+ });
+ const resolver = new TestDoHResolver();
+ const result = await resolver.resolveRecordType('example.com', 'RRSIG');
+ expect(result.records).toEqual([]);
+ expect(result.error).toBeUndefined();
+ expect(result.trace).toEqual([
+ expect.stringMatching(/type=RRSIG -> (DNS status 2|Truncated)/),
+ ]);
+ },
+ );
+
+ it('still fails an RRSIG lookup the resolver could not be reached for', async () => {
+ fetchMock.mockResolvedValue({
+ ok: false,
+ status: 503,
+ statusText: 'Service Unavailable',
+ url: 'https://dns.google/resolve',
});
+ const resolver = new TestDoHResolver();
+ await expect(
+ resolver.resolveRecordType('example.com', 'RRSIG'),
+ ).rejects.toThrow();
+ });
+
+ it('still fails a lookup where only RRSIG came back, and empty', async () => {
+ fetchMock.mockResolvedValue({
+ ok: true,
+ json: async () => ({ Status: 2 }),
+ url: 'https://dns.google/resolve',
+ });
+ const resolver = new TestDoHResolver();
+ await expect(
+ resolver.resolveRecordTypes('example.com', ['A', 'RRSIG']),
+ ).rejects.toMatchObject({ payload: INCOMPLETE_LOOKUP_ERROR });
});
it.each([
diff --git a/lib/resolvers/base-doh.ts b/lib/resolvers/base-doh.ts
index 1f29860..927a01a 100644
--- a/lib/resolvers/base-doh.ts
+++ b/lib/resolvers/base-doh.ts
@@ -1,7 +1,10 @@
import { z } from 'zod';
import { RECORD_TYPES_BY_DECIMAL } from '../data';
-import { UserFacingError } from '../user-facing-error';
+import {
+ UserFacingError,
+ type UserFacingErrorPayload,
+} from '../user-facing-error';
import { DnsResolver, type RecordType, type ResolverResponse } from './base';
const answerSchema = z.object({
@@ -33,6 +36,15 @@ export type DoHResolverOptions = {
const recordTypes: Readonly<Record<number, RecordType | undefined>> =
RECORD_TYPES_BY_DECIMAL;
+// The resolver answered, but with an error status (e.g. SERVFAIL for a
+// broken DNSSEC chain) or a truncated response.
+export const INCOMPLETE_LOOKUP_ERROR: UserFacingErrorPayload = {
+ title: 'DNS resolver could not complete the lookup',
+ description:
+ 'The DNS resolver returned an error or incomplete response. Please try again shortly.',
+ retryable: true,
+};
+
export abstract class BaseDoHResolver extends DnsResolver {
constructor(
private readonly endpoint: string,
@@ -105,26 +117,25 @@ export abstract class BaseDoHResolver extends DnsResolver {
): Promise<DoHResolverResponse> {
const { failure, ...response } = await this.query(domain, type);
if (failure) {
- throw new UserFacingError(
- {
- title: 'DNS resolver could not complete the lookup',
- description:
- 'The DNS resolver returned an error or incomplete response. Please try again shortly.',
- retryable: true,
- },
- { cause: new Error(failure) },
- );
+ throw new UserFacingError(INCOMPLETE_LOOKUP_ERROR, {
+ cause: new Error(failure),
+ });
}
return response;
}
- // A DNS-level failure yields no records and is noted in the trace, so one
- // unanswerable type (e.g. SERVFAIL for RRSIG) cannot fail a whole batch.
+ // Throws on a DNS-level failure, except for RRSIG: Cloudflare and Google
+ // SERVFAIL direct RRSIG queries for healthy zones (e.g. Cloudflare-hosted
+ // ones), so a failed RRSIG lookup reads as empty (noted in the trace), like
+ // a refused one on the authoritative path. HTTP failures still throw.
public async resolveRecordType(
domain: string,
type: RecordType,
): Promise<ResolverResponse> {
- const { answers, trace } = await this.query(domain, type);
+ const { answers, trace } =
+ type === 'RRSIG'
+ ? await this.query(domain, type)
+ : await this.resolveAnswers(domain, type);
const records = answers
.filter((answer) => recordTypes[answer.type] === type)
diff --git a/lib/resolvers/base.test.ts b/lib/resolvers/base.test.ts
index dddb6f4..bc0cf12 100644
--- a/lib/resolvers/base.test.ts
+++ b/lib/resolvers/base.test.ts
@@ -1,6 +1,8 @@
-import { describe, expect, it } from 'vitest';
+import { log } from 'evlog';
+import { afterEach, describe, expect, it, vi } from 'vitest';
import { ALL_RECORD_TYPES } from '@/lib/data';
+import { UserFacingError } from '@/lib/user-facing-error';
import { DnsResolver, type RecordType, type ResolverResponse } from './base';
@@ -23,7 +25,45 @@ class MockResolver extends DnsResolver {
}
}
+// Resolves each type to its listed values, or rejects with its error.
+class FakeResolver extends DnsResolver {
+ constructor(
+ private readonly outcomes: Partial<Record<RecordType, string[] | Error>>,
+ ) {
+ super();
+ }
+
+ public async resolveRecordType(
+ domain: string,
+ type: RecordType,
+ ): Promise<ResolverResponse> {
+ const outcome = this.outcomes[type] ?? [];
+ if (outcome instanceof Error) throw outcome;
+ return {
+ records: outcome.map((data) => ({ name: domain, type, TTL: 300, data })),
+ trace: [],
+ };
+ }
+}
+
+const refused = {
+ title: 'Lookup refused',
+ description: 'The server refused the lookup.',
+ retryable: true,
+};
+
+// evlog JSON-stringifies events in production, so only what survives a JSON
+// round-trip reaches the log.
+const loggedFailures = (spy: { mock: { calls: unknown[][] } }) =>
+ spy.mock.calls
+ .map(([event]) => JSON.parse(JSON.stringify(event)))
+ .filter((event) => event.message === 'record_type_lookup_failed');
+
describe('DnsResolver', () => {
+ afterEach(() => {
+ vi.restoreAllMocks();
+ });
+
describe('resolveRecordType', () => {
it('should resolve a specific record type for a domain', async () => {
const resolver = new MockResolver();
@@ -80,5 +120,81 @@ describe('DnsResolver', () => {
expect(resolvedRecords['A'].records[0].data).toBe('data-for-A');
expect(resolvedRecords['AAAA'].records[0].data).toBe('data-for-AAAA');
});
+
+ it('keeps resolved types and marks each failed type with its error', async () => {
+ vi.spyOn(log, 'warn').mockImplementation(() => {});
+ const resolver = new FakeResolver({
+ A: ['192.0.2.1'],
+ PTR: new UserFacingError(refused),
+ });
+
+ const result = await resolver.resolveRecordTypes('example.com', [
+ 'A',
+ 'PTR',
+ ]);
+
+ expect(result.A.records[0].data).toBe('192.0.2.1');
+ expect(result.A.error).toBeUndefined();
+ expect(result.PTR).toEqual({ records: [], trace: [], error: refused });
+ expect(JSON.parse(JSON.stringify(result))).toEqual(result);
+ });
+
+ it('throws the first failure when every type fails', async () => {
+ const first = new UserFacingError(refused);
+ const resolver = new FakeResolver({ A: first, AAAA: new Error('bug') });
+
+ await expect(
+ resolver.resolveRecordTypes('example.com', ['A', 'AAAA']),
+ ).rejects.toBe(first);
+ });
+
+ it('throws when the only successes are empty, instead of reporting no records', async () => {
+ const failure = new UserFacingError(refused);
+ const resolver = new FakeResolver({ A: failure, AAAA: [] });
+
+ await expect(
+ resolver.resolveRecordTypes('example.com', ['A', 'AAAA']),
+ ).rejects.toBe(failure);
+ });
+
+ it('warns for expected failures and errors for bugs without exposing them', async () => {
+ const logWarn = vi.spyOn(log, 'warn').mockImplementation(() => {});
+ const logError = vi.spyOn(log, 'error').mockImplementation(() => {});
+ const resolver = new FakeResolver({
+ A: ['192.0.2.1'],
+ PTR: new UserFacingError(refused, { cause: new Error('192.0.2.53') }),
+ TXT: new Error('private transport details'),
+ });
+
+ const result = await resolver.resolveRecordTypes('example.com', [
+ 'A',
+ 'PTR',
+ 'TXT',
+ ]);
+
+ expect(result.PTR.error).toEqual(refused);
+ expect(result.TXT.error?.title).toBe('DNS lookup failed');
+ expect(JSON.stringify(result)).not.toContain('private transport details');
+ expect(loggedFailures(logWarn)).toEqual([
+ expect.objectContaining({
+ domain: 'example.com',
+ recordType: 'PTR',
+ error: expect.objectContaining({
+ message: refused.title,
+ stack: expect.any(String),
+ cause: expect.stringContaining('192.0.2.53'),
+ }),
+ }),
+ ]);
+ expect(loggedFailures(logError)).toEqual([
+ expect.objectContaining({
+ recordType: 'TXT',
+ error: expect.objectContaining({
+ message: 'private transport details',
+ stack: expect.any(String),
+ }),
+ }),
+ ]);
+ });
});
});
diff --git a/lib/resolvers/base.ts b/lib/resolvers/base.ts
index cea21d2..198b2a9 100644
--- a/lib/resolvers/base.ts
+++ b/lib/resolvers/base.ts
@@ -1,5 +1,11 @@
+import { log } from 'evlog';
+
import type { ALL_RECORD_TYPES } from '@/lib/data';
-import type { UserFacingErrorPayload } from '@/lib/user-facing-error';
+import {
+ UserFacingError,
+ type UserFacingErrorPayload,
+} from '@/lib/user-facing-error';
+import { serializeError } from '@/lib/utils';
export type RecordType = (typeof ALL_RECORD_TYPES)[number];
@@ -17,36 +23,68 @@ export type RawRecord = {
export type ResolverResponse = {
records: RawRecord[];
trace: string[];
- // Set only by AuthoritativeResolver.resolveRecordTypes for a type whose
- // lookup failed: it is unknown, not a confirmed empty RRset. Other paths
- // (DoH DNS-level errors, tolerated RRSIG refusals) still return a failed
- // lookup as an empty response noted only in the trace. Keep this plain
- // data so the response stays JSON-serializable.
+ // Set by resolveRecordTypes for a type whose lookup failed: it is unknown,
+ // not a confirmed empty RRset. Keep this plain data so the response stays
+ // JSON-serializable.
error?: UserFacingErrorPayload;
};
export type ResolverMultiResponse = Record<string, ResolverResponse>;
+const FALLBACK_LOOKUP_ERROR: UserFacingErrorPayload = {
+ title: 'DNS lookup failed',
+ description:
+ 'This record type could not be retrieved. Please try again shortly.',
+ retryable: true,
+};
+
export abstract class DnsResolver {
+ // Throws when the lookup fails, so an empty result means a confirmed empty
+ // RRset. The exception is RRSIG: resolvers return it empty when servers
+ // refuse (authoritative) or fail (DoH) direct RRSIG queries.
public abstract resolveRecordType(
domain: string,
type: RecordType,
): Promise<ResolverResponse>;
+ // Keeps every type that resolved and marks each failed type with an error.
public async resolveRecordTypes(
domain: string,
types: readonly RecordType[],
): Promise<ResolverMultiResponse> {
- const results = await Promise.all(
+ const results = await Promise.allSettled(
types.map((type) => this.resolveRecordType(domain, type)),
);
-
- return types.reduce(
- (res, type, index) => ({
- ...res,
- [type]: results[index],
- }),
- {} as ResolverMultiResponse,
+ const firstFailure = results.find((result) => result.status === 'rejected');
+ const hasRecords = results.some(
+ (result) =>
+ result.status === 'fulfilled' && result.value.records.length > 0,
);
+ // Empty successful types must not turn an entirely unusable lookup into
+ // a misleading "no records" page (e.g. a broken DNSSEC chain or Route 53
+ // delegation).
+ if (firstFailure && !hasRecords) throw firstFailure.reason;
+
+ const response: ResolverMultiResponse = {};
+ for (const [index, result] of results.entries()) {
+ if (result.status === 'fulfilled') {
+ response[types[index]] = result.value;
+ continue;
+ }
+ const { reason } = result;
+ const expected = reason instanceof UserFacingError;
+ // Not rethrown, so onRequestError never sees it. Expected failures
+ // (e.g. a refused PTR) only warn, so error alerts stay reserved for
+ // bugs.
+ log[expected ? 'warn' : 'error']({
+ message: 'record_type_lookup_failed',
+ domain,
+ recordType: types[index],
+ error: serializeError(reason),
+ });
+ const error = expected ? reason.payload : FALLBACK_LOOKUP_ERROR;
+ response[types[index]] = { records: [], trace: [], error };
+ }
+ return response;
}
}
diff --git a/lib/resolvers/internal.ts b/lib/resolvers/internal.ts
index 8d6591e..dff5049 100644
--- a/lib/resolvers/internal.ts
+++ b/lib/resolvers/internal.ts
@@ -1,6 +1,9 @@
import { env } from '@/env';
-import { UserFacingError } from '../user-facing-error';
+import {
+ isUserFacingErrorPayload,
+ UserFacingError,
+} from '../user-facing-error';
import {
DnsResolver,
type RecordType,
@@ -45,32 +48,11 @@ export class InternalDoHResolver extends DnsResolver {
domain: string,
type: RecordType,
): Promise<ResolverResponse> {
- const url = this.getBaseUrl(this.location);
- url.searchParams.set('resolver', this.resolver);
- url.searchParams.set('type', type);
- url.searchParams.set('domain', domain);
-
- const response = await fetch(url, this.requestInit);
- if (!response.ok) {
- throw new UserFacingError(
- {
- title: `DNS resolver (${this.location}) is unavailable`,
- description: `The DNS resolver for ${this.location} returned an error and may be temporarily down. Please try again shortly.`,
- retryable: true,
- },
- {
- cause: new Error(
- `Failed to fetch results for ${this.location} from ${url}: ${response.status} ${response.statusText}`,
- ),
- },
- );
- }
-
- const results = (await response.json()) as ResolverMultiResponse;
-
- return results[type];
+ return (await this.resolveRecordTypes(domain, [type]))[type];
}
+ // One request for every type; the route applies resolveRecordTypes' policy
+ // and returns failed types with their error intact.
public async resolveRecordTypes(
domain: string,
types: readonly RecordType[],
@@ -82,6 +64,15 @@ export class InternalDoHResolver extends DnsResolver {
const response = await fetch(url, this.requestInit);
if (!response.ok) {
+ // The route answers a lookup where every type failed with its reason.
+ const payload: unknown = await response.json().catch(() => null);
+ if (isUserFacingErrorPayload(payload)) {
+ throw new UserFacingError(payload, {
+ cause: new Error(
+ `Lookup failed for ${this.location} at ${url}: ${response.status}`,
+ ),
+ });
+ }
throw new UserFacingError(
{
title: `DNS resolver (${this.location}) is unavailable`,
diff --git a/lib/user-facing-error.ts b/lib/user-facing-error.ts
index cd456a0..e44a191 100644
--- a/lib/user-facing-error.ts
+++ b/lib/user-facing-error.ts
@@ -25,21 +25,26 @@ export class UserFacingError extends Error {
}
}
+export const isUserFacingErrorPayload = (
+ value: unknown,
+): value is UserFacingErrorPayload => {
+ const payload = value as Record<string, unknown> | null;
+ return (
+ typeof payload === 'object' &&
+ payload !== null &&
+ typeof payload.title === 'string' &&
+ typeof payload.description === 'string' &&
+ (payload.retryable === undefined || typeof payload.retryable === 'boolean')
+ );
+};
+
export const parseUserFacingDigest = (
digest: string | undefined,
): UserFacingErrorPayload | null => {
if (!digest || !digest.startsWith(SENTINEL)) return null;
try {
- const parsed = JSON.parse(digest.slice(SENTINEL.length));
- if (
- parsed &&
- typeof parsed === 'object' &&
- typeof parsed.title === 'string' &&
- typeof parsed.description === 'string' &&
- (parsed.retryable === undefined || typeof parsed.retryable === 'boolean')
- ) {
- return parsed as UserFacingErrorPayload;
- }
+ const parsed: unknown = JSON.parse(digest.slice(SENTINEL.length));
+ if (isUserFacingErrorPayload(parsed)) return parsed;
} catch {
// fall through
}