Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/foundation/src/outbound-http.test.ts
T
2026-07-08 16:15:37 +02:00

87 lines
4.9 KiB
TypeScript

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("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 () => 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("does not pin when a custom fetchImpl is used without opting in", async () => {
const fetchImpl = vi.fn(async () => 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();
});
});