import { describe, expect, test, vi } from "vitest"; import { fetchOutboundHttpWithRedirects, readLimitedResponseText, validateOutboundHttpUrl } from "./outbound-http.js"; describe("outbound HTTP guard", () => { const publicResolver = vi.fn(async () => ["93.184.216.34"]); test("blocks private and internal targets", () => { expect(() => validateOutboundHttpUrl("http://127.0.0.1/metadata")).toThrow("Outbound HTTP endpoint host is not allowed."); expect(() => validateOutboundHttpUrl("http://[fec0::1]/internal")).toThrow("Outbound HTTP endpoint host is not allowed."); expect(() => validateOutboundHttpUrl("http://[ff02::1]/multicast")).toThrow("Outbound HTTP endpoint host is not allowed."); expect(() => validateOutboundHttpUrl("https://service.internal/hook")).toThrow("Outbound HTTP endpoint host is not allowed."); expect(validateOutboundHttpUrl("https://hooks.example.com/workflow")).toBe("https://hooks.example.com/workflow"); }); test("follows public redirects manually and rejects private redirect targets", async () => { const fetchImpl = vi .fn() .mockResolvedValueOnce(new Response(null, { status: 302, headers: { location: "https://hooks.example.com/ok" } })) .mockResolvedValueOnce(new Response("done", { status: 200 })); await expect(fetchOutboundHttpWithRedirects("https://api.example.com/start", { method: "POST", body: "payload" }, { fetchImpl, addressResolver: publicResolver })).resolves.toMatchObject({ status: 200 }); expect(fetchImpl).toHaveBeenNthCalledWith(1, "https://api.example.com/start", expect.objectContaining({ method: "POST", redirect: "manual", signal: expect.any(AbortSignal) })); expect(fetchImpl).toHaveBeenNthCalledWith(2, "https://hooks.example.com/ok", expect.objectContaining({ method: "GET", body: undefined, redirect: "manual", signal: expect.any(AbortSignal) })); const privateRedirectFetch = vi.fn(async () => new Response(null, { status: 302, headers: { location: "http://10.0.0.1/metadata" } })); await expect(fetchOutboundHttpWithRedirects("https://api.example.com/start", { method: "GET" }, { fetchImpl: privateRedirectFetch, addressResolver: publicResolver })).rejects.toThrow( "Outbound HTTP endpoint host is not allowed." ); expect(privateRedirectFetch).toHaveBeenCalledTimes(1); }); test("rejects public hostnames that resolve to private addresses before fetch", async () => { const fetchImpl = vi.fn(async () => new Response("should not happen", { status: 200 })); const addressResolver = vi.fn(async () => ["169.254.169.254"]); await expect(fetchOutboundHttpWithRedirects("https://public.example.test/metadata", { method: "GET" }, { fetchImpl, addressResolver })).rejects.toThrow( "Outbound HTTP endpoint resolved to a blocked address." ); expect(addressResolver).toHaveBeenCalledWith("public.example.test"); expect(fetchImpl).not.toHaveBeenCalled(); }); test("rejects redirect hostnames that resolve to private addresses before following them", async () => { const fetchImpl = vi.fn(async () => new Response(null, { status: 302, headers: { location: "https://metadata.example.test/latest" } })); const addressResolver = vi.fn(async (hostname: string) => (hostname === "metadata.example.test" ? ["10.0.0.10"] : ["93.184.216.34"])); await expect(fetchOutboundHttpWithRedirects("https://api.example.com/start", { method: "GET" }, { fetchImpl, addressResolver })).rejects.toThrow( "Outbound HTTP endpoint resolved to a blocked address." ); expect(fetchImpl).toHaveBeenCalledTimes(1); expect(addressResolver).toHaveBeenCalledWith("api.example.com"); expect(addressResolver).toHaveBeenCalledWith("metadata.example.test"); }); test("limits response body reads without buffering the full stream", async () => { const response = new Response("abcdef"); await expect(readLimitedResponseText(response, 3)).resolves.toEqual({ body: "abc", truncated: true }); }); test("pins the connection to the DNS-validated address to defeat rebinding", async () => { const fetchImpl = vi.fn(async (input: Parameters[0], init?: Parameters[1]) => { void input; void init; return new Response("ok", { status: 200 }); }); const addressResolver = vi.fn(async () => ["93.184.216.34"]); await fetchOutboundHttpWithRedirects( "https://hooks.example.com/webhook", { method: "GET" }, { fetchImpl, addressResolver, pinResolvedAddress: true }, ); // The request must carry a pinned undici dispatcher so undici connects to // the validated IP instead of re-resolving the hostname at connect time. const init = fetchImpl.mock.calls[0]?.[1] as (RequestInit & { dispatcher?: unknown }) | undefined; expect(init?.dispatcher).toBeDefined(); }); test("closes every pinned dispatcher after redirect and final-response hops", async () => { const { Agent } = await import("undici"); const closeSpy = vi.spyOn(Agent.prototype, "close"); const fetchImpl = vi .fn() .mockResolvedValueOnce(new Response(null, { status: 307, headers: { location: "https://hooks.example.com/final" } })) .mockResolvedValueOnce(new Response("ok", { status: 200 })); try { const response = await fetchOutboundHttpWithRedirects( "https://hooks.example.com/start", { method: "POST", body: "payload" }, { fetchImpl, addressResolver: publicResolver, pinResolvedAddress: true } ); await response.text(); expect(fetchImpl).toHaveBeenCalledTimes(2); const hopDispatchers = fetchImpl.mock.calls.map((call) => (call[1] as RequestInit & { dispatcher?: unknown }).dispatcher); expect(new Set(hopDispatchers).size).toBe(2); const closedDispatchers = new Set(closeSpy.mock.instances); expect(hopDispatchers.every((dispatcher) => closedDispatchers.has(dispatcher))).toBe(true); } finally { closeSpy.mockRestore(); } }); test("does not pin when a custom fetchImpl is used without opting in", async () => { const fetchImpl = vi.fn(async (input: Parameters[0], init?: Parameters[1]) => { void input; void init; return new Response("ok", { status: 200 }); }); const addressResolver = vi.fn(async () => ["93.184.216.34"]); await fetchOutboundHttpWithRedirects("https://hooks.example.com/webhook", { method: "GET" }, { fetchImpl, addressResolver }); const init = fetchImpl.mock.calls[0]?.[1] as (RequestInit & { dispatcher?: unknown }) | undefined; expect(init?.dispatcher).toBeUndefined(); }); });