Skip to content

feat(sandbox): run third-party stdio MCP servers in bubblewrap, opt-in - #734

Merged
ajianaz merged 7 commits into
developfrom
feat/sandbox-pr1
Nov 14, 2026
Merged

ajianaz merged 7 commits into
developfrom
feat/sandbox-pr1

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What

PR 1b of the first slice of the sandbox (#601, docs/sandbox-design.md, owner decisions of 2026-11-14): the wiring of tole_core::sandbox (merged in #743) into the stdio MCP launch, the [sandbox] config and a tole doctor line. 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 optional sandbox counts as required in unattended runs, bubblewrap only, no macOS sandbox, no host-allowlist proxy yet.

Changes

  • mcp.rs: start_server asks sandbox::launch; Direct is the old code path, Sandboxed spawns bwrap (the ENV-1 scrub applies to the bwrap process itself), Refused does not start the server.
  • Per session, not per process: the session workspace is McpServerConfig::workspace (set by the roster), and the reactor keys a wrapped stdio server by name@<resolved workspace> (tools, /mcp: prompts and mcp_read carry the key). Two sessions of one serve process 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.
  • Life of a wrapped server: each session's tool registry holds a claim on the servers it registered (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.
  • Without a sandbox, for HTTP servers and for skipped servers the key stays the plain name and nothing in the lifecycle applies: behaviour is exactly as before.
  • [sandbox] config (mode, profile, network, read, write, skip, max_servers, idle_secs): user layer only, validated, shown in config check (a project's lines are marked IGNORED).
  • tole serve and cron runs mark the process unattended, so optional without 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.
  • Docs: threat-model row for excessive agency (the sandbox is now in scope; residuals listed, including the same-user bind race), design status and a section on the life of a sandboxed server, epics, CHANGELOG under Unreleased.
  • The integration tests are 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-core and tole-cli with --no-default-features.
  • Integration tests with real bubblewrap 0.9.0 (kernel 6.18, run as root). 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, CapEff 0, and no canary secret in its environment or in /proc/1/environ (bwrap's own). Without a working bwrap these print a SKIPPED: line with the reason; TOLE_REQUIRE_BWRAP=1 turns the skip into a failure.
  • Per-session isolation: one server name in two workspaces is two servers; prompts, mcp_read and 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".
  • Lifecycle (mcp_sandbox_lifecycle, mcp_sandbox_idle, host-side /proc check 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.
  • A differing second set_policy is refused. Config tests: default off, strict keys and values and the lifecycle bounds, user layer only, a project cannot set it.
  • Not run here: Windows and macOS builds (no cross C toolchain for ring); CI covers them.

Refs #601

Comment thread crates/tole-core/src/sandbox.rs Fixed
@github-actions

github-actions Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

Comment thread crates/tole-cli/src/tool_roster.rs Fixed
…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
ajianaz changed the base branch from develop to feat/sandbox-core November 14, 2026 11:11
@ajianaz ajianaz changed the title feat(sandbox): opt-in bubblewrap sandbox for third-party stdio MCP servers on Linux feat(sandbox): run third-party stdio MCP servers in bubblewrap, opt-in Nov 14, 2026
`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
@ajianaz
ajianaz changed the base branch from feat/sandbox-core to develop November 14, 2026 11:41
# Conflicts:
#	docs/epics.md
#	docs/sandbox-design.md
Comment thread crates/tole-core/src/mcp.rs Outdated
}
};
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
@ajianaz
ajianaz merged commit b128e61 into develop Nov 14, 2026
21 checks passed
@ajianaz
ajianaz deleted the feat/sandbox-pr1 branch November 20, 2026 08:37
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.

3 participants