Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/services/billing/invoice-download-fetch.server.test.ts
T

110 lines
4.7 KiB
TypeScript

// @vitest-environment node
import { describe, expect, test, vi } from "vitest";
import {
createBoundedInvoiceDownloadBody,
fetchInvoiceDownloadFile,
} from "./invoice-download-fetch.server";
describe("invoice download fetch boundary", () => {
test("uses a direct manual-redirect fetch only for the configured provider origin", async () => {
const fetchImpl = vi.fn(async () => new Response("pdf"));
const outboundFetch = vi.fn();
await fetchInvoiceDownloadFile(
"http://lago.internal.test/rails/active_storage/invoice.pdf",
{ trustedProviderOrigin: "http://lago.internal.test/api/v1" },
{ fetchImpl: fetchImpl as typeof fetch, outboundFetch },
);
expect(fetchImpl).toHaveBeenCalledWith(
new URL("http://lago.internal.test/rails/active_storage/invoice.pdf"),
expect.objectContaining({ redirect: "manual", signal: expect.any(AbortSignal) }),
);
expect(outboundFetch).not.toHaveBeenCalled();
});
test("follows a same-origin Active-Storage redirect on the trusted origin", async () => {
const fetchImpl = vi
.fn(async () => new Response("pdf"))
.mockResolvedValueOnce(new Response(null, { status: 302, headers: { location: "/rails/active_storage/disk/abc/invoice.pdf" } }));
const outboundFetch = vi.fn();
const response = await fetchInvoiceDownloadFile(
"http://lago.internal.test/rails/active_storage/blobs/redirect/xyz/invoice.pdf",
{ trustedProviderOrigin: "http://lago.internal.test" },
{ fetchImpl: fetchImpl as typeof fetch, outboundFetch },
);
expect(await response.text()).toBe("pdf");
expect(fetchImpl).toHaveBeenCalledTimes(2);
expect(fetchImpl).toHaveBeenLastCalledWith(new URL("http://lago.internal.test/rails/active_storage/disk/abc/invoice.pdf"), expect.anything());
expect(outboundFetch).not.toHaveBeenCalled();
});
test("hands an off-origin redirect target to the SSRF-safe outbound client", async () => {
const fetchImpl = vi.fn(async () =>
new Response(null, { status: 302, headers: { location: "https://objects.example.test/signed/invoice.pdf?sig=1" } }),
);
const outboundFetch = vi.fn(async () => new Response("pdf"));
const response = await fetchInvoiceDownloadFile(
"http://lago.internal.test/rails/active_storage/blobs/redirect/xyz/invoice.pdf",
{ trustedProviderOrigin: "http://lago.internal.test" },
{ fetchImpl: fetchImpl as typeof fetch, outboundFetch },
);
expect(await response.text()).toBe("pdf");
expect(fetchImpl).toHaveBeenCalledTimes(1);
expect(outboundFetch).toHaveBeenCalledWith(
"https://objects.example.test/signed/invoice.pdf?sig=1",
expect.objectContaining({ headers: expect.any(Object) }),
{ timeoutMs: 10_000 },
);
});
test("stops after the trusted redirect limit instead of looping", async () => {
const fetchImpl = vi.fn(async () => new Response(null, { status: 302, headers: { location: "/rails/active_storage/loop.pdf" } }));
await expect(
fetchInvoiceDownloadFile(
"http://lago.internal.test/rails/active_storage/loop.pdf",
{ trustedProviderOrigin: "http://lago.internal.test" },
{ fetchImpl: fetchImpl as typeof fetch, outboundFetch: vi.fn() },
),
).rejects.toThrow("redirect limit");
});
test("routes any other origin through the SSRF-safe outbound client", async () => {
const fetchImpl = vi.fn();
const outboundFetch = vi.fn(async () => new Response("pdf"));
await fetchInvoiceDownloadFile(
"https://objects.example.test/invoice.pdf",
{ trustedProviderOrigin: "https://lago.example.test" },
{ fetchImpl: fetchImpl as typeof fetch, outboundFetch },
);
expect(fetchImpl).not.toHaveBeenCalled();
expect(outboundFetch).toHaveBeenCalledWith(
"https://objects.example.test/invoice.pdf",
expect.objectContaining({ headers: expect.any(Object) }),
{ timeoutMs: 10_000 },
);
});
test("rejects private network targets outside the configured provider origin", async () => {
await expect(fetchInvoiceDownloadFile("http://127.0.0.1:3000/private")).rejects.toMatchObject({
name: "OutboundHttpUrlValidationError",
});
});
test("bounds both declared and actual streamed response bytes", async () => {
const declaredTooLarge = new Response(new Uint8Array([1]), { headers: { "content-length": "4" } });
expect(() => createBoundedInvoiceDownloadBody(declaredTooLarge, 3)).toThrow("size limit");
const understated = new Response(new Uint8Array([1, 2, 3, 4]), { headers: { "content-length": "2" } });
const body = createBoundedInvoiceDownloadBody(understated, 3);
await expect(new Response(body).arrayBuffer()).rejects.toThrow("size limit");
});
});