mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-18 14:17:27 +00:00
test: merge base64 oversize guard variants
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user