Skip to content

Commit b28b36a

Browse files
committed
fix(read): reject unsupported image formats before sending to provider
The Read tool accepted any image/* MIME type (except SVG and fastbidsheet) and sent it to the LLM provider. Providers like Anthropic only support JPEG, PNG, GIF, and WebP, so formats like BMP, TIFF, AVIF, and HEIC caused API validation errors. Because the unsupported attachment was persisted in the conversation history, every subsequent message replayed it, making the session unrecoverable. Closes anomalyco#17772 Closes anomalyco#15264
1 parent 3a0e00d commit b28b36a

2 files changed

Lines changed: 46 additions & 1 deletion

File tree

‎packages/opencode/src/tool/read.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,20 @@ export const ReadTool = Tool.defineEffect(
150150
const loaded = yield* instruction.resolve(ctx.messages, filepath, ctx.messageID)
151151

152152
const mime = AppFileSystem.mimeType(filepath)
153-
const isImage = mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet"
153+
const supportedImageMimes = new Set(["image/jpeg", "image/png", "image/gif", "image/webp"])
154+
const isImage = supportedImageMimes.has(mime)
155+
const isUnsupportedImage =
156+
!isImage && mime.startsWith("image/") && mime !== "image/svg+xml" && mime !== "image/vnd.fastbidsheet"
154157
const isPdf = mime === "application/pdf"
158+
159+
if (isUnsupportedImage) {
160+
return yield* Effect.fail(
161+
new Error(
162+
`Cannot read image: ${mime} is not a supported format. Supported image formats: JPEG, PNG, GIF, WebP.`,
163+
),
164+
)
165+
}
166+
155167
if (isImage || isPdf) {
156168
const msg = `${isImage ? "Image" : "PDF"} read successfully`
157169
return {

‎packages/opencode/test/tool/read.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,39 @@ root_type Monster;`
425425
expect(result.output).toContain("table Monster")
426426
}),
427427
)
428+
429+
it.live("rejects unsupported image formats like BMP", () =>
430+
Effect.gen(function* () {
431+
const dir = yield* tmpdirScoped()
432+
// minimal BMP file header
433+
const bmp = Buffer.from("BM", "ascii")
434+
yield* put(path.join(dir, "image.bmp"), bmp)
435+
436+
const err = yield* fail(dir, { filePath: path.join(dir, "image.bmp") })
437+
expect(err.message).toContain("image/bmp is not a supported format")
438+
expect(err.message).toContain("JPEG, PNG, GIF, WebP")
439+
}),
440+
)
441+
442+
it.live("rejects unsupported image formats like TIFF", () =>
443+
Effect.gen(function* () {
444+
const dir = yield* tmpdirScoped()
445+
yield* put(path.join(dir, "photo.tiff"), Buffer.from("II", "ascii"))
446+
447+
const err = yield* fail(dir, { filePath: path.join(dir, "photo.tiff") })
448+
expect(err.message).toContain("is not a supported format")
449+
}),
450+
)
451+
452+
it.live("rejects unsupported image formats like AVIF", () =>
453+
Effect.gen(function* () {
454+
const dir = yield* tmpdirScoped()
455+
yield* put(path.join(dir, "photo.avif"), Buffer.from([0x00, 0x00, 0x00, 0x1c]))
456+
457+
const err = yield* fail(dir, { filePath: path.join(dir, "photo.avif") })
458+
expect(err.message).toContain("is not a supported format")
459+
}),
460+
)
428461
})
429462

430463
describe("tool.read loaded instructions", () => {

0 commit comments

Comments
 (0)