Skip to content

feat(sandbox): core module for the bubblewrap sandbox of third-party servers - #743

Merged
ajianaz merged 2 commits into
developfrom
feat/sandbox-core
Nov 14, 2026
Merged

ajianaz merged 2 commits into
developfrom
feat/sandbox-core

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

What

PR 1a of the first slice of the sandbox (#601, docs/sandbox-design.md, owner decisions of 2026-11-14): the core module tole_core::sandbox. Nothing calls it yet, so behaviour is unchanged. The wiring (stdio MCP launch, [sandbox] config, tole doctor, integration tests) is PR 1b, stacked on this one (#734).

Why

Scrubbing a child's environment is not a durable boundary (#594 audit). The design picked bubblewrap with an argument list built by tole. Splitting keeps each review small enough to read in full: this PR is the pure, unit-tested part.

Changes

  • tole_core::sandbox (new): profile types (server: workspace read-only; strict: no workspace; network none or host), the pure bwrap_args builder, check_grant, backend detection (bwrap --version >= 0.8 plus a bwrap --ro-bind / / true smoke run, cached; Linux only, other platforms report no sandbox), and plan, which decides one launch (sandboxed, direct, or refused) from the owner policy: required refuses without a working bwrap; optional warns when attended and refuses when unattended (owner decision 2).
  • The builder always passes --unshare-all --unshare-user --disable-userns --cap-drop ALL --new-session --die-with-parent, a private /proc, a read-only system view, .git/hooks, .git/config and .tole read-only under a writable workspace, and --setenv only for HOME and TMPDIR (never a secret). The caller spawns bwrap with the scrubbed environment (PR 1b).
  • check_grant resolves the path (symlinks, .., trailing slash, relative) and refuses one that cannot be resolved, /, /proc, /sys, /dev, $HOME and every ancestor of it, the tole data dir, its inside and its ancestors; with $HOME unknown, an ancestor of /home or /root. It returns the resolved path, and ONLY that path reaches the builder (workspace, read, write, the server's install dir, the program through it), as both bind source and destination. The plan is rebuilt for every launch, right before the spawn, so a link swapped later is re-resolved or refused. The remaining same-user race between the last resolution and bubblewrap's mount is documented in the design note.
  • The owner policy is process-wide and set once (set_policy refuses a different second value); the workspace is an argument of each launch, never shared state. The unattended flag is process-wide and monotone.
  • paths::canon_lenient becomes pub(crate). Docs: design status, the grant-resolution note and the epics line.

Testing

  • cargo fmt, cargo clippy --workspace --all-targets --all-features -D warnings, cargo test -p tole-core (also --no-default-features).
  • One unit test per hardening flag; mutation check: deleting each of 17 pieces from bwrap_args makes a test fail (all killed).
  • Grant checks: one test per rule on a real temp tree (ancestor of $HOME with the data dir elsewhere, ancestor of a data dir outside $HOME, inside the data dir, symlink to $HOME, .., trailing slash, unresolvable path, /, /proc, /sys, /dev, no $HOME); mutation checks kill every rule except the explicit / test, which $HOME's ancestor rule also covers.
  • Resolved binds: a symlinked workspace, read, write and install dir are bound as their resolved targets and the argv never holds the link; a property-style test over links, .. and trailing slashes checks every path in the argv under the tree equals the validated one; a link swapped between launches is followed to its new target or refused when it now points at $HOME. Mutants that pass the original workspace, the original read/write paths or the original install dir are each killed.
  • Policy tests: off is the direct path; required without bwrap refuses; optional warns when attended and refuses when unattended; a skipped server is never wrapped; a workspace holding the data dir refuses; sessions planning concurrently each get their own workspace bind and a later launch never changes an earlier argv.
  • Tests that need unix paths are cfg(unix); the Linux-only probe is cfg(target_os = "linux"). Not run here: Windows and macOS builds (no cross C toolchain for ring); CI covers them.

Refs #601

…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
Comment thread crates/tole-core/src/sandbox.rs Outdated
let ws = env.workspace.as_deref();
if p.profile.workspace != Workspace::None {
let ws = ws.ok_or("no workspace to bind")?;
check_grant(ws, home, data_dir)?;
@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

`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
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