From 5e7e63250a1ee0181d90728649fcfcf674fe9b77 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Wed, 18 Feb 2026 23:26:41 +0000 Subject: [PATCH] test: merge base64 oversize guard variants --- src/media/input-files.fetch-guard.test.ts | 79 ++++++++++++----------- 1 file changed, 40 insertions(+), 39 deletions(-) diff --git a/src/media/input-files.fetch-guard.test.ts b/src/media/input-files.fetch-guard.test.ts index 3ae622564e5..68224200d1f 100644 --- a/src/media/input-files.fetch-guard.test.ts +++ b/src/media/input-files.fetch-guard.test.ts @@ -59,48 +59,49 @@ describe("fetchWithGuard", () => { }); describe("base64 size guards", () => { - it("rejects oversized base64 images before decoding", async () => { + it.each([ + { + kind: "images", + expectedError: "Image too large", + run: async (data: string) => { + const { extractImageContentFromSource } = await import("./input-files.js"); + return await extractImageContentFromSource( + { type: "base64", data, mediaType: "image/png" }, + { + allowUrl: false, + allowedMimes: new Set(["image/png"]), + maxBytes: 6, + maxRedirects: 0, + timeoutMs: 1, + }, + ); + }, + }, + { + kind: "files", + expectedError: "File too large", + run: async (data: string) => { + const { extractFileContentFromSource } = await import("./input-files.js"); + return await extractFileContentFromSource({ + source: { type: "base64", data, mediaType: "text/plain", filename: "x.txt" }, + limits: { + allowUrl: false, + allowedMimes: new Set(["text/plain"]), + maxBytes: 6, + maxChars: 100, + maxRedirects: 0, + timeoutMs: 1, + pdf: { maxPages: 1, maxPixels: 1, minTextChars: 1 }, + }, + }); + }, + }, + ] as const)("rejects oversized base64 $kind before decoding", async (testCase) => { const data = Buffer.alloc(7).toString("base64"); - const { extractImageContentFromSource } = await import("./input-files.js"); const fromSpy = vi.spyOn(Buffer, "from"); - await expect( - extractImageContentFromSource( - { type: "base64", data, mediaType: "image/png" }, - { - allowUrl: false, - allowedMimes: new Set(["image/png"]), - maxBytes: 6, - maxRedirects: 0, - timeoutMs: 1, - }, - ), - ).rejects.toThrow("Image too large"); - - // Regression check: the oversize reject must happen before Buffer.from(..., "base64") allocates. - const base64Calls = fromSpy.mock.calls.filter((args) => (args as unknown[])[1] === "base64"); - expect(base64Calls).toHaveLength(0); - fromSpy.mockRestore(); - }); - - it("rejects oversized base64 files before decoding", async () => { - const data = Buffer.alloc(7).toString("base64"); - const { extractFileContentFromSource } = await import("./input-files.js"); - const fromSpy = vi.spyOn(Buffer, "from"); - await expect( - extractFileContentFromSource({ - source: { type: "base64", data, mediaType: "text/plain", filename: "x.txt" }, - limits: { - allowUrl: false, - allowedMimes: new Set(["text/plain"]), - maxBytes: 6, - maxChars: 100, - maxRedirects: 0, - timeoutMs: 1, - pdf: { maxPages: 1, maxPixels: 1, minTextChars: 1 }, - }, - }), - ).rejects.toThrow("File too large"); + await expect(testCase.run(data)).rejects.toThrow(testCase.expectedError); + // Regression check: oversize reject happens before Buffer.from(..., "base64") allocates. const base64Calls = fromSpy.mock.calls.filter((args) => (args as unknown[])[1] === "base64"); expect(base64Calls).toHaveLength(0); fromSpy.mockRestore();