Skip to content

fix(command-risk): see through wrappers and program paths, judge .. as the parent - #480

Merged
mrnasil merged 2 commits into
mainfrom
fix/command-risk-wrappers
Nov 25, 2026
Merged

mrnasil merged 2 commits into
mainfrom
fix/command-risk-wrappers

Conversation

@mrnasil

@mrnasil mrnasil commented Nov 25, 2026

Copy link
Copy Markdown
Member

Problem

The shell risk gate (whycodes-command-risk) let destructive commands through as Safe:

command before now
/bin/rm -rf ~, C:/tools/rm.exe -rf ~ Safe (command /bin/rm matched nothing) Catastrophic
timeout 5 rm -rf /, nice rm -rf ~, env -i rm -rf ~, xargs rm -rf ~, nohup …, stdbuf …, exec … Safe (judged as the wrapper) Catastrophic
sudo -u root rm -rf / Caution (-u taken as the command, then the sudo bump) Catastrophic
rm -rf .., rm -rf foo/../.. Caution, in-project (.. stripped as a "glob tail") Destructive (outside)
rm -rf /home/.. judged as /home Catastrophic (/)
rm -rf /home, /c, /c/Users, /C/Windows (Git Bash spelling of C:) Destructive Catastrophic

With the default threshold (Destructive), everything in the Safe/Caution rows ran without a prompt, and the "always refused" tier never fired.

Fix

  • Segment::command_index skips assignments, shell keywords and a table of wrappers — sudo, doas, env, command, builtin, exec, nohup, time, nice, ionice, stdbuf, timeout, xargs — with their options, the values of options that take one (sudo -u root, nice -n 10, xargs -I {}), --, and leading operands (timeout's duration). Wrappers nest (nice timeout 5 sudo -u root rm).
  • The command is matched by file name (/bin/rm, rm.exe → rm); args() is the words after the command index (it used to search for the first word equal to the command text).
  • Segment::elevated replaces the "any word is sudo" check: a sudo/doas wrapper by name (/usr/bin/sudo counts, an argument named sudo does not).
  • strip_glob_tail only strips a tail containing * or ?; .. resolves lexically to the parent.
  • /home joins the protected roots; msys_drive maps /c[/rest] to c:[/rest] for the Windows protected list and the bare-drive check.

Tests

New in assess.rs: wrappers and program paths (18 forms, incl. nested and --), the sudo bump through a wrapper, .. / ../ / foo/../.. / /home/.., /home and MSYS drive roots (and that /c/Users/me/tmp and /cache are not). All 115 command-risk tests, agent gate/risk tests, clippy, budgets; command-risk line coverage measured locally (only the 2 pre-existing misses).

🤖 Generated with Claude Code

mrnasil and others added 2 commits November 25, 2026 15:58
… as the parent

The risk gate matched the command word exactly against `rm`, `find`, …
and skipped only `sudo`, `command` and `env`, so `/bin/rm -rf ~`,
`timeout 5 rm -rf /`, `nice rm -rf ~`, `xargs rm -rf ~` passed as safe,
and `sudo -u root rm -rf /` took `-u` for the command. A trailing `..`
was stripped as a "glob", so `rm -rf ..` was judged in-project.

- The command is the first word past assignments, keywords and known
  wrappers (sudo, doas, env, command, builtin, exec, nohup, time, nice,
  ionice, stdbuf, timeout, xargs) with their options, option values and
  leading operands; it is matched by file name (`/bin/rm`, `rm.exe`).
- The sudo bump comes from a sudo/doas wrapper by name (`/usr/bin/sudo`).
- Only a tail with `*` or `?` is a glob; `..` resolves to the parent.
- `/home` is protected, and MSYS drive paths (`/c`, `/c/Users`) are
  compared as `c:`, `c:/users`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@mrnasil
mrnasil merged commit 5e91b17 into main Nov 25, 2026
7 checks passed
@mrnasil
mrnasil deleted the fix/command-risk-wrappers branch November 25, 2026 13:21
@mrnasil mrnasil mentioned this pull request Nov 26, 2026
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.

1 participant