The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add the DoH failure reason to the safeFetch hostname error
Rafi4e0d1aa829b0
diff --git a/apps/web/lib/api/safe-fetch.ts b/apps/web/lib/api/safe-fetch.tsindex 37b1507c7d..4148b2b4ca 100644--- a/apps/web/lib/api/safe-fetch.ts+++ b/apps/web/lib/api/safe-fetch.ts@@ -35,48 +35,58 @@ function ipLiteralFromHostname(hostname: string): string | null {const DNS_NOERROR = 0;const DNS_NXDOMAIN = 3;+type DohResult = { ips: string[] } | { error: string };+/**- * Resolves A and AAAA records. Returns null if either lookup fails, so the+ * Resolves A and AAAA records. Returns an error if either lookup fails, so the* caller can fail closed instead of checking only one address family.*/async function resolveHostnameToIps(hostname: string,signal: AbortSignal,-): Promise<string[] | null> {- const query = async (type: "A" | "AAAA"): Promise<string[] | null> => {+): Promise<DohResult> {+ const query = async (type: "A" | "AAAA"): Promise<DohResult> => {try {const res = await fetch(`https://dns.google/resolve?name=${encodeURIComponent(hostname)}&type=${type}`,{ signal },);- if (!res.ok) return null;+ if (!res.ok) {+ return { error: `${type} lookup returned HTTP ${res.status}` };+ }const data = (await res.json()) as {Status?: number;Answer?: { data: string; type: number }[];};if (data.Status !== DNS_NOERROR && data.Status !== DNS_NXDOMAIN) {- return null;+ return { error: `${type} lookup returned DNS status ${data.Status}` };}// 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 null;+ return {+ ips: (data.Answer ?? [])+ .filter((a) => a.type === wantedType)+ .map((a) => a.data),+ };+ } catch (error) {+ const reason = error instanceof Error ? error.message : String(error);+ return { error: `${type} lookup failed: ${reason}` };}};// Retry a failed lookup once, so a short DoH error does not reject a valid host.- const queryWithRetry = async (type: "A" | "AAAA") =>- (await query(type)) ?? (signal.aborted ? null : query(type));+ const queryWithRetry = async (type: "A" | "AAAA") => {+ const result = await query(type);+ return "error" in result && !signal.aborted ? query(type) : result;+ };const [a, aaaa] = await Promise.all([queryWithRetry("A"),queryWithRetry("AAAA"),]);- if (a === null || aaaa === null) return null;- return [...a, ...aaaa];+ if ("error" in a) return a;+ if ("error" in aaaa) return aaaa;+ return { ips: [...a.ips, ...aaaa.ips] };}async function assertUrlIsSafe(@@ -105,11 +115,19 @@ async function assertUrlIsSafe(// The transport checks the resolved IPs itself when it connects.if (!resolveHostnames) return;- const ips = await resolveHostnameToIps(url.hostname, signal);+ const result = await resolveHostnameToIps(url.hostname, signal);if (signal.aborted) {throw new Error("Request timed out");}- if (ips === null || ips.length === 0) {+ // Keep the same message prefix for both cases, and add the reason when the DoH lookup failed.+ if ("error" in result) {+ throw new DubApiError({+ code: "unprocessable_entity",+ message: `Could not resolve hostname: ${url.hostname} (${result.error})`,+ });+ }+ const { ips } = result;+ if (ips.length === 0) {throw new DubApiError({code: "unprocessable_entity",message: `Could not resolve hostname: ${url.hostname}`,diff --git a/apps/web/tests/misc/safe-fetch.test.ts b/apps/web/tests/misc/safe-fetch.test.tsindex 52d572604e..2ae05f7ee0 100644--- a/apps/web/tests/misc/safe-fetch.test.ts+++ b/apps/web/tests/misc/safe-fetch.test.ts@@ -241,7 +241,7 @@ describe("safeFetch", () => {});await expect(safeFetch("https://example.com")).rejects.toThrow(- "Could not resolve hostname",+ "Could not resolve hostname: example.com (AAAA lookup returned HTTP 500)",);});