Skip to content

fix(security): under an agent session a flag or TOLE_TRUST may only narrow the owner's policy lists (#865) - #871

Merged
ajianaz merged 3 commits into
developfrom
fix/865-narrow-only-under-session
Nov 16, 2026
Merged

ajianaz merged 3 commits into
developfrom
fix/865-narrow-only-under-session

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What

Option B2 of #865 (chosen by the owner; B as first written was withdrawn, see Why). A process that carries TOLE_UNDER_SESSION and is not serve, acp or mcp is refused at start-up when its flags or TOLE_TRUST would loosen a policy list the USER config sets. Closes #865.

Why

The resolved lists follow flag > env > config and a higher layer replaces the whole list below it. So a script that run_command or job_start ran could run tole run --allow '*', --trust workspace, TOLE_TRUST=workspace or --on-turnend x.sh and replace the owner's allow, trust or hook lists (dropping the stop gate and the deny patterns). Item 3 of the not-fixed list of #858.

The first design (compare every session process with the user config) was reported and withdrawn because it breaks legitimate flows, verified in the code: an agent_start child gets --allow patterns that are glob-covered by the PARENT's resolved allow list (children/agents.rs, list_covers(parent_allows), every parent deny inherited), which can be wider than the user config after an owner's --allow; a delegated child takes allow and trust from its flags alone (config/apply.rs, the delegated branch); a trusted project config replaces the user's hook lists by design (config::layered); and an owner's TOLE_TRUST is inherited by children. B2 therefore exempts tole-started children and does not look at the project layer.

How the child exemption is done (corrected after Cora's review of the first head): TOLE_UNDER_SESSION is set by session_guard::mark, whose only callers are run_command and job_start, and by nothing else. A child tole itself starts (agent_start, delegation) therefore does not carry it of its own and needs no special case. The first head exempted any process with a child variable (TOLE_AGENT_DEPTH, ...), but those are inherited by every process a child's own run_command starts, so a script at depth 1 carried both and escaped; the child variables are not an exemption any more.

Known limits, stated plainly (also in the module doc, Context, design note §6.2, the threat-model row and the CHANGELOG):

  • A child INHERITS the marker when its PARENT is itself marked: agent_start and delegation scrub secrets only (children/agents.rs, around line 878), so a marked process's children carry TOLE_UNDER_SESSION. A child in a chain that already contains a script-started tole is therefore guarded too, and if its flags are glob-covered by the parent but not exact entries of the user config (a script runs tole run --allow 'read_*' and its model calls agent_start with allow=["read_file"]) it is refused. That fails safe and only happens in chains that already contain a marked process. No new marker is added.
  • Not covered: --profile (a profile's allow replaces the allow list when no --allow flag is given, and a user config that sets a key only under a profile is unconstrained here), --yes (auto-approve), the MCP server flags and --workspace. Profiles live in the user config and are an owner choice; the rest are separate surfaces.

Changes

  • New tole-cli/src/narrow_guard.rs (next to no_config_guard; tole-core is untouched): Context::from_env, Inputs::from_flags, and one check(user, inputs, ctx). Applies only when TOLE_UNDER_SESSION is set (non-blank) and the subcommand is not serve, acp or mcp (sandbox_flag_class(..).0). For keys the USER config actually sets: allow entries must be exact entries of the user's list, and every user deny pattern (!x) must be kept when --allow is given (a flag may add its own deny); trust presets (the flag, else TOLE_TRUST: the effective input) exact entries of the user's; on_pretool, on_posttool, on_turnend must contain every user hook (adding is fine). plan_mode has no off switch on the command line or in the environment (checked by a unit test through Settings::resolve), so nothing needs refusing. The refusal names the key, the user-config value, what is extra or dropped, and the way out ("change the user config yourself, or run this from a terminal (issue security: flags, env and --config replace the owner's policy lists instead of merging or being refused #865)").
  • main.rs: one call before Settings::resolve, with the raw user config (cloned before the layered one is built), the flags and the environment; a refusal is a startup error.
  • lib.rs, tests/layering.rs: the module registered as a face module like no_config_guard.
  • Not inputs (by design): the project config and --config (owner-trust-gated by tole config trust), --profile.
  • Docs: design note §6.2 (the rule, who it applies to, why children are exempt, why the child variables are not, the inherited-marker case, what is not covered), threat-model row, CHANGELOG (Fixed). All three say plainly that it is keyed on the same environment marker as security: the session marker is an environment variable; a child can clear or forge it #867 and is NOT a boundary: a copied binary run with a cleared environment, a deliberate pty, or a forged marker is not stopped; the boundary is the command sandbox ([sandbox.consumers] run_command = "required" masks the data dir).

Delegated child verification, as asked: the child's scope is derived from the parent's, not from the user config. agent_start computes the child's --allow as the parent's own resolved list or the requested patterns each covered by it (children/agents.rs around lines 745-775, list_covers), delegate.rs builds the delegated child with with_parent_allows(spec.allow), and the child resolves allow and trust from those flags only. That is why narrowing against the user config would refuse a legitimate child, and why this PR exempts it (it lacks the marker of its own, unless its parent was marked): the bound is already enforced in the parent, before spawn.

Callers and impact (grep, each checked): narrow_guard::check has one caller (main.rs, startup); session_guard::mark has two callers (run_command.rs, jobs.rs), which is what makes the marker precise; Settings::resolve and the flag/env resolution are unchanged; config_flags(&cli) is only read; the no_config_guard and sandbox_flags guards are untouched. Behaviour change: a script started by run_command or job_start (at any depth) that runs tole with a wider --allow, --trust, TOLE_TRUST or fewer hooks than a key the user config sets is now refused; nothing changes for an owner at a terminal, for children tole starts from an unmarked parent, for serve/acp/mcp, or when the user config does not set the key. Tests to rerun: narrow_guard_e2e, no_config_guard_e2e, config_apply, config_check, sandbox_flags_cli, delegated_approval_e2e, serve_delegation_e2e, owner_only_e2e, layering.

Testing

  • Fail without the fix: narrow_guard_e2e (real binary, scrubbed environment, no provider, TOLE_UNDER_SESSION=1) with the guard neutered by hand (the check given no user config): the three refusal tests FAILED (wider --allow, a dropped deny, --trust and TOLE_TRUST wider, fewer hooks on --on-pretool and a replaced --on-turnend); restored, they pass. The depth-1 script test (a_descendant_script_of_a_child_agent_is_guarded_despite_the_inherited_child_variables) fails on the first head by construction (it exempted any child variable) and passes now.
  • Pass cases: equal or narrower allow (keeping the deny and adding one), equal trust, more hooks, nothing given, a user config that does not set the keys, no user config at all. Exemptions: an attended owner (no marker) with a wider --allow; a process with only child variables (TOLE_AGENT_DEPTH, TOLE_AGENT_ID, TOLE_DELEGATION_NODE, TOLE_DELEGATION_LOG: what a child tole started by an unmarked parent carries) with a wider --allow; tole acp --allow under the session marker. Refused: the same child variables TOGETHER with TOLE_UNDER_SESSION (a script a child's run_command started, or a child of a marked parent). Unit tests (narrow_guard.rs): the marker read from the environment (a blank one is none, child variables alone are not the marker, both together are guarded), the exact-subset rules (a narrower-looking glob that is not listed is refused, an empty user list refuses any entry), flag-first-then-environment for trust, hook superset, and plan_mode having no off switch.
  • The e2e file is gated on shell-tools (the hook keys need it: without it the user config is refused earlier by the unsupported-key check); the unit tests run in every profile.
  • Gates on the code head 3e32661 (the last commit, a71f7e5, only changes comments, the design note, the threat model and the CHANGELOG): 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, with TOLE_REQUIRE_BWRAP=1 and --no-fail-fast: green. After the docs commit: fmt check and a clippy of tole-cli with all features. 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; Cora found the inherited-variable exemption (fixed in 3e32661) and the independent review found the two doc inaccuracies this head corrects. The guard reads the raw USER config (not the layered one, which cannot tell the lists apart); only exact text matches count, so no glob reasoning is trusted; every refusal path returns before any config is applied or any provider is touched; the flag and environment effective input follows the same precedence as Settings::resolve (trust: flag else env). The documented limit is the marker (security: the session marker is an environment variable; a child can clear or forge it #867). An independent reviewer reviewed the code head.

…arrow the owner's policy lists (#865)

A process with TOLE_UNDER_SESSION and no child marker, off serve, acp and mcp,
is refused when its --allow, --trust, TOLE_TRUST or hook flags would loosen a
key the user config sets: allow and trust entries must be exact entries of the
user's (user denies kept), hook lists must keep every user hook. Children tole
itself starts, the project config and --config are not inputs. Same environment
marker as #867: not a boundary.
Comment thread crates/tole-cli/src/narrow_guard.rs Outdated
let set = |k: &str| env(k).is_some_and(|v| !v.trim().is_empty());
Context {
session_marker: set(child_env::UNDER_SESSION),
child_marker: [
@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

…rrow-only guard (#865)

TOLE_UNDER_SESSION is set only by run_command and job_start, so a child tole
itself starts does not carry it and needs no exemption, while a script a
child agent's run_command started carries it together with the inherited
TOLE_AGENT_DEPTH and is guarded.
@ajianaz
ajianaz merged commit 5e3cbe5 into develop Nov 16, 2026
23 checks passed
@ajianaz
ajianaz deleted the fix/865-narrow-only-under-session 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: flags, env and --config replace the owner's policy lists instead of merging or being refused

3 participants