diff options
| author | Aiden Cline <[email protected]> | 2025-07-30 11:00:23 -0500 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-30 11:00:23 -0500 |
| commit | 1b3d58e7919714dbeefc571c278ec65b90154c7d (patch) | |
| tree | 3794c266383fa397cc732e16c97bdc98ea2d281a | |
| parent | 772c83c1d5d4ba3f57dc15a394392cfe53f7217d (diff) | |
| download | opencode-1b3d58e7919714dbeefc571c278ec65b90154c7d.tar.gz opencode-1b3d58e7919714dbeefc571c278ec65b90154c7d.zip | |
fix: prevent read tool from opening binary files and corrupting session (#1425)
| -rw-r--r-- | packages/opencode/src/tool/read.ts | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/packages/opencode/src/tool/read.ts b/packages/opencode/src/tool/read.ts index f93840afb..c66716413 100644 --- a/packages/opencode/src/tool/read.ts +++ b/packages/opencode/src/tool/read.ts @@ -48,6 +48,8 @@ export const ReadTool = Tool.define("read", { const offset = params.offset || 0 const isImage = isImageFile(filePath) if (isImage) throw new Error(`This is an image file of type: ${isImage}\nUse a different tool to process images`) + const isBinary = await isBinaryFile(file) + if (isBinary) throw new Error(`Cannot read binary file: ${filePath}`) const lines = await file.text().then((text) => text.split("\n")) const raw = lines.slice(offset, offset + limit).map((line) => { return line.length > MAX_LINE_LENGTH ? line.substring(0, MAX_LINE_LENGTH) + "..." : line @@ -99,3 +101,14 @@ function isImageFile(filePath: string): string | false { return false } } + +async function isBinaryFile(file: Bun.BunFile): Promise<boolean> { + const buffer = await file.arrayBuffer() + const bytes = new Uint8Array(buffer.slice(0, 512)) // Check first 512 bytes + + for (let i = 0; i < bytes.length; i++) { + if (bytes[i] === 0) return true // Null byte indicates binary + } + + return false +} |
