// @vitest-environment node import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ fetchOutboundHttpWithRedirects: vi.fn(), })); vi.mock("~/utils/outboundHttp.server", () => ({ fetchOutboundHttpWithRedirects: mocks.fetchOutboundHttpWithRedirects, })); const { StorageScanRejectedError, UploadContentReadError, fetchUploadContent, requireCleanMalwareScan } = await import("./storageService"); beforeEach(() => { vi.clearAllMocks(); }); describe("fetchUploadContent body limits", () => { it("reads a bounded remote body without changing its content type", async () => { mocks.fetchOutboundHttpWithRedirects.mockResolvedValue( new Response(Uint8Array.from([1, 2, 3]), { headers: { "content-type": "image/png" }, }) ); const blob = await fetchUploadContent("https://assets.example.test/avatar.png", { maxBytes: 3, bodyTimeoutMs: 100, }); expect(blob.type).toBe("image/png"); expect(new Uint8Array(await blob.arrayBuffer())).toEqual(Uint8Array.from([1, 2, 3])); }); it("cancels a streamed response as soon as it exceeds the byte limit", async () => { let cancelled = false; const body = new ReadableStream({ start(controller) { controller.enqueue(Uint8Array.from([1, 2, 3])); controller.enqueue(Uint8Array.from([4, 5, 6])); }, cancel() { cancelled = true; }, }); mocks.fetchOutboundHttpWithRedirects.mockResolvedValue(new Response(body)); await expect(fetchUploadContent("https://assets.example.test/avatar.png", { maxBytes: 5, bodyTimeoutMs: 100 })).rejects.toMatchObject({ name: UploadContentReadError.name, code: "too_large", }); expect(cancelled).toBe(true); }); it("times out a remote body that never finishes streaming", async () => { const body = new ReadableStream({ pull() { return new Promise(() => undefined); }, }); mocks.fetchOutboundHttpWithRedirects.mockResolvedValue(new Response(body)); await expect(fetchUploadContent("https://assets.example.test/avatar.png", { maxBytes: 5, bodyTimeoutMs: 20 })).rejects.toMatchObject({ name: UploadContentReadError.name, code: "timeout", }); }); }); describe("synchronous malware scan gate", () => { const input = { body: Uint8Array.from([1, 2, 3]), bucket: "uploads", key: "tenants/tenant_1/avatar.png", filename: "avatar.png", }; it("allows storage only after an explicit clean verdict", async () => { const scanner = { scanBuffer: vi.fn(async () => ({ verdict: "clean" as const, engine: "clamav" })) }; await expect(requireCleanMalwareScan({ ...input, scanner })).resolves.toBeUndefined(); expect(scanner.scanBuffer).toHaveBeenCalledOnce(); }); it("rejects infected uploads and persists a redacted audit verdict", async () => { const onReject = vi.fn(); const scanner = { scanBuffer: vi.fn(async () => ({ verdict: "infected" as const, signature: "secret-signature", engine: "clamav" })), }; await expect(requireCleanMalwareScan({ ...input, scanner, onReject })).rejects.toMatchObject({ name: StorageScanRejectedError.name, scanResult: { status: "rejected", reason: "malware_detected" }, }); expect(onReject).toHaveBeenCalledWith( expect.objectContaining({ status: "rejected", reason: "malware_detected" }), ); expect(JSON.stringify(onReject.mock.calls)).not.toContain("secret-signature"); }); it("fails closed when the scanner is unavailable", async () => { const scanner = { scanBuffer: vi.fn(async () => { throw new Error("connection failed with secret details"); }), }; await expect(requireCleanMalwareScan({ ...input, scanner })).rejects.toMatchObject({ name: StorageScanRejectedError.name, scanResult: { status: "rejected", reason: "scanner_error" }, }); }); }); describe("storeFile scan contract", () => { it("does not contain the legacy remote-URL shortcut when a scan is requested", async () => { const source = await import("node:fs/promises").then(({ readFile }) => readFile(new URL("./storageService.ts", import.meta.url), "utf8")); expect(source).toContain('content.trim().toLowerCase().startsWith("http") && !replace && !scan'); }); });