From 0b7a1a04e6ea82dcac8777dacb881bab7ac52436 Mon Sep 17 00:00:00 2001 From: Aiden Cline <63023139+rekram1-node@users.noreply.github.com> Date: Fri, 13 Nov 2026 12:22:53 -0600 Subject: [PATCH] refactor(core): skip the text MIME scan when checking for media (#63823) --- packages/core/src/mime.ts | 6 +++++- packages/core/src/tool/plugin/image-generate.ts | 7 ++++--- packages/core/src/tool/read-filesystem.ts | 4 ++-- packages/core/test/mime.test.ts | 9 +++++++++ 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/packages/core/src/mime.ts b/packages/core/src/mime.ts index 3424be634dd..31338268aa4 100644 --- a/packages/core/src/mime.ts +++ b/packages/core/src/mime.ts @@ -1,6 +1,10 @@ export * as Mime from "./mime.js" export function detect(bytes: Uint8Array) { + return detectMedia(bytes) ?? (isText(bytes) ? "text/plain" : "application/octet-stream") +} + +export function detectMedia(bytes: Uint8Array) { if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "image/png" if (startsWith(bytes, [0xff, 0xd8, 0xff])) return "image/jpeg" const signature = ascii(bytes, 0, 6) @@ -29,7 +33,7 @@ export function detect(bytes: Uint8Array) { if (brand === "M4A ") return "audio/mp4" return "video/mp4" } - return isText(bytes) ? "text/plain" : "application/octet-stream" + return undefined } export const isTextual = (mime: string) => diff --git a/packages/core/src/tool/plugin/image-generate.ts b/packages/core/src/tool/plugin/image-generate.ts index 726e35c9b39..adff3d10630 100644 --- a/packages/core/src/tool/plugin/image-generate.ts +++ b/packages/core/src/tool/plugin/image-generate.ts @@ -198,8 +198,8 @@ export const Plugin = { return yield* new ToolFailure({ message: `${source} is larger than 20 MiB.` }) return (yield* environment.files.read(target.absolute)).bytes }) - const mime = Mime.detect(bytes) - if (!INPUT_TYPES.includes(mime)) + const mime = Mime.detectMedia(bytes) + if (!mime || !INPUT_TYPES.includes(mime)) return yield* new ToolFailure({ message: `${source} is not a PNG, JPEG, WebP or GIF image.` }) return { data: Buffer.from(bytes).toString("base64"), mime, name: path.basename(source) } }) @@ -314,7 +314,8 @@ export const Plugin = { const image = result.images[0] if (!image) return yield* Effect.fail(new Error(`${ref} returned no image.`)) const bytes = Buffer.from(image.data, "base64") - const file = stem + (EXTENSIONS[Mime.detect(bytes)] ?? ".png") + const mime = Mime.detectMedia(bytes) + const file = stem + (EXTENSIONS[mime ?? "image/png"] ?? ".png") // The edit prompt covered the requested path. Replacing a different existing file needs its own // approval; a new file only has to pass the deny rules. if (target && file !== target.absolute) { diff --git a/packages/core/src/tool/read-filesystem.ts b/packages/core/src/tool/read-filesystem.ts index 444379e5c71..5f1a91dd3d2 100644 --- a/packages/core/src/tool/read-filesystem.ts +++ b/packages/core/src/tool/read-filesystem.ts @@ -138,8 +138,8 @@ export const read = Effect.fn("ReadTool.read")(function* ( ) if (first instanceof ListPage) return first - const media = Mime.detect(first.bytes) - if (MEDIA_MIMES.has(media)) { + const media = Mime.detectMedia(first.bytes) + if (media && MEDIA_MIMES.has(media)) { if (first.info.size > MAX_MEDIA_INGEST_BYTES) return yield* new MediaIngestLimitError({ resource, maximumBytes: MAX_MEDIA_INGEST_BYTES }) const whole = yield* readFile(files, input, resource) diff --git a/packages/core/test/mime.test.ts b/packages/core/test/mime.test.ts index a63f1900512..efe65b4b254 100644 --- a/packages/core/test/mime.test.ts +++ b/packages/core/test/mime.test.ts @@ -4,6 +4,15 @@ import { Mime } from "@opencode/core/mime" const ftyp = (hex: string) => Uint8Array.from(Buffer.from(hex, "hex")) describe("Mime.detect", () => { + test.each([ + [Uint8Array.of(65), "text/plain"], + [Uint8Array.of(0xff), "application/octet-stream"], + [Uint8Array.of(0xe2, 0x82), "text/plain"], // A sample may end inside a UTF-8 character. + ] as const)("leaves samples without media headers to the text fallback: %j", (bytes, mime) => { + expect(Mime.detectMedia(bytes)).toBeUndefined() + expect(Mime.detect(bytes)).toBe(mime) + }) + test("tells ISO-BMFF brands apart", () => { expect(Mime.detect(ftyp("000000206674797061766966000000004d695072617669666d6961666d696631"))).toBe("image/avif") expect(Mime.detect(ftyp("000000186674797061766973000000006d73663161766973"))).toBe("image/avif")