Skip to content

Commit 86b97da

Browse files
committed
fix(opencode): validate clipboard content type on Linux
On Linux, xclip/wl-paste output raw bytes even when clipboard contains text (not an image). The code only checked byteLength > 0, so text was incorrectly labeled as image/png and sent to the model, which failed with 'could not process image'. Use existing sniffAttachmentMime() to validate PNG magic bytes before returning clipboard content as an image. Fixes #23458
1 parent 16caaa2 commit 86b97da

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

packages/opencode/src/cli/cmd/tui/util/clipboard.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from "path"
55
import fs from "fs/promises"
66
import * as Filesystem from "../../../../util/filesystem"
77
import * as Process from "../../../../util/process"
8+
import { sniffAttachmentMime } from "../../../../util/media"
89

910
// Lazy load which and clipboardy to avoid expensive execa/which/isexe chain at startup
1011
const getWhich = lazy(async () => {
@@ -93,13 +94,19 @@ export async function read(): Promise<Content | undefined> {
9394
if (os === "linux") {
9495
const wayland = await Process.run(["wl-paste", "-t", "image/png"], { nothrow: true })
9596
if (wayland.stdout.byteLength > 0) {
96-
return { data: Buffer.from(wayland.stdout).toString("base64"), mime: "image/png" }
97+
const mime = sniffAttachmentMime(new Uint8Array(wayland.stdout), "text/plain")
98+
if (mime === "image/png") {
99+
return { data: Buffer.from(wayland.stdout).toString("base64"), mime: "image/png" }
100+
}
97101
}
98102
const x11 = await Process.run(["xclip", "-selection", "clipboard", "-t", "image/png", "-o"], {
99103
nothrow: true,
100104
})
101105
if (x11.stdout.byteLength > 0) {
102-
return { data: Buffer.from(x11.stdout).toString("base64"), mime: "image/png" }
106+
const mime = sniffAttachmentMime(new Uint8Array(x11.stdout), "text/plain")
107+
if (mime === "image/png") {
108+
return { data: Buffer.from(x11.stdout).toString("base64"), mime: "image/png" }
109+
}
103110
}
104111
}
105112

0 commit comments

Comments
 (0)