Commit Graph

13 Commits

Author SHA1 Message Date
Maciej Lisiewski
216bd9b6e6 fix(approval): keep heredoc bodies on the hardline and deny floors
Row 18 of the review (with @teknium1's analysis): the floors restored the
raw command only when DownloadThenRunMatcher recognized a run of the file
a `cat` heredoc wrote. What can execute that file has no end (`mv` then
run, a variable, find -exec, xargs, make, npm scripts, crontab, git hooks,
a bin dir, rc files), so each unrecognized run dropped the body from the
floors and `approvals.mode: off` approved it.

The hardline and deny floors now always scan the raw command alongside
the body-dropped copies, as main does, so they are never looser than main.
The dropped copies still expose a command after the terminator that a
body quote hid (#140704), so those rows stay stricter than main. Only the
dangerous-prompt layer drops a `cat` body as data (a miss there costs a
prompt, not a block), and it keeps the run tracking so a written script
that the same string runs still prompts.

Trade-off, as on main: a data-only heredoc that names a hardline or denied
command hits the floor again. Tests that pinned "quiet on the deny list"
now pin "the doc adds no prompt", and a new test pins the floors for
unrecognized runs.

Co-authored-by: Bear Huddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-15 22:23:01 +01:00
Maciej Lisiewski
769999bc12 fix(approval): keep a written script on the floors when a command string or PowerShell runs it
Rows 16 and 17 of the review: two spelling families that run the file a
heredoc just wrote never reached the run or read-run passes, so main's
hardline and deny floors were lost on them.

- A command string handed to a shell, pwsh/powershell, cmd or su
  (`bash -c 'bash job.sh'`, `sudo sh -c "sh job.sh"`, `su -c '…' root`,
  `cmd /c "call j.bat"`) is a command line of its own. Both run regexes
  now accept that carrier prefix, and `call` counts as a run keyword.
- PowerShell runs a file's content through Invoke-Expression or a script
  block (`iex (gc j.ps1 -Raw)`, `Get-Content j.ps1 | iex`,
  `[scriptblock]::Create((gc j.ps1 -Raw))`), or dot-sources/calls it in a
  -Command string. A new _PS_READ_RUN_RE pass catches these when FILE was
  written earlier in the same string.

Patch from the review applied unchanged.

Co-authored-by: Bear Huddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-15 22:07:17 +01:00
Maciej Lisiewski
3d2ad3cd9e refactor(approval): take the pipe-wrapper regexes from approval_pipe_launchers
#155452 moved _PIPE_PATH_PREFIX_RE and _PIPE_WRAPPERS_RE into
tools/approval_pipe_launchers.py. This branch had defined the same two
names, with identical values, in approval_fetch_exec.py, so after the
rebase approval_detection.py imported them twice (F811). Import them from
approval_pipe_launchers in both modules and drop the duplicate
definitions. No behaviour change: the compiled patterns are the same
objects.
2026-11-15 22:07:17 +01:00
Maciej Lisiewski
fa0eca7f1b fix(approval): keep a written script on the floors when its content or options run it
A `cat > f <<'EOF'` body dropped from the floors' scan copies when the same
command ran the file's content (`eval "$(cat f)"`, `cat f | bash`,
`bash <(cat f)`, `sh -c "$(< f)"`), passed runner option values before the
script (`bash -euo pipefail f`, `bash -o pipefail f`, `python3 -W ignore f`),
or ran it through pwsh/powershell/cmd. main scans those bodies; the
download-then-run matcher now recognises the content-run forms and parses
each runner's option values, keeping syntax checks and piped programs with
their own script operand quiet.

Refs #108235

Co-authored-by: Bear Huddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-15 22:07:16 +01:00
Maciej Lisiewski
2c27c4d50b fix(approval): floor a written script run by a quoted or .exe runner
After a same-string heredoc write, `"/bin/bash" job.sh`, `sudo 'bash'
job.sh` and `C:\Git\bin\bash.exe job.sh` did not count as running the
file, so its body left the hardline floor (main floors all three). Accept
the pipe sink's token shape (optional quotes, `.exe`) around the runner.
A path-qualified program word that is itself the download counts as a
run too, which restores `./node --check app.js` / `./python3 s.py` after
downloading node/python3 and keeps `./node.exe --version` flagged; bare
names resolve through PATH and stay quiet.

Refs #108235

Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-15 22:07:16 +01:00
Maciej Lisiewski
13e2ada325 fix(approval): read write-then-run runs with the pipe sink's launcher grammar
A written cat script run through a path-qualified shell, a launcher with
valued options (sudo -u root, timeout 60), a VAR= prefix, a compound
keyword, ( / { / | starts or `shell < FILE` lost main's hardline and deny
floors. The run matcher now reuses the pipe sink's launcher grammar
with whitespace kept on one line, and `command -v` becomes a post-filter
on the matched span. The launcher table stays in approval_pipe_launchers;
_pipe_launcher_re gains a `ws` parameter defaulting to `\s`, so the pipe
sink's pattern is unchanged. _PIPE_PATH_PREFIX_RE and _PIPE_WRAPPERS_RE
move here from approval_detection.

Refs #140704

Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-15 22:07:16 +01:00
Kevin Rajan
06446c2970 fix(approval): retain cat heredoc floors for absolute-path shell runs
Review follow-up to NousResearch/hermes-agent#155467 (c2h5oh).
A /bin/bash invocation executes the just-written script, so scan its
heredoc body through the existing hardline and deny gates.

(cherry picked from commit afa410228d4475ffcf0db671301e4f24abca5c33)
2026-11-15 22:07:16 +01:00
Maciej Lisiewski
71d5908b8e fix(approval): keep a trailing check flag from taking the next line's run
The flag group in the write-then-run matcher allowed \s, so `bash -n` at the
end of a line took the next line's `./i.sh` as its operand; the run was then
skipped as a syntax check and a heredoc body it wrote fell off the floors.
Flag whitespace now stays on one line. Also pin that every flag must be the
tool's check flag (bash -n -e, node --inspect, php -f stay flagged).

Refs #108235

Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-15 22:07:16 +01:00
Maciej Lisiewski
db32d71363 fix(approval): command -v lookups and syntax checks after a download are not a run
`curl -o f URL && command -v f` and `... && bash -n f` / `node --check f` /
`ruby -c f` / `php -l f` name the downloaded file without executing it.
The skip applies only when every flag is the check flag; `perl -c` stays
flagged because it runs BEGIN/use blocks.

Refs #108235

Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-15 22:07:16 +01:00
Maciej Lisiewski
40e9a7726a refactor(approval): move heredoc scan sources next to the write-then-run matcher
Keeps tools/approval_detection.py at its 2,000-line cap after the rebase onto main.

Refs #108235
2026-11-15 22:07:16 +01:00
Maciej Lisiewski
441477ee40 fix(approval): match curl -O names and quoted/launcher/cd run spellings in write-then-run
curl -O/--remote-name saves under the URL basename; written files may be quoted (> 'j'), appended (>>), run behind sudo/env/nohup/exec with VAR= words, quoted, after a cd (basename on both sides, over-matching on purpose), or by a script interpreter. Timing bound raised to the 2 s tests/AGENTS.md floor with curl -O stress inputs; every new repeat is possessive.

Refs #108235
2026-11-15 22:07:15 +01:00
Maciej Lisiewski
56621f7ff3 fix(approval): keep a cat heredoc body on the floors when the same string runs its file
cat <<'EOF' > job.sh ... bash job.sh lost its body from the hardline, dangerous and deny scans. Scan the raw command too whenever a dropped body's file is executed later in the string, in both _command_detection_variants and the deny projections.

Refs #108235

Co-authored-by: BearHuddleston <196404565+BearHuddleston@users.noreply.github.com>
2026-11-15 22:07:15 +01:00
Maciej Lisiewski
a09db3ddb9 fix(approval): drop only provably inert heredoc bodies; linear download-then-run match
The #140708 masking blanked every heredoc body whatever its consumer, and to end-of-string on a
`<<` that is not a heredoc, so `cat <<EOF | sh`, `sh -s`/`bash`/`ssh host <<EOF` bodies and
`echo $((1<<2))\n<cmd>` fell off the hardline floor. Masking now reuses the
strip_inert_heredoc_bodies rule (quoted delimiter, exact terminator, allowlisted consumer, no
pipe/list after the heredoc, nothing unknown or unterminated anywhere): `cat` bodies are dropped
outright; python/osascript bodies are code, so their dropped copy is scanned in addition to the raw
one. Bodies are removed rather than blanked, since long whitespace runs made the `\s*`
command-position prefixes backtrack (300-line `cat <<'EOF'`: 108 s -> 0.16 s).

The download-then-run regex retried a lazy `[^\n]*?(?P=dl)` from every fetch (64 KB of
`curl -o f`: 12.8 s for the rule). It is now a two-step matcher in tools/approval_fetch_exec.py:
collect each fetch segment's -o/-O/--output/> targets, then check later command starts against
them (rule alone < 0.05 s).

Fixes #140704
Refs #108235 #140708
2026-11-15 22:07:15 +01:00