Skip to content

feat(tools): OS-level sandbox for agent-spawned processes (macOS) - #386

Merged
github-actions[bot] merged 2 commits into
mainfrom
feat/sandbox-macos-seatbelt
Aug 17, 2026
Merged

github-actions[bot] merged 2 commits into
mainfrom
feat/sandbox-macos-seatbelt

Conversation

@mohitranka

Copy link
Copy Markdown
Contributor

First half of the permission-model overhaul (docs/permission-model-overhaul.md): build the enforcement floor. Nothing is deleted yet — per the plan's mandatory ordering, the sandbox must be enforced by default before readonly.rs goes, never the reverse.

What it does

Confines agent-spawned shells with Seatbelt on macOS. Scope matches Codex's workspace-write:

writes workspace root only (plus an opt-in session scratch dir)
.git / .forge read-only inside the writable root
network denied
reads broad — toolchains need ~/.gitconfig, ~/.cargo

Reads being broad is deliberate, and pinned by a test so it stays visible: a secret can be read, and the network denial is what stops it leaving.

.git/.forge are read-only for two reasons, not one. Git is the recovery mechanism this model leans on for in-workspace damage — and .forge/permissions.toml would otherwise let a confined process widen its own permissions on the next load using nothing but an ordinary in-workspace write, which this model permits by design.

Two defects the enforcement tests caught

Both were invisible in the profile text and only showed up when spawning real processes.

Paths were not canonicalised. The kernel resolves symlinks before matching, and /var → /private/var, so (deny file-write* (subpath "/var/…/.git")) silently never fired. The sandbox still started and still blocked other things — only the .git rule was dead. Now every rule is built from the canonical root, and an unresolvable workspace yields no profile at all.

The temp allowance was (subpath "/private/var/folders") — the whole shared per-user tree, making "writes outside the workspace are denied" false across a large part of the filesystem. Replaced with an opt-in, session-scoped scratch directory.

builtins.rs is not the single choke point

sandboxing-followup.md says it is. It isn't — unified_exec spawns too, and its sessions outlive the turn while write_stdin feeds them. write_stdin is in neither is_shell_tool() nor default_shell_hitl_tools(), so today, once a session is approved, the agent writes arbitrary commands into it with no further approval. Command classification can never fix that: approval happens at session start, dangerous content arrives later. Confining at spawn closes it structurally.

Tests

  • 10 unit over the profile: canonicalisation, SBPL rule ordering (last-match-wins, so .git becomes writable if the deny is emitted before the allow), quote/backslash escaping, fail-closed on an unresolvable root.
  • 13 enforcement spawning real confined processes: escape denied, cd .. denied, .git and .forge read-only, network denied, broad reads permitted, in-workspace destruction permitted by design, and the real bash tool confined.

The enforcement tests are the planned replacement for the negative coverage that dies with readonly.rs. ls | tee out, cat f | sh, cd .. && write are no longer recognised — they are contained. That is the whole point: the guarantee stops depending on parsing a shell command.

Not in this PR

  • Linux (bubblewrap) and the mode-inference rule that a sandbox-less host falls back to Manual. When the host cannot confine, this PR still runs the command; that rule belongs upstream where the mode is decided.
  • Network allowlist, exec policy, readonly.rs deletion.

Pre-existing local failure

forge-tui's approving_a_hitl_gated_tool_resumes_the_turn_instead_of_stalling_forever fails on my machine. It also fails at 7fdb7a0 — before any of this work — both standalone and in-suite, and it passes in CI. Bisected, not caused here, not fixed here. Worth its own issue: it's environment-dependent and I could not root-cause it (ruled out user permissions.toml, ~/.config/forge, and FORGE_* env vars).

Otherwise cargo fmt --all -- --check, cargo clippy --all-targets and cargo test --workspace are clean — that one test is the only failure in the workspace.

…belt)

First half of the permission-model overhaul: build the enforcement floor.
Nothing is wired into the spawn sites yet and no behaviour changes — this
commit adds the mechanism and the coverage that the decision layer will later
be allowed to rely on.

Scope matches Codex's workspace-write: writes confined to the workspace,
network denied, reads broad (toolchains need ~/.gitconfig and ~/.cargo).
Reading a secret stays possible; the network denial is what stops it leaving.

.git and .forge are read-only inside the writable root. Two reasons, not one:
git is the recovery mechanism this model leans on for in-workspace damage, and
.forge/permissions.toml would otherwise let a confined process widen its own
permissions on the next load using an ordinary in-workspace write.

Two defects found by writing the enforcement tests rather than trusting the
profile text:

* Paths were not canonicalised. The kernel resolves symlinks before matching,
  and /var is a symlink to /private/var, so a deny rule written against an
  unresolved path silently never fired. The sandbox still started and still
  blocked other things — only the .git rule was dead. Now every rule is built
  from the canonical root, and a workspace that cannot be resolved yields no
  profile at all.
* The temp allowance was (subpath "/private/var/folders"), which grants the
  whole shared per-user tree and made "writes outside the workspace are denied"
  false for a large part of the filesystem. Replaced with an opt-in,
  session-scoped scratch directory.

Tests: 10 unit tests over the profile (canonicalisation, SBPL rule ordering
where last-match-wins makes .git writable if reversed, quote escaping,
fail-closed on an unresolvable root) and 10 enforcement tests that spawn real
confined processes and assert the kernel refuses. The latter are the
replacement for the negative coverage that dies with readonly.rs: `ls | tee`,
`cat f | sh` and `cd .. && write` are no longer recognised, they are contained.
Wires the sandbox into both agent shell-spawn paths. This is where behaviour
changes: an agent command that escapes the workspace now fails at the kernel
rather than succeeding.

`sandboxing-followup.md` describes builtins.rs as "the single execution choke
point". It is not — `unified_exec` spawns too, and its sessions outlive the
turn that started them while `write_stdin` feeds them afterwards. `write_stdin`
is in neither `is_shell_tool()` nor `default_shell_hitl_tools()`, so once a
session exists the agent writes arbitrary commands into it with no further
approval. Confining at spawn closes that structurally: a confined session stays
confined regardless of what is later written to it. No amount of command
classification could have fixed it, because the approval happens at session
start and the dangerous content arrives later.

When a host cannot confine, the command still runs here. The rule that a
sandbox-less host falls back to asking the user belongs upstream where the
permission mode is decided, and lands with mode inference; this commit does not
claim it.

Tests: three more enforcement tests driving the real `bash` tool — escaping the
workspace fails, `.git` stays read-only, and ordinary in-workspace work is
unaffected.

Note for reviewers: `forge-tui`'s
`approving_a_hitl_gated_tool_resumes_the_turn_instead_of_stalling_forever`
fails on this machine. It also fails at 7fdb7a0, before any of this work, both
standalone and in-suite, and it passes in CI. Pre-existing and unrelated; not
caused by the sandbox and not fixed here.
@github-actions
github-actions Bot enabled auto-merge (squash) August 17, 2026 13:21
@github-actions
github-actions Bot merged commit eb4742c into main Aug 17, 2026
7 checks passed
@mohitranka
mohitranka deleted the feat/sandbox-macos-seatbelt branch August 17, 2026 13:24
mohitranka added a commit that referenced this pull request Aug 21, 2026
* feat(tools): OS-level sandbox for agent-spawned processes (macOS Seatbelt)

First half of the permission-model overhaul: build the enforcement floor.
Nothing is wired into the spawn sites yet and no behaviour changes — this
commit adds the mechanism and the coverage that the decision layer will later
be allowed to rely on.

Scope matches Codex's workspace-write: writes confined to the workspace,
network denied, reads broad (toolchains need ~/.gitconfig and ~/.cargo).
Reading a secret stays possible; the network denial is what stops it leaving.

.git and .forge are read-only inside the writable root. Two reasons, not one:
git is the recovery mechanism this model leans on for in-workspace damage, and
.forge/permissions.toml would otherwise let a confined process widen its own
permissions on the next load using an ordinary in-workspace write.

Two defects found by writing the enforcement tests rather than trusting the
profile text:

* Paths were not canonicalised. The kernel resolves symlinks before matching,
  and /var is a symlink to /private/var, so a deny rule written against an
  unresolved path silently never fired. The sandbox still started and still
  blocked other things — only the .git rule was dead. Now every rule is built
  from the canonical root, and a workspace that cannot be resolved yields no
  profile at all.
* The temp allowance was (subpath "/private/var/folders"), which grants the
  whole shared per-user tree and made "writes outside the workspace are denied"
  false for a large part of the filesystem. Replaced with an opt-in,
  session-scoped scratch directory.

Tests: 10 unit tests over the profile (canonicalisation, SBPL rule ordering
where last-match-wins makes .git writable if reversed, quote escaping,
fail-closed on an unresolvable root) and 10 enforcement tests that spawn real
confined processes and assert the kernel refuses. The latter are the
replacement for the negative coverage that dies with readonly.rs: `ls | tee`,
`cat f | sh` and `cd .. && write` are no longer recognised, they are contained.

* feat(tools): confine bash and exec_command at spawn

Wires the sandbox into both agent shell-spawn paths. This is where behaviour
changes: an agent command that escapes the workspace now fails at the kernel
rather than succeeding.

`sandboxing-followup.md` describes builtins.rs as "the single execution choke
point". It is not — `unified_exec` spawns too, and its sessions outlive the
turn that started them while `write_stdin` feeds them afterwards. `write_stdin`
is in neither `is_shell_tool()` nor `default_shell_hitl_tools()`, so once a
session exists the agent writes arbitrary commands into it with no further
approval. Confining at spawn closes that structurally: a confined session stays
confined regardless of what is later written to it. No amount of command
classification could have fixed it, because the approval happens at session
start and the dangerous content arrives later.

When a host cannot confine, the command still runs here. The rule that a
sandbox-less host falls back to asking the user belongs upstream where the
permission mode is decided, and lands with mode inference; this commit does not
claim it.

Tests: three more enforcement tests driving the real `bash` tool — escaping the
workspace fails, `.git` stays read-only, and ordinary in-workspace work is
unaffected.

Note for reviewers: `forge-tui`'s
`approving_a_hitl_gated_tool_resumes_the_turn_instead_of_stalling_forever`
fails on this machine. It also fails at 7fdb7a0, before any of this work, both
standalone and in-suite, and it passes in CI. Pre-existing and unrelated; not
caused by the sandbox and not fixed here.
mohitranka added a commit that referenced this pull request Sep 11, 2026
* feat(tools): OS-level sandbox for agent-spawned processes (macOS Seatbelt)

First half of the permission-model overhaul: build the enforcement floor.
Nothing is wired into the spawn sites yet and no behaviour changes — this
commit adds the mechanism and the coverage that the decision layer will later
be allowed to rely on.

Scope matches Codex's workspace-write: writes confined to the workspace,
network denied, reads broad (toolchains need ~/.gitconfig and ~/.cargo).
Reading a secret stays possible; the network denial is what stops it leaving.

.git and .forge are read-only inside the writable root. Two reasons, not one:
git is the recovery mechanism this model leans on for in-workspace damage, and
.forge/permissions.toml would otherwise let a confined process widen its own
permissions on the next load using an ordinary in-workspace write.

Two defects found by writing the enforcement tests rather than trusting the
profile text:

* Paths were not canonicalised. The kernel resolves symlinks before matching,
  and /var is a symlink to /private/var, so a deny rule written against an
  unresolved path silently never fired. The sandbox still started and still
  blocked other things — only the .git rule was dead. Now every rule is built
  from the canonical root, and a workspace that cannot be resolved yields no
  profile at all.
* The temp allowance was (subpath "/private/var/folders"), which grants the
  whole shared per-user tree and made "writes outside the workspace are denied"
  false for a large part of the filesystem. Replaced with an opt-in,
  session-scoped scratch directory.

Tests: 10 unit tests over the profile (canonicalisation, SBPL rule ordering
where last-match-wins makes .git writable if reversed, quote escaping,
fail-closed on an unresolvable root) and 10 enforcement tests that spawn real
confined processes and assert the kernel refuses. The latter are the
replacement for the negative coverage that dies with readonly.rs: `ls | tee`,
`cat f | sh` and `cd .. && write` are no longer recognised, they are contained.

* feat(tools): confine bash and exec_command at spawn

Wires the sandbox into both agent shell-spawn paths. This is where behaviour
changes: an agent command that escapes the workspace now fails at the kernel
rather than succeeding.

`sandboxing-followup.md` describes builtins.rs as "the single execution choke
point". It is not — `unified_exec` spawns too, and its sessions outlive the
turn that started them while `write_stdin` feeds them afterwards. `write_stdin`
is in neither `is_shell_tool()` nor `default_shell_hitl_tools()`, so once a
session exists the agent writes arbitrary commands into it with no further
approval. Confining at spawn closes that structurally: a confined session stays
confined regardless of what is later written to it. No amount of command
classification could have fixed it, because the approval happens at session
start and the dangerous content arrives later.

When a host cannot confine, the command still runs here. The rule that a
sandbox-less host falls back to asking the user belongs upstream where the
permission mode is decided, and lands with mode inference; this commit does not
claim it.

Tests: three more enforcement tests driving the real `bash` tool — escaping the
workspace fails, `.git` stays read-only, and ordinary in-workspace work is
unaffected.

Note for reviewers: `forge-tui`'s
`approving_a_hitl_gated_tool_resumes_the_turn_instead_of_stalling_forever`
fails on this machine. It also fails at 830d372, before any of this work, both
standalone and in-suite, and it passes in CI. Pre-existing and unrelated; not
caused by the sandbox and not fixed here.
mohitranka added a commit that referenced this pull request Sep 11, 2026
* feat(tools): OS-level sandbox for agent-spawned processes (macOS Seatbelt)

First half of the permission-model overhaul: build the enforcement floor.
Nothing is wired into the spawn sites yet and no behaviour changes — this
commit adds the mechanism and the coverage that the decision layer will later
be allowed to rely on.

Scope matches Codex's workspace-write: writes confined to the workspace,
network denied, reads broad (toolchains need ~/.gitconfig and ~/.cargo).
Reading a secret stays possible; the network denial is what stops it leaving.

.git and .forge are read-only inside the writable root. Two reasons, not one:
git is the recovery mechanism this model leans on for in-workspace damage, and
.forge/permissions.toml would otherwise let a confined process widen its own
permissions on the next load using an ordinary in-workspace write.

Two defects found by writing the enforcement tests rather than trusting the
profile text:

* Paths were not canonicalised. The kernel resolves symlinks before matching,
  and /var is a symlink to /private/var, so a deny rule written against an
  unresolved path silently never fired. The sandbox still started and still
  blocked other things — only the .git rule was dead. Now every rule is built
  from the canonical root, and a workspace that cannot be resolved yields no
  profile at all.
* The temp allowance was (subpath "/private/var/folders"), which grants the
  whole shared per-user tree and made "writes outside the workspace are denied"
  false for a large part of the filesystem. Replaced with an opt-in,
  session-scoped scratch directory.

Tests: 10 unit tests over the profile (canonicalisation, SBPL rule ordering
where last-match-wins makes .git writable if reversed, quote escaping,
fail-closed on an unresolvable root) and 10 enforcement tests that spawn real
confined processes and assert the kernel refuses. The latter are the
replacement for the negative coverage that dies with readonly.rs: `ls | tee`,
`cat f | sh` and `cd .. && write` are no longer recognised, they are contained.

* feat(tools): confine bash and exec_command at spawn

Wires the sandbox into both agent shell-spawn paths. This is where behaviour
changes: an agent command that escapes the workspace now fails at the kernel
rather than succeeding.

`sandboxing-followup.md` describes builtins.rs as "the single execution choke
point". It is not — `unified_exec` spawns too, and its sessions outlive the
turn that started them while `write_stdin` feeds them afterwards. `write_stdin`
is in neither `is_shell_tool()` nor `default_shell_hitl_tools()`, so once a
session exists the agent writes arbitrary commands into it with no further
approval. Confining at spawn closes that structurally: a confined session stays
confined regardless of what is later written to it. No amount of command
classification could have fixed it, because the approval happens at session
start and the dangerous content arrives later.

When a host cannot confine, the command still runs here. The rule that a
sandbox-less host falls back to asking the user belongs upstream where the
permission mode is decided, and lands with mode inference; this commit does not
claim it.

Tests: three more enforcement tests driving the real `bash` tool — escaping the
workspace fails, `.git` stays read-only, and ordinary in-workspace work is
unaffected.

Note for reviewers: `forge-tui`'s
`approving_a_hitl_gated_tool_resumes_the_turn_instead_of_stalling_forever`
fails on this machine. It also fails at bec9575, before any of this work, both
standalone and in-suite, and it passes in CI. Pre-existing and unrelated; not
caused by the sandbox and not fixed here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant