Skip to content

Commit b7d2647

Browse files
committed
fix: reject unsupported MIME types in image blocks
Extract the shared image MIME guard from PR Zoo-Code-Org#1950 as a standalone prerequisite for Zoo-Code-Org#1948. Preserve the supported SDK payload contract and cover both formatter entry points, malformed headers, Unicode lookalikes, and opaque payload boundaries.
1 parent f43dd37 commit b7d2647

3 files changed

Lines changed: 150 additions & 4 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { formatResponse } from "../responses"
2+
import { IMAGE_MIME_TYPES } from "../../tools/helpers/imageHelpers"
3+
import { getImageMimeType, isSupportedImageMimeType } from "../../../utils/imageMime"
4+
5+
const supportedMimeTypes = ["image/jpeg", "image/png", "image/gif", "image/webp"] as const
6+
const supportedMimeTypeSet = new Set<string>(supportedMimeTypes)
7+
const unsupportedMimeTypes = [
8+
...new Set([...Object.values(IMAGE_MIME_TYPES), "image/heic", "application/octet-stream", "image/svg+xml"]),
9+
].filter((mimeType) => !supportedMimeTypeSet.has(mimeType))
10+
const payload = "c3ludGhldGlj"
11+
12+
describe("image MIME utilities", () => {
13+
it.each(supportedMimeTypes)("extracts and accepts the SDK MIME type %s", (mimeType) => {
14+
expect(getImageMimeType(`data:${mimeType};base64,${payload}`)).toBe(mimeType)
15+
expect(isSupportedImageMimeType(mimeType)).toBe(true)
16+
})
17+
18+
it.each(unsupportedMimeTypes)("extracts but rejects the unsupported MIME type %s", (mimeType) => {
19+
expect(getImageMimeType(`data:${mimeType};base64,${payload}`)).toBe(mimeType)
20+
expect(isSupportedImageMimeType(mimeType)).toBe(false)
21+
})
22+
23+
it("returns no MIME for a missing header and rejects absent or empty MIME values", () => {
24+
expect(getImageMimeType("")).toBeUndefined()
25+
expect(isSupportedImageMimeType(undefined)).toBe(false)
26+
expect(isSupportedImageMimeType("")).toBe(false)
27+
})
28+
})
29+
30+
describe("shared image-block MIME contract", () => {
31+
it.each(supportedMimeTypes)("preserves the complete image block for %s through both entry points", (mimeType) => {
32+
const dataUrl = `data:${mimeType};base64,${payload}`
33+
const image = { type: "image", source: { type: "base64", media_type: mimeType, data: payload } }
34+
expect(formatResponse.imageBlocks([dataUrl])).toEqual([image])
35+
expect(formatResponse.toolResult("read result", [dataUrl])).toEqual([
36+
{ type: "text", text: "read result" },
37+
image,
38+
])
39+
})
40+
41+
it.each(unsupportedMimeTypes)("rejects %s through both entry points before returning a payload", (mimeType) => {
42+
const dataUrl = `data:${mimeType};base64,${payload}`
43+
const error = new Error(`Unsupported image MIME type: ${mimeType}. Use JPEG, PNG, GIF, or WebP.`)
44+
expect(() => formatResponse.imageBlocks([dataUrl])).toThrowError(error)
45+
expect(() => formatResponse.toolResult("read result", [dataUrl])).toThrowError(error)
46+
})
47+
48+
it.each([
49+
"",
50+
"not a data URL",
51+
"data:;base64,c3ludGhldGlj",
52+
"data:image/png,c3ludGhldGlj",
53+
"data:image/png;base64",
54+
"data:image/png;base64;c3ludGhldGlj",
55+
"data:image/png;charset=utf-8;base64,c3ludGhldGlj",
56+
"DATA:image/png;base64,c3ludGhldGlj",
57+
"data:image/png;BASE64,c3ludGhldGlj",
58+
"prefix data:image/png;base64,c3ludGhldGlj",
59+
" data:image/png;base64,c3ludGhldGlj",
60+
"\uFEFFdata:image/png;base64,c3ludGhldGlj",
61+
"data\uFF1Aimage/png;base64,c3ludGhldGlj",
62+
"data:image/png;base64\uFF0Cc3ludGhldGlj",
63+
])("rejects a malformed or noncanonical header: %j", (dataUrl) => {
64+
const error = new Error("Unsupported image MIME type: unknown. Use JPEG, PNG, GIF, or WebP.")
65+
expect(() => formatResponse.imageBlocks([dataUrl])).toThrowError(error)
66+
expect(() => formatResponse.toolResult("read result", [dataUrl])).toThrowError(error)
67+
})
68+
69+
it.each(["image/PNG", "image/jpg", "image/png ", "image/p\u200Bng", "image/\u0440ng", "image\uFF0Fpng"])(
70+
"does not normalize unsupported MIME spelling %j into an allowed type",
71+
(mimeType) => {
72+
const dataUrl = `data:${mimeType};base64,${payload}`
73+
const error = new Error(`Unsupported image MIME type: ${mimeType}. Use JPEG, PNG, GIF, or WebP.`)
74+
expect(() => formatResponse.imageBlocks([dataUrl])).toThrowError(error)
75+
expect(() => formatResponse.toolResult("read result", [dataUrl])).toThrowError(error)
76+
},
77+
)
78+
79+
it.each([{ images: undefined }, { images: [] }])("preserves the no-image response for $images", ({ images }) => {
80+
expect(formatResponse.imageBlocks(images)).toEqual([])
81+
expect(formatResponse.toolResult("read result", images)).toBe("read result")
82+
expect(formatResponse.toolResult("", images)).toBe("")
83+
})
84+
85+
it("preserves image order, duplicate images, Unicode feedback, and the input array", () => {
86+
const images = [
87+
"data:image/png;base64,Zmlyc3Q=",
88+
"data:image/jpeg;base64,c2Vjb25k",
89+
"data:image/png;base64,Zmlyc3Q=",
90+
]
91+
const originalImages = [...images]
92+
const expected = [
93+
{ type: "image", source: { type: "base64", media_type: "image/png", data: "Zmlyc3Q=" } },
94+
{ type: "image", source: { type: "base64", media_type: "image/jpeg", data: "c2Vjb25k" } },
95+
{ type: "image", source: { type: "base64", media_type: "image/png", data: "Zmlyc3Q=" } },
96+
]
97+
const text = "\u041E\u0442\u0432\u0435\u0442 \uD83D\uDDBC\uFE0F e\u0301"
98+
expect(formatResponse.imageBlocks(images)).toEqual(expected)
99+
expect(formatResponse.toolResult(text, images)).toEqual([{ type: "text", text }, ...expected])
100+
expect(images).toEqual(originalImages)
101+
})
102+
103+
it.each([0, 1, 2])("rejects an unsupported image at index %i without changing the input", (index) => {
104+
const images = Array<string>(3).fill(`data:image/png;base64,${payload}`)
105+
images[index] = `data:image/bmp;base64,${payload}`
106+
const originalImages = [...images]
107+
const error = new Error("Unsupported image MIME type: image/bmp. Use JPEG, PNG, GIF, or WebP.")
108+
expect(() => formatResponse.imageBlocks(images)).toThrowError(error)
109+
expect(() => formatResponse.toolResult("read result", images)).toThrowError(error)
110+
expect(images).toEqual(originalImages)
111+
})
112+
113+
it.each(["", "not decoded here", "first,second", "\uD83D\uDE00e\u0301"])(
114+
"preserves the opaque payload %j without adding byte or base64 validation",
115+
(data) => {
116+
const dataUrl = `data:image/png;base64,${data}`
117+
const image = { type: "image", source: { type: "base64", media_type: "image/png", data } }
118+
expect(formatResponse.imageBlocks([dataUrl])).toEqual([image])
119+
expect(formatResponse.toolResult("", [dataUrl])).toEqual([{ type: "text", text: "" }, image])
120+
},
121+
)
122+
})

‎src/core/prompts/responses.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as path from "path"
33
import * as diff from "diff"
44
import { RooIgnoreController, LOCK_TEXT_SYMBOL } from "../ignore/RooIgnoreController"
55
import { RooProtectedController } from "../protect/RooProtectedController"
6+
import { getImageMimeType, isSupportedImageMimeType } from "../../utils/imageMime"
67

78
export const formatResponse = {
89
toolDenied: () =>
@@ -226,14 +227,19 @@ Otherwise, if you have not completed the task and do not need additional informa
226227
// to avoid circular dependency
227228
const formatImagesIntoBlocks = (images?: string[]): Anthropic.ImageBlockParam[] => {
228229
return images
229-
? images.map((dataUrl) => {
230+
? images.map((dataUrl): Anthropic.ImageBlockParam => {
230231
// data:image/png;base64,base64string
231-
const [rest, base64] = dataUrl.split(",")
232-
const mimeType = rest.split(":")[1].split(";")[0]
232+
const mimeType = getImageMimeType(dataUrl)
233+
if (!isSupportedImageMimeType(mimeType)) {
234+
throw new Error(
235+
`Unsupported image MIME type: ${mimeType ?? "unknown"}. Use JPEG, PNG, GIF, or WebP.`,
236+
)
237+
}
238+
const base64 = dataUrl.slice(dataUrl.indexOf(",") + 1)
233239
return {
234240
type: "image",
235241
source: { type: "base64", media_type: mimeType, data: base64 },
236-
} as Anthropic.ImageBlockParam
242+
}
237243
})
238244
: []
239245
}

‎src/utils/imageMime.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import type { Anthropic } from "@anthropic-ai/sdk"
2+
3+
export function getImageMimeType(dataUrl: string): string | undefined {
4+
return /^data:([^;]+);base64,/.exec(dataUrl)?.[1]
5+
}
6+
7+
/**
8+
* All providers consume the shared Anthropic image-block contract. Since model
9+
* capabilities expose only supportsImages, conservatively use its four MIME
10+
* types rather than assuming provider-specific support for other encodings.
11+
*/
12+
export function isSupportedImageMimeType(
13+
mimeType: string | undefined,
14+
): mimeType is Anthropic.Base64ImageSource["media_type"] {
15+
return (
16+
mimeType === "image/jpeg" || mimeType === "image/png" || mimeType === "image/gif" || mimeType === "image/webp"
17+
)
18+
}

0 commit comments

Comments
 (0)