Skip to content

feat(box): report the kind of an env refusal and a read-grant refusal to library callers - #274

Merged
notowen333 merged 2 commits into
strands-agents:mainfrom
notowen333:feat/check-workload-env
Nov 3, 2026
Merged

notowen333 merged 2 commits into
strands-agents:mainfrom
notowen333:feat/check-workload-env

Conversation

@notowen333

@notowen333 notowen333 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Description

Problem

A library caller that writes box.toml must know, before it writes, which entries Box refuses. Today it cannot learn the kind of a refusal, so it copies Box's private lists:

  • env. A caller that writes [agent] env, [tool.<name>] env, or [mcp.<name>] env has no check at all. One refused name fails the whole load. The caller handles two refusals differently: it drops a name that Box sets itself (a proxy variable, a CA bundle variable, OTEL_EXPORTER_OTLP_*, PWD, USER), and it skips the server that needs a name that makes a runtime load code (LD_*, DYLD_*, PYTHON*, NODE_OPTIONS, and others). To tell the two apart, it must copy reserved_workload_environment.
  • Read grants. check_read_grant (feat(box): expose the interpreter alias names and a read-grant check to library callers #242) returns GrantRefused, which carries only a message. A caller drops a grant on a system tree such as /usr or /opt/homebrew, and it skips what needed any other refused grant. To tell the two apart, it must copy the rows of forbidden-paths.json.

A copy can differ from Box, and then the caller writes a box.toml that Box refuses at load.

Solution

Each refusal now states its kind. The kinds are the split that Box Core already has internally. This change adds no new rule.

1. strands_box::check_workload_env(name: &str, value: &str) -> Result<(), EnvRefused>. EnvRefused::kind() returns EnvRefusal (#[non_exhaustive]):

Kind Why it exists
InvalidName valid_environment_name refuses the name.
SetByBox Box composes the name itself (proxy routing, CA trust, the OTLP exporter, PWD, USER).
LoadsCode The name is in a family that can make a runtime load code before the workload's first line (LD_*, DYLD_*, PYTHON*, and exact names). Box refuses the whole family, so a member that loads no code, such as PYTHONUNBUFFERED, is also LoadsCode. reserved_workload_environment already checked these first, and its comment called them "a different kind of refusal".
NotAbsolute HOME or TMPDIR holds a relative path.

The check takes the value, because ProcessSpec::validate refuses a relative env.HOME or env.TMPDIR at load. A check of the name only would pass an entry that the load then refuses, and that is the failure this check removes.

ProcessSpec::validate and the new check call the same functions (env::refused_name, env::refused_value, env::refusal_reason). The refusal text and the order of the checks at load do not change. reserved_workload_environment keeps its name and its result. The check does not cover a credential env:// name, which Box also refuses for HOME and PATH.

2. GrantRefused::kind() -> GrantRefusal (#[non_exhaustive]): NotAbsolute, Absent, Link, NotCanonical, Forbidden(ForbiddenClass), and Other. These are the steps of entry_grant, in its order. Absent is only a path that does not exist (NotFound, or NotADirectory when a parent is a file). Any other read error, such as PermissionDenied, is Other with the message "cannot read : ". Forbidden carries the class of the floor row that refused the path, such as SystemTree for /usr. Other is every other refusal, for example a path that encloses a credential store without naming it. / is Other for that reason: the credential-store check in validate_grant runs before the rows. Ok(true) does not change. The Display text changes only for a read error that is not absence: it was "is not there: Permission denied", and it is now "cannot read".

3. containment exports ForbiddenClass, and ContainmentError::forbidden_class() -> Option<ForbiddenClass>. The class is what each row of forbidden-paths.json already carries (class). GrantTooBroad keeps its fields. forbidden_class maps its reason back to the class, because each class has one constant reason. A GrantTooBroad that box builds for its own state maps to None. I did not add a field to GrantTooBroad, because a new field breaks each caller that builds or matches the variant by its fields.

Interface change. This is additive. It adds to the strands_box library: check_workload_env, EnvRefused, EnvRefusal, GrantRefused::kind, GrantRefusal, and the re-export ForbiddenClass. It adds to the containment crate root: ForbiddenClass and ContainmentError::forbidden_class. Nothing breaks. No CLI verb, box.toml key, or refusal text changes. The operator approved the strands_box additions before the code was written. The containment export is what carries the class to strands_box, and it needs maintainer approval as a change to that crate's pub use set.

Load-bearing choices.

  • ForbiddenClass keeps its Deserialize derive, because the row loader reads it from forbidden-paths.json. Its snake_case names are then a public form too. They are the names in that file.
  • A LoadsCode refusal keeps the load's text, "is set by the box itself". This change does not change a message an operator reads. The kind now states the difference.
  • forbidden_class reads the class back from the reason text. every_class_is_listed_once_and_states_its_own_reason fails when a new class is missing from ForbiddenClass::ALL (an exhaustive match) or when two classes share one text.
  • A read grant never reports Forbidden(WholeFilesystem) or Forbidden(CredentialStore). The credential-store check in validate_grant runs first, so those paths are Other. A caller that needs to know "encloses a credential store" needs a typed signal from containment. I did not add one.

Deferred. crates/box/AGENTS.md still states that the crate has no lib.rs, as #242 noted. That section needs a rewrite of its own.

Docs. docs/design/containment.md states both kinds beside check_read_grant. crates/containment/README.md lists the new export, and its export count now names all 19 names (the old count of 13 was stale). crates/containment/AGENTS.md names forbidden_class.

Related Issues

None.

Type of Change

New feature

Testing

  • tests/library_env_check.rs: each kind for its names (a_name_the_box_sets_itself_is_refused_as_set_by_the_box, a_name_that_makes_a_runtime_load_code_is_refused_as_loading_code, a_name_that_is_not_a_variable_name_is_refused_as_invalid, a_relative_home_or_tmpdir_is_refused_and_a_relative_path_is_not, a_member_of_a_loader_family_that_loads_no_code_is_refused_as_loading_code for PYTHONUNBUFFERED, PYTHONIOENCODING, and LD_BIND_NOW), and ordinary names pass. the_check_and_a_box_load_refuse_the_same_entries_with_the_same_reason writes each entry into [agent] env and a stdio [mcp.<name>] env, loads it through check_policy, and asserts that the load refuses each refused entry with the check's reason and accepts each accepted entry.

  • record::config::tests::a_loader_hook_is_refused_as_loading_code_and_a_box_name_as_set_by_the_box: every entry of the private LOADER_HOOKS and LOADER_HOOK_PREFIXES is LoadsCode, so a hook added there cannot be reported as another kind.

  • tests/library_grant_check.rs: /usr and /usr/bin are Forbidden(SystemTree); on macOS, /private/etc/sudoers, /private/var/db/dslocal, and /Library/Keychains are Forbidden with their own classes; / is Other; a relative path is NotAbsolute; the absent, link, and dotted cases now also assert Absent, Link, and NotCanonical. a_read_grant_through_a_file_is_refused_as_absent pins NotADirectory as Absent. a_read_grant_under_an_unreadable_directory_is_not_refused_as_absent pins a 0o000 parent as Other with no "is not there" text; it fails before this change, and it skips as root.

  • tests/library_grant_check_credential_store.rs: a home that encloses ~/.ssh is Other.

  • containment error::tests::a_floor_refusal_names_its_class_and_any_other_refusal_names_none: each class maps back from its reason, and another reason maps to None. every_class_is_listed_once_and_states_its_own_reason guards the mapping.

  • On Linux, the_password_database_is_refused_as_a_floor_row_of_its_own_class checks /etc/shadow when the host has it.

  • just check (fmt check, clippy, and cargo test --workspace --all-features) on macOS: 4389 passed, 0 failed, 16 ignored.

  • Not run: Linux. The Linux-only test runs in CI.

  • Reviewed by the repository's feature-critic checklist in a fresh context. It found no blocker. I fixed its findings on the docs, the guard on forbidden_class, two stale comments, and the Linux case. The items above under Load-bearing choices and Deferred are the ones I kept.

  • I ran the relevant suites (just check, or cargo test --workspace --all-features)

  • If I touched Rust, I ran cargo fmt --all and cargo clippy --workspace --all-targets --all-features -- -D warnings

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • My changes generate no new warnings

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

… to library callers

## Problem

A library caller that writes `box.toml` must know, before it writes, which entries Box refuses, and the kind of each refusal. It drops an `env` name that Box sets itself, and it skips a server that needs a name that makes a runtime load code. It drops a read grant on a system tree, and it skips what needed any other refused grant. No library call reports an `env` refusal, and `GrantRefused` carries only a message. So the caller copies `reserved_workload_environment` and the rows of `forbidden-paths.json`, and a copy can differ from Box.

## Solution

- Add `strands_box::check_workload_env(name, value) -> Result<(), EnvRefused>`. `EnvRefused::kind()` returns `EnvRefusal` (`InvalidName`, `SetByBox`, `LoadsCode`, `NotAbsolute`). `ProcessSpec::validate` and the check call the same functions in `record::config::env`. The refusal text and order at load do not change.
- Add `GrantRefused::kind()`, which returns `GrantRefusal` (`NotAbsolute`, `Absent`, `Link`, `NotCanonical`, `Forbidden(ForbiddenClass)`, `Other`). `entry_grant` tags each refusal with its kind.
- `containment` exports `ForbiddenClass` and `ContainmentError::forbidden_class()`, which reads the class of a floor refusal back from `GrantTooBroad`'s reason. `GrantTooBroad` keeps its fields.

These are additive interface changes. Both enums are `#[non_exhaustive]`.

## Tests

- `tests/library_env_check.rs` pins each env kind, and `the_check_and_a_box_load_refuse_the_same_entries_with_the_same_reason` pins that a load through `check_policy` refuses the same entries with the same text, in `[agent]` and in a stdio `[mcp.<name>]`.
- `a_loader_hook_is_refused_as_loading_code_and_a_box_name_as_set_by_the_box` covers every private loader-hook entry.
- `tests/library_grant_check.rs` and `tests/library_grant_check_credential_store.rs` pin each grant kind.
- `containment` `error::tests` pin `forbidden_class` and its class list.
- `just check` passed on macOS. Linux was not run locally.
@notowen333
notowen333 marked this pull request as ready for review November 2, 2026 22:25
Comment thread crates/box/src/record/config/env.rs Resolved
Comment thread crates/box/src/lib.rs Resolved
@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

Assessment: Comment

The shared refused_name / refused_value / refusal_reason path keeps the load and the check in agreement, and the test for that agreement is good. The gaps are on the new typed kinds: two kinds claim a fact that is not true for common inputs, and I proved both by running probes at 5e919b6.

Review Categories
  • Correctness of the kinds: LoadsCode covers prefix families that include names which load no code. Absent covers I/O errors on paths that exist. In both cases the caller is told to skip when the true answer is "drop" or "floor class X".
  • Interface (frozen surfaces, root AGENTS.md): The PR states that the change to the containment pub use set needs maintainer approval. That approval should be recorded before merge. The PR currently carries no api/needs-review label.
Assumptions and rules checked that held
  • The load's refusal text and check order are unchanged: name checks over all names, then value checks.
  • reserved_workload_environment keeps its name and result (frozen surface).
  • expand_home has exactly one error, so NotAbsolute is exact.
  • forbidden_class text mapping: class reasons are distinct, and Box's own GrantTooBroad reasons collide with none of them.
  • On Linux, /opt and /usr/local report Forbidden(SystemTree), /etc/shadow reports Forbidden(PasswordDatabase), and /home reports Other, as the docs state.
  • The HOME-in-or-above-box_dir exclusion is documented on check_workload_env.

The env/grant split is well scoped and additive.

@github-actions

github-actions Bot commented Nov 3, 2026

Copy link
Copy Markdown

Assessment: Approve (pending the containment interface approval)

85ccfbd fixes all three findings from the review of 5e919b6, and it adds no new defects. library_grant_check and library_env_check pass on Linux as a non-root user (17/17), so the unreadable-directory case really ran.

Remaining before merge
  • Interface (root AGENTS.md, frozen surfaces): The new ForbiddenClass / forbidden_class exports change containment's pub use set, which is a frozen surface. Maintainer approval for that should still be recorded on the PR.
  • PR description: Update the "No … refusal text changes" claim. A grant under an unreadable directory now loads with "cannot read …" instead of "is not there".
Checked in this round, and held
  • The new Absent arm: an absent leaf returns Absent. A path through a file returns Absent (NotADirectory). EACCES, ELOOP, and ENAMETOOLONG return Other. Each one is correct for its case.
  • std::io::ErrorKind::NotADirectory is stable on the pinned 1.98.1 toolchain.
  • The 0o000 test restores permissions before its assertion and skips itself as root.

Comment thread docs/design/containment.md Resolved
@notowen333
notowen333 merged commit 5f0428c into strands-agents:main Nov 3, 2026
16 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants