1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Claude Code hookのifフィールド実用レシピ集——コピペで使える10パターン

1
Last updated at Posted at 2026-05-19

v2.1.85で追加されたifフィールドは、hookの条件付き実行を可能にする。全てのBashコマンドでhookが起動する無駄がなくなる。

この記事では、すぐにコピペで使える10パターンを紹介する。

基本構文

{
  "matcher": "Bash",
  "if": "tool_input.command matches 'git push'",
  "hooks": [{"type": "command", "command": "bash ~/.claude/hooks/push-guard.sh"}]
}

ifの条件がtrueのときだけhookプロセスが起動する。falseなら何も起きない。

レシピ1: git pushだけブロック

{
  "matcher": "Bash",
  "if": "tool_input.command matches 'git push'",
  "hooks": [{"type": "command", "command": "bash -c 'echo BLOCKED >&2; exit 2'"}]
}

レシピ2: npm/pip installだけ監視

{
  "matcher": "Bash",
  "if": "tool_input.command matches '(npm|pip|gem) install'",
  "hooks": [{"type": "command", "command": "bash ~/.claude/hooks/dependency-install-guard.sh"}]
}

レシピ3: rmコマンドだけ警戒

{
  "matcher": "Bash",
  "if": "tool_input.command matches 'rm '",
  "hooks": [{"type": "command", "command": "bash ~/.claude/hooks/rm-safety-net.sh"}]
}

レシピ4: curl POST/PUT/DELETEだけブロック

{
  "matcher": "Bash",
  "if": "tool_input.command matches 'curl.*-X (POST|PUT|DELETE)'",
  "hooks": [{"type": "command", "command": "bash ~/.claude/hooks/network-guard.sh"}]
}

レシピ5: dockerコマンドだけ監視

{
  "matcher": "Bash",
  "if": "tool_input.command matches 'docker '",
  "hooks": [{"type": "command", "command": "bash ~/.claude/hooks/docker-dangerous-guard.sh"}]
}

レシピ6: .envファイルへの操作だけ検出

{
  "matcher": "Edit",
  "if": "tool_input.file_path matches '\\.env'",
  "hooks": [{"type": "command", "command": "bash -c 'echo \"⚠ .env file edit detected\" >&2'"}]
}

レシピ7: テストコマンドの結果を検証

{
  "matcher": "Bash",
  "if": "tool_input.command matches '(npm test|pytest|go test|cargo test)'",
  "hooks": [{"type": "command", "command": "bash ~/.claude/hooks/test-exit-code-verify.sh"}]
}

※ これはPostToolUseで使う。テスト実行後に結果を検証する。

レシピ8: settings.jsonの編集を保護

{
  "matcher": "Edit",
  "if": "tool_input.file_path matches 'settings\\.json'",
  "hooks": [{"type": "command", "command": "bash -c 'echo \"BLOCKED: settings.json edit\" >&2; exit 2'"}]
}

レシピ9: 大きなファイルの書き込みだけ検出

{
  "matcher": "Write",
  "if": "tool_input.content length > 10000",
  "hooks": [{"type": "command", "command": "bash ~/.claude/hooks/large-file-write-guard.sh"}]
}

length > N はv2.1.85+で動作確認が必要。使えない場合はhookスクリプト内でサイズチェックする。

レシピ10: sudoだけブロック

{
  "matcher": "Bash",
  "if": "tool_input.command matches 'sudo '",
  "hooks": [{"type": "command", "command": "bash -c 'echo \"BLOCKED: sudo\" >&2; exit 2'"}]
}

if vs hookスクリプト内チェック

方法 プロセス起動 チェック位置
ifフィールド 条件不一致→起動しない CC内部(高速)
hookスクリプト内 常に起動 外部プロセス(低速)

hookが10個以上ある場合、ifフィールドでフィルタリングするとパフォーマンスが大幅に改善する。

まとめ

ifフィールドは「このhookはこのパターンのときだけ必要」という条件を宣言的に書ける。hookスクリプトの中でgrepする必要がなくなり、コードもシンプルになる。

442個のexample hookの多くは既にifフィールド対応済み:

npx cc-safe-setup --examples | grep "if:"

📌 関連記事:

🛡 634個のhook例をワンコマンドで: npx cc-safe-setup — 13,931テストで検証済み。

hookが10個以上ある? ifフィールドで速くしよう。


本記事の hook の if フィールドの経路の延長で、 利用者の側の防衛の組み合わせを体系で整理した有料の資料を2件。 Edition 2 は既存購入者は無料の更新。

  • Claude Code Migration Playbook ($19) — 6週間の regression の整理と stay/switch/hybridize の3経路の決定の枠組み、 14件の dated public trigger と 77件の起票の参照。 販売頁
  • Claude Code Claim-Verify Handbook ($19) — 道具の主張と実態の乖離の130件の事例の整理と14件の防衛の手順、 233時間の継続の証拠の事例集 (本文15件 + 付録D 115件)。 販売頁
  • CC Safety Lab Founder (¥500/月、 Ko-fi) — 月号の便りで毎月の事例と新規の hook と実測の節約の道具を届ける。 購読頁

📅 本日 2026 年 5 月 22 日発売。 Claim-Verify Handbook (Gumroad、 19 米ドル) は、 operator の意図と system の主張と runtime の動作の三層で 100 件以上の Claude Code の失敗事例を整理した本です。 試し読み (無料、 JP) で序文と試し読みの章を読めます。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?