Update hivegrid-cloud reference to latest main
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e98d38e716
commit
7b21524c86
3781 files changed
+373484
-225607
No files matched your search
+189
@@ -0,0 +1,189 @@
|
||||
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("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("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 });
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user