Skip to content

fix(hooks): judge gh beside commands that write nothing as gh alone - #256

Merged
furedea merged 7 commits into
mainfrom
fix/gh-joined-read-only-lines
Nov 20, 2026
Merged

furedea merged 7 commits into
mainfrom
fix/gh-joined-read-only-lines

Conversation

@furedea-agent

@furedea-agent furedea-agent Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

概要

gh を読み取り専用のコマンドや cd とつないだ行は,「同じ行の別のコマンドが PATH の前の方に別の gh を置けるかもしれない」として確認になっていた.hook の時点で分かる事実(各コマンドが何も書かず別のプログラムも走らせない,リダイレクト先が PATH の要素でない,PATH に相対パスが無い)で判定し,この理由の確認をなくす.gh 自体の判定は #201 の policy table のまま.

Closes #255
Refs #197, #253, #181

変更

  • read_only_commands.py(新規):語だけで「ファイルを書かず,別のプログラムも走らせない」と言えるコマンドの判定.
    • そのまま読み取り:cat column cut echo false grep head jq ls nl printf pwd sleep tail tr true wc
    • 語を見て判定:awk(-F/-v だけ,プログラムに > | @ system getline が無い),sed(-n -E -r -e だけ,スクリプトは行番号・パターンの p か w/e フラグの無い s///),sort(-o・--compress-program などを除く),uniq(オペランド 1 つまで),base64(-d だけ)
    • run time に決まる語は,そのまま読み取りの組以外と echo/printf(printf -v PATH になりうる)では確認.
    • git の読み取りサブコマンドは入れていない(下の「記録」参照).git を含む行は従来どおり確認.
  • gh_command_line.command_line:
    • plants_nothing:gh 以外がすべて上の読み取り(またはリテラルの cd)で,gh 呼び出しが 1 つ,gh 以外のリダイレクト先は /dev/null だけ,gh 自身の出力先は /dev/null か PATH の要素でないディレクトリ(run time に決まる先・PATH に相対要素があるときは不可).このとき gh_environment._stand_in_problem は PATH を理由に確認しない.
    • directories:hook の cwd と,リテラルの cd が入りうる各ディレクトリ(それまでの各ディレクトリから,bash が保つ論理パスと物理パスの両方).cd が失敗しても,サブシェル・パイプ内でも gh はこのどこかで動く.存在しない先,- で始まる語(cd -,-P,-- など),pushd/popd,run time の先,PATH に相対・空の要素がある,CDPATH 下の相対の cd,環境の cd 関数は従来どおり確認.
    • { },( ),if,ループなど CallExpr 以外の文にリダイレクトがある行は,中のコマンドに書き込みとして記録されないので確認.
    • ヒアドキュメント・ヒアストリング・コマンド/プロセス/算術置換を含む行は,従来の判定(feat(hooks): judge shell function bodies instead of asking for every function #200 の組の filter だけ)に戻す.ループ・関数・代入は従来どおり changes_environment で確認.
  • command_objections.gh_objection:gh 呼び出しを directories の各ディレクトリで判定し,全部で allow のときだけ allow(作業リポジトリの remote,設定,プログラムの所在をそれぞれのディレクトリで見る).hook_input.working_directory を切り出した.
  • gh_environment:_stand_in_problem の条件に plants_nothing を足しただけ(main との差は 1 行).

再生(2026-11-18 14:24〜11-19 15:13 JST の確認 390 件)

issue255_replay.py を本ブランチの hook と origin/main の hook に向けて実行した(XDG_STATE_HOME を scratch に向け,実際の audit log には書かない).390 件のうち command が無い 2 件を除く 388 件,guard_command 以外の hook による 16 件は対象外.再生環境の PATH には書き込める要素が gh より前に無かったため,実際の hook 環境(PATH 理由 74 件)を近似する「書き込める要素を先頭に足した PATH」でも測った.

通常の PATH 書き込める要素を先頭に足した PATH
main で確認 → 本ブランチで allow 36 件 19 件
うち,消えた worktree への cd を既存チェックアウトに置き換えると追加で allow +20 件 +6 件

git 読み取りを入れていた段階(43 件 / 25 件)から 7 件 / 6 件減った.

確認のまま残る内訳(通常の PATH,388 件中 allow 49 件・他 hook 16 件を除く 323 件):

理由 件数
代入・ループ・wrapper・環境(#181) 142
別のコマンドがディレクトリ/remote を変えうる(git 全般,python3,jev-lint,mkdir,消えた worktree への cd 20 件など) 64
gh 呼び出しを分類できない(run time の値など) 28
未対応の構文($'...' など) 28
ほかのコマンド規則(cleanup_worktrees,agent-browser など) 24
rm guard 16
書き込み先(class C/D) 12
送るファイルの横に別コマンド 9

書き込める PATH の場合は,確認のまま 260 件が PATH 理由で出る.その内訳(通常の PATH での理由)は,代入・ループ 129,別コマンド 64,gh 呼び出しが複数(通常の PATH なら allow)30,分類不可 24,class C/D 7,送るファイル 6.

新たに allow になった 36 件はすべて目視で確認した:gh 以外は grep/head/tail/jq/awk '{print ...}'/sed -n/sort/uniq/ls/cat/sleep/echo/wc/base64 -d,リテラルの cd(/tmp,相対の tests/hooks→../..),2>/dev/null と 2>&1 だけ.gh はすべて読み取り(pr view/list/checks/diff,run view/list/watch,issue view,GET の gh api/api graphql query)で,#201 の policy table で許可される操作.

テスト

  • tests/hooks/test_gh_joined_read_only_lines.py(新規,書き込める PATH 要素を gh より前に置いた状態で):
    • allow:Issue の例 gh pr checks 240 -R … 2>&1 | grep -c pass,/dev/null への出力,awk | sort | uniq -c,sleep/echo,ls/wc,sed -n の行番号・パターン,base64 -d,PATH 外への gh の出力.
    • 作業リポジトリを読む gh は,読み取りだけの行でも remote が github.com 外なら確認.
    • 確認のまま:cp x later/gh,cp x later/git(Issue の例),mv,ln,install,chmod,export PATH=,PATH の要素へのリダイレクト(gh 自身の出力も),run time のリダイレクト先,ほかのコマンドの echo x > notes.txt・>> .git/config,sort -o/-uo/--compress-program,uniq の出力ファイル,awk の書き込み・system・-f,sed w/-i,base64 -o,printf $OPT PATH /tmp,echo $WORDS,{ echo x; } > later/gh,(echo x) > later/gh,if …; fi > later/gh,{ echo x; } >> .git/config,git status,git log,git commit,python3.
    • cd:github.com のチェックアウトへの cd は allow,github.com 外へは確認(-R 付きなら allow),cwd 側が github.com 外なら確認,存在しない先・$DIR・cd -・cd -P・pushd・cd -- -foreign・symlink 越しの cd ..・CDPATH 下の相対・環境の cd 関数・PATH の相対要素(.,空)は確認.
    • ヒアドキュメント,ヒアストリング,$(...),<(...),ループ,関数,代入の行は確認.
    • ゲートを外す・修正前のコードに戻すとこれらのテストが失敗することを確認した(ヒアドキュメント/置換,cd --,symlink,-P,run time の printf/echo,まとまりへのリダイレクト).
  • tests/hooks/test_read_only_commands.py(新規):writes_nothing の単体テスト(sort -t -o,sed -e p -e 'w out',awk -v x=1 -F: …,git log/git status は読み取りに入らない,など).
  • 既存の test_gh_targets.py(gh … && gh … は確認,printf x > .git/config; gh pr list は確認など)は変更なしで通過.
  • uv run pytest tests(6220 passed,2 skipped),ruff check,ruff format --check,ty check,deptry,vulture を通過.

記録(範囲外・前提・限界)

  • git 読み取りは feat(agent-runtime): build persistent, isolated, provider-neutral agent infrastructure #181 に回した(受け入れ条件 1 つ目の「読み取りの git サブコマンドを含む」から外れる).レビューで,git の読み取りがリポジトリ設定(textconv,diff.external,filter,fsmonitor,gpg.program,hook.*,core.alternateRefsCommand),post-index-change フック,submodule(gitlink),環境(GIT_PAGER,GIT_EXTERNAL_DIFF,GIT_CONFIG_*),promisor remote の遅延 fetch などからプログラムを走らせうる指摘が直すたびに続き,feat(hooks): judge shell function bodies instead of asking for every function #200 と同じく収束しなかった.ユーザー判断(どちらでも可)を受けて親セッションが「外す」を選んだ.git を含む行は従来どおり確認になり,そのための設定検査も削除した.減る件数は通常の PATH で 7 件,書き込める PATH で 6 件.
  • Issue の受け入れ条件 3 つ目(リダイレクト先は /dev/null と PATH 外を許す)より厳しくした:PATH 外を許すのは gh 自身の出力だけで,gh 以外のコマンドは /dev/null だけ.書いたファイルが同じ行の後のプログラムの設定になりうるため(自動セキュリティレビューの指摘).
  • 判定と実行の間の変化(TOCTOU)は範囲外:hook が見た PATH の要素・cd 先・設定が実行時に変わりうる(同じ行のコマンドは書かないが,並行する別プロセスは書ける).feat(agent-runtime): build persistent, isolated, provider-neutral agent infrastructure #181 で扱う.
  • cd は「gh が cwd と各 cd 先のどれで動いても allow」とした.cd 先 だけで判定する Issue の文言より厳しく,cwd の remote が github.com 外の場合は cd しても確認になる(構文から cd の成否・サブシェルを読まずに済ませるため).
  • gh 呼び出しが 2 つ以上の行は,PATH に書き込める要素がある環境では従来どおり確認(gh repo clone/run download が後の gh の前に置きうるため).書き込める PATH の再生で 30 件がこれで残る.
  • レビュー最終回で出た指摘は直さずに残した(後続で扱う):
    • P1:静的な printf の %n は引数名に文字数を代入する(printf x%n PATH で PATH が 1 になる).書き込める PATH 要素がある環境では誤った allow になりうるので,最初に直す項目(対処案:%n を含む printf を読み取りに数えない).
    • P2:zsh の 2 オペランドの cd OLD NEW を追跡していない(対処案:オペランドが 2 つ以上なら追わない).
    • P2:gh のリダイレクト先のブレース展開(zsh の MULTIOS)と ~+/~- を run time の先として扱っていない(対処案:literal_words.EXPANDING_TEXT を再利用する).
    • MEDIUM(ローカルの自動セキュリティレビュー):コマンドの無い文のリダイレクト(> f 単独)を,まとまりへのリダイレクトと同じく扱っていない(対処案:Cmd が引数のある CallExpr でない文の Redirs をすべて書き込みとみなす).
  • 誤検知として残るもの(確認が増えるだけで誤った allow にはならない):awk の比較 >,値を取る uniq -f N,一覧に無い sort の option,base64 のファイル入力,cd -P.
  • レビュー対応:GitHub のスレッドはすべて返信して解決した(修正またはこの git 読み取りの除外で対処).ローカルの自動セキュリティレビューの指摘(他コマンドの書き込み,gitlink の submodule,promisor の遅延 fetch)も,書き込みの制限と git 読み取りの除外で対処済み.

🤖 Generated with Claude Code

A line joining gh with commands that only read and print, such as jq, grep,
head, awk, sort, sed -n, git reads, or a literal cd, can no longer plant a
stand-in on PATH, so it no longer asks for that reason. The working
repository is checked in every directory the call may run in. Lines that
may write a program, change PATH, or build text with a heredoc or a
substitution still ask.

Closes #255

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@furedea

furedea commented Nov 19, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-19T15:01:28.891076Z 084e323 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Comment thread agents/hooks/lib/gh_command_line.py Resolved
@github-actions

Copy link
Copy Markdown

Review summary

I read the new read_only_commands.py word checks, gh_command_line.command_line, gh_environment and the command_objections per-directory loop. I found one issue that should block the merge. I did not run the tests.

P1: a same-line redirect can plant what a later git read executes (inline comment on gh_command_line.py:108)

  • plants_nothing only considers redirect targets that land in a PATH entry.
  • A line such as echo '[core]\n fsmonitor=./x' >> .git/config; git status; gh pr list ... passes every check.
  • git_read_may_run_program() reads the config while the hook runs, before the redirect has written it.
  • At run time git status then executes the planted program.
  • Before this PR, a line with git beside a redirect asked. The new tests do not combine a redirect with a git read.

Checked and found fine

  • printf -v is still caught by _changes_environment.
  • The git, sort, uniq, sed and awk word checks err on the side of asking.
  • The per-directory git config check runs, because hook_input.working_directory changes the cwd for each directory.

No other findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 82a20876a2

ℹ️ 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".

Comment thread agents/hooks/lib/gh_environment.py Resolved
A file written beside gh may be configuration a later git read runs, such
as .git/config naming a diff program, which could then place a stand-in
gh on PATH. Only gh's own output may go to a file off PATH; every other
command may write only to /dev/null.

Refs #255

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@furedea

furedea commented Nov 19, 2026

Copy link
Copy Markdown
Owner

@codex review

Comment thread agents/hooks/lib/gh_command_line.py Resolved
@github-actions

Copy link
Copy Markdown

Review summary

I reviewed the diff read-only. I did not run the test suite. I found one blocking issue.

P1 — gh's own redirect can plant git configuration that a later git read runs (inline comment on gh_command_line.py). plants_nothing only checks gh's redirect targets against PATH entries. git_read_may_run_program() runs at hook time, before gh writes. So gh ... > .git/config; git status is allowed, then gh overwrites the repository config with remote-controlled content and git runs it. No test covers a gh redirect into git config. Fix: when reads_git is true, allow only /dev/null as a gh redirect target, or reject git config paths, and add an ask test.

What I checked and found fine:

  • read_only_commands: the sort, uniq, awk, sed, base64 and git option filters err on the conservative side.
  • READ_PROGRAM_SETTINGS and the scope handling.
  • Per-directory evaluation of cd targets, including CDPATH and relative PATH entries.

🤖 Generated with Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 30052ef47a

ℹ️ 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".

Comment thread agents/hooks/lib/gh_environment.py Resolved
… git read

gh's own output may land in .git/config or another file a later git read
on the line consumes, and GIT_EXTERNAL_DIFF makes git diff run a program.
A line with a git read now allows gh's output only to /dev/null, and an
inherited GIT_EXTERNAL_DIFF keeps the git read untrusted.

Refs #255

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@furedea

furedea commented Nov 19, 2026

Copy link
Copy Markdown
Owner

@codex review

Comment thread agents/hooks/lib/gh_environment.py Resolved
@github-actions

Copy link
Copy Markdown

Review summary

I read the whole diff. I couldn't run anything, so the findings below come from reading the code. The core design holds up:

  • plants_nothing is conservative. It falls back to the old behaviour for heredocs, substitutions, assignments and relative PATH entries.
  • The per-directory verdict loop and the word-level parsers in read_only_commands.py are strict. I found no way to get -o, --compress-program, w/e sed flags, awk system or >, or git --output past them.
  • printf -v is still caught by _changes_environment.

Findings:

  • P2 (gh_environment.py:53): the git-read check covers core.hooksPath but not hooks in the default .git/hooks/. git status and git diff can run post-index-change when they refresh the index. A hook planted in an earlier call could put a fake gh on PATH on a line the guard now allows.
  • P3 (tests): no test that the git-config check runs in the directory a cd enters. There are also no direct unit tests for read_only_commands.writes_nothing edge cases.

No P0 or P1 findings.

🤖 Generated with Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 02f1ba91cd

ℹ️ 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".

Comment thread agents/hooks/lib/gh_environment.py Resolved
…a git read

A git read that refreshes the index runs the repository's
post-index-change hook, which the agent can write, and settings injected
through GIT_CONFIG_* may name a program. Either keeps a git read beside
gh untrusted; injected settings that run nothing, such as the sandbox's
core.fsmonitor=false, still pass. Unit tests pin the option checks of
read_only_commands.

Refs #255

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@furedea

furedea commented Nov 19, 2026

Copy link
Copy Markdown
Owner

@codex review

@github-actions

Copy link
Copy Markdown

Review summary

I found no P0 or P1 problems, and no correctness or security bugs in the changed behaviour. I read the diff and the surrounding code (gh_invocation, gh_options, shell_syntax, gh_environment). I did not run the test suite or the replay.

What I checked

  • Read-only word checks in read_only_commands.py. sort, uniq, awk, sed, base64 and git are all matched against an allowlist, not a denylist. Abbreviated or bundled write options such as --out, -uo and --ext therefore fall through to ask.
  • Run-time words. A word known only at run time is accepted only for the plain filters.
  • Writing gh forms. repo clone and run download still get single_call=False on a joined line. require_new_subdirectory then rejects them, so plants_nothing does not widen them.
  • Per-directory judgment. git_output is not cached, so the repository configuration and remote are really re-read in each directory of the directories loop in gh_objection.
  • Redirect targets. _may_plant resolves each target from every candidate directory and checks both the parent and the resolved final path, so .. and symlinks cannot slip through.
  • Git reads and configuration. GIT_EXTERNAL_DIFF, a post-index-change hook and repository-scope program settings all turn into ask.
  • Not a git repository. There, rev-parse and config print nothing and the git-read check passes, which is correct.

Finding

  • P3 (inline): no test covers the CDPATH branch in _entered, or the has_inherited_function("cd") guard in _directories. The PR description promises CDPATH behaviour, so a regression there would not be caught.

Residual risks the PR already notes

  • TOCTOU between the hook's check and the run.
  • Submodule repositories whose own configuration could name programs for git status. This is not covered by the git-config scan and is a minor residual risk.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e7e0db9c9e

ℹ️ 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".

Comment thread agents/hooks/lib/gh_environment.py Resolved
Comment thread agents/hooks/lib/gh_environment.py Resolved
git names the post-index-change hook's path itself, following
core.hooksPath and worktrees. A repository with submodules, whose
configuration and hooks a read also enters, and a hook defined in
repository configuration keep a git read beside gh untrusted.

Refs #255

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@furedea

furedea commented Nov 19, 2026

Copy link
Copy Markdown
Owner

@codex review

Comment thread agents/hooks/lib/gh_command_line.py Resolved
@github-actions

Copy link
Copy Markdown

Review summary: one P2 finding, no blockers found.

  • P2 (gh_command_line._directories, inline): cd through a symlink followed by cd .. can leave gh in a logical directory that is never judged. found stores physical paths, but bash tracks logical ones. It needs a pre-planted symlink, so exploitability is limited. There is no test for it.

I also checked the word-level read-only checks in read_only_commands.py (awk, sed, sort, uniq, git, base64) and the git-config and hook gate in gh_environment.py. I found no bypass there. The test coverage for the new behavior is thorough.

🤖 Generated with Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: de1b3bd9d6

ℹ️ 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".

Comment thread agents/hooks/lib/gh_command_line.py Resolved
git reads may run programs that repository configuration, hooks,
submodules, the environment, or a lazy fetch name, and each fix uncovered
another, so a line holding git keeps asking as before and the git
configuration checks that served only these reads are gone. A cd is now
followed by both the logical path bash keeps after a symlink and the
physical one, and any option or `--` before its operand is not followed.

Refs #255

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@furedea

furedea commented Nov 19, 2026

Copy link
Copy Markdown
Owner

@codex review

Comment thread agents/hooks/lib/gh_command_line.py Resolved
@github-actions

Copy link
Copy Markdown

Review summary

I read the diff and the surrounding parser code. The sandbox blocked every attempt to run Python or the tests, so the points below come from reading the code, not from running it.

P2 (inline): item.writes only covers redirects on the statement that directly owns a CallExpr. A redirect on an enclosing compound statement, such as { echo x; } >> .git/config; gh pr view 1, is not seen. plants_nothing can then be True, which skips the writable-PATH check, and the working repository's remotes are judged before the write happens. The old code asked on this shape of line. No test covers it.

P3: PR description vs diff. The description says read_only_commands.py treats git read subcommands as read-only. It also says gh_environment checks git config, hooks, submodules and GIT_EXTERNAL_DIFF. Neither is in the diff, and gh_environment.py changes by one line. The tests assert that git status and git log still ask ("left to #181"). The code is the safer of the two, but the description should be updated or the missing part added.

I checked the sort, uniq, awk, sed and base64 word checks and found no bypass. Abbreviated long options are rejected, -o is excluded, and the sed w/e flags and the awk > | system getline @ characters are excluded. The cd directory handling looks sound: it covers a failed cd, a symlink followed by .., CDPATH, a relative PATH entry, and per-directory remote checks. The tests cover these well.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ff71434a1f

ℹ️ 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".

Comment thread agents/hooks/lib/read_only_commands.py Resolved
A word known only at run time may turn printf into `printf -v PATH`, so
echo and printf with one no longer count as read-only. A redirection on a
group, subshell, branch, or loop belongs to no call inside it, so a line
holding one keeps asking.

Refs #255

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@furedea

furedea commented Nov 19, 2026

Copy link
Copy Markdown
Owner

@codex review

Comment thread agents/hooks/lib/gh_command_line.py Resolved
Comment thread agents/hooks/lib/gh_command_line.py Resolved
@github-actions

Copy link
Copy Markdown

Review summary

I read the new read_only_commands.py and the changes to gh_command_line, command_objections.gh_objection and gh_environment. I did not run the test suite.

Nothing blocks the merge (no P0 or P1). The word-level checks for awk, sed, sort, uniq and base64 are strict allowlists. Anything unrecognised falls back to asking, and I found no bypass in them.

Two P2 findings, both in gh_command_line.py, posted inline:

  • P2: a zsh two-operand cd OLD NEW is followed as cd OLD, so directories can miss where gh actually runs. The fix is to reject a cd with more than one operand.
  • P2: RUN_TIME_PATH ignores brace expansion and ~+/~- in gh's redirect target. In zsh with MULTIOS this could write into a PATH entry while plants_nothing stays true.

Neither case has a test yet. The replay data covers real traffic, not these inputs.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 084e32322e

ℹ️ 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".

Comment thread agents/hooks/lib/read_only_commands.py Resolved
@furedea
furedea merged commit 540bbad into main Nov 20, 2026
29 checks passed
@furedea
furedea deleted the fix/gh-joined-read-only-lines branch November 20, 2026 05:56
furedea added a commit that referenced this pull request Nov 20, 2026
…thing (#262)

## 概要

`printf x%n PATH` は PATH に `1` を代入するのに,#256
の「何も書かないコマンド」判定では読み取りに数えられ,`printf x%n PATH; gh pr list -R
furedea/agent-config` が allow になっていた.書式に `%n` 変換を持つ `printf`
を読み取りに数えず,こうした行は確認にする.これで #256 の「記録」に残した P1(`%n`)の限界を閉じる.

Refs #255

## 変更前後

| 行 | 変更前 | 変更後 |
| --- | --- | --- |
| `printf x%n PATH; gh pr list -R furedea/agent-config` | allow |
**ask** |
| `printf '%s\n' x; gh pr list -R furedea/agent-config` | allow | allow
|
| `printf $OPT PATH /tmp; gh …`(run time の書式) | ask | ask(#256 のまま) |
| `printf x{%,}n PATH; gh …`,`sort {-o,later/gh} x; gh …`(ブレース展開) |
allow | **ask** |
| `printf x%n GH_HOST; gh issue comment 1 -R furedea/agent-config -b ok`
| allow | **ask** |

## 変更

- `agents/hooks/lib/read_only_commands.py`:`writes_nothing` で,`printf`
の書式オペランド(先頭の `--` は飛ばす)に `%n` 変換があれば読み取りに数えない.
- `%n` の形:`%` のあとにフラグ・幅・精度(`-+ #0'*.`
と数字),位置指定(`$`),長さ修飾子(`hlLqjzt`)を挟んだ `n`.`%%` は文字の `%` として飛ばす.
- zsh は書式のエスケープを変換の解釈より先に文字にする.`\u`/`\U` で作った `%` は変換になり,`\x`/8 進で作った
`%` は文字のまま,`n` はどのエスケープから作っても変換になる.この挙動どおりに展開した書式でも判定する(bash
はエスケープから変換を作らないので,bash にとっては確認が増えるだけ).

- `agents/hooks/lib/gh_command_line.py`:
- `_writes_nothing`:ブレース展開などで別の語になりうる語(`Command.spreading`)を run time
の語と同じに扱う.`printf x{%,}n` や `sort {-o,later/gh}` は展開後に `%n`/`-o`
になる(ローカルの自動セキュリティレビューの指摘).
- `_changes_environment`:`printf` の `%n`,zsh の `print -f`(`print -f x%n
V` も代入する),print/printf の run time に決まる語や広がりうる語(`printf "$F"
GH_HOST`,`printf {-v,GH_REPO} x` は `-v`
になる)を変数の代入とみなす(自動セキュリティレビューの指摘).`%n` で GH_HOST を変えると `-R OWNER/REPO` の
host が変わるため.

## 実際の挙動の確認(bash 5.3.9,macOS の bash 3.2.57,zsh 5.9.1)

`V=old; printf "<書式>" V` の後の `V`:

| 書式 | bash 5.3 / 3.2 | zsh |
| --- | --- | --- |
| `x%n` `x%5n` `x%-n` `x%.3n` `x%#n` `x%0n` `x% n` `x%+n` `x%'n` `x%ln`
`x%Ln` | 1 | 1 |
| `x%hhn` `x%zn` `x%jn` `x%tn` `x%lln` | 1 | `%hhn` 以外は代入しない |
| `x%*n`,`x%.*n`(`*` が引数を 1 つ取る) | 1 | 1 |
| `x%1$n`,`x%2$n` | 代入しない | 1 |
| `x%%%n` | 2 | 2 |
| `x%%n`,`x%qn`,`x%bn` | 代入しない | 代入しない |
| `x%n`,`x\U00000025n`,`x%\x6e`,`x%\156`,`x%l\x6e` | 代入しない | 1 |
| `x\045n`,`x\x25n`,`x\x25\x6e` | 代入しない | 代入しない |

## テスト

- [x] Red:追加した `printf x%n PATH; gh pr list -R furedea/agent-config`
が修正前は `allow`
で失敗(`test_a_line_that_may_plant_a_program_or_change_path_still_asks[printf
assigning PATH with %n]`),`writes_nothing` の単体テスト 17 件も失敗.
- [x] Green:同じテストが修正後に通過.
- [x] `tests/hooks/test_gh_joined_read_only_lines.py`:`%n` の行は確認,普通の
`printf '%s\n' x` の行は allow.
- [x] `tests/hooks/test_read_only_commands.py`:上表で代入した書式(`--`
付きも)は読み取りでない,`%s\n`・`x%%n`・`100%%`・`%d items`・`x\045n`・`%s`・`-n`
は読み取り,`\UFFFFFFFF` のような Unicode 範囲外のエスケープでも落ちない.
- [x] `test_gh_joined_read_only_lines.py`:`printf x{%,}n PATH`,`sort
{-o,later/gh} x` の行は確認(修正前は allow で失敗).
- [x] `test_gh_environment.py`:`printf x%n GH_HOST`(修正前は allow
で失敗),`print -f x%n GH_HOST`,`printf {-v,GH_REPO} other/r`,`printf "$F"
GH_HOST`(修正前は allow で失敗)の行は確認.
- [x] 手動の mutation(8 個:判定の削除,フラグ・長さ修飾子の文字クラスの縮小,`%%` 判定,zsh 展開の削除,`\x`/8
進の `%` の扱い,`--` の扱い,8 進の桁数)はすべて検出.
- [x] `uv run pytest -n auto tests`(6253 passed,2 skipped),`ruff
check`,`ruff format --check`,`ty check`,`deptry`,`vulture`.

<details>
<summary>範囲・前提・限界</summary>

- 範囲は #256 の限界のうち `%n` だけ.zsh の `cd OLD
NEW`,リダイレクト先のブレース展開・`~+`/`~-`,コマンドの無い文の `> f`,git 読み取り(#181)は範囲外.
- 脅威モデルは #256 と同じ(エージェントは引数・環境変数・PATH を作れる.hook,Nix store,GitHub
設定は信頼する).
- 判定は書式オペランドだけを見る.書式の後のオペランドは変数名として使われるだけで変換にはならない.
- 誤検知として残るもの(確認が増えるだけ):`\\156` のようにエスケープされたバックスラッシュの後の数字も zsh
のエスケープとして展開するので,`%\\156` は確認になる.bash で `%n` などを使う書式も確認になる.
- `%n` による gh の読む変数の代入(`changes_environment`)とブレース展開は,同じ `%n` の取りこぼしとして本
PR で扱った.
- 誤検知として増えるもの:`print -f` はどの書式でも確認になる.`printf '%s\n' "$X"` のように run time
の語を持つ print/printf と gh を並べた行は確認になる.ブレースなどで広がる語を持つ
`printf`/`echo`/`sort`/`sed`/`awk`/`uniq`/`base64` は確認になる.
- レビュー対応(1 回分):
- Codex P1「zsh の printf は `\0NNN`(4 桁)を読む」:zsh 5.9.1 で `printf
'a\0156b'` は `a \r 6 b`,`printf 'a\0045b'` は `a \004 5 b`,`V=old; printf
'x\0045n' V` で V は変わらず,`\0` を含めて 3 桁までと確認したため変更しない(スレッドに証拠を返信して解決).8
進で作った `%` は zsh でも文字のままなので,`\0045` が `%` になっても `%n`
変換にはならない.claude-review の同じ P3 の疑問も同じ理由で変更しない.
- claude-review P3「`printf -v VAR fmt` では `assigns_a_count` が `-v`
を書式と読む」:`-v` は `_changes_environment` が変数の代入として捕らえるので,`printf -v PATH x;
gh pr list -R …`,`printf -v X x%n PATH; gh …` はどちらも確認になることを確かめた.誤った
allow にならないため変更しない.
  - ローカルの自動セキュリティレビュー(ブレース展開,run time の書式)は修正した(上の「変更」).

</details>

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: furedea <132188853+furedea@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
furedea pushed a commit that referenced this pull request Nov 20, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.3.1](v0.3.0...v0.3.1)
(2026-11-20)


### Bug Fixes

* **hooks:** do not count a printf whose format holds %n as writing
nothing ([#262](#262))
([f3a4543](f3a4543))
* **hooks:** judge gh beside commands that write nothing as gh alone
([#256](#256))
([540bbad](540bbad))
* **hooks:** stop asking for gh calls whose target and arguments are
known at hook time
([#254](#254))
([f4aa355](f4aa355))
* **lint:** close the Rust and TypeScript items of
[#196](#196)
([#259](#259))
([1d2a81e](1d2a81e))
* **lint:** select the Go model-rule forms deferred in
[#196](#196)
([#257](#257))
([8c347fe](8c347fe))
* **lint:** widen three Python model-rule selectors deferred in
[#196](#196)
([#258](#258))
([b3fc801](b3fc801))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: furedea-release[bot] <353651101+furedea-release[bot]@users.noreply.github.com>
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.

fix(hooks): judge gh on lines joined only with read-only commands and cd

1 participant