The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fail closed when a DoH lookup fails
Rafib45bda1a981f
diff --git a/apps/web/lib/api/safe-fetch.ts b/apps/web/lib/api/safe-fetch.tsindex 4ca3467b7d..1d4504ca25 100644--- a/apps/web/lib/api/safe-fetch.ts+++ b/apps/web/lib/api/safe-fetch.ts@@ -173,27 +173,42 @@ function ipLiteralFromHostname(hostname: string): string | null {return null;}-async function resolveHostnameToIps(hostname: string): Promise<string[]> {- const query = async (type: "A" | "AAAA"): Promise<string[]> => {+// DoH response codes. Any other status (SERVFAIL, REFUSED, ...) is a failed lookup.+const DNS_NOERROR = 0;+const DNS_NXDOMAIN = 3;++/**+ * Resolves A and AAAA records. Returns null if either lookup fails, so the+ * caller can fail closed instead of checking only one address family.+ */+async function resolveHostnameToIps(+ hostname: string,+): Promise<string[] | null> {+ const query = async (type: "A" | "AAAA"): Promise<string[] | null> => {try {const res = await fetch(`https://dns.google/resolve?name=${encodeURIComponent(hostname)}&type=${type}`,);- if (!res.ok) return [];+ if (!res.ok) return null;const data = (await res.json()) as {+ Status?: number;Answer?: { data: string; type: number }[];};+ if (data.Status !== DNS_NOERROR && data.Status !== DNS_NXDOMAIN) {+ return null;+ }// Type 1 = A, type 28 = AAAA. Ignore CNAME / other intermediate records.const wantedType = type === "A" ? 1 : 28;return (data.Answer ?? []).filter((a) => a.type === wantedType).map((a) => a.data);} catch {- return [];+ return null;}};const [a, aaaa] = await Promise.all([query("A"), query("AAAA")]);+ if (a === null || aaaa === null) return null;return [...a, ...aaaa];}@@ -217,7 +232,7 @@ async function assertUrlIsSafe(url: URL): Promise<void> {}const ips = await resolveHostnameToIps(url.hostname);- if (ips.length === 0) {+ if (ips === null || ips.length === 0) {throw new DubApiError({code: "unprocessable_entity",message: `Could not resolve hostname: ${url.hostname}`,diff --git a/apps/web/tests/misc/import-tracked-sitemaps.test.ts b/apps/web/tests/misc/import-tracked-sitemaps.test.tsindex b90c0c2658..fbf57e4a6c 100644--- a/apps/web/tests/misc/import-tracked-sitemaps.test.ts+++ b/apps/web/tests/misc/import-tracked-sitemaps.test.ts@@ -30,7 +30,7 @@ function makeFetchResponse(function dohResponseFor(ip: string, type: 1 | 28 = 1): Response {return makeFetchResponse(- Buffer.from(JSON.stringify({ Answer: [{ data: ip, type }] })),+ Buffer.from(JSON.stringify({ Status: 0, Answer: [{ data: ip, type }] })),);}