{"record":{"id":"847c94030e377d81","repo":"JuliusBrussee/caveman","slug":"cave-sandbox-source-read-root-refused","errorCode":"cave_sandbox_source_read_root_refused","errorMessage":"cave_sandbox_source_read_root_refused","messagePattern":"cave_sandbox_source_read_root_refused","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/agent/src/runtime.ts","lineNumber":4893,"sourceCode":" * files to their common ancestor directory. A large project would\n * otherwise blow the OS argument limit (E2BIG) and the tool could not spawn at\n * all. The collapse is safe here: `sourceFiles` are paths inside the per-run\n * STAGED COPY, which already contains only the reachable source graph — never\n * the real project root with its .env and credentials.\n */\nconst SANDBOX_FS_READ_FLAG_THRESHOLD = 1024;\n\nfunction commonAncestorDir(paths: readonly string[]): string {\n  const dirs = paths.map((path) => resolve(dirname(path)));\n  const first = dirs[0];\n  if (first === undefined) {\n    throw new Error(\"cave_sandbox_source_staging_root_required\");\n  }\n  let ancestor = first;\n  while (dirs.some((path) => escapesRoot(relative(ancestor, path)))) {\n    const parent = dirname(ancestor);\n    if (parent === ancestor) {\n      throw new Error(\"cave_sandbox_source_read_root_refused\");\n    }\n    ancestor = parent;\n  }\n  if (dirname(ancestor) === ancestor) {\n    throw new Error(\"cave_sandbox_source_read_root_refused\");\n  }\n  return ancestor;\n}\n\nexport function sandboxSourceReadFlags(\n  sourceFiles: readonly string[],\n  stagingRoot?: string,\n): string[] {\n  if (sourceFiles.length <= SANDBOX_FS_READ_FLAG_THRESHOLD) {\n    return sourceFiles.map((path) => `--allow-fs-read=${path}`);\n  }\n  if (stagingRoot === undefined) {\n    throw new Error(\"cave_sandbox_source_staging_root_required\");","sourceCodeStart":4875,"sourceCodeEnd":4911,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/packages/agent/src/runtime.ts#L4875-L4911","documentation":"When computing the common ancestor directory of the sandbox source files, the walk up the directory tree hit the filesystem root without finding an ancestor that contains all paths. The library refuses to grant `--allow-fs-read` on `/` (or a drive root) because that would give the sandboxed worker read access to the entire filesystem. It is a deliberate fail-closed security refusal.","triggerScenarios":"Calling `sandboxSourceReadFlags` with more than 1024 files whose common ancestor is the root directory — e.g. files spread across `/etc/foo.ts` and `/home/x/bar.ts`, or staged files placed directly under `/`. Also thrown when the while loop climbs to the root (`dirname(ancestor) === ancestor`) inside `commonAncestorDir`.","commonSituations":"A staging root created at `/` or `/tmp` on systems where resolved paths diverge across devices/mounts, source files spread across multiple top-level directories (e.g. different drive letters on Windows), or symlinks resolving outside the project so the shared ancestor collapses to `/`.","solutions":["Stage all source files under one dedicated subdirectory (e.g. `<project>/.cave-stage/`) so the common ancestor is a real project path, not `/`","Check for symlinks or path spellings that resolve outside the intended staging tree and normalize them","Reduce the source set (or split into separate workers) so it fits under the 1024-file per-path flag threshold, avoiding ancestor consolidation entirely"],"exampleFix":"// before\nsandboxSourceReadFlags([\"/etc/a.ts\", \"/home/u/b.ts\"], \"/stage\")\n// after\nsandboxSourceReadFlags([\"/stage/etc/a.ts\", \"/stage/home/u/b.ts\"], \"/stage/src\")","handlingStrategy":"validation","validationCode":"const resolved = files.map((f) => resolve(dirname(f)));\nif (resolved.some((d) => dirname(d) === d || d === '/')) throw new Error('source paths must live under a staging subdirectory, not the filesystem root');","typeGuard":null,"tryCatchPattern":"try { const flags = sandboxSourceReadFlags(files, stagingRoot); } catch (e) { if (String((e as Error).message).includes('cave_sandbox_source_read_root_refused')) { /* restage under one subdir */ } else throw e; }","preventionTips":["Stage every source file under one dedicated non-root directory","Resolve symlinks before collecting source paths","Keep source paths on one volume/drive so a common ancestor exists below the root"],"tags":["sandbox","security","filesystem"],"backgroundTag":"path-traversal-blocked","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}