The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Test the safeFetch redirect, DNS and timeout paths
Raficd6c22c87c40
diff --git a/apps/web/tests/misc/safe-fetch.test.ts b/apps/web/tests/misc/safe-fetch.test.tsindex f627c15df7..05eacf3f9c 100644--- a/apps/web/tests/misc/safe-fetch.test.ts+++ b/apps/web/tests/misc/safe-fetch.test.ts@@ -1,11 +1,16 @@-import { describe, expect, it, vi } from "vitest";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";// `server-only` throws on import outside of a Next.js server bundle. Stub it// so we can import `safe-fetch.ts`, which depends transitively on it via// `@/lib/api/errors`.vi.mock("server-only", () => ({}));+vi.mock("node:dns", () => ({ default: { lookup: vi.fn() } }));+import { isPrivateIp } from "@/lib/api/is-private-ip";+import { safeFetch } from "@/lib/api/safe-fetch";+import { safeLookup } from "@/lib/api/safe-fetch-node";+import dns from "node:dns";describe("isPrivateIp", () => {describe("IPv4", () => {@@ -115,3 +120,213 @@ describe("isPrivateIp", () => {});});});++const PUBLIC_IP = "93.184.216.34";++function dohAnswer(type: "A" | "AAAA", ips: string[]) {+ return Response.json({+ Status: 0,+ Answer: ips.map((data) => ({ data, type: type === "A" ? 1 : 28 })),+ });+}++function redirect(location: string, status = 302) {+ return new Response("redirect body", { status, headers: { location } });+}++describe("safeFetch", () => {+ // Maps hostname to its A records. Hostnames that are not listed get none.+ let aRecords: Record<string, string[]>;+ // Handles the requests that are not DoH lookups.+ let handler: (url: string, init: RequestInit) => Promise<Response>;+ const defaultFetch = async (+ input: string | URL | Request,+ init: RequestInit = {},+ ) => {+ const url = new URL(input.toString());+ if (url.hostname === "dns.google") {+ const name = url.searchParams.get("name")!;+ const type = url.searchParams.get("type") as "A" | "AAAA";+ return dohAnswer(type, type === "A" ? aRecords[name] ?? [] : []);+ }+ return handler(url.toString(), init);+ };+ const fetchMock = vi.fn(defaultFetch);++ beforeEach(() => {+ aRecords = { "example.com": [PUBLIC_IP], "other.com": [PUBLIC_IP] };+ handler = async () => new Response("ok");+ fetchMock.mockReset();+ fetchMock.mockImplementation(defaultFetch);+ vi.stubGlobal("fetch", fetchMock);+ });++ afterEach(() => {+ vi.unstubAllGlobals();+ });++ it("returns the redirect response as is when maxRedirects is 0", async () => {+ handler = async () => redirect("https://example.com/next");++ const res = await safeFetch("https://example.com", undefined, {+ maxRedirects: 0,+ });++ expect(res.status).toBe(302);+ expect(res.headers.get("location")).toBe("https://example.com/next");+ });++ it("follows a redirect to a public host", async () => {+ handler = async (url) =>+ url === "https://example.com/" ? redirect("/next") : new Response("ok");++ const res = await safeFetch("https://example.com");++ expect(await res.text()).toBe("ok");+ });++ it("rejects a redirect to a private IP literal", async () => {+ handler = async () => redirect("http://169.254.169.254/latest/meta-data");++ await expect(safeFetch("https://example.com")).rejects.toThrow(+ "disallowed IP address",+ );+ });++ it("rejects a hostname that resolves to a private IP", async () => {+ aRecords["internal.example.com"] = [PUBLIC_IP, "10.0.0.1"];++ await expect(safeFetch("https://internal.example.com")).rejects.toThrow(+ "disallowed IP address",+ );+ });++ it("fails closed when one DoH lookup fails", async () => {+ fetchMock.mockImplementation(async (input) => {+ const url = new URL(input.toString());+ if (url.searchParams.get("type") === "AAAA") {+ return new Response("error", { status: 500 });+ }+ return dohAnswer("A", [PUBLIC_IP]);+ });++ await expect(safeFetch("https://example.com")).rejects.toThrow(+ "Could not resolve hostname",+ );+ });++ it("throws after too many redirects", async () => {+ handler = async () => redirect("https://example.com/loop");++ await expect(+ safeFetch("https://example.com", undefined, { maxRedirects: 2 }),+ ).rejects.toThrow("Too many redirects");+ });++ it("switches to GET without a body after a 303", async () => {+ const seen: RequestInit[] = [];+ handler = async (url, init) => {+ seen.push(init);+ return url === "https://example.com/"+ ? redirect("/done", 303)+ : new Response("ok");+ };++ await safeFetch("https://example.com", {+ method: "POST",+ body: "payload",+ headers: { "content-type": "text/plain" },+ });++ expect(seen[1].method).toBe("GET");+ expect(seen[1].body).toBeUndefined();+ expect(new Headers(seen[1].headers).get("content-type")).toBeNull();+ });++ it("drops credential headers on a cross-origin redirect", async () => {+ const seen: RequestInit[] = [];+ handler = async (url, init) => {+ seen.push(init);+ return url === "https://example.com/"+ ? redirect("https://other.com/")+ : new Response("ok");+ };++ await safeFetch("https://example.com", {+ headers: { authorization: "Bearer token", "x-custom": "kept" },+ });++ const headers = new Headers(seen[1].headers);+ expect(headers.get("authorization")).toBeNull();+ expect(headers.get("x-custom")).toBe("kept");+ });++ it("applies the timeout to the DoH lookups", async () => {+ fetchMock.mockImplementation(+ (_input, init) =>+ new Promise((_, reject) => {+ init?.signal?.addEventListener("abort", () =>+ reject(new DOMException("Aborted", "AbortError")),+ );+ }),+ );++ await expect(+ safeFetch("https://example.com", undefined, { timeoutMs: 50 }),+ ).rejects.toThrow("Request timed out");+ });+});++describe("safeLookup", () => {+ const lookupMock = vi.mocked(dns.lookup) as unknown as ReturnType<+ typeof vi.fn+ >;++ function resolveTo(addresses: { address: string; family: number }[]) {+ lookupMock.mockImplementation((_hostname, _options, callback) =>+ callback(null, addresses),+ );+ }++ function lookup(options: dns.LookupOptions) {+ return new Promise<{ err: Error | null; address: unknown }>((resolve) =>+ safeLookup(+ "example.com",+ options as dns.LookupOneOptions,+ (err, address) => resolve({ err, address }),+ ),+ );+ }++ it("returns the first address for a public host", async () => {+ resolveTo([{ address: PUBLIC_IP, family: 4 }]);++ const { err, address } = await lookup({});++ expect(err).toBeNull();+ expect(address).toBe(PUBLIC_IP);+ });++ it("returns all addresses when called with all: true", async () => {+ const addresses = [+ { address: PUBLIC_IP, family: 4 },+ { address: "2606:4700:4700::1111", family: 6 },+ ];+ resolveTo(addresses);++ const { address } = await lookup({ all: true });++ expect(address).toEqual(addresses);+ });++ it("rejects a host with any private address", async () => {+ resolveTo([+ { address: PUBLIC_IP, family: 4 },+ { address: "127.0.0.1", family: 4 },+ ]);++ const { err } = await lookup({ all: true });++ expect(err?.message).toContain("disallowed IP address");+ });+});