mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 19:18:26 +00:00
revert(agents): revert base64 image validation (#19221)
This commit is contained in:
@@ -2,153 +2,6 @@ import sharp from "sharp";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { sanitizeContentBlocksImages, sanitizeImageBlocks } from "./tool-images.js";
|
||||
|
||||
describe("base64 validation", () => {
|
||||
it("rejects invalid base64 characters and replaces with error text", async () => {
|
||||
const blocks = [
|
||||
{
|
||||
type: "image" as const,
|
||||
data: "not-valid-base64!!!@#$%",
|
||||
mimeType: "image/png",
|
||||
},
|
||||
];
|
||||
|
||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||
expect(out.length).toBe(1);
|
||||
expect(out[0].type).toBe("text");
|
||||
if (out[0].type === "text") {
|
||||
expect(out[0].text).toContain("omitted image payload");
|
||||
expect(out[0].text).toContain("invalid");
|
||||
}
|
||||
});
|
||||
|
||||
it("strips data URL prefix and processes valid base64", async () => {
|
||||
// Create a small valid image
|
||||
const jpeg = await sharp({
|
||||
create: {
|
||||
width: 10,
|
||||
height: 10,
|
||||
channels: 3,
|
||||
background: { r: 255, g: 0, b: 0 },
|
||||
},
|
||||
})
|
||||
.jpeg()
|
||||
.toBuffer();
|
||||
|
||||
const base64 = jpeg.toString("base64");
|
||||
const dataUrl = `data:image/jpeg;base64,${base64}`;
|
||||
|
||||
const blocks = [
|
||||
{
|
||||
type: "image" as const,
|
||||
data: dataUrl,
|
||||
mimeType: "image/jpeg",
|
||||
},
|
||||
];
|
||||
|
||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||
expect(out.length).toBe(1);
|
||||
expect(out[0].type).toBe("image");
|
||||
});
|
||||
|
||||
it("rejects base64 with invalid padding", async () => {
|
||||
const blocks = [
|
||||
{
|
||||
type: "image" as const,
|
||||
data: "SGVsbG8===", // too many padding chars
|
||||
mimeType: "image/png",
|
||||
},
|
||||
];
|
||||
|
||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||
expect(out.length).toBe(1);
|
||||
expect(out[0].type).toBe("text");
|
||||
if (out[0].type === "text") {
|
||||
expect(out[0].text).toContain("omitted image payload");
|
||||
}
|
||||
});
|
||||
|
||||
it("rejects base64 with padding in wrong position", async () => {
|
||||
const blocks = [
|
||||
{
|
||||
type: "image" as const,
|
||||
data: "SGVs=bG8=", // = in middle is invalid
|
||||
mimeType: "image/png",
|
||||
},
|
||||
];
|
||||
|
||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||
expect(out.length).toBe(1);
|
||||
expect(out[0].type).toBe("text");
|
||||
if (out[0].type === "text") {
|
||||
expect(out[0].text).toContain("omitted image payload");
|
||||
}
|
||||
});
|
||||
|
||||
it("normalizes URL-safe base64 to standard base64", async () => {
|
||||
// Create a small valid image
|
||||
const jpeg = await sharp({
|
||||
create: {
|
||||
width: 10,
|
||||
height: 10,
|
||||
channels: 3,
|
||||
background: { r: 255, g: 0, b: 0 },
|
||||
},
|
||||
})
|
||||
.jpeg()
|
||||
.toBuffer();
|
||||
|
||||
// Convert to URL-safe base64 (replace + with -, / with _)
|
||||
const standardBase64 = jpeg.toString("base64");
|
||||
const urlSafeBase64 = standardBase64.replace(/\+/g, "-").replace(/\//g, "_");
|
||||
|
||||
const blocks = [
|
||||
{
|
||||
type: "image" as const,
|
||||
data: urlSafeBase64,
|
||||
mimeType: "image/jpeg",
|
||||
},
|
||||
];
|
||||
|
||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||
expect(out.length).toBe(1);
|
||||
expect(out[0].type).toBe("image");
|
||||
});
|
||||
|
||||
it("rejects base64 with invalid length", async () => {
|
||||
const blocks = [
|
||||
{
|
||||
type: "image" as const,
|
||||
data: "AAAAA", // length 5 without padding is invalid (remainder 1)
|
||||
mimeType: "image/png",
|
||||
},
|
||||
];
|
||||
|
||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||
expect(out.length).toBe(1);
|
||||
expect(out[0].type).toBe("text");
|
||||
if (out[0].type === "text") {
|
||||
expect(out[0].text).toContain("omitted image payload");
|
||||
}
|
||||
});
|
||||
|
||||
it("handles empty base64 data gracefully", async () => {
|
||||
const blocks = [
|
||||
{
|
||||
type: "image" as const,
|
||||
data: " ",
|
||||
mimeType: "image/png",
|
||||
},
|
||||
];
|
||||
|
||||
const out = await sanitizeContentBlocksImages(blocks, "test");
|
||||
expect(out.length).toBe(1);
|
||||
expect(out[0].type).toBe("text");
|
||||
if (out[0].type === "text") {
|
||||
expect(out[0].text).toContain("omitted empty image payload");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("tool image sanitizing", () => {
|
||||
it("shrinks oversized images to <=5MB", async () => {
|
||||
const width = 2800;
|
||||
|
||||
Reference in New Issue
Block a user