Skip to content

Commit cd69363

Browse files
committed
fix(session): sanitize image attachments before sending to API
Prevents sessions from getting stuck replaying image attachments that exceed provider limits. Three new guards added to unsupportedParts(): - Unsupported MIME type: only image/jpeg, image/png, image/gif, image/webp are accepted (all major providers). image/jpg is normalized to image/jpeg. Anything else (bmp, tiff, svg, etc.) is replaced with a descriptive error. - Size limit: base64 payload > 5 MB is rejected. Anthropic enforces a hard 5 MB cap; exceeding it poisons the session history and causes every subsequent request to fail with a 400. - Dimension limit: PNG images with any side > 2000px are rejected. Anthropic rejects these in multi-image requests with a 400. Dimensions are read from the PNG IHDR header without a full decode (fast, no deps). Adds 6 new tests covering: unsupported MIME, jpg normalization, oversized payload, oversized PNG dimensions, valid small PNG, all four accepted types. Closes: anomalyco#4781 Related: anomalyco#9785, anomalyco#9512, BASIC-BIT#1
1 parent 1f290fc commit cd69363

2 files changed

Lines changed: 211 additions & 1 deletion

File tree

‎packages/opencode/src/provider/transform.ts‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,24 +237,101 @@ export namespace ProviderTransform {
237237
return msgs
238238
}
239239

240+
// Image MIME types supported by all major providers (Anthropic, OpenAI, Google)
241+
const SUPPORTED_IMAGE_MIMES = new Set(["image/jpeg", "image/png", "image/gif", "image/webp"])
242+
243+
// Anthropic enforces a 5 MB limit on base64-encoded image data
244+
const IMAGE_MAX_BASE64_BYTES = 5 * 1024 * 1024
245+
246+
// Anthropic rejects images with any dimension > 2000px in multi-image requests
247+
const IMAGE_MAX_DIMENSION_PX = 2000
248+
249+
/**
250+
* Reads width and height from a PNG or JPEG data URL without a full decode.
251+
* Returns [0, 0] if the format is unrecognised or the buffer is too short.
252+
*/
253+
function imageDimensions(b64: string): [number, number] {
254+
try {
255+
// Only need the first ~32 bytes for PNG (IHDR) and JPEG (SOF marker)
256+
const header = atob(b64.slice(0, 64))
257+
const bytes = Uint8Array.from(header, (c) => c.charCodeAt(0))
258+
259+
// PNG: signature \x89PNG at offset 0, IHDR width at 16, height at 20
260+
if (
261+
bytes[0] === 0x89 &&
262+
bytes[1] === 0x50 &&
263+
bytes[2] === 0x4e &&
264+
bytes[3] === 0x47
265+
) {
266+
const w = (bytes[16] << 24) | (bytes[17] << 16) | (bytes[18] << 8) | bytes[19]
267+
const h = (bytes[20] << 24) | (bytes[21] << 16) | (bytes[22] << 8) | bytes[23]
268+
return [w >>> 0, h >>> 0]
269+
}
270+
271+
// JPEG: signature \xFF\xD8
272+
if (bytes[0] === 0xff && bytes[1] === 0xd8) {
273+
// We can't reliably get dimensions from just 32 bytes of JPEG without
274+
// scanning for the SOF marker, so skip dimension check for JPEG.
275+
return [0, 0]
276+
}
277+
} catch {
278+
// atob can throw on malformed input; treat as unknown dimensions
279+
}
280+
return [0, 0]
281+
}
282+
240283
function unsupportedParts(msgs: ModelMessage[], model: Provider.Model): ModelMessage[] {
241284
return msgs.map((msg) => {
242285
if (msg.role !== "user" || !Array.isArray(msg.content)) return msg
243286

244287
const filtered = msg.content.map((part) => {
245288
if (part.type !== "file" && part.type !== "image") return part
246289

247-
// Check for empty base64 image data
248290
if (part.type === "image") {
249291
const imageStr = part.image.toString()
250292
if (imageStr.startsWith("data:")) {
251293
const match = imageStr.match(/^data:([^;]+);base64,(.*)$/)
294+
295+
// Empty / corrupted image
252296
if (match && (!match[2] || match[2].length === 0)) {
253297
return {
254298
type: "text" as const,
255299
text: "ERROR: Image file is empty or corrupted. Please provide a valid image.",
256300
}
257301
}
302+
303+
if (match) {
304+
const rawMime = match[1].toLowerCase()
305+
const b64 = match[2]
306+
307+
// Normalize image/jpg -> image/jpeg
308+
const mime = rawMime === "image/jpg" ? "image/jpeg" : rawMime
309+
310+
// Unsupported MIME type
311+
if (!SUPPORTED_IMAGE_MIMES.has(mime)) {
312+
return {
313+
type: "text" as const,
314+
text: `ERROR: Image format "${mime}" is not supported. Supported formats: jpeg, png, gif, webp.`,
315+
}
316+
}
317+
318+
// Exceeds 5 MB base64 payload limit
319+
if (b64.length > IMAGE_MAX_BASE64_BYTES) {
320+
return {
321+
type: "text" as const,
322+
text: `ERROR: Image is too large (${Math.round(b64.length / 1024 / 1024 * 10) / 10} MB). Maximum allowed size is 5 MB. Please resize the image before attaching it.`,
323+
}
324+
}
325+
326+
// Exceeds 2000px dimension limit (PNG only — fast header read)
327+
const [w, h] = imageDimensions(b64)
328+
if (w > IMAGE_MAX_DIMENSION_PX || h > IMAGE_MAX_DIMENSION_PX) {
329+
return {
330+
type: "text" as const,
331+
text: `ERROR: Image dimensions (${w}×${h}) exceed the maximum allowed size of ${IMAGE_MAX_DIMENSION_PX}px per side. Please resize the image before attaching it.`,
332+
}
333+
}
334+
}
258335
}
259336
}
260337

‎packages/opencode/test/provider/transform.test.ts‎

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,6 +1014,139 @@ describe("ProviderTransform.message - empty image handling", () => {
10141014
})
10151015
})
10161016

1017+
describe("ProviderTransform.message - image sanitization", () => {
1018+
const mockModel = {
1019+
id: "anthropic/claude-3-5-sonnet",
1020+
providerID: "anthropic",
1021+
api: {
1022+
id: "claude-3-5-sonnet-20241022",
1023+
url: "https://api.anthropic.com",
1024+
npm: "@ai-sdk/anthropic",
1025+
},
1026+
name: "Claude 3.5 Sonnet",
1027+
capabilities: {
1028+
temperature: true,
1029+
reasoning: false,
1030+
attachment: true,
1031+
toolcall: true,
1032+
input: { text: true, audio: false, image: true, video: false, pdf: true },
1033+
output: { text: true, audio: false, image: false, video: false, pdf: false },
1034+
interleaved: false,
1035+
},
1036+
cost: { input: 0.003, output: 0.015, cache: { read: 0.0003, write: 0.00375 } },
1037+
limit: { context: 200000, output: 8192 },
1038+
status: "active",
1039+
options: {},
1040+
headers: {},
1041+
} as any
1042+
1043+
// Smallest valid 1×1 PNG as base64
1044+
const tiny1x1Png =
1045+
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=="
1046+
1047+
test("should reject unsupported MIME type (image/bmp)", () => {
1048+
const msgs = [
1049+
{
1050+
role: "user",
1051+
content: [{ type: "image", image: `data:image/bmp;base64,${tiny1x1Png}` }],
1052+
},
1053+
] as any[]
1054+
1055+
const result = ProviderTransform.message(msgs, mockModel, {}) as any[]
1056+
expect(result[0].content[0].type).toBe("text")
1057+
expect(result[0].content[0].text).toMatch(/not supported/)
1058+
expect(result[0].content[0].text).toMatch(/image\/bmp/)
1059+
})
1060+
1061+
test("should normalize image/jpg to image/jpeg and accept it", () => {
1062+
const msgs = [
1063+
{
1064+
role: "user",
1065+
content: [{ type: "image", image: `data:image/jpg;base64,${tiny1x1Png}` }],
1066+
},
1067+
] as any[]
1068+
1069+
const result = ProviderTransform.message(msgs, mockModel, {}) as any[]
1070+
// Should NOT be replaced with an error — image/jpg normalizes to image/jpeg which is supported
1071+
expect(result[0].content[0].type).toBe("image")
1072+
})
1073+
1074+
test("should reject image exceeding 5 MB base64 limit", () => {
1075+
// Generate a base64 string that exceeds 5 * 1024 * 1024 chars
1076+
const hugeb64 = "A".repeat(5 * 1024 * 1024 + 1)
1077+
const msgs = [
1078+
{
1079+
role: "user",
1080+
content: [{ type: "image", image: `data:image/png;base64,${hugeb64}` }],
1081+
},
1082+
] as any[]
1083+
1084+
const result = ProviderTransform.message(msgs, mockModel, {}) as any[]
1085+
expect(result[0].content[0].type).toBe("text")
1086+
expect(result[0].content[0].text).toMatch(/too large/)
1087+
expect(result[0].content[0].text).toMatch(/5 MB/)
1088+
})
1089+
1090+
test("should reject PNG image with dimension exceeding 2000px", () => {
1091+
// Craft a fake PNG header with width=2001, height=100
1092+
// PNG signature: 89 50 4E 47 0D 0A 1A 0A
1093+
// IHDR chunk: length(4) + "IHDR"(4) + width(4) + height(4) + ...
1094+
const buf = new Uint8Array(32)
1095+
// PNG signature
1096+
buf[0] = 0x89; buf[1] = 0x50; buf[2] = 0x4e; buf[3] = 0x47
1097+
buf[4] = 0x0d; buf[5] = 0x0a; buf[6] = 0x1a; buf[7] = 0x0a
1098+
// IHDR length = 13
1099+
buf[8] = 0; buf[9] = 0; buf[10] = 0; buf[11] = 13
1100+
// "IHDR"
1101+
buf[12] = 0x49; buf[13] = 0x48; buf[14] = 0x44; buf[15] = 0x52
1102+
// width = 2001 = 0x000007D1
1103+
buf[16] = 0x00; buf[17] = 0x00; buf[18] = 0x07; buf[19] = 0xd1
1104+
// height = 100 = 0x00000064
1105+
buf[20] = 0x00; buf[21] = 0x00; buf[22] = 0x00; buf[23] = 0x64
1106+
1107+
const b64 = btoa(String.fromCharCode(...buf))
1108+
const msgs = [
1109+
{
1110+
role: "user",
1111+
content: [{ type: "image", image: `data:image/png;base64,${b64}` }],
1112+
},
1113+
] as any[]
1114+
1115+
const result = ProviderTransform.message(msgs, mockModel, {}) as any[]
1116+
expect(result[0].content[0].type).toBe("text")
1117+
expect(result[0].content[0].text).toMatch(/dimensions/)
1118+
expect(result[0].content[0].text).toMatch(/2001/)
1119+
expect(result[0].content[0].text).toMatch(/2000px/)
1120+
})
1121+
1122+
test("should accept a valid small PNG image unchanged", () => {
1123+
const msgs = [
1124+
{
1125+
role: "user",
1126+
content: [{ type: "image", image: `data:image/png;base64,${tiny1x1Png}` }],
1127+
},
1128+
] as any[]
1129+
1130+
const result = ProviderTransform.message(msgs, mockModel, {}) as any[]
1131+
expect(result[0].content[0].type).toBe("image")
1132+
expect(result[0].content[0].image).toContain(tiny1x1Png)
1133+
})
1134+
1135+
test("should accept supported MIME types: jpeg, png, gif, webp", () => {
1136+
for (const mime of ["image/jpeg", "image/png", "image/gif", "image/webp"]) {
1137+
const msgs = [
1138+
{
1139+
role: "user",
1140+
content: [{ type: "image", image: `data:${mime};base64,${tiny1x1Png}` }],
1141+
},
1142+
] as any[]
1143+
1144+
const result = ProviderTransform.message(msgs, mockModel, {}) as any[]
1145+
expect(result[0].content[0].type).toBe("image")
1146+
}
1147+
})
1148+
})
1149+
10171150
describe("ProviderTransform.message - anthropic empty content filtering", () => {
10181151
const anthropicModel = {
10191152
id: "anthropic/claude-3-5-sonnet",

0 commit comments

Comments
 (0)