Repository navigation
test(rules): verify deny and ask prefixes survive guard transformations - #100
Conversation
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
|
||
| @PROPERTY | ||
| @given(data=st.data()) | ||
| def test_a_deny_prefix_is_denied_in_every_supported_spelling(data: st.DataObject) -> None: |
There was a problem hiding this comment.
P2: max_examples=150 で 125 件の deny prefix から sampled_from で引くと,期待値でも約 70%(≈87 件)しか一度も引かれず,derandomize=True のため残り約 38 件は毎回同じく未検査のまま固定される(shrink は先頭要素に寄るので実際にはさらに偏る).ask 側(48 件)も同様に一部の prefix は変形を伴って検査されない.このため「全 deny prefix が全変形で deny」という性質は,一部の prefix で回帰しても緑のまま通りうる.prefix を pytest.mark.parametrize で全件回し,変形だけを Hypothesis で引く(prefix ごとに数例)形にすれば,全件を確実に検査できる.
There was a problem hiding this comment.
同じ指摘(全 prefix の列挙)への対応は 6bc2aa0 に入れました.ask 側も 48 件を parametrize で全件回しています.
| "pytest-cov==7.1.0", | ||
| "pytest-mock==3.15.1", | ||
| "pytest-xdist==3.8.0", | ||
| "z3-solver>=5.3.3.0", |
There was a problem hiding this comment.
P3: z3-solver はこの PR のどこからも import されていない(P2・P3 用の先取り).約 30〜50MB の wheel が test グループの全インストールに加わり,deptry の設定次第では未使用依存として検出されうる.実際に使う PR で追加する方が安全.あわせて,他の test 依存と同様に == 固定へ揃えることも検討を.
There was a problem hiding this comment.
反映せず,理由を残します.(1) z3-solver は #102 の tests/ere_to_z3.py が実際に import しており,#102 と #104 はこの PR の上に積んだ PR です.(2) ユーザーが uv add --group test hypothesis z3-solver を 1 回で実行した結果が uv.lock に入っているため,ここで外すには uv lock の再実行(ユーザー作業)が要ります.(3) audit ジョブ(deptry・vulture)はこの PR で pass しており,未使用依存の検出は出ていません.== 固定への変更も uv lock が要るため,ユーザー作業として報告します.
レビュー概要テストと依存の追加のみで,guard 本体は変更されていない.マージを止める問題(P0/P1)は見つからなかった.
補足:この環境ではテストを実行する権限がなかったため,guard_command.py との静的な照合によるレビューである. 🤖 Generated with Claude Code |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 3e4dff3ba4
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| DENY = [ | ||
| tuple(rule["prefix"]) | ||
| for rule in PREFIXES | ||
| if rule["decision"] == "deny" and rule["prefix"][0] not in SHELL_PROGRAMS |
There was a problem hiding this comment.
Exercise valid shell-prefix commands instead of excluding them
When a regression affects transformation handling for bash -c <script>, bash -lc <script>, sh -c <script>, or the corresponding zsh forms, this filter removes every relevant deny rule and the suite remains green. Although a bare two-word prefix is incomplete, these are prefix rules and can be tested with a harmless script argument; construct such a valid representative rather than excluding all five rules.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
修正しました(6bc2aa0).shell 系 5 件の除外をやめ,bash -c true のように無害な script を後ろに付けた代表形で検査します.この形は素の判定でも deny になることを確認しました.meta テストでも bash -c の deny prefix を消すと該当ケースが赤になります.
| prefix = data.draw(st.sampled_from(DENY)) | ||
| command, _ = data.draw(spellings(prefix)) |
There was a problem hiding this comment.
Enumerate every policy prefix before sampling spellings
When only one specific deny or ask prefix regresses, drawing the prefix from sampled_from does not guarantee that it is exercised: the deny population alone has 125 entries, while the entire property is limited to 150 examples that must also explore quoting and wrapper choices. The deterministic run can therefore stay green without checking the affected rule; parameterize over DENY and ASK, and use Hypothesis to generate transformations for each prefix.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
修正しました(6bc2aa0).deny 125 件と ask 48 件を parametrize で全件回し,Hypothesis は 1 prefix ごとに 12 例の変形だけを引く形にしました.1 つの prefix だけが壊れても,その prefix のテストが赤になります.meta テストも,prefix を 1 件だけ消したポリシーで該当ケースが赤になることを確認する形にしました.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6bc2aa0950
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| "pytest-cov==7.1.0", | ||
| "pytest-mock==3.15.1", | ||
| "pytest-xdist==3.8.0", | ||
| "z3-solver>=5.3.3.0", |
There was a problem hiding this comment.
Remove the unused z3-solver dependency
Because test is a default dependency group, every normal uv sync now installs a 32–49 MB platform-specific Z3 wheel even though a repository-wide search finds no import or use of Z3 outside the dependency metadata. This adds substantial setup time and storage—and may require a source build on unsupported platforms—without supporting any test in this commit; add it only when the planned Z3-based tests are introduced.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
反映せず,根拠を残します.hypothesis と z3-solver は,ユーザーが 1 回の uv add --group test hypothesis z3-solver で追加し,「依存の変更は最初にそれを使う性質のコミットに含める」と指示された内容です.z3-solver は #102(tests/ere_to_z3.py)と #104(tests/denial_difference.py)が実際に使っており,この PR の上に積んだ stacked PR です.外すには uv.lock の再生成(uv lock,ユーザー作業)が必要です.サイズと == 固定の件は報告に載せ,ユーザーが判断します.
Add the P4 check of #73 with Hypothesis. Every deny prefix must stay denied, and every ask prefix must still ask behind a wrapper, so long as the command is quoted, path-qualified, wrapped, or run through a shell -c script or the shell builtin, in any combination. Two meta tests drop the deny or ask prefixes from the policy and require the matching property to fail. Add hypothesis and z3-solver to the test group; z3-solver serves P2 and P3. Refs #73 Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
…n test Parametrize the P4 properties over all 125 deny and 48 ask prefixes instead of sampling one per example, so a regression in a single rule cannot hide behind the others. Test the five shell prefixes with a harmless script after them instead of excluding them, and run the meta tests against single removed prefixes. Refs #73 Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
6bc2aa0 to
a460a23
Compare
|
@codex review |
| suppress_health_check=[HealthCheck.function_scoped_fixture], | ||
| ) | ||
|
|
||
| WRAPPERS = ["nohup", "env A=1", "timeout 5", "stdbuf -o0", "setsid", "nice -n 5", "sudo", "time", "command"] |
There was a problem hiding this comment.
P2: the deny property passes trivially whenever a sudo or shell -c layer is drawn.
shell_syntax.parse returns the outer layer as its own segment (layers = [command, *unwrapped(command)], plus the nested parse of each -c script), and forbidden_objection checks each segment against the deny prefixes. sudo (command_permissions.json:2779) and bash -c / sh -c / zsh -c (2684–2716) are deny prefixes themselves, and canonical() turns /bin/bash into bash. So any spelling that includes sudo … or <shell> -c '…' is denied by the wrapper, whatever the inner prefix is.
one_of draws a shell layer about 1/3 of the time and sudo about 1/27 of the time, for up to 3 layers. A large share of the 12 examples per prefix is therefore vacuous. If the guard stops seeing through sudo, bash -c, or /bin/bash -c to the inner command, this property cannot catch it. The removed-rule tests still turn red only because some drawn example has no such layer.
Suggested fix: drop sudo from WRAPPERS and the shell layers from the deny strategy. Alternatively, assert that the deny reason names the target prefix (e.g. " ".join(prefix) in verdict.reason) instead of only checking decision == "deny".
Review summary1 finding, none blocking (0 × P0/P1).
Other checks: the ask property, the removed-rule tests ( |
|
Codex Review: Didn't find any major issues. Another round soon, please! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
| "pytest-cov==7.1.0", | ||
| "pytest-mock==3.15.1", | ||
| "pytest-xdist==3.8.0", | ||
| "z3-solver>=5.3.3.0", |
There was a problem hiding this comment.
P3 z3-solver isn't imported anywhere in this PR. The PR body says it's for P2/P3. But test is in default-groups, so every uv sync now downloads a 32–48 MB wheel (it's ~38 MB on manylinux x86_64) before anything uses it. Consider adding it in the PR that first imports it (#102/#104). Also, both new specifiers use >= while the other test deps are pinned with ==, which the PR already notes.
|
Review summary (read-only) I found no P0/P1/P2 issues.
One P3 inline: Blocking findings: 0 |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: a460a23e72
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| "tests/codex/test_execpolicy.py", | ||
| "tests/test_command_permission_sync.py", | ||
| "tests/test_prefix_rules.py", | ||
| "tests/hooks/test_guard_transformations.py", |
There was a problem hiding this comment.
Select transformation tests for parser changes
Register this test for changes to agents/hooks/lib/shell_syntax.py and agents/hooks/lib/command_policy.py, not only the top-level guard and policy JSON. The repository's targeted-test selector currently treats both implementation modules as covered by their existing explicit mappings, and I verified that changing either module does not select test_guard_transformations.py; consequently, a wrapper-parsing or prefix-matching regression can merge without running the new property tests.
Useful? React with 👍 / 👎.
…es with Z3 (#108) ## 概要 #73 の P2(allow prefix と禁止正規表現の重なりの列挙)を Z3 で追加する.`Refs #73`.#102 の引き継ぎ.#100 のマージで base ブランチが削除され,#102 が自動で close されたあと,force push 済みのため reopen できなかった.レビューの経緯(スレッドと返信)は #102 に残っている.#100 は main にマージ済みで,この PR の base は main.#104 がこの PR の上に積まれている. - `tests/ere_to_z3.py`: POSIX ERE から Z3 正規表現への変換器.規則が使う文法(`^ $ . [] [:class:] () | * + ? {m,n}` とエスケープ)だけを扱い,後方参照・先読み・`\w` などは `UnsupportedPattern` で拒否する. - `tests/rule_overlap.py`: allow prefix の語列(と,その後ろに空白と任意の文字列が続く形)と,禁止規則(例外を除く)の共通部分を Z3 で解き,witness を 1 行返す. - `tests/test_forbidden_overlap.py`: 現行規則の 20 件を「確認済み」として固定する.増えた重なりも,規則の削除で消えた重なりも赤になる. | 変換器の検査 | 内容 | | ------------- | ----------------------------------------------------------------------------------------------------------------------------- | | 文法ごとの例 | 受理 / 拒否する文字列 26 組 | | grep との差分 | 全 forbidden パターン × 大小文字の区別あり / なし で,ランダム文字列と,Z3 の witness を少し編集した文字列を `grep -E` と比較 | | 範囲外の文法 | 後方参照,`\w`,括弧の不整合などを拒否 | | witness | Z3 の witness を grep が受理する.充足不能な `a^b` などは witness なし | ## 判断 - 未アンカーのパターン(`(^|[[:space:]])git add`)は,どの prefix の後ろの自由な tail にも現れるため,全 278 prefix と重なり 1405 件のノイズになった.マッチが prefix の語の範囲内から始まるものだけを数える定義に絞り,20 件になった. - `agent-browser` の `file:` など,引数に現れる禁止形は tail から始まるため P2 の対象外.これらは P4 の変形検査と既存テストで見る. - 20 件はいずれも「provider は prefix を許可し,hook が狭い形を deny する」意図どおりの構成で,修正は不要と判断した(`git add` と `git add .`,`uv run ... mutmut apply` など).自動修正はしない. - `REVIEWED` リスト(禁止形 7 種と,それに重なる allow prefix)は,2026-11-09 にユーザーが確認し,すべて意図どおりであることを確認済み. - `.hypothesis/` を ls-lint の ignore に追加した(ローカルで Hypothesis を動かすと pre-commit の ls-lint が落ちるため.P4 のコミット時点では未生成で気づかなかった). ## 検証 - [x] 壊したルールの検査:allow prefix を広げる(`git` を追加),deny 規則を消すと,確認済みリストと一致せず赤になる 2 件 - [x] pytest 224 件 pass(関連テスト選択),ruff check / format,ty - 実行時間:20 件の列挙に約 5 秒 - `flake.nix`:Linux の devShell で `libstdc++` が見つからず `libz3.so` を読めなかったため,`LD_LIBRARY_PATH` を追加した(CI の `test` が落ちた原因). - 変換器は C ロケールの grep を模す.ロケール依存の構文(文字クラス,等価クラス,非 ASCII の大小畳み込みなど)は拒否する.`[[:space:]]` だけは ASCII の空白として扱う限界を docstring に記載した. 計画:#73 (comment) 未対応のレビュー指摘(`xargs` など wrapper の内側コマンドの重なり):#107 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: furedea <[email protected]> Co-authored-by: Claude Sonnet 5.5 <[email protected]>
…104) ## 概要 #73 の P3(生成された Claude / Codex / Devin のルールが元と同じ判定をすること)を追加する.`Refs #73`.#102(P2)に積んだ PR で,base は `test/73-forbidden-overlap`(#102 のマージ後に main へ付け替える).マージ順は #100 → #102 → #104. | 対象 | 手段 | 結果 | | ------------------------------------------------------------------ | --------------------------------------------------------------------------------------- | ------------------------------- | | Claude `Bash(prefix:*)`,Codex `prefix_rule`,Devin `Exec(prefix)` | 全 prefix・短い形・長い形・未登録コマンドを列挙し,最も厳しい判定が勝つモデルで元と比較 | 3 つとも差なし | | `generate-forbidden-commands` の正規表現規則 | 規則を位置で対にして,Z3 で「片方だけが deny する行」を探す | 6 規則中 4 規則で差あり(下記) | ## 発見:`generate-forbidden-commands` が `exceptions` と `programs` を落とす 固定している agent-harness 0.9.9 の出力では,次の差が出る.guard は両フィールドを読むため,この出力を `~/.claude/hooks/rules/forbidden_commands.json` に置くと判定が変わる(agent-harness の README にその置き場所の記載がある). | 規則 | Z3 の witness | 差 | | ---------------------------------- | ------------------------------------------------------------------- | ----------------------------------------------------------- | | 2(`herdr agent start ... --`) | `herdr agent start a --timeout 0 -- --model sonnet --effort medium` | 例外で許可される形まで deny する | | 3(cargo / pnpm / uv の in-place) | `uv ruN MUTMUT APPLY` | scope と大小文字の無視が消え,元が deny する行を allow する | | 4(agent-browser の `file:`) | `agent-browser filE:` | 同上 | | 5(agent-desktop のアプリ名) | `agent-desktop warP` | 同上 | - この 4 件は `xfail(strict=True)` にして,agent-harness が両フィールドを保持するようになったらマーカーを外す. - 追跡:furedea/agent-harness#203.xfail のマーカーとコメントから参照している.本 PR では guard や harness を変更していない. - #87 の抜け道が原因で落ちる性質は P3 にはなかった. ## 判断 - Devin の判定意味は未確認のため,最も厳しい判定が勝つと仮定して比較している.仮定はテストの docstring に書いた. - 規則全体の補集合を使う Z3 の問いは 13 パターンでは 30 秒で解けなかったため,生成規則と元規則を位置で対にした.generator は順序を保つ.規則の数が同じであることも検査する. - Claude の `Bash(dangerouslyDisableSandbox:true)` は prefix ではないため比較から外した. ## 検証 - [x] 壊した生成規則(allow の追加,deny の欠落,判定の書き換え,長い形だけの欠落)で gap が出る単体テスト - [x] 未知の形の項目は読み飛ばさず `ValueError` - [x] pytest 37 件(33 pass,4 xfail),ruff check / format,ty - 実行時間:Z3 の比較で約 20 秒(規則 5 の大文字小文字を無視する比較が支配的) 計画:#73 (comment) 未対応のレビュー指摘(xfail の `raises=`,裸の `Bash` エントリの拒否):#107 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: furedea <[email protected]> Co-authored-by: Claude Sonnet 5.5 <[email protected]>
概要
#73 の P4(guard が対応を謳う変形で禁止が効くこと)を Hypothesis で追加する.
Refs #73.#98(P1)のマージ後に main へ付け替えた.#102・#104 がこの PR の上に積まれている(マージ順は #100 → #102 → #104).forbiddenで deny になることを検査する(shell 系 5 件はbash -c trueのように script を付けた代表形).allowedが ask になり,直接なら allow(provider の判定)のままであることを検査する./usr/bin/付きパス,wrapper 10 種,bash・sh・zshの-c,シェル組み込みの実行,先頭の!を最大 3 層まで組み合わせる.hypothesisとz3-solverを test グループに追加した(z3-solverは P2・P3 用).検証
tests/hooks/test_guard_transformations.py183 件 pass(deny 125 件・ask 48 件を個別に,1 prefix あたり 12 例,derandomize=Trueで再現可能.prefix を 1 件だけ消したポリシーで赤になるテスト 10 件を含む)発見と判断
bash -c,bash -lc,sh -c,zsh -c,zsh -lcは deny prefix だが,script がない素の形では guard が先に ask を返す.deny にはならないが,安全側で止まる.script を付けた代表形で検査している.xfailは使っていない.guard 本体は変更していない.uv addが>=で入れた.repo の他の test 依存は==固定のため,==に揃えるならuv lockの再実行が必要(ユーザー作業).計画:#73 (comment)
未対応のレビュー指摘(
sudoと shell 層で deny 性質が自明に通る件):#107🤖 Generated with Claude Code