Repository navigation
feat(box): send python3 and each alias's folder to the shell - #417
Conversation
## Problem A `python3` or `python` that a program starts skips the Shell. The alias reads the script with the agent's own access, sends the source straight to Monty with no `shell:exec` decision, and waits under its own 35-second limit, while Monty allows 120 seconds and the Shell's deadline is configurable. The same script typed in the Shell takes a different route with different decisions and limits. Every alias also starts its command in the workspace, not in the folder it was run from. Measured on main: from a subfolder, `zsh -c 'cat here.txt'` read the workspace's `here.txt` and exited 0. A read silently gets the wrong file, and a write lands in the wrong folder. ## Solution The broker protocol moves to version 9, with one new client frame, `run`. It carries a command and the caller's folder. The command is text for `zsh`, `bash` and `sh`, or a name and an argument list for `python3` and `python`. `call` and every other frame are unchanged; an alias built for version 8 is refused by version. - Every alias sends the folder it was run from: `PWD` when it names `.`, otherwise `getcwd`. If the alias can't tell, it exits 125 and sends nothing, so a command never falls back to the workspace. - The `python3` and `python` aliases open a Shell Program and send their name and arguments unchanged. They parse no option and read no file. The trusted process single-quotes every word, the quoting `xargs` and `find -exec` already use, so `$(...)`, backticks and newlines stay literal. The Shell's own `python3` then runs Monty, exactly as a typed `python3` does, and refuses a bare `python3` or `python3 -` with status 2. - The alias's 35-second Python wait is gone; `limits.shell.timeout` applies, and a `Signal` can end the call. - Before the command runs, `Program::enter` moves the Shell into the folder through `Mediated::change_dir`, the `fs:read` `change_dir` decision and reachable paths check a typed `cd` gets. The folder the Program already starts in, the workspace, takes no decision, so a file-scoped policy still runs `zsh -c` from there. A refused folder, a missing one, or a file runs nothing and exits 126 with `strands-box: cannot start in <folder>: <reason>`. Interfaces that move, which need a maintainer's approval (the broker protocol and the aliases are frozen surfaces): - The `python3` alias now raises a `shell:exec` decision. A policy with only `fs:*` permits refuses it with 126. - The Shell reads a script file under `fs:read`, from the caller's folder, not with the agent's own access. - `bash -c`, `zsh -c`, `sh -c`, `python3` and `python` start in the caller's folder, and a folder other than the workspace raises an `fs:read` `change_dir` decision. - Through the Python alias, a refused form exits 2 instead of 125, and `--version` and `--help` work. Monty still roots a relative path at the workspace. Making it follow the Shell's folder changes the vendored Shell's script hook, which the move to upstream Strands Shell replaces, so it is a follow-up. The direct `Interpreter::Python` route stays in the daemon for one release; no alias opens it. Docs: `docs/user/monty.md`, `docs/user/shell.md`, `docs/user/config.md`, `docs/design/monty.md`, `docs/design/shell.md`, `docs/design/binaries.md`, `docs/design/architecture.md`, the `python-in-the-shell-is-monty` entry in `docs/design/decisions.md`, and `crates/box/AGENTS.md`. ## Tests Ran on macOS: `cargo fmt -p strands-box --check`, clippy with the CI allow list and `-D warnings`, the alias tests (45 pass), the library tests (948 pass, 1 ignored), and every test target with `--no-run`. `Cargo.lock` is unchanged. - Alias: `a_python_alias_forwards_its_name_and_arguments_unchanged` (no arguments, `-`, `-m`, `$(rm x)`, backticks, newlines, quotes, spaces, the empty string), `a_python_alias_refuses_a_non_utf8_argument`, `the_working_directory_comes_from_pwd_only_when_pwd_names_it`, `an_unknown_working_directory_is_refused`, `every_submission_sends_the_callers_directory`. The `-c`, `-lc`, `-c -l` and `SCRIPT` tests still pass. - Protocol: `a_run_frame_round_trips_in_both_command_shapes`, `a_malformed_run_frame_is_refused`, `the_protocol_version_that_carries_run_is_nine`. - Broker: `a_command_as_words_keeps_every_argument_literal`, `a_run_starts_in_the_callers_directory`, `a_python_run_reaches_the_shells_python_and_reads_the_callers_script`, `a_python_run_with_no_script_is_refused_by_the_shell`, `a_python_run_needs_a_shell_exec_permit`, `only_a_folder_other_than_the_programs_own_meets_the_change_dir_decision`, `a_folder_the_box_cannot_enter_is_refused_and_runs_nothing`, `a_python_program_refuses_a_run`, `the_version_before_run_is_refused`. Replacing the `change_dir` call with a plain assignment, and dropping the quoting, each fails four of these. - In a box (`box_shell.rs`), compiled here and run only by CI: `an_alias_run_from_a_subfolder_runs_in_that_subfolder`, `an_alias_in_a_folder_the_policy_refuses_runs_nothing`, `the_python_alias_needs_a_shell_exec_permit`. Not run: anything that starts a box (`box_shell.rs` and the other end-to-end suites, `test-integ`), and Linux. This Mac can't start a box inside its own sandbox. The Mermaid blocks in the changed design pages were not compiled here.
## Problem Two review findings on the previous commit. - The broker still served `open` in Python mode. A `call` on that Program ran Monty with no `shell:exec` decision. No version 9 alias opens it, but any process in the agent's sandbox can connect to `run/box.sock`, so a policy that forbids `shell:exec` on `python3` did not stop a client the workload wrote itself. - A caller's folder that no host path backs was entered. The reachable paths check passes a name outside the host set, and the Shell's in-memory tree holds `/`, `/tmp`, `/home`, `/bin` and `/usr/bin`. So `printf RAN` run from `/` or `/tmp` ran in memory and exited 0, and a write landed nowhere on the host. A `..` spelling that starts inside the workspace reached the same place. Smaller findings: entering the folder ran outside the call's deadline, and the docs left out the input limit on `python3 -c`, a script that never pauses, a folder name that isn't valid UTF-8, and the agent's home in one troubleshooting row. The previous commit message also said a `Signal` can end a Python call through the alias; the alias sends no `Signal`. ## Solution - The broker refuses `open` in Python mode with a `transport_error` denial that says the mode was removed in protocol 9. The Shell is now the only way to Monty. The old serving code stays, unreachable: deleting it also retires `REQUEST_TIMEOUT` and the `fetch.rs` timeout ordering check, which is a separate change. - `Program::enter` refuses a folder `Reach::names_a_host_path` doesn't back, both as the alias spelled it and as the Shell resolved it after `change_dir`, and restores the old folder. The refusal is the usual 126 with `strands-box: cannot start in <folder>: outside this box's home and every declared bind`. - Entering the folder counts against the call's deadline, and the command gets what is left of it. - Docs: `docs/user/monty.md` (input limit, never-pausing script, non-UTF-8 folder, the troubleshooting row), `docs/user/shell.md`, `docs/design/shell.md`, `docs/design/monty.md`, `docs/design/binaries.md`, the `python-in-the-shell-is-monty` entry, `crates/box/AGENTS.md`, and the protocol and Python module docs. ## Tests Ran on macOS: fmt check, clippy with the CI allow list and `-D warnings`, the alias tests, the library tests, and every test target with `--no-run`. `Cargo.lock` is unchanged. - `an_open_in_python_mode_is_refused_and_runs_nothing` replaces `a_python_program_refuses_input_and_signal_by_name` and `a_python_program_refuses_a_run`: the open is refused by name, a `call` for that id finds nothing open and writes nothing, and the connection still serves a Shell Program. With the old open restored, it fails. - `a_folder_the_box_cannot_enter_is_refused_and_runs_nothing` gains `/`, `/tmp`, a made-up absolute path, and a `..` spelling from the workspace into `/tmp`. Without both checks, `/` prints `RAN`. Without the check after `change_dir`, the `..` row prints `RAN`. Not run here: the in-box suites and Linux.
… docs ## Problem Three small findings from the review of the previous commit. - `tokio::time::timeout` polls the folder entry before its timer. A synchronous lookup that overran the call's deadline still returned `Ok`, and the command then started with no time left, so it could run before the timeout was reported. - `docs/design/architecture.md` said the broker hands Python scripts to Monty, and the `monty-is-per-request-and-buffers-its-output` entry in `docs/design/decisions.md` described an outer limit on direct Python calls. The previous commit removed that route. - `an_open_in_python_mode_is_refused_and_runs_nothing` waited on an unbounded read, so a regression would fail only at the 300-second idle timeout. ## Solution - After the folder entry, the broker reports a timeout and exits 125 without running the command if no time is left. - `architecture.md` now says the Shell handles the Python aliases and runs scripts in Monty. The decisions entry drops the outer limit and the "separate five-minute allowance" sentence, and says Python runs through the Shell. - The refusal test bounds each read to 5 seconds. ## Tests Ran on macOS: fmt check, clippy with the CI allow list and `-D warnings`, the alias tests, the library tests, and every test target with `--no-run`. `Cargo.lock` is unchanged. - `a_run_whose_folder_entry_passes_the_deadline_runs_nothing` runs a `run` into a subfolder 20 times with a zero deadline and checks that each reports the timeout, exits 125, and writes nothing. With the old remaining-time line restored, it fails on the first attempt. - On this code, replacing `change_dir` with a plain assignment fails 2 broker tests, and removing the quoting fails 4. Not run here: the in-box suites and Linux.
crates/box/src/bin/strands-box-sock-alias.rs: kept the run frame, the caller's folder and python3 words; submission now sends Open and Run, and forward attaches main's stdin frames (each Input, then InputEof, or InputEof alone) after Run for every alias; forwards_stdin is true for Text (shell aliases) and false for Words (python3/python); main's stdin tests now send Run. crates/box/src/run/broker/host.rs: imported both Command and main's MAX_CHUNK_BYTES and MAX_ENCODED_CHUNK_BYTES; main's Input handling is per Program, so it serves a Run as it serves a Call. crates/box/src/run/broker/program.rs: kept no_host_folder for Program::enter and dropped poisoned(), which main's stdin buffer no longer calls. crates/box/AGENTS.md: kept main's stdin notes with Call changed to Run, said python3/python send InputEof right after Run and forward no stdin, and kept the StdinEof note for every alias. docs/user/shell.md: kept the caller's folder rule and main's credential route placeholder, and said the python3 and python aliases forward no standard input. PROTOCOL_VERSION stays 9: main is still at 8, and the Input frames it uses existed before.
|
Assessment: Comment Review of head Cases and rules checked that held
One structural follow-up is inline: the Python-mode path can be removed from |
abhisheksp
left a comment
There was a problem hiding this comment.
Approving. Making the Shell the one place that picks the interpreter is the right shape, and closing the direct Monty mode removes an undecided route. I found no new reach: the caller's folder is untrusted input that only chooses where the command starts, and it goes through the same fs:read change_dir decision and host-path check as a typed cd.
This approval also covers the frozen-surface changes: PROTOCOL_VERSION 8 to 9 with the new run frame, open in python mode refused, the python3 alias needing shell:exec, and every alias starting in the caller's folder.
Two comments, neither blocking:
- macOS exit 125. An alias can now exit 125 where main ran the command, when
PWDis stale orgetcwdis refused. Please say indocs/user/shell.mdwhat the user sees and how to recover, or fall back to the workspace in that case if you think it is safe. - Dead Monty serving code. The direct Monty mode's serving code is still present and kept unreachable by one deny line. Please remove it here or in a follow-up, so the refusal does not rest on a single line.
Review asked that Box not know about Python installs: with Monty off, Python is an ordinary tool the operator declares and grants, and Box special-cases `python3` only to run Monty. So this restores the branch to #417's state and drops what the earlier commits on it added: - install detection and protection (`run/python_install.rs`, the refused writes into the install); - the grants Box added for a Python tool, including the libraries and the certificates; - `[tool.python]` answering both names, the 126 refusal when no Python tool is declared, the write-only stdin and its stderr line, and the read-only install line; - the Shell hooks those needed in the vendored Shell, which `main` has since replaced (#414). The `monty = false` switch comes back on top of `main` in a later commit. The containment fix for a link chain through a linked folder moves to its own pull request, since it affects any tool.
Description
Today a
python3that a program starts (a#!/usr/bin/env python3line,subprocess.run(["python3", ...])) takes a different path from apython3typed in the Shell. Thebin/python3alias reads the script itself, with the agent's own access, sends the source straight to Monty with noshell:execdecision, and waits under its own 35-second limit, while Monty allows 120 seconds. Every alias also starts its command in the workspace, not in the folder it was run from. Measured onmain(f476373, macOS): aftercd sub,zsh -c 'cat here.txt'read the workspace'shere.txtand exited 0, and a file the command wrote landed at the workspace top.This follows the review discussion on #389: the
python3alias stops deciding which interpreter runs and becomes a pipe to the Shell, so the Shell is the one place that decides whatpython3means, and every alias tells the Shell which folder it was started in. It doesn't add any way to run real Python; on this branch the Shell'spython3is still Monty.What changes
run. It carries a command and the caller's folder. The command is text forzsh,bashandsh, or a name plus an argument list forpython3andpython.calland every other frame are unchanged.PWDwhen it names.(same device and inode), otherwisegetcwd. If the alias can't tell, it exits 125 and sends nothing. It never falls back to the workspace.python3andpythonaliases open a Shell Program and send their name and arguments unchanged. They parse no option and read no file. The trusted process single-quotes every word, with the same quotingxargsandfind -execalready use, so$(...), backticks and newlines stay literal. The Shell's ownpython3then runs Monty, exactly as a typedpython3does.Program::enterfirst refuses a folder no host path backs (see below). Then it callsMediated::change_dir, thefs:readchange_dirdecision and reachable-paths check a typedcdgets, and checks the resolved folder again. The workspace, where the Program already starts, takes no decision, as today. A refused folder, a missing one, or a file runs nothing and exits 126 withstrands-box: cannot start in <folder>: <reason>. Entering the folder counts against the call's deadline, and a call with no time left after entering reports a timeout (125) without running the command.limits.shell.timeoutapplies (default 30 s). Monty's own 120 s limit is unchanged, and it's what stops a script that never hands back control.openwith modepythonfollowed bycallused to run Monty with noshell:execdecision. No version-9 client uses it, and any program in the box can connect tobox.sock, so leaving it would let the agent skip the decision the docs now say gatespython3. The broker refuses it with a reason naming protocol 9. The serving code is unreachable but not deleted yet, because deleting it also touches a shared timeout used byfetch./,/tmp,/home,/usr/binand/bin. Onmaina call from/, or from Linux's private/tmp, runs in the workspace like every other call. Without this check, entering the caller's folder would put the Shell in that in-memory folder, where the command exits 0 and its files go nowhere.Program::enterrefuses a folder no host path backs, both as the alias spelled it and afterchange_dirresolves it (which catches a..that climbs out of the workspace), with 126. A typedcd /tmpinside a command is unchanged.Visible behaviour changes (each needs approval)
python3alias now raises ashell:execdecision. A policy with onlyfs:*permits refuses it with 126.fs:readdecision, from the caller's folder, instead of the alias reading it with the agent's own access.bash -c,zsh -c,sh -c,python3andpythonstart in the caller's folder.python3orpython3 -exits 2 (the Shell's refusal) instead of 125, and--versionand--helpwork.openwith modepythonis refused at version 9./, Linux's private/tmp) exits 126. Onmainit ran in the workspace.Not in this PR
python3 show.pyrun fromsub/findssub/show.py, butopen("here.txt")inside it still opens the workspace'shere.txt. That's the same as onmaintoday, where the alias reads the script from the caller's folder and Monty opens files from the workspace. Fixing it is in the vendored Shell, which refactor(shell): migrate Box to upstream Strands Shell #414 replaces, so it's a follow-up on top of refactor(shell): migrate Box to upstream Strands Shell #414.python3. feat(shell): forward a Shell alias's stdin to its command #397 has merged, and this branch mergesmain. Thebash,zshandshaliases forward stdin withrunas they did withcall. Thepython3andpythonaliases forward none, and send end-of-input right afterrun.Related Issues
Part of #371. Follows the alias proposal in the review of #389. #397 has merged; 52e51a0 merges
maininto this branch and resolves the conflicts in the alias,host.rs,program.rs,crates/box/AGENTS.mdanddocs/user/shell.md. #444 is stacked on this branch. This PR also overlaps #414 inhost.rs,program.rs(a newProgram::enternext touse_shell, usingshell.procandkernel().change_dir) anddocs/design/decisions.md, so whichever lands second rebases.Type of Change
Other: a new broker frame and a change to how the
python3alias routes (frozen surfaces).Testing
Run on macOS for every commit (logs not attached):
cargo fmt --allandcargo fmt -p strands-box --check: pass.cargo clippy -p strands-box --all-targets --all-features --locked -- -D warningswith CI's allow list: 0 warnings.cargo test -p strands-box --bin strands-box-sock-alias --locked: 45 pass.cargo test -p strands-box --lib --all-features --locked: 948 pass, 1 ignored on afff460 (two tests of the removed Python mode were replaced by one that checks it's refused, and one deadline test was added).cargo test -p strands-box --all-features --locked --no-run: every target compiles.change_dircall with a plain assignment fails 2 of the new broker tests, and dropping the quoting fails 4 (measured on afff460; the host-path check now refuses some folders beforechange_dirruns).CI on the first commit (834d539): macOS and Linux unit tests passed, including the in-box tests; lint, site manifest and CodeQL passed.
CI on the merge with
main(52e51a0): macOS and Linux unit tests, lint, site manifest and CodeQL pass.New tests:
-,-m pytest,-c,--version, arguments after a script,$(rm x), backticks, newlines, quotes, spaces, the empty string); non-UTF-8 argument refused; folder taken fromPWDonly when it names.; unknown folder refused; every submission sends the folder.runround-trips in both command shapes; malformedrunframes refused; version is 9; version 8 refused.printf; a file survives$(rm x)); a run starts in the caller's folder; the Python alias reaches the Shell'spython3and reads a script that exists only in the caller's folder; barepython3andpython3 -exit 2; the Python alias needsshell:exec; a policy forbidding everychange_dirstill runs in the workspace but refusessuband writes nothing; a folder outside reach, missing, a file, relative, or 200 KiB long is refused and runs nothing;/,/tmp, a made-up absolute path, and a..that climbs out of the workspace are refused and run nothing;openwith modepythonis refused and runs nothing.crates/box/tests/box_shell.rs), compiled here, run only by CI: an alias run from a subfolder runs there (pwd,cat,python3 show.py); a refused folder runs nothing; the Python alias needsshell:exec.Not run locally: anything that starts a box (
box_shell.rsand the other end-to-end suites,test-integ), Linux, and the Mermaid blocks in the changed design pages.Docs updated in the same commit:
docs/user/monty.md,docs/user/shell.md,docs/user/config.md,docs/design/monty.md,docs/design/shell.md,docs/design/binaries.md,docs/design/architecture.md,crates/box/AGENTS.md, theprotocol.rsmodule docs, and one entry indocs/design/decisions.md(#python-in-the-shell-is-monty, reworded to say both ways reach the Shell'spython3).just check, orcargo test --workspace --all-features)cargo fmt --allandcargo clippy(scoped tostrands-box, see above)Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.