Repository navigation
fix(sandbox): bind a sessions directory inside the workspace read-only, refuse one that cannot be protected (#866) - #870
Merged
Conversation
…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.
🔍 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.
5 of 8 tasks
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
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
commandprofile as one more protected path of the existing plan, so a sandboxedrun_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/sessionsunder 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 evidencetole resume, the audit and the cost report read). Item 4 of the not-fixed list of #858.Changes
tole-coresandbox/command.rs: newset_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 toprotect_plan_with(ws, sessions_dir)andplan_sessions_diradds the directory to the SAME protect list (no second plan;buildandrecheck_planunchanged, 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,.toleor.tole-worktreesit adds nothing. A refusal isFail::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/../sessionswithlnk -> ws/a/bisws/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 bycanonicalize, 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/../gdprotected 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)..gitmodulespaths cannot contain..(only normal components are accepted), so they are not affected.tole-climain.rs: the EFFECTIVE sessions directory is told to the sandbox, not only a moved one. Reproduced: the default.tole/sessionsis relative to the working directory, and with--workspace /repostarted in/repo/subit 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_commandis not affected (its workspace is the working directory, so.toleis the one next to the sessions), which the docs now say instead of "the default needs nothing".serve,acpandmcpkeep sessions per session workspace, so only an explicit-scounts there. A directory under.toleadds nothing to the plan, and an absent one is not an error (the depth rule only applies to a directory that exists), so the usualtole runin the workspace root is unchanged;tole run --workspace /repofrom/repo/subwithjob_startor verify on now gets a refusal naming the remedy (run from the workspace root,./sessions, or a directory outside).current_dir()fails, orset_sessions_direrrors) stops the start whenrun_commandorjob_startisrequired, or when verify or the stop gates are on (always unattended); anoptionalrun_commandorjob_startwith a human attached gets a loudtole: warning: ... sandboxed commands are not kept from writing the sessions directory; with every consumer off nothing is said.Sandbox (bubblewrap)job runs the two newtole-coretargets (7 expected, was 5) and thetole-clistep also runssessions_default_dir_e2e(2 expected, was 1).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_planhas two production callers,buildandrecheck_plan(both via the global, sorun_command,job_start, verify and the stop gates all see it);protect_plan_withispub(crate);set_sessions_dirhas one production caller (main.rs).lexicalstill 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,.tolelinks, 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
set_sessions_dirrecorded the path and protected nothing):command_sandbox_sessionsFAILED (echo x >> sessions/a.jsonlsucceeded inside the sandbox). With the plan wired in: 16 attempts (append, overwrite, truncate,rm, new file,mkdir,mva file or the dir,mvthenmkdirand forge an entry,rmdir,ln, replace by a link,cp -aplus 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...after a link: unita_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-bwrapcommand_sandbox_sessions_refusednow names the directory asbase/out/lnk/../sessions:run_commandis refused with the reason and the command never ran. All three FAIL on the previous head'scommand.rs(checked by swapping it in, then restored), andan_absent_default_sessions_dir_is_not_an_errortoo.--workspace(sessions_default_dir_e2e, real binary, mock provider, real bwrap): ajob_startwriting intosub/.tole/sessionsFAILED before themain.rschange (the tamper file appeared) and passes after: the model is told "the sessions directory ... deeper than a direct child" and nothing is written.an_unresolvable_sessions_dir_refuses_the_start_when_a_sandbox_is_required(tole sessionsstarted in a deleted working directory:run_commandrequired,job_startrequired, verify optional, turnend optional all refuse with the message) andan_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 oldmain.rs..tolelink refusal intact, absolute path),verify_sandbox.rssessions tests (fail with themain.rswiring neutered by hand).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 testof 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 withTOLE_REQUIRE_BWRAP=1and--no-fail-fast: green. The attribution grep overorigin/develop..HEADis empty.scripts/review-local.shcannot run (cora is not installed here: exit 1, not a pass), so I self-reviewedgit diff origin/develop...HEADas 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.