Repository navigation
feat(sandbox): run third-party stdio MCP servers in bubblewrap, opt-in - #734
Merged
Merged
Conversation
🔍 Cora AI Code Review✅ No issues found. Code looks good! Review powered by cora-code · BYOK · MIT |
…servers `tole_core::sandbox` (Linux bubblewrap, #601, docs/sandbox-design.md and the owner decisions of 2026-11-14): profiles (`server`, `strict`; network none or host), the pure `bwrap_args` builder, `check_grant`, backend detection (`bwrap --version` plus a smoke run), and `plan`, which decides one launch: sandboxed, direct, or refused. Every launch unshares all namespaces including the user one, disables nested user namespaces and drops all capabilities; the only `--setenv` values are HOME and TMPDIR; the caller spawns bwrap with the scrubbed environment. Grants are resolved and refused when they are or hold `/`, `/proc`, `/sys`, `/dev`, $HOME or the tole data dir, or sit inside the data dir. The owner policy is set once per process; the workspace is an argument of each launch. Not wired yet: nothing calls it, so behaviour is unchanged. The wiring follows in its own PR. Refs #601
Wires `tole_core::sandbox` into the stdio MCP launch: a `[sandbox]` table in the user config (off by default, never from a project config) with mode, profile, network, read, write and skip. A wrapped server runs as `bwrap ... -- <server>` with bwrap itself started with the scrubbed environment; `required` refuses to start without a working bubblewrap and `optional` refuses too in `tole serve` and cron runs. The session's workspace travels with the server config, and the reactor keys a wrapped server by name and workspace so two sessions of one process never share a server or each other's workspace bind. `tole doctor` shows whether bubblewrap is usable and the configured mode; macOS and Windows say they have no sandbox. Tests: a real-bwrap integration test (private view, scrubbed pid-1 environment, per-workspace servers that never mix), the unsandboxed path unchanged, and config tests. Without a working bwrap the integration tests print a SKIPPED line with the reason; TOLE_REQUIRE_BWRAP=1 turns that into a failure. Refs #601
ajianaz
force-pushed
the
feat/sandbox-pr1
branch
from
November 14, 2026 11:11
e8f25d7 to
cfb33e1
Compare
`check_grant` validated the resolved path but the plan then handed the original spelling to bwrap, which resolves a source symlink at exec time: a link swapped after the check could mount `/` or `$HOME`, writable for a workspace or `write` grant. `check_grant` now returns the resolved path and the argument builder only ever receives that one (workspace, read, write and the server's install dir; the program is addressed through the resolved dir). The plan is rebuilt for every launch, right before the spawn, so a swapped link is re-resolved or refused. The remaining same-user race is documented in the design note. Refs #601
…check The synthetic canary secrets no longer start with a `ghp_` prefix that makes secret scanners fire on the test files; the scrub is still proven by the variable name. The connection key test also checks that a link and its target are one workspace, like the bind. The threat-model row records the remaining same-user race on a bind. Refs #601
# Conflicts: # docs/epics.md # docs/sandbox-design.md
| } | ||
| }; | ||
| conns.insert(cfg.name.clone(), McpConnection { service }); | ||
| conns.insert(cfg.conn_key(), McpConnection { service }); |
…e stop and a cap With connections keyed by name and workspace, nothing stopped a server when its session ended, so a long-running serve would pile up one bwrap'd server per workspace. Each session's tool registry now holds a claim on the sandboxed servers it registered; the reactor stops a server (process killed, config dropped) when the last claim is released, so dropping one session never stops a server another session still uses. A wrapped server unused for `[sandbox] idle_secs` (default 900) is stopped and restarts on its next call, and `[sandbox] max_servers` (default 16) caps how many run at once: a launch past it is refused with a message naming the limit, never by evicting another session's server. Servers that are not wrapped keep the old behaviour. Tests with real bwrap check the server process is gone after its last session ends, a shared key survives one release, the idle reaper stops and the next call restarts, and the cap refuses the next launch and frees the slot on release; each part is mutation-checked. Refs #601
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
PR 1b of the first slice of the sandbox (#601,
docs/sandbox-design.md, owner decisions of 2026-11-14): the wiring oftole_core::sandbox(merged in #743) into the stdio MCP launch, the[sandbox]config and atole doctorline. Off by default; nothing changes for existing setups.Why
Scrubbing a child's environment is not a durable boundary (#594 audit): a same-user MCP server can read the owner's files, write owner-only records (#684) and reach any host. The owner decided: first PR is opt-in, an
optionalsandbox counts asrequiredin unattended runs, bubblewrap only, no macOS sandbox, no host-allowlist proxy yet.Changes
mcp.rs:start_serveraskssandbox::launch;Directis the old code path,Sandboxedspawnsbwrap(the ENV-1 scrub applies to the bwrap process itself),Refuseddoes not start the server.McpServerConfig::workspace(set by the roster), and the reactor keys a wrapped stdio server byname@<resolved workspace>(tools,/mcp:prompts andmcp_readcarry the key). Two sessions of oneserveprocess that register the same server name get two server processes, each with its own bind, and a workspace that never registered a server reaches none.ToolRegistry::hold), and the reactor stops the server (process killed, config dropped) when the last claim is released, so one session ending never stops a server another session with the same key uses. A wrapped server unused for[sandbox] idle_secs(default 900, 1 to 86400) is stopped and restarts on its session's next call.[sandbox] max_servers(default 16, 1 to 64) caps wrapped servers per process: a launch past it is refused with a message naming the limit, never by evicting another session's server, and a released slot is reusable.[sandbox]config (mode,profile,network,read,write,skip,max_servers,idle_secs): user layer only, validated, shown inconfig check(a project's lines are marked IGNORED).tole serveand cron runs mark the process unattended, sooptionalwithout a working bwrap refuses there instead of running unsandboxed.tole doctor: whether bubblewrap is present and usable, and the configured mode. macOS and Windows say they have no sandbox.cfg(unix); the Linux-only code lives in the merged core module.Not in this slice (later phases): model-driven commands (
run_command,job_start, cron scripts, verify, stop gates), lazy start, lock-entry profile binding, the allowlist proxy.Testing
cargo fmt,cargo clippy --workspace --all-targets --all-features -D warnings,cargo test --workspace,tole-coreandtole-cliwith--no-default-features.mcp_sandbox_off: with no policy the server sees the host network, host files and the real$HOME, and the reactor key is the plain name.mcp_sandbox: sandboxed it sees no network, no data dir,$HOME=/scratch, a read-only workspace,CapEff0, and no canary secret in its environment or in/proc/1/environ(bwrap's own). Without a working bwrap these print aSKIPPED:line with the reason;TOLE_REQUIRE_BWRAP=1turns the skip into a failure.mcp_readand tool calls reach only the server of their own workspace; a third workspace gets "never registered"; a link and its target are one workspace; concurrent launches from 8 threads each bind their own workspace. Mutation checks kill keying by name only, plain-name tools, prompts or reads, and a global "last workspace".mcp_sandbox_lifecycle,mcp_sandbox_idle, host-side/proccheck that the server process is alive or gone): the process is gone after its last session ends; two sessions with the same key keep it until the last one ends; the idle reaper stops an unused server and the next call restarts it; the cap refuses the next launch without evicting anyone and a released slot is reused. Mutation checks kill: release doing nothing, release ignoring other sessions, the cap ignored, the reaper off, the claim not held by the registry, and the config kept after the last release.set_policyis refused. Config tests: default off, strict keys and values and the lifecycle bounds, user layer only, a project cannot set it.ring); CI covers them.Refs #601