Skip to content

fix(hooks): do not count a printf whose format holds %n as writing nothing - #262

Merged
furedea merged 3 commits into
mainfrom
fix/printf-percent-n
Nov 20, 2026
Merged

furedea merged 3 commits into
mainfrom
fix/printf-percent-n

Conversation

@furedea-agent

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

Copy link
Copy Markdown
Contributor

概要

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 代入しない 代入しない

テスト

  • 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 件も失敗.
  • Green:同じテストが修正後に通過.
  • tests/hooks/test_gh_joined_read_only_lines.py:%n の行は確認,普通の printf '%s\n' x の行は allow.
  • tests/hooks/test_read_only_commands.py:上表で代入した書式(-- 付きも)は読み取りでない,%s\n・x%%n・100%%・%d items・x\045n・%s・-n は読み取り,\UFFFFFFFF のような Unicode 範囲外のエスケープでも落ちない.
  • test_gh_joined_read_only_lines.py:printf x{%,}n PATH,sort {-o,later/gh} x の行は確認(修正前は allow で失敗).
  • 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 で失敗)の行は確認.
  • 手動の mutation(8 個:判定の削除,フラグ・長さ修飾子の文字クラスの縮小,%% 判定,zsh 展開の削除,\x/8 進の % の扱い,-- の扱い,8 進の桁数)はすべて検出.
  • uv run pytest -n auto tests(6253 passed,2 skipped),ruff check,ruff format --check,ty check,deptry,vulture.
範囲・前提・限界
  • 範囲は fix(hooks): judge gh beside commands that write nothing as gh alone #256 の限界のうち %n だけ.zsh の cd OLD NEW,リダイレクト先のブレース展開・~+/~-,コマンドの無い文の > f,git 読み取り(feat(agent-runtime): build persistent, isolated, provider-neutral agent infrastructure #181)は範囲外.
  • 脅威モデルは fix(hooks): judge gh beside commands that write nothing as gh alone #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 の書式)は修正した(上の「変更」).

🤖 Generated with Claude Code

…thing

bash and zsh store the count of printed characters into the variable a %n
operand names, so `printf x%n PATH; gh ...` set PATH to 1 and was judged
read-only. Treat %n with any flags, width, precision, positional index or
length as an assignment, including a %n zsh builds from \u, \U, \x or octal
escapes in the format.

Refs #255

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

furedea commented Nov 20, 2026

Copy link
Copy Markdown
Owner

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 20, 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-20T06:42:34.676227Z 7d9223e 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.

@github-actions

Copy link
Copy Markdown

Review summary: no blocking findings.

I read the diff and traced the PRINTF_CONVERSION regex and the zsh escape expansion by hand. %%%n, x%%n, the -- prefix, an empty operand list, and a format with a run-time word all behave as the PR describes. The new tests cover each of these. Failures only go in the safe direction: some formats ask when they didn't need to.

I did not run the tests, and zsh isn't installed in the review environment. That means I could not check the zsh-specific claims against a real shell: \u escapes producing a conversion, \x and octal % staying literal, and \0NNN octal handling. I'm relying on the PR's own verification table for those.

Possible follow-up (P3, unverified): FORMAT_ESCAPE reads \0156 as \015 followed by 6. If zsh's printf also accepts the \0NNN form, %\0156 would be read as % + CR + 6 and miss the n. Worth a one-line check in zsh.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: f5028db594

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

…nment

printf x%n GH_HOST set a variable gh reads without the line being judged
as changing the environment, and a brace such as printf x{%,}n PATH or
sort {-o,later/gh} turned a word into %n or -o after the read-only check.
Judge a word a brace may spread as known only at run time, and treat
printf %n, zsh's print -f, and a spreading print or printf word as setting
a variable.

Refs #255

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

furedea commented Nov 20, 2026

Copy link
Copy Markdown
Owner

@codex review

@github-actions

Copy link
Copy Markdown

Review summary: no blocking findings.

I read the diff and the shell_syntax spreading fields it uses. I did not run the tests, and zsh isn't installed in the review sandbox, so I couldn't re-check the shell behavior table.

  • %n detection: PRINTF_CONVERSION handles flags, width, precision, $ positions, length modifiers and %%. Checking the zsh-expanded format as well as the raw one is the conservative choice.
  • Detection runs in both places it needs to: read_only_commands.writes_nothing and _changes_environment, which also covers print -f and brace-spread -v. The tests cover both.
  • Not verified: I couldn't confirm whether zsh's printf accepts a four-digit \0NNN octal escape. If it does, %\0156 would read as %n in zsh but not match here, because FORMAT_ESCAPE takes at most 3 octal digits. This is a guess and worth a quick check against zsh 5.9.1. A mismatch is only a possible miss on a very narrow input.

🤖 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: 2f78f1111c

ℹ️ 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
…gnment

A format or option held in a variable, such as printf "$F" GH_HOST with
F='x%n', may assign a variable gh reads, so judge such a line as changing
the environment, as a spreading word already is.

Refs #255

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

furedea commented Nov 20, 2026

Copy link
Copy Markdown
Owner

@codex review

@github-actions

Copy link
Copy Markdown

Review summary (read-only): no blocking findings. I posted no inline comments.

I read the diff and checked the following by reading the code. I could not run the tests in this environment.

  • %n regex: PRINTF_CONVERSION over-approximates bash's flag, width, precision and length grammar, so it errs toward asking. %% is skipped, and %%%n is still caught because finditer consumes %% first and then matches %n.
  • zsh escape expansion: \x and octal % stay literal, while \u/\U % and any escape-built n become conversions. This matches the behaviour table in the description. The out-of-range \U case is guarded against chr() raising.
  • zip(..., strict=True) in _writes_nothing: Command.__post_init__ fills dynamic and spreading to len(arguments) when they are empty, and the parser sets both from the same Args. The lengths always match, so no ValueError.
  • _changes_environment: treating any run-time or spreading word after the program name of print/printf as a possible assignment, and print -f as one always, is conservative. The only cost is extra prompts, which the description lists as intended.

Residual notes, none blocking:

  • P3: assigns_a_count takes the first non--- operand as the format. For printf -v VAR fmt ... it therefore reads -v as the format and does not see a %n in the real format. That is harmless only because -v assignments are caught by the separate -v check in _changes_environment. read_only_commands.writes_nothing(["printf", "-v", ...]) still returns True for a literal -v, so the PATH/gh joined-line case relies on other layers. I saw printf -v PATH evil covered only in test_removal_paths.py, and I did not confirm that the gh joined-line path asks for it. Adding a joined-line test such as printf -v PATH x; gh pr list -R furedea/agent-config would pin that down. It is pre-existing and outside this PR's %n scope.
  • P3: Test coverage for the changed behaviour is good, and the description reports mutation checks.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 7d9223e3c1

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

@furedea
furedea merged commit f3a4543 into main Nov 20, 2026
29 checks passed
@furedea
furedea deleted the fix/printf-percent-n branch November 20, 2026 06:48
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.

1 participant