Repository navigation
fix(policy): enforce shell working-directory restrictions - #296
Conversation
## Problem A shell rule for ~/workspace missed commands whose working directory was reported as an absolute path. A home-relative forbid could allow the command it intended to deny. ## Solution Use the existing approved-path helper for the cwd of shell:exec and shell:spawn decisions and their request and outcome history. Retain the actual execution directory and telemetry path. Extend the existing static path diagnostics to cwd and document the policy spelling. Existing history retains its original values. Changing a history rule starts empty state. Counter-preserving migration is a Priority 1 follow-up; this repair does not introduce a history migration or upgrade gate. ## Tests an_exec_forbid_matches_exact_and_descendant_working_directories and a_spawn_forbid_matches_exact_and_descendant_working_directories prove the corrected matches and permitted sibling controls. The four configured box_shell_cwd tests prove actual command refusal and unchanged execution and telemetry paths. temporal_rules_distinguish_requests_and_outcomes_in_the_reported_working_directory proves request/outcome consistency. reopened_history_preserves_old_cwd_spelling_and_records_new_spelling proves existing predicates retain their observations, not rule migration. All 14 new tests pass; 13 fail on the original baseline and one positive loader control passes. Linux ARM64 workspace tests: 4,898 passed, zero failed, 17 ignored. Deterministic integration: 131 passed, nine macOS-only exclusions, zero failures or errors. Workspace build, formatting, and Clippy with repository lint allowances passed. Post-rebase documentation manifest: 27 passed. Native macOS and the SDK tutorial did not run locally; known runtime fixture skips and ignored tests remain disclosed in the PR.
|
Assessment: Request Changes The change fixes Checks that held
The tests covering exec, spawn, and recovery are thorough, and each restriction test includes a permitted sibling directory as a control. |
|
Assessment: Comment (merge decision deferred to a maintainer) Re-review of Checks that held on this head
|
Select the configured launch directory before policy admission and carry it through each spawn permit. Refuse directory drift before execution while preserving command-not-found behavior and completion accounting. Cover workspace precedence, aliases, pipelines, cancellation, and native program dispatch. Record the approved Shell interface addition and retain the accepted history-migration limits.
|
Assessment: Comment (merge decision deferred to a maintainer) Re-review of Checks that held on this head
The PR is now about 2,000 changed lines and contains two fixes plus a vendored Shell interface addition. If a maintainer prefers, the launch-directory binding ( |
|
Assessment: Comment (merge decision deferred to a maintainer) Re-review of Merge resolutions and checks that held
|
|
Suggestion (non-blocking): after the latest |
|
Assessment: Comment (merge decision deferred to a maintainer) Re-review of Checks that held
|
|
Assessment: Comment (I have no new findings; the merge decision is a maintainer's call) Re-review of Checks that held
|
ec715e5
|
Assessment: Comment (I have no new findings; the merge decision is a maintainer's call) Re-review of Assumptions and rules checked that held
|
|
Assessment: Comment. I found nothing new; whether to merge is up to a maintainer. Since Assumptions and rules checked that held
|
Human readable section
Problem: A working-directory rule could allow a command it was meant to deny. Policy could compare
/home/alex/projectagainst~/project, even though both identify the same directory. It could also check the caller's directory before launching a program in a different, configured tool workspace.Solution: Box selects the program's launch directory before checking policy, then carries that absolute path into execution. If launch validation resolves a different directory, Box doesn't start the program and returns status
126. For ordinary shell policy decisions and new history, paths under the operator's home use~/…. The caller's shell directory stays unchanged.Trade-off: After upgrade, existing deny rules based on previous shell operations can silently stop denying commands if they match absolute paths under the operator's home. Box doesn't rewrite existing history records. Migration that preserves accumulated rule counts and an accurate upgrade warning remain Priority 1 follow-ups.
Description
Example: deny a launch from a protected directory
Assume the operator's home is
/home/operatorand/usr/bin/printfis an ordinary external program governed by Box'sshell:spawnaction. The following policy permits external-program launches except from~/project. Thecontext.input.cwdfield identifies the launch directory./home/operator/project/home/operator/project, so theforbidcondition did not match.~/project, so theforbidcondition matches./home/operator/project-siblingforbidcondition does not match.~/project-sibling, which does not match.Box still launches permitted programs with an absolute directory path. Only the path spelling supplied to policy changes in this example. The exact-match condition covers
~/projectitself; the regression test separately checks descendant matching withlike "~/project/*".This example covers the external-program policy decision. Other operations and tool-specific grammar actions need their own applicable permissions.
Check the directory used for execution
Strands Shell is the shell interpreter used by Box. For an external program launch, Box selects the tool and resolves the launch directory. Selection keeps the existing rules for tool workspaces, invoked aliases, longest command-prefix matching, and fallback to the agent specification.
Box checks policy once for the selected launch. Policy receives a reported copy of the directory: paths under the operator's home use
~/…; other paths stay absolute. If policy allows the command, Box stores the original absolute directory in a command permit, the approval object passed to execution.The following diagram shows an ordinary program launch evaluated through Box's
shell:spawnpolicy action. It assumes tool selection and directory resolution succeed:%%{init: {"flowchart": {"rankSpacing": 30, "nodeSpacing": 35}}}%% flowchart TB accTitle: Box checks the directory used to launch a program accDescr { Box selects the tool and absolute launch directory. Policy checks a reported copy: paths under the operator's home use a tilde; other paths remain absolute. A denial prevents execution. An approval creates a permit that retains the selected absolute directory, without converting it to the reported spelling. Strands Shell passes that directory to Box's launcher. After existing launch validation succeeds, Box compares the launch directory with the checked path. A match permits program startup in that directory; a mismatch prevents startup and returns status 126. The dashed arrow carries directory data, not permission to execute. } select["Box selects the tool<br/>and absolute launch directory"] policy["Box checks policy<br/>using the reported directory"] permit["Approval object retains<br/>the checked absolute directory"] validate["Box validates the launch,<br/>then compares directory paths"] run["Start the program<br/>in the checked directory"] denied["Do not execute<br/>the command"] mismatch["Do not start the program<br/>Return status 126"] select -->|"Reported copy: ~/… under home;<br/>absolute elsewhere"| policy policy -->|Allow| permit policy -->|Deny| denied select -. "Retain absolute directory" .-> permit permit -->|"Strands Shell passes the directory"| validate validate -->|"Paths match"| run validate -->|"Directory mismatch"| mismatch classDef refusal fill:#fff1f0,stroke:#b42318,color:#7a271a class denied,mismatch refusalFigure 1. Policy checks the reported directory; execution uses the retained absolute directory. The dashed arrow carries directory data. It doesn't bypass the policy decision. Tool-specific grammar actions, local Model Context Protocol server startup, and other launch errors are outside this diagram's scope.
Strands Shell passes the permit's directory to Box's launcher. After the existing launch validation succeeds, Box compares the launch directory with the checked path. A mismatch prevents program startup and returns status
126. Each pipeline stage carries its own checked directory.If tool selection or directory resolution fails, Box checks and retains the caller's directory, then follows the existing dispatch path. Missing commands retain status
127.Carry approval into execution
The Strands Shell
EffectPermitinterface tracks an approved operation through completion. Its new default method,EffectPermit::spawn_working_directory() -> Option<&Path>, supplies the directory for program execution. It returnsNonefor existing implementations, preserving use of the caller's directory.Shell reads the method once. If the method returns a relative path, Shell doesn't execute the command and returns status
125. Box's permit wrapper preserves completion and cancellation accounting.Keep policy decisions and history consistent
The change reuses the existing path-reporting and policy-loading checks for two ordinary shell policy actions:
shell:execfor commands handled by Strands Shell, andshell:spawnfor resolved external programs. Box records the same reported directory and request identifier for the policy decision and the command outcome.Some tools map parsed commands to tool-specific policy actions through a grammar. Those actions retain absolute working-directory (
cwd) values. If Box rejects a program's grammar binding, it records a refusedshell:spawnrequest using the reported directory.Local Model Context Protocol (MCP) server startup keeps its separate preparation path.
Compatibility and scope
Existing rules can include conditions based on previous operations. For ordinary shell actions, conditions that match absolute paths under the operator's home still load without a warning, but miss new home-relative events. Editing a rule starts it with empty state, even if the edited rule matches both spellings.
The contributor accepted this migration risk and approved the default permit method. The approval and the change to the vendored Strands Shell interface are recorded in
crates/shell/UPSTREAM.md. Grammar-action spelling and historical migration remain outside this repair.The launch check compares paths. It doesn't retain a directory's filesystem identity if another directory replaces it at the same path.
Configuration keys, stored-record formats, broker-protocol fields, dependencies, and lockfile entries remain unchanged.
Related Issues
No public issue is linked. Migration that preserves rule counts and upgrade warnings remain separate Priority 1 follow-ups.
Type of Change
Bug fix
Testing
November 10 merge
Revision
a393234incorporates main8cfdddc, including configured-tool name selection, absolute MCP commands, per-tool history recovery, and support for incoming TCP connections. The conflict resolution retains both the configured executable and its checked launch directory.Main added
ingressto Box'sProcessSpecRust struct but omitted the field from an existing command-selection test fixture. The first hosted build failed before workspace tests ran. This revision supplies an empty list because the fixture exposes no incoming connections. The correction changes only the test fixture.The explicit-workspace regression now launches the tool by its full path and by its bare name,
native-pwd. Both permitted launches must execute in the configured directory. Both protected launches must return status126, and all four decision records must identify the selected directory and the applicable policy rule.ec715e5)box_librarytarget also passed.127. Restoring the candidate passed the same test.-D warningsand no allowances.a393234)a393234found no new issues. Its sandbox could not run the tool-launch tests; the hosted results above cover those tests.GitHub tested merge commit
5590636, which combines PR heada393234with mainc8cdbd1. The shared-file changes from that main revision were reviewed; no working-directory enforcement issue was found.The full local validation ran on Amazon Linux 2023 ARM64 against source tree
66ec942e92c32289e43811d29eb98d302c26169c(ec715e5). The final local checks cover tree6b05579d37fc9af231297680b0fb6241e108717d, which adds the new main changes and the missing test initializer. The earlier full suite was not rerun locally for that one-field correction; hosted Linux and macOS checks cover the final revision. Workspace Clippy used the repository's seven existing lint allowances, listed with the earlier evidence. The full-workspace and Box-only all-feature builds enable different optional Shell bindings; the mutation and restored control used the same Box-only feature selection.The earlier local namespace-mount startup failure is fixed by the incorporated main changes. Remaining local limits: the real credential daemon was unavailable, the executable-alias read assertion did not run, and the examples script found no runnable scripts. Alias write assertions ran. The live SDK tutorial and native macOS execution were not run locally. These checks do not establish history migration or release qualification.
Earlier validation on November 9, November 6, and November 4
November 9 revision
Head
0d06a52passed hosted Linux and macOS validation. The local full-workspace run before the count correction failed for the reasons detailed below.The merge incorporates main
fbe415c. The conflict resolution preserves main'sgitfixture and this PR's assertions about the refused command's request identifier and directory.A separate test correction updates main's stale syscall counts after #356 added
statfs, the Linux system call that reports filesystem statistics. The test now expects 113 workload entries and 116 entries in the rule map used to construct the system-call filter. It preserves the remaining assertions and changes no production policy.The automated review found no new behavior defect and one documentation inconsistency: the grammar-action table named the caller's shell directory instead of the program's selected launch directory. The final commit corrects that row. The final re-review confirms the correction and reports no new findings. All code, tests, and workflows are unchanged from the preceding reviewed revision.
0d06a520d06a520d06a52pythonmade it fail at the intended history assertion; restoringgitpassed.box_librarytarget also failed. The failure analysis is described below.0d06a52on November 10.Tested revisions and workspace Clippy allowances
The full local run tested merge tree
0eeb1156d221871d53aa9a3e9b6755b3e4a34f32. The count correction produced heade358c7952ac7e49517704028859f4c1e71f33e47, tree3bfc7e534e19ba5bee2bc24a05127759602af3ca.The preceding hosted CI run tested merge
2c1e11e57503e7cf9551ec6678e03d8082d52c0a, combining that head with mainfbe415cbdfec284bf608a46f125ff8e437a99bda. Its tree exactly matches the code revision above. The automated reviewer also checked oute358c79.The documentation correction is head
0d06a523fad5c33307e22ba6311d390725fd4d82, tree7c56fcb19fb8455300d85fe108d6e37e74be1f13. Its only difference frome358c79is one row indocs/user/policy/actions.md. Final hosted CI tested merge0f87cf0146d37485612625a402da43f41f474ce9, combining0d06a52with mainfbe415c. Its tree exactly matches the final head. The final site-manifest check used main's expected manifest and retirement files. The final automated re-review checked this documentation-only head and confirmed that the preceding code-test evidence still applies.Workspace Clippy used
-D warningswith the repository's allowances forclippy::too_many_arguments,clippy::only_used_in_recursion,clippy::await_holding_refcell_ref,clippy::explicit_auto_deref,clippy::chunks_exact_to_as_chunks,dead_code, andnon_snake_case. Default-policy Clippy used-D warningswithout these allowances.November 9 validation limits
box_libraryfailure. The original logs expose the error in 382 failures; isolated diagnostic runs expose it in the remaining three. The parser matches main byte for byte, and an exact-main parser control reproduces the failure. The other counted failure is the syscall test corrected in this update. The full local workspace suite was not repeated after that three-line correction; the corrected code passed hosted validation.coverage_gate=fullfield does not establish that those cases ran. The complete hosted case counts are reported above.0d06a52. The local checks retain the revision limits described above. These checks do not establish migration, performance, or release qualification.Earlier validation on November 4 and November 6
November 6 revision
The November 6 conflict resolution is validated at head
68b527c2fd41a6900599715d24e3eb2cffc520f0. Local validation ran on Amazon Linux 2023 ARM64 with Linux support enabled and matching helper binaries.box_libraryexecutable also passed.Hosted validation includes newer main changes, which account for the difference between local and hosted workspace totals.
Tested revisions and lint allowances
The local tested and reviewed tree is
e6c57d5d2c51a09fd8adbc3c78fad0f79f039270, with main parent6bd1b2ff90f432dce430b0b05f5697df38237fa8.Hosted validation tested merge
775b7b0d0e0559d8649720e04561e83386a9d8c3, which combines this PR head with mainc8856a1aa60e8e024101fad06336ef52c7e9bedd.Clippy used
-D warningswith the repository's allowances:clippy::too_many_arguments,clippy::only_used_in_recursion,clippy::await_holding_refcell_ref,clippy::explicit_auto_deref,clippy::chunks_exact_to_as_chunks,dead_code, andnon_snake_case.November 6 validation limits
November 4 revision
Regression coverage and validation before conflict resolution
This evidence applies to
f19d1b756c06e3d86806f47585c108d480934840and its earlier baseline. It doesn't replace the current-revision results.Validation ran on Amazon Linux 2023 ARM64 with Rust 1.98.1 and
EXPERIMENTAL_LINUX_SUPPORT=TRUE.box_libraryharness also passed. This added 24 passing tests relative to the normalization-only revision.The regression cases cover tool workspaces, agent fallback, aliases, longest command-prefix selection, separate pipeline directories, concurrent calls, denied pipeline stages, relative permit rejection, and an unchanged caller directory. They also cover command-not-found behavior, completion, recording errors, abandonment, and cancellation before or during recording.
A launch test retargets a workspace symlink after policy admission and verifies refusal before the program writes its marker. Native-program fixtures use a workspace alias so macOS doesn't route the external
pwdtest through Shell's builtin.The unchanged source passed
cargo build --workspace --all-features --locked,cargo test --workspace --all-features --locked --no-fail-fast -- --nocapture,cargo fmt --all --check, and workspace Clippy. Box and Shell builds used--no-default-features --locked. Deterministic validation usedtest-integ/run.sh --locked; the examples check usedscripts/test-all.sh box-examples. Source hashes and the primary deterministic executables remained unchanged through validation.Hosted run 41797948089 tested merge
0ccdf71d656bbf2497a8471e5625f5f14596b1ce. All 17 PR file blobs matchedf19d1b7; the four additional files came from main's integration-harness change in #249.The 17 ignored tests and four existing early-return gaps were unchanged: the credential daemon, an alias helper beside a unit-test binary, removed cardinality-probe scaffolding, and the Linux alias-read assertion. The live SDK tutorial wasn't run. Three native macOS binary filters selected zero tests, and the full signed native suite didn't run.
just check, orcargo test --workspace --all-features)cargo fmt --allandcargo clippy --workspace --all-targets --all-features -- -D warningsChecklist
AI assistance: Codex implemented this repair and performed code review and validation under the contributor's direction.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.