fix: sanitize oversized image payloads
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import sharp from "sharp";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { sanitizeContentBlocksImages } from "./tool-images.js";
|
||||
import { sanitizeContentBlocksImages, sanitizeImageBlocks } from "./tool-images.js";
|
||||
|
||||
describe("tool image sanitizing", () => {
|
||||
it("shrinks oversized images to <=5MB", async () => {
|
||||
@@ -33,6 +33,56 @@ describe("tool image sanitizing", () => {
|
||||
expect(image.mimeType).toBe("image/jpeg");
|
||||
}, 20_000);
|
||||
|
||||
it("sanitizes image arrays and reports drops", async () => {
|
||||
const width = 2600;
|
||||
const height = 400;
|
||||
const raw = Buffer.alloc(width * height * 3, 0x7f);
|
||||
const png = await sharp(raw, {
|
||||
raw: { width, height, channels: 3 },
|
||||
})
|
||||
.png({ compressionLevel: 9 })
|
||||
.toBuffer();
|
||||
|
||||
const images = [
|
||||
{ type: "image" as const, data: png.toString("base64"), mimeType: "image/png" },
|
||||
];
|
||||
const { images: out, dropped } = await sanitizeImageBlocks(images, "test");
|
||||
expect(dropped).toBe(0);
|
||||
expect(out.length).toBe(1);
|
||||
const meta = await sharp(Buffer.from(out[0].data, "base64")).metadata();
|
||||
expect(meta.width).toBeLessThanOrEqual(2000);
|
||||
expect(meta.height).toBeLessThanOrEqual(2000);
|
||||
}, 20_000);
|
||||
|
||||
it("shrinks images that exceed max dimension even if size is small", async () => {
|
||||
const width = 2600;
|
||||
const height = 400;
|
||||
const raw = Buffer.alloc(width * height * 3, 0x7f);
|
||||
const png = await sharp(raw, {
|
||||
raw: { width, height, channels: 3 },
|
||||
})
|
||||
.png({ compressionLevel: 9 })
|
||||
.toBuffer();
|
||||
|
||||
const blocks = [
|
||||
{
|
||||
type: "image" as const,
|
||||
data: png.toString("base64"),
|
||||
mimeType: "image/png",
|
||||
},
|
||||
];
|
||||
|
||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||
const image = out.find((b) => b.type === "image");
|
||||
if (!image || image.type !== "image") {
|
||||
throw new Error("expected image block");
|
||||
}
|
||||
const meta = await sharp(Buffer.from(image.data, "base64")).metadata();
|
||||
expect(meta.width).toBeLessThanOrEqual(2000);
|
||||
expect(meta.height).toBeLessThanOrEqual(2000);
|
||||
expect(image.mimeType).toBe("image/jpeg");
|
||||
}, 20_000);
|
||||
|
||||
it("corrects mismatched jpeg mimeType", async () => {
|
||||
const jpeg = await sharp({
|
||||
create: {
|
||||
|
||||
Reference in New Issue
Block a user