Skip to content

fix(#197): add project root + worktrees to sandbox allowRead/Write - #198

Merged
potiuk merged 1 commit into
apache:mainfrom
potiuk:fix-197-sandbox-project-roots
May 17, 2026
Merged

potiuk merged 1 commit into
apache:mainfrom
potiuk:fix-197-sandbox-project-roots

Conversation

@potiuk

@potiuk potiuk commented May 17, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #197 — the harness pre-resolves sandbox.filesystem.allowRead: ["."] at session start and silently drops the literal, so reads under CWD fall through to denyRead: ["~/"] and fail under the sandbox even though writes work. Defensive fix: add the project root as an explicit absolute path to both allowRead and allowWrite in the adopter's project-local <repo>/.claude/settings.local.json (gitignored, per-machine). Worktrees inherit access automatically — each worktree carries its own settings.local.json entry, populated for pre-existing worktrees at adopt/upgrade time and for newly-added worktrees via the post-checkout git hook.

Why project-local, and is it safe?

Scope File Safe to write the fix to?
User ~/.claude/settings.json Sandbox-protected, but every Claude Code session on the host carries every adopter project's path — broader exposure than needed.
Project (committed) <repo>/.claude/settings.json Machine-specific abs paths would leak into the repo.
Project (local, gitignored) <repo>/.claude/settings.local.json Selected. Per-machine, per-project, never committed. Sandbox-write-protected by Claude Code's built-in denyWithinAllow (verified empirically below).

Verified empirically in a sandboxed session:

echo "test" > .claude/settings.local.json.sandbox-test   → succeeds (.claude/ is generally writable)
echo "test" >> .claude/settings.local.json               → operation not permitted (this file is built-in-protected)

Claude Code's runtime adds .claude/settings.{json,local.json} and .claude/skills/ to the sandbox's denyWithinAllow set at the bubblewrap/Seatbelt syscall level — not user-configurable, owned by the harness. A sandboxed Bash invocation cannot mutate the file the fix writes to.

The harness's protection is for Bash subprocesses; the agent's Edit/Write/MultiEdit tools bypass it (they're agent-direct). The PR closes that bypass via per-tool permissions.deny rules in the committed .claude/settings.json. Symmetric protection on .claude/settings.json itself is included — maintainers occasionally edit it, but per-call permissions.ask keeps every write operator-approved.

The same protection blocks the framework's own helper when invoked from inside a sandboxed agent session. /setup-steward adopt, upgrade, and worktree-init therefore invoke the helper with dangerouslyDisableSandbox: true after proposing the bypass to the operator; sandbox-bypass-warn.sh fires its bold-red banner as a backstop. The post-checkout hook fired from a user-terminal git checkout / git worktree add writes without bypass because the user shell is not sandboxed. All three write paths are auditable.

Full threat-model walk in docs/setup/secure-agent-setup.md → Security rationale — why project-local is safe to write to.

What landed

  • New helper tools/agent-isolation/sandbox-add-project-root.sh (executable, Apache 2.0). With --all-worktrees, enumerates git worktree list --porcelain and writes each worktree's path into that worktree's own .claude/settings.local.json. Without the flag (post-checkout hook mode), writes only the current worktree's path. Refuses to write when the target file is not gitignored (git check-ignore guard — defense in depth against a stale .gitignore). Creates the file if missing; updates atomically (jq → tmp → mv); idempotent.

  • setup-isolated-setup-install — new Step P installs the script under ~/.claude/scripts/ and runs it once with --all-worktrees against the adopter repo. Documents the sandbox-bypass requirement for agent-session invocations.

  • setup-isolated-setup-verify — new Check 8: live sandboxed read+write probe of the project root, plus a static cross-check that the abs path is in the current worktree's .claude/settings.local.json.

  • /setup-steward adopt — Step 7 gitignore template gains /.claude/settings.local.json. Step 10 post-checkout hook content extends to chain into the helper. Step 12 grows pass 3 that runs the helper with --all-worktrees after the worktree-init propagation, invoked with dangerouslyDisableSandbox: true and visible bypass proposal.

  • /setup-steward upgrade Step 6c — after the per-worktree worktree-init loop, helper runs with --all-worktrees under sandbox bypass.

  • /setup-steward worktree-init new Step 1c — invokes the helper for this worktree's root under sandbox bypass.

  • /setup-steward verify — Check 4 now requires /.claude/settings.local.json in the adopter's .gitignore as ✗. New Check 8b: static cross-check that the current worktree's abs path is in its own .claude/settings.local.json.

  • .claude/settings.json (framework dogfood + adopter template) — permissions.deny gains Edit/Write/MultiEdit(.claude/settings.{json,local.json}) to close the agent-Edit-tool bypass. tools/sandbox-lint/expected.json baseline extended to match.

  • docs/setup/secure-agent-setup.md — new section Project-root coverage in the sandbox allowlists with: harness-behaviour explanation, scope-choice table, empirically-grounded security rationale, helper surface, four invocation points.

  • tools/agent-isolation/README.md — table row for the new helper.

Test plan

  • On a fresh adopter clone with the secure setup already installed, run /setup-steward and confirm the project root lands in <repo>/.claude/settings.local.json's sandbox.filesystem.allowRead/allowWrite.
  • On an adopted repo, git worktree add ../foo feature-x from a terminal; confirm the post-checkout hook fires in the new worktree and the path lands in <new-worktree>/.claude/settings.local.json (not in the main's).
  • In a sandboxed agent session, run /setup-steward worktree-init and confirm the sandbox-bypass proposal fires the sandbox-bypass-warn.sh banner before the write.
  • Run setup-isolated-setup-verify on a stale session whose project root is not in settings.local.json; confirm Check 8's live probe surfaces ✗.
  • Confirm the harness's built-in protection holds: echo "x" >> .claude/settings.local.json from inside a sandboxed Bash returns operation not permitted.
  • Confirm a compromised agent cannot Edit-tool-write the file: an Edit('.claude/settings.local.json', ...) call hits the permissions.deny rule.
  • Run the helper with --dry-run --all-worktrees and verify the per-worktree diff matches expectations without writing.
  • Run the helper from a directory with no .claude/ yet — confirm it creates .claude/settings.local.json from scratch with only the sandbox.filesystem block.

🤖 Generated with Claude Code

The harness pre-resolves `sandbox.filesystem.allowRead: ["."]` at
session start and silently drops the literal, so reads under CWD
fall through to denyRead: ["~/"] and fail under the sandbox even
though writes work. Defensive fix: add the project root as an
explicit absolute path to both allowRead and allowWrite in the
adopter's project-local <repo>/.claude/settings.local.json
(gitignored, per-machine). Worktrees inherit access automatically
— each worktree carries its own settings.local.json entry,
populated for pre-existing worktrees at adopt/upgrade time and for
newly-added worktrees via the post-checkout git hook.

Why project-local, and is it safe? User-scope ~/.claude/settings.json
is sandbox-write-protected but pollutes every Claude Code session
on the host with every adopter project's path. Committed
project-scope leaks machine-specific paths into the repo. Project-
local settings.local.json is per-machine, per-project, never
committed, and sandbox-write-protected — verified empirically:
`echo >> .claude/settings.local.json` from a sandboxed Bash returns
"operation not permitted" because Claude Code's runtime adds
.claude/settings.{json,local.json} and .claude/skills/ to the
sandbox's denyWithinAllow set at the bubblewrap/Seatbelt syscall
level (not user-configurable, owned by the harness).

The harness's protection is for Bash subprocesses; the agent's
Edit/Write/MultiEdit tools bypass it. This PR closes that bypass
via per-tool permissions.deny rules in the committed
.claude/settings.json on both settings files.

The same protection blocks the framework's own helper when invoked
from inside a sandboxed agent session. /setup-steward adopt,
upgrade, and worktree-init therefore invoke the helper with
dangerouslyDisableSandbox: true after proposing the bypass to the
operator; sandbox-bypass-warn.sh fires its bold-red banner as a
backstop. The post-checkout hook fired from a user-terminal git
checkout / git worktree add writes without bypass because the user
shell is not sandboxed. All three write paths are auditable.

What landed:

- tools/agent-isolation/sandbox-add-project-root.sh: new helper.
  With --all-worktrees, writes each worktree's path into that
  worktree's own .claude/settings.local.json. Refuses to write
  when the target file is not gitignored (git check-ignore guard).
  Creates the file if missing; atomic via jq → tmp → mv;
  idempotent.

- setup-isolated-setup-install Step P: installs the script under
  ~/.claude/scripts/ and runs it once with --all-worktrees.
  Documents the sandbox-bypass requirement for agent-session
  invocations.

- setup-isolated-setup-verify Check 8: live sandboxed read+write
  probe of the project root, plus a static cross-check that the
  abs path is in the current worktree's settings.local.json.

- /setup-steward adopt: Step 7 gitignore template gains
  /.claude/settings.local.json. Step 10 post-checkout hook
  extends to chain into the helper. Step 12 grows pass 3 that
  runs the helper with --all-worktrees under sandbox bypass.

- /setup-steward upgrade Step 6c: helper runs with --all-worktrees
  under sandbox bypass after the worktree-init loop.

- /setup-steward worktree-init Step 1c: helper runs for the
  current worktree under sandbox bypass.

- /setup-steward verify: Check 4 requires
  /.claude/settings.local.json in .gitignore. Check 8b: static
  cross-check that the current worktree's abs path is in its own
  settings.local.json.

- .claude/settings.json permissions.deny: gains
  Edit/Write/MultiEdit(.claude/settings.{json,local.json}) to
  close the agent-Edit-tool bypass. tools/sandbox-lint/expected.json
  baseline updated to match.

- docs/setup/secure-agent-setup.md: new "Project-root coverage in
  the sandbox allowlists" section with harness-behaviour
  explanation, scope-choice table, empirically-grounded security
  rationale, helper surface, four invocation points.

- tools/agent-isolation/README.md: table row for the new helper.

Closes apache#197.

Generated-by: Claude Code (Opus 4.7)
@potiuk
potiuk force-pushed the fix-197-sandbox-project-roots branch from e94bc19 to c9a3b6e Compare May 17, 2026 17:56
@potiuk
potiuk merged commit e75be92 into apache:main May 17, 2026
13 checks passed
@potiuk potiuk added the substrate:sandbox Tool substrate: agent isolation, egress control, settings audit label Sep 25, 2026
@potiuk potiuk added capability:platform Framework / agent substrate skills (install, verify, doctor, override, status, setup bootstrap) family:docs Docs, MISSION.md, READMEs family:setup setup-* skills family:tools tools/* labels Nov 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

capability:platform Framework / agent substrate skills (install, verify, doctor, override, status, setup bootstrap) family:docs Docs, MISSION.md, READMEs family:setup setup-* skills family:tools tools/* substrate:sandbox Tool substrate: agent isolation, egress control, settings audit

Projects

None yet

Development

Successfully merging this pull request may close these issues.

setup-isolated-setup-install: "." in allowRead does not cover CWD; need defensive abs-path resolution at install time

1 participant