一覧へEnglish

H02

PowerShell のフックを -Command で呼ぶと、Claude Code の拒否が素通りする

症状

exit 2 で拒否する PowerShell スクリプトをフックにしたのに、書き込みが通る。スクリプトを手で動かすと確かに 2 を返す。

30秒で確かめる

PowerShell(Windows PowerShell 5.1)に貼って実行します。一時フォルダ(%TEMP% の下)にだけファイルを作ります。最後に REPRODUCED と出たら、あなたの PC で起きています。 claude を1回呼ぶので、1分ほどかかります。

$d = Join-Path $env:TEMP ('cc-trap-' + (Get-Date -Format 'yyyyMMddHHmmss')); New-Item -ItemType Directory $d -Force | Out-Null
[IO.File]::WriteAllText("$d\deny.ps1", '[IO.File]::WriteAllText(''MARK'', ''x''); [Console]::Error.WriteLine("denied"); exit 2'.Replace('MARK', "$d\called.txt"))
$hook = 'powershell -NoProfile -ExecutionPolicy Bypass -Command \"& ''' + ($d -replace '\\', '/') + '/deny.ps1''\"'
[IO.File]::WriteAllText("$d\s.json", '{"hooks":{"PreToolUse":[{"matcher":"Write|Edit","hooks":[{"type":"command","command":"HOOK"}]}]}}'.Replace('HOOK', $hook))
Push-Location $d
'Use the Write tool once to create the file written.txt with the content hi. Do not retry if it is blocked.' | claude -p --settings s.json --permission-mode acceptEdits | Out-Null
Pop-Location
$w = Test-Path "$d\written.txt"; $c = Test-Path "$d\called.txt"
"deny hook called through -Command: hook called=$c, written=$w"
if (-not $c) { 'CANNOT CHECK (the hook was never called: check disableAllHooks or your settings)' } elseif ($w) { 'REPRODUCED' } else { 'NOT REPRODUCED' }

なぜ起きるか

フックの拒否は終了コード 2。powershell -Command "& 'deny.ps1'" で呼ぶと、2 が 1 に化ける(T01 と同じ)。1 は「止めないエラー」なので、書き込みは進む。Git Bash が入っていない Windows では、失敗が止める側に倒れることがあり、そこではこの穴は出ない見込み(公式の説明書。作者の PC には Git Bash が入っている)。

直し方

PowerShell のフックは -File で呼ぶ。念のため "onFailure": "block" も付ける(-Command のままでも、これで止まった)。

"command": "powershell -NoProfile -ExecutionPolicy Bypass -File C:/hooks/deny.ps1",
"onFailure": "block"

作者の PC での結果

-File は止まった/-Command は書き込まれた/-Command に onFailure: block を足すと止まった

作者の PC: Windows 11 / Windows PowerShell 5.1.26100 / Python 3.13.7 / Claude Code 2.1.304、日本語の Windows(コードページ 932)。2026-10-20〜22 に確認。

こんな言葉で探した人へ

関連

この穴を最初から踏まないようにした部品(書き込み先を絞る安全装置〈onFailure: block 付き〉、夜の無人実行、ファイルとフックの設定を調べる健康診断、CLAUDE.md の型、試験一式)は cc-win-kit にまとめています。

cc-win-kit 英語版(Payhip、19ドル。日本語版は 2026年10月31日に note で公開)

ほかの症状