Skip to content

fix(sandbox): bind a sessions directory inside the workspace read-only, refuse one that cannot be protected (#866) - #870

Merged
ajianaz merged 3 commits into
developfrom
fix/866-sessions-dir-protected
Nov 16, 2026
Merged

ajianaz merged 3 commits into
developfrom
fix/866-sessions-dir-protected

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What

Option B of #866 (chosen by the owner, with A as the fallback): the effective sessions directory, when it is INSIDE the workspace, is bound read-only in the command profile as one more protected path of the existing plan, so a sandboxed run_command, job_start, verify command or stop gate cannot write, create, rename, remove or replace anything in it, while tole itself (a host process) keeps writing sessions. A directory the sandbox cannot protect is refused like the other layouts of the plan. Closes #866.

Why

Sessions are append-only JSONL. The default .tole/sessions under the sandbox's workspace root is protected through .tole (#849). But the location is relative to the working directory and can be moved (-s, sessions_dir), so it can be inside the workspace without being under the workspace's own .tole: that part of the writable workspace let a sandboxed command rewrite or delete past entries (the evidence tole resume, the audit and the cost report read). Item 4 of the not-fixed list of #858.

Changes

  • tole-core sandbox/command.rs: new set_sessions_dir(PathBuf) (public, absolute, set once per process; the doc comment says an embedder hosting several sessions with different directories protects only the first, and that never calling it gives no protection for a non-default directory). protect_plan(ws) delegates to protect_plan_with(ws, sessions_dir) and plan_sessions_dir adds the directory to the SAME protect list (no second plan; build and recheck_plan unchanged, re-checking at every launch). Rules, those of .tole: a real directory directly under the workspace root; deeper is refused (a mount point cannot be renamed, but its parent can, so a command could move the parent away and plant a copy that tole then writes into); a link, a file, the workspace itself, or a path reaching the workspace through a link is refused; outside the workspace is not in the sandbox's view; absent is not protected (like .tole); under .git, .tole or .tole-worktrees it adds nothing. A refusal is Fail::Unsandboxable, so the consumer's policy decides as for the other refusals.
  • .. after a symbolic link (reproduced by the independent review, fixed). A .. that follows a link means the LINK TARGET's parent; the plan collapsed it as text, naming another directory than the host uses (-s base/out/lnk/../sessions with lnk -> ws/a/b is ws/a/sessions, but the plan protected nothing and the depth refusal was bypassed). The path is now resolved the way the OS does (resolved: the longest existing prefix by canonicalize, the rest appended; the final component is not resolved, a link there is refused). The same flaw existed in the other places the plan reads a path from, and they now use the same resolution: gitdir_target (a gitdir file's target: gitdir: lnk/../gd protected a decoy), effective_gitdir (the gitdir whose worktree registry is read) and the linked worktree back-pointer (both its collapsed and its real name are checked). .gitmodules paths cannot contain .. (only normal components are accepted), so they are not affected.
  • tole-cli main.rs: the EFFECTIVE sessions directory is told to the sandbox, not only a moved one. Reproduced: the default .tole/sessions is relative to the working directory, and with --workspace /repo started in /repo/sub it lands in /repo/sub/.tole/sessions, inside the workspace and not under /repo/.tole; job_start (its sandbox workspace is --workspace) wrote into it. run_command is not affected (its workspace is the working directory, so .tole is the one next to the sessions), which the docs now say instead of "the default needs nothing". serve, acp and mcp keep sessions per session workspace, so only an explicit -s counts there. A directory under .tole adds nothing to the plan, and an absent one is not an error (the depth rule only applies to a directory that exists), so the usual tole run in the workspace root is unchanged; tole run --workspace /repo from /repo/sub with job_start or verify on now gets a refusal naming the remedy (run from the workspace root, ./sessions, or a directory outside).
  • Fail-closed start-up: a failure to tell the sandbox the directory (current_dir() fails, or set_sessions_dir errors) stops the start when run_command or job_start is required, or when verify or the stop gates are on (always unattended); an optional run_command or job_start with a human attached gets a loud tole: warning: ... sandboxed commands are not kept from writing the sessions directory; with every consumer off nothing is said.
  • CI: the Sandbox (bubblewrap) job runs the two new tole-core targets (7 expected, was 5) and the tole-cli step also runs sessions_default_dir_e2e (2 expected, was 1).
  • Docs: docs/sandbox-design.md §9.3 (sessions directory paragraph), threat-model row, CHANGELOG (Fixed, including the new public function and the API limits).

Callers and impact (grep, each checked): protect_plan has two production callers, build and recheck_plan (both via the global, so run_command, job_start, verify and the stop gates all see it); protect_plan_with is pub(crate); set_sessions_dir has one production caller (main.rs). lexical still serves the .. collapse where no link can intervene (the collapsed name of a worktree pointer is checked alongside the real one; the gitdir target compares both). Existing refusals (.git, .tole links, submodules, worktrees, .tole-worktrees) are untouched, and all existing plan tests pass. Behaviour change: with a command consumer on, a sessions directory below the workspace root is now refused instead of silently writable. Tests to rerun: command_tests.rs, command_sandbox{,_none}.rs, command_sandbox_sessions{,_refused}.rs, stopgate_sandbox.rs, verify_sandbox.rs, sessions_default_dir_e2e.rs, sandbox_consumers.rs, config_apply.rs, layering.rs.

Testing

  • Fail without the fix, host-side proof (stub first: set_sessions_dir recorded the path and protected nothing): command_sandbox_sessions FAILED (echo x >> sessions/a.jsonl succeeded inside the sandbox). With the plan wired in: 16 attempts (append, overwrite, truncate, rm, new file, mkdir, mv a file or the dir, mv then mkdir and forge an entry, rmdir, ln, replace by a link, cp -a plus swap, chmod, touch) each really runs and fails inside, the host tree is byte-identical after each, the host still appends and creates files, a sandboxed command still reads the entries, the rest of the workspace stays writable.
  • The .. after a link: unit a_dotdot_after_a_link_is_resolved_by_the_os_not_collapsed_as_text (the exact reviewed layout is refused as deeper; naming a direct child through the same trick is bound by its real path), a_dotdot_after_a_link_in_a_gitdir_or_worktree_pointer_is_not_collapsed_either (gitdir target and worktree back-pointer), and the real-bwrap command_sandbox_sessions_refused now names the directory as base/out/lnk/../sessions: run_command is refused with the reason and the command never ran. All three FAIL on the previous head's command.rs (checked by swapping it in, then restored), and an_absent_default_sessions_dir_is_not_an_error too.
  • The default directory below --workspace (sessions_default_dir_e2e, real binary, mock provider, real bwrap): a job_start writing into sub/.tole/sessions FAILED before the main.rs change (the tamper file appeared) and passes after: the model is told "the sessions directory ... deeper than a direct child" and nothing is written.
  • Fail-closed start: an_unresolvable_sessions_dir_refuses_the_start_when_a_sandbox_is_required (tole sessions started in a deleted working directory: run_command required, job_start required, verify optional, turnend optional all refuse with the message) and an_unresolvable_sessions_dir_warns_for_optional_and_is_silent_when_off. These cover behaviour the old code had no code for (it ignored the error), so they fail on it by construction; I did not run the old main.rs.
  • Earlier tests stay: unit tests of the plan (direct child, outside, missing, default, a parent reached through a link, deeper, workspace itself, link, file, alias, the .tole link refusal intact, absolute path), verify_sandbox.rs sessions tests (fail with the main.rs wiring neutered by hand).
  • Gates on the final head: cargo fmt --all --check; cargo clippy --workspace --all-targets --all-features -- -D warnings; cargo clippy -p tole-core -p tole-cli -p tole-tui --no-default-features --all-targets -- -D warnings; cargo clippy -p tole-cli --no-default-features --features shell-tools --all-targets -- -D warnings; cargo check -p tole-core --no-default-features --tests; cargo test of tole-core and tole-cli (default), of tole-core, tole-cli, tole-tui with --no-default-features, and of tole-cli with --no-default-features --features shell-tools, all with TOLE_REQUIRE_BWRAP=1 and --no-fail-fast: green. The attribution grep over origin/develop..HEAD is empty.
  • How it was reviewed: scripts/review-local.sh cannot run (cora is not installed here: exit 1, not a pass), so I self-reviewed git diff origin/develop...HEAD as an adversarial reviewer, then the independent review found the .. flaw, which this head fixes everywhere the plan resolves a path. Residual, documented: a sessions directory that does not exist at launch is not protected, a sandboxed command can still create a sibling copy of the directory (a workspace file), set-once per process for embedders, and the environment-reach limits of security: the session marker is an environment variable; a child can clear or forge it #867. An independent reviewer will review this head before merge.

…y, refuse one that cannot be protected (#866)

The host tells the command sandbox where the owner moved the sessions
(set_sessions_dir); the existing protect plan binds it read-only like .git and
.tole when it is a real direct child of the workspace and refuses a link, a
file, the workspace itself, a deeper path or a path that reaches the workspace
through a link. tole itself keeps writing sessions from the host process.
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

…e OS does, protect the effective sessions dir, never fail open (#866)

A .. after a symbolic link is the link target's parent: it was collapsed as
text for the sessions dir, a gitdir file's target, the gitdir behind the
worktree registry and a linked worktree's back-pointer. The effective sessions
dir (also the default relative to a cwd below --workspace, which job_start left
writable) is now passed to the sandbox, a failure to do so stops the start when
a command sandbox is required or the consumer is always unattended, and the
set_sessions_dir doc says it is set once per process.
@ajianaz
ajianaz merged commit 0d26571 into develop Nov 16, 2026
23 checks passed
@ajianaz
ajianaz deleted the fix/866-sessions-dir-protected branch November 20, 2026 08:38
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.

security: a sessions directory inside the workspace but outside .tole is writable by a sandboxed command

2 participants