Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/foundation/src/outbound-http-extra.test.ts
T

293 lines
14 KiB
TypeScript

import { describe, expect, test, vi } from "vitest";
import { fetchOutboundHttpWithRedirects, readLimitedResponseText, validateOutboundHttpUrl } from "./outbound-http.js";
const publicResolver = vi.fn(async () => ["93.184.216.34"]);
describe("validateOutboundHttpUrl input validation", () => {
test("rejects non-absolute URLs", () => {
expect(() => validateOutboundHttpUrl("not-a-url")).toThrow("Outbound HTTP endpoint must be an absolute URL.");
expect(() => validateOutboundHttpUrl("/relative/path")).toThrow("Outbound HTTP endpoint must be an absolute URL.");
});
test("rejects non-http(s) protocols", () => {
expect(() => validateOutboundHttpUrl("ftp://example.com/file")).toThrow("Outbound HTTP endpoint must use http or https.");
expect(() => validateOutboundHttpUrl("file:///etc/passwd")).toThrow("Outbound HTTP endpoint must use http or https.");
});
test("rejects an empty host and bare single-label hosts", () => {
expect(() => validateOutboundHttpUrl("http:///path")).toThrow("Outbound HTTP endpoint host is not allowed.");
expect(() => validateOutboundHttpUrl("http://intranet/path")).toThrow("Outbound HTTP endpoint host is not allowed.");
});
test("rejects internal host suffixes", () => {
expect(() => validateOutboundHttpUrl("http://payments.svc/charge")).toThrow("Outbound HTTP endpoint host is not allowed.");
expect(() => validateOutboundHttpUrl("https://db.lan/query")).toThrow("Outbound HTTP endpoint host is not allowed.");
});
test("allows a public hostname", () => {
expect(validateOutboundHttpUrl("https://api.example.com/v1")).toBe("https://api.example.com/v1");
});
});
describe("validateOutboundHttpUrl IPv4 SSRF ranges", () => {
test.each([
["http://0.0.0.0/", "this-network"],
["http://10.1.2.3/", "private-A"],
["http://127.0.0.1/", "loopback"],
["http://100.64.0.1/", "carrier-grade-NAT"],
["http://169.254.169.254/", "link-local-metadata"],
["http://172.16.5.4/", "private-B"],
["http://192.168.1.1/", "private-C"],
["http://198.18.0.1/", "benchmarking"],
["http://224.0.0.1/", "multicast"],
])("blocks %s (%s)", (url) => {
expect(() => validateOutboundHttpUrl(url)).toThrow("Outbound HTTP endpoint host is not allowed.");
});
test("allows a routable public IPv4 literal", () => {
expect(validateOutboundHttpUrl("http://93.184.216.34/")).toBe("http://93.184.216.34/");
});
test("treats a dotted host with the wrong segment count as a normal hostname", () => {
// Five labels cannot be an IPv4 literal, so the multi-label public host is allowed through.
expect(validateOutboundHttpUrl("http://a.b.c.d.e/")).toBe("http://a.b.c.d.e/");
});
});
describe("validateOutboundHttpUrl IPv6 SSRF ranges", () => {
test.each([
["http://[::1]/", "loopback"],
["http://[::]/", "unspecified"],
["http://[fc00::1]/", "unique-local"],
["http://[fe80::1]/", "link-local"],
["http://[::ffff:7f00:1]/", "ipv4-mapped-loopback"],
["http://[::7f00:1]/", "ipv4-compatible-loopback"],
])("blocks %s (%s)", (url) => {
expect(() => validateOutboundHttpUrl(url)).toThrow(/Outbound HTTP endpoint host is not allowed\./);
});
test("allows a public IPv6 literal", () => {
expect(validateOutboundHttpUrl("http://[2606:4700:4700::1111]/")).toBe("http://[2606:4700:4700::1111]/");
});
test("allows a fully-expanded eight-group public IPv6 literal", () => {
// No zero-run means the URL parser keeps all eight groups, exercising the non-"::" parse path.
expect(validateOutboundHttpUrl("http://[2001:db8:1:2:3:4:5:6]/")).toBe("http://[2001:db8:1:2:3:4:5:6]/");
});
test("does not misclassify malformed IPv6 with multiple :: groups", () => {
// Two "::" sequences make this an invalid IPv6 literal; the URL parser rejects it outright.
expect(() => validateOutboundHttpUrl("http://[1::2::3]/")).toThrow("Outbound HTTP endpoint must be an absolute URL.");
});
});
describe("fetchOutboundHttpWithRedirects redirect handling", () => {
test("applies a feature-specific URL policy before every redirect hop", async () => {
const fetchImpl = vi.fn(async () => new Response(null, { status: 302, headers: { location: "https://hooks.example.com:8443/private" } }));
const urlValidator = vi.fn((value: string) => {
const url = new URL(validateOutboundHttpUrl(value));
if (url.port) throw new Error("feature policy rejected non-default port");
return url.toString();
});
await expect(
fetchOutboundHttpWithRedirects("https://api.example.com/start", { method: "GET" }, { fetchImpl, addressResolver: publicResolver, urlValidator })
).rejects.toThrow("feature policy rejected non-default port");
expect(fetchImpl).toHaveBeenCalledTimes(1);
expect(urlValidator).toHaveBeenNthCalledWith(1, "https://api.example.com/start");
expect(urlValidator).toHaveBeenNthCalledWith(2, "https://hooks.example.com:8443/private");
});
test("does not let a feature-specific validator weaken the foundation SSRF policy", async () => {
const fetchImpl = vi.fn(async () => new Response("unexpected", { status: 200 }));
await expect(fetchOutboundHttpWithRedirects("http://127.0.0.1/private", { method: "GET" }, { fetchImpl, urlValidator: (value) => value })).rejects.toThrow(
"Outbound HTTP endpoint host is not allowed."
);
expect(fetchImpl).not.toHaveBeenCalled();
});
test("returns the redirect response unchanged when no location header is present", async () => {
const fetchImpl = vi.fn(async () => new Response("redirect-without-location", { status: 302 }));
const result = await fetchOutboundHttpWithRedirects("https://api.example.com/start", { method: "GET" }, { fetchImpl, addressResolver: publicResolver });
expect(result.status).toBe(302);
expect(fetchImpl).toHaveBeenCalledTimes(1);
});
test("throws once the redirect limit is exhausted", async () => {
const fetchImpl = vi.fn(async () => new Response(null, { status: 302, headers: { location: "https://hooks.example.com/loop" } }));
await expect(
fetchOutboundHttpWithRedirects("https://api.example.com/start", { method: "GET" }, { fetchImpl, addressResolver: publicResolver, redirectLimit: 0 })
).rejects.toThrow("Outbound HTTP redirect limit exceeded.");
expect(fetchImpl).toHaveBeenCalledTimes(1);
});
test("converts a 303 redirect into a bodyless GET and strips content headers", async () => {
const fetchImpl = vi
.fn()
.mockResolvedValueOnce(new Response(null, { status: 303, headers: { location: "https://hooks.example.com/result" } }))
.mockResolvedValueOnce(new Response("ok", { status: 200 }));
await expect(
fetchOutboundHttpWithRedirects(
"https://api.example.com/start",
{ method: "POST", body: "payload", headers: { "content-type": "application/json", "content-length": "7", "x-trace": "keep" } },
{ fetchImpl, addressResolver: publicResolver }
)
).resolves.toMatchObject({ status: 200 });
const secondInit = fetchImpl.mock.calls[1][1] as RequestInit & { headers: Headers };
expect(secondInit.method).toBe("GET");
expect(secondInit.body).toBeUndefined();
expect(secondInit.headers.get("content-type")).toBeNull();
expect(secondInit.headers.get("content-length")).toBeNull();
expect(secondInit.headers.get("x-trace")).toBe("keep");
});
test("preserves the method and body for a 307-style redirect", async () => {
const fetchImpl = vi
.fn()
.mockResolvedValueOnce(new Response(null, { status: 307, headers: { location: "https://hooks.example.com/again" } }))
.mockResolvedValueOnce(new Response("ok", { status: 200 }));
await fetchOutboundHttpWithRedirects("https://api.example.com/start", { method: "PUT", body: "payload" }, { fetchImpl, addressResolver: publicResolver });
const secondInit = fetchImpl.mock.calls[1][1] as RequestInit;
expect(secondInit.method).toBe("PUT");
expect(secondInit.body).toBe("payload");
});
test.each([307, 308])("strips target-bound credentials on a cross-origin %s redirect", async (status) => {
const fetchImpl = vi
.fn()
.mockResolvedValueOnce(new Response(null, { status, headers: { location: "https://receiver.example/again" } }))
.mockResolvedValueOnce(new Response("ok", { status: 200 }));
await fetchOutboundHttpWithRedirects(
"https://hooks.example.com/start",
{
method: "POST",
body: "payload",
headers: {
authorization: "Bearer auth-secret",
cookie: "session=cookie-secret",
"proxy-authorization": "Basic proxy-secret",
"x-api-key": "api-secret",
"x-client-credential": "client-secret",
"x-custom-secret": "custom-secret",
"x-hook-hmac": "hook-secret",
"x-hivegrid-signature": "webhook-secret",
"x-webhook-key": "opaque-secret",
"content-type": "application/json",
"x-idempotency-key": "delivery-1",
"x-trace-id": "trace-1",
},
},
{ fetchImpl, addressResolver: publicResolver }
);
const secondInit = fetchImpl.mock.calls[1][1] as RequestInit;
const redirectedHeaders = new Headers(secondInit.headers);
expect(secondInit.method).toBe("POST");
expect(secondInit.body).toBe("payload");
expect(redirectedHeaders.get("authorization")).toBeNull();
expect(redirectedHeaders.get("cookie")).toBeNull();
expect(redirectedHeaders.get("proxy-authorization")).toBeNull();
expect(redirectedHeaders.get("x-api-key")).toBeNull();
expect(redirectedHeaders.get("x-client-credential")).toBeNull();
expect(redirectedHeaders.get("x-custom-secret")).toBeNull();
expect(redirectedHeaders.get("x-hook-hmac")).toBeNull();
expect(redirectedHeaders.get("x-hivegrid-signature")).toBeNull();
expect(redirectedHeaders.get("x-webhook-key")).toBeNull();
expect(redirectedHeaders.get("content-type")).toBe("application/json");
expect(redirectedHeaders.get("x-idempotency-key")).toBe("delivery-1");
expect(redirectedHeaders.get("x-trace-id")).toBe("trace-1");
});
test("keeps target-bound credentials on a same-origin redirect", async () => {
const fetchImpl = vi
.fn()
.mockResolvedValueOnce(new Response(null, { status: 307, headers: { location: "/again" } }))
.mockResolvedValueOnce(new Response("ok", { status: 200 }));
await fetchOutboundHttpWithRedirects(
"https://hooks.example.com/start",
{ method: "POST", body: "payload", headers: { authorization: "Bearer secret", "x-hivegrid-signature": "sha256=secret" } },
{ fetchImpl, addressResolver: publicResolver }
);
const redirectedHeaders = new Headers(fetchImpl.mock.calls[1][1]?.headers);
expect(redirectedHeaders.get("authorization")).toBe("Bearer secret");
expect(redirectedHeaders.get("x-hivegrid-signature")).toBe("sha256=secret");
});
test("can reject cross-origin redirects before target-bound credentials are forwarded", async () => {
const fetchImpl = vi.fn(async () => new Response(null, { status: 307, headers: { location: "https://attacker.example/steal" } }));
await expect(
fetchOutboundHttpWithRedirects(
"https://hooks.example.com/start",
{ method: "POST", headers: { "x-hivegrid-signature": "secret" }, body: "payload" },
{ fetchImpl, addressResolver: publicResolver, allowCrossOriginRedirects: false }
)
).rejects.toThrow("cross-origin redirect is not allowed");
expect(fetchImpl).toHaveBeenCalledTimes(1);
});
test("rejects a redirect to a literal private IPv4 target", async () => {
const fetchImpl = vi.fn(async () => new Response(null, { status: 301, headers: { location: "http://192.168.0.5/internal" } }));
await expect(
fetchOutboundHttpWithRedirects("https://api.example.com/start", { method: "GET" }, { fetchImpl, addressResolver: publicResolver })
).rejects.toThrow("Outbound HTTP endpoint host is not allowed.");
});
test("surfaces a resolver failure as an unresolved-host error", async () => {
const fetchImpl = vi.fn(async () => new Response("nope", { status: 200 }));
const addressResolver = vi.fn(async () => {
throw new Error("ENOTFOUND");
});
await expect(fetchOutboundHttpWithRedirects("https://api.example.com/start", { method: "GET" }, { fetchImpl, addressResolver })).rejects.toThrow(
"Outbound HTTP endpoint host could not be resolved."
);
expect(fetchImpl).not.toHaveBeenCalled();
});
test("rejects a host that resolves to zero addresses", async () => {
const fetchImpl = vi.fn(async () => new Response("nope", { status: 200 }));
const addressResolver = vi.fn(async () => []);
await expect(fetchOutboundHttpWithRedirects("https://api.example.com/start", { method: "GET" }, { fetchImpl, addressResolver })).rejects.toThrow(
"Outbound HTTP endpoint host could not be resolved."
);
});
test("skips DNS resolution for literal IP endpoints", async () => {
const fetchImpl = vi.fn(async () => new Response("ok", { status: 200 }));
const addressResolver = vi.fn(async () => ["should.not.be.called"]);
await fetchOutboundHttpWithRedirects("http://93.184.216.34/", { method: "GET" }, { fetchImpl, addressResolver });
expect(addressResolver).not.toHaveBeenCalled();
expect(fetchImpl).toHaveBeenCalledTimes(1);
});
});
describe("readLimitedResponseText", () => {
test("returns an empty body when the response has no stream", async () => {
await expect(readLimitedResponseText(new Response(null), 100)).resolves.toEqual({ body: "", truncated: false });
});
test("reads a full short body without truncation", async () => {
await expect(readLimitedResponseText(new Response("ab"), 100)).resolves.toEqual({ body: "ab", truncated: false });
});
test("truncates a body that exceeds the byte limit", async () => {
await expect(readLimitedResponseText(new Response("abcdef"), 3)).resolves.toEqual({ body: "abc", truncated: true });
});
});