#!/usr/bin/env node /** * Patcher for Claude Code CLI — fixes image reading crash on Windows. * * Root cause: In some code paths, `media_type` field is undefined when * constructing image content blocks for the API. JSON.stringify omits * undefined values, so the field is absent from the request body. * The API returns 400 "media_type: Field required" which permanently * poisons the conversation context and kills the session. * * This patcher: * 1. Installs `sharp` into claude-code's node_modules (if missing) * 2. Patches the Nv8 (image reader) function to gracefully handle errors * 3. Patches the image mapper to guarantee media_type is always present * * Usage: * node tools/patch_claude_code.js # apply patch * node tools/patch_claude_code.js --check # check status only * node tools/patch_claude_code.js --revert # revert patch * * Safe to run multiple times — idempotent. */ const fs = require("fs"); const path = require("path"); const { execSync } = require("child_process"); // Find claude-code installation function findClaudeCodeDir() { const npmGlobal = execSync("npm root -g", { encoding: "utf8" }).trim(); const claudeDir = path.join(npmGlobal, "@anthropic-ai", "claude-code"); if (fs.existsSync(path.join(claudeDir, "cli.js"))) return claudeDir; // Fallback: try common paths const fallbacks = [ path.join(process.env.APPDATA || "", "npm", "node_modules", "@anthropic-ai", "claude-code"), path.join(process.env.HOME || "", ".npm-global", "lib", "node_modules", "@anthropic-ai", "claude-code"), "/usr/local/lib/node_modules/@anthropic-ai/claude-code", ]; for (const dir of fallbacks) { if (fs.existsSync(path.join(dir, "cli.js"))) return dir; } return null; } // Check if sharp is installed function isSharpInstalled(claudeDir) { try { const sharpDir = path.join(claudeDir, "node_modules", "sharp"); return fs.existsSync(sharpDir); } catch { return false; } } // Install sharp function installSharp(claudeDir) { console.log("[*] Installing sharp..."); try { execSync("npm install sharp", { cwd: claudeDir, encoding: "utf8", stdio: "pipe" }); console.log("[+] sharp installed successfully"); return true; } catch (e) { console.error("[-] Failed to install sharp:", e.message); return false; } } const PATCH_MARKER = "/* PATCHED_NV8_SAFE_IMAGE_READ */"; const MAPPER_PATCH_MARKER = "/* PATCHED_IMAGE_MEDIA_TYPE */"; function readCliJs(claudeDir) { return fs.readFileSync(path.join(claudeDir, "cli.js"), "utf8"); } function writeCliJs(claudeDir, code) { // Backup first const backupPath = path.join(claudeDir, "cli.js.bak"); if (!fs.existsSync(backupPath)) { fs.copyFileSync(path.join(claudeDir, "cli.js"), backupPath); console.log("[+] Backup created: cli.js.bak"); } fs.writeFileSync(path.join(claudeDir, "cli.js"), code, "utf8"); } function isPatched(code) { return code.includes(PATCH_MARKER); } function isMapperPatched(code) { return code.includes(MAPPER_PATCH_MARKER); } // Patch 1: Nv8 safety wrapper (try/catch around image reader) function applyNv8Patch(code) { if (code.includes(PATCH_MARKER)) { console.log("[=] Nv8 safety patch already applied"); return code; } const ORIGINAL_NV8_SIGNATURE = "async function Nv8(A,q=Tv8(),K){let Y=await X1().readFileBytes(A,K)"; const idx = code.indexOf(ORIGINAL_NV8_SIGNATURE); if (idx === -1) { console.error("[-] Could not find Nv8 function signature in cli.js"); console.error(" Claude Code may have been updated."); return code; } const endMarker = "}var Ns9"; const endIdx = code.indexOf(endMarker, idx); if (endIdx === -1) { console.error("[-] Could not find end of Nv8 function"); return code; } const patchedNv8 = `${PATCH_MARKER}async function Nv8(A,q=Tv8(),K){try{let Y=await X1().readFileBytes(A,K),z=Y.length;if(z===0)throw Error("Image file is empty: "+A);let w=kp6(Y),_=w.split("/")[1]||"png",$;try{let H=await ig(Y,z,_);$=q01(H.buffer,H.mediaType,z,H.dimensions)}catch(H){$6(H);$=q01(Y,_,z)}if(Math.ceil($.file.base64.length*0.125)>q)try{let H=await DP1(Y,q,w);return{type:"image",file:{base64:H.base64,type:H.mediaType||"image/png",originalSize:z}}}catch(H){$6(H);try{let j=await Promise.resolve().then(()=>q6(yN8(),1)),M=await(j.default||j)(Y).resize(400,400,{fit:"inside",withoutEnlargement:!0}).jpeg({quality:20}).toBuffer();return q01(M,"jpeg",z)}catch(j){return $6(j),q01(Y,_,z)}}return $}catch(_err){return{type:"text",file:{content:"[Error reading image: "+_err.message+"] File: "+A,totalLines:1}}}}`; code = code.slice(0, idx) + patchedNv8 + code.slice(endIdx + 1); console.log("[+] Nv8 safety patch applied"); return code; } // Patch 2: Image mapper — guarantee media_type is always a valid string // This is the ROOT CAUSE fix: A.file.type can be undefined in some code paths, // and JSON.stringify silently drops undefined fields, causing API 400 error. function applyMapperPatch(code) { if (code.includes(MAPPER_PATCH_MARKER)) { console.log("[=] Image mapper patch already applied"); return code; } const ORIGINAL_MAPPER = 'case"image":return{tool_use_id:q,type:"tool_result",content:[{type:"image",source:{type:"base64",data:A.file.base64,media_type:A.file.type}}]}'; const idx = code.indexOf(ORIGINAL_MAPPER); if (idx === -1) { console.error("[-] Could not find image mapper in cli.js"); console.error(" Claude Code may have been updated."); return code; } // Patched version: fallback media_type to "image/png" if undefined const PATCHED_MAPPER = `${MAPPER_PATCH_MARKER}case"image":return{tool_use_id:q,type:"tool_result",content:[{type:"image",source:{type:"base64",data:A.file.base64,media_type:A.file.type||"image/png"}}]}`; code = code.slice(0, idx) + PATCHED_MAPPER + code.slice(idx + ORIGINAL_MAPPER.length); console.log("[+] Image mapper patched — media_type guaranteed non-empty"); return code; } function revertPatch(claudeDir) { const backupPath = path.join(claudeDir, "cli.js.bak"); if (!fs.existsSync(backupPath)) { console.error("[-] No backup found at cli.js.bak"); return false; } fs.copyFileSync(backupPath, path.join(claudeDir, "cli.js")); console.log("[+] Reverted to original cli.js from backup"); return true; } // Main function main() { const args = process.argv.slice(2); const checkOnly = args.includes("--check"); const revert = args.includes("--revert"); console.log("=== Claude Code Image Read Patcher v2 ===\n"); const claudeDir = findClaudeCodeDir(); if (!claudeDir) { console.error("[-] Claude Code installation not found!"); console.error(" Install it with: npm install -g @anthropic-ai/claude-code"); process.exit(1); } console.log("[*] Found Claude Code at:", claudeDir); // Read package version try { const pkg = JSON.parse(fs.readFileSync(path.join(claudeDir, "package.json"), "utf8")); console.log("[*] Version:", pkg.version); } catch {} // Check sharp const hasSharp = isSharpInstalled(claudeDir); console.log("[*] sharp module:", hasSharp ? "installed" : "MISSING"); // Check patch status const code = readCliJs(claudeDir); const nv8Patched = isPatched(code); const mapperPatched = isMapperPatched(code); console.log("[*] Nv8 safety patch:", nv8Patched ? "applied" : "not applied"); console.log("[*] Image mapper patch:", mapperPatched ? "applied" : "not applied"); if (checkOnly) { const fullyProtected = hasSharp && nv8Patched && mapperPatched; const status = fullyProtected ? "FULLY PROTECTED" : (mapperPatched ? "PROTECTED (mapper fix applied)" : "VULNERABLE"); console.log("\nStatus:", status); process.exit(fullyProtected ? 0 : 1); } if (revert) { revertPatch(claudeDir); process.exit(0); } console.log(""); // Step 1: Install sharp if (!hasSharp) { if (!installSharp(claudeDir)) { console.error("\n[-] Could not install sharp. Applying safety patches anyway..."); } } else { console.log("[=] sharp already installed, skipping"); } // Step 2: Apply Nv8 safety patch let patchedCode = applyNv8Patch(readCliJs(claudeDir)); // Step 3: Apply image mapper patch (ROOT CAUSE FIX) patchedCode = applyMapperPatch(patchedCode); writeCliJs(claudeDir, patchedCode); console.log("\n=== Done! Claude Code is now protected against image read crashes ==="); console.log("Patches applied:"); console.log(" 1. Nv8 try/catch — prevents binary leak on image read failure"); console.log(" 2. Image mapper — guarantees media_type is always present (ROOT FIX)"); console.log("\nNote: After updating Claude Code (npm update -g @anthropic-ai/claude-code),"); console.log(" re-run this patcher to reapply the fixes."); } main();