{"record":{"id":"c59d142e5bf3a4c0","repo":"Hmbown/CodeWhale","slug":"unsafe-update-path","errorCode":null,"errorMessage":"Unsafe update path.","messagePattern":"Unsafe update path\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"crates/tui/plugins/computer-use/app/updates.mjs","lineNumber":64,"sourceCode":"  return releaseUpdate(JSON.parse((await responseBytes(response,1024*1024)).toString(\"utf8\")));\n}\n\n/** Inspect both ZIP headers before extraction: no links, traversal or bombs. */\nexport function validateReleaseZip(bytes) {\n  const minimum=Math.max(0,bytes.length-65557); let end=-1;\n  for(let i=bytes.length-22;i>=minimum;i--) if(bytes.readUInt32LE(i)===0x06054b50&&i+22+bytes.readUInt16LE(i+20)===bytes.length) { end=i; break; }\n  if(end<0||bytes.readUInt16LE(end+4)||bytes.readUInt16LE(end+6)) throw new Error(\"Invalid update archive.\");\n  const count=bytes.readUInt16LE(end+10); let position=bytes.readUInt32LE(end+16),total=0;\n  if(!count||count>2000||bytes.readUInt16LE(end+8)!==count||position+bytes.readUInt32LE(end+12)!==end) throw new Error(\"Invalid update archive index.\");\n  const seen=new Set();\n  for(let i=0;i<count;i++) {\n    if(position+46>end||bytes.readUInt32LE(position)!==0x02014b50) throw new Error(\"Invalid update entry.\");\n    const flags=bytes.readUInt16LE(position+8),method=bytes.readUInt16LE(position+10),length=bytes.readUInt16LE(position+28),extra=bytes.readUInt16LE(position+30),comment=bytes.readUInt16LE(position+32);\n    const name=bytes.subarray(position+46,position+46+length).toString(\"utf8\");\n    const kind=(bytes.readUInt32LE(position+38)>>>16)&0xf000,offset=bytes.readUInt32LE(position+42),compressed=bytes.readUInt32LE(position+20);\n    const size=bytes.readUInt32LE(position+24); total+=size;\n    if(flags&1||![0,8].includes(method)||![0,0x4000,0x8000].includes(kind)||total>512*1024*1024||position+46+length+extra+comment>end) throw new Error(\"Unsupported update entry.\");\n    if(!name.startsWith(`${APP_NAME}.app/`)||name.includes(\"\\\\\")||name.includes(\":\")||name.includes(\"\\0\")||name.split(\"/\").some(part=>part===\"..\"||part===\".\")||seen.has(name)) throw new Error(\"Unsafe update path.\");\n    seen.add(name);\n    if(offset+30>position||bytes.readUInt32LE(offset)!==0x04034b50) throw new Error(\"Invalid update file header.\");\n    const localLength=bytes.readUInt16LE(offset+26),localExtra=bytes.readUInt16LE(offset+28);\n    if(offset+30+localLength+localExtra+compressed>bytes.readUInt32LE(end+16)||bytes.subarray(offset+30,offset+30+localLength).toString(\"utf8\")!==name) throw new Error(\"Inconsistent update file header.\");\n    if(bytes.readUInt16LE(offset+8)!==method||bytes.readUInt16LE(offset+6)!==flags||(!(flags&8)&&(bytes.readUInt32LE(offset+18)!==compressed||bytes.readUInt32LE(offset+22)!==size))) throw new Error(\"Inconsistent update sizes or compression.\");\n    const start=offset+30+localLength+localExtra;\n    // Header sizes are untrusted. Bound actual expansion before ditto writes\n    // anything, including a compressed payload whose headers understate size.\n    const payload=bytes.subarray(start,start+compressed);\n    let expanded;\n    try { expanded=method===0?payload.length:inflateRawSync(payload,{maxOutputLength:Math.max(size,1)}).length; }\n    catch { throw new Error(\"Invalid or oversized compressed update entry.\"); }\n    if(expanded!==size) throw new Error(\"The update entry size did not match its contents.\");\n    position+=46+length+extra+comment;\n  }\n  if(position!==end) throw new Error(\"Invalid update archive length.\");\n  return count;\n}","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/Hmbown/CodeWhale/blob/73e0f67d83c59909b571efdfc88c4bc28c309cb1/crates/tui/plugins/computer-use/app/updates.mjs#L46-L82","documentation":"Every central-directory entry name must be safe to extract into the app bundle: it must live under the `<APP_NAME>.app/` prefix, contain no backslashes, colons or NUL bytes, have no `.` or `..` path segments, and not duplicate an already-seen name. This is path-traversal and aliasing defense: without it a zip entry like `../../LaunchAgents/evil` could write outside the bundle, or a duplicate name could overwrite an earlier file. The validator throws before any bytes are written to disk.","triggerScenarios":"A zip whose entries don't all start with `Codewhale-Computer-Use.app/` (e.g. zipping the folder contents instead of the folder), entries containing `..` segments, absolute-path or Windows-style separators in names, or two entries with the exact same name.","commonSituations":"Running `zip -r app.zip .` from inside the bundle directory so entries lack the `.app/` prefix; a build script embedding `./` segments; zips created on Windows with backslash separators; malicious zips in a path-traversal security test; duplicate entries produced by appending to an existing zip twice.","solutions":["Repackage so the archive root is exactly `Codewhale-Computer-Use.app/`: run `ditto -c -k --keepParent MyApp.app app.zip` from the directory containing the bundle.","List the archive (`unzip -l`) and remove entries with `..`, `.`, backslashes, colons, or duplicate names from the packaging step.","Do not append to an existing zip; build a fresh archive each release so names cannot repeat.","If entries intentionally start with a different name, rename the bundle to match APP_NAME — the prefix is not configurable."],"exampleFix":"// before\ncd MyApp.app && zip -r ../app.zip .  // entries lack 'MyApp.app/' prefix\n// after\nditto -c -k --keepParent MyApp.app app.zip","handlingStrategy":"validation","validationCode":"const names = execFileSync(\"unzip\", [\"-Z1\", zipPath]).toString().split(\"\\n\").filter(Boolean);\nconst ok = names.every(n => n.startsWith(`${APP_NAME}.app/`) && !/[\\\\:\\0]/.test(n) && !n.split(\"/\").some(p => p===\"..\"||p===\".\"));","typeGuard":null,"tryCatchPattern":"try { validateReleaseZip(bytes); } catch (e) { if (e.message === \"Unsafe update path.\") throw new Error(\"Zip entry escapes the bundle — repackage so the root is <APP_NAME>.app/\"); throw e; }","preventionTips":["Always zip the bundle folder itself, not its contents (use --keepParent)","List archive entries in CI and assert the `<APP_NAME>.app/` prefix","Build a fresh archive per release — never append entries","Treat any `..` or duplicate entry in a published artifact as a release blocker"],"tags":["zip","path-traversal","security","update-integrity"],"backgroundTag":"path-traversal-blocked","analyzedSha":"73e0f67d83c59909b571efdfc88c4bc28c309cb1","analyzedAt":"2026-09-22T01:30:00.501Z","contentChangedAt":"2026-09-22T01:30:00.501Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}