講座 › 分野7 セキュリティと安全性
7.3 Claudeのフック
配点 1.0%Claude Hooks
フックは「必ず起きる」処理。破壊的な操作を、モデルの判断に関係なく実行前に止める。
未読
フックとは 読了

フックは、Claude Codeの動きの決まったタイミングで実行される、利用者が定義した処理です。モデルが実行を選ぶかどうかに頼らず、決まった処理が必ず起きる、決定論的な制御を与えます。編集後の整形、実行前のコマンドの阻止、入力待ちの通知などに使います。
出典:Hooks guide
which gives you deterministic control: certain actions always happen rather than relying on the LLM to choose to run themhttps://code.claude.com/docs/en/hooks-guide.md(取得 2026-11-17)
出典:Hooks guide
format files after edits, block commands before they execute, send notifications when Claude needs inputhttps://code.claude.com/docs/en/hooks-guide.md(取得 2026-11-17)
破壊的な操作を止める 読了

ツールが実行される直前の PreToolUse のフックで、コマンドの中身を調べ、rm -rf のような破壊的な操作なら permissionDecision に deny を返して止めます。CLAUDE.mdに禁止を書いても文脈として扱われるだけなので、判断に関係なく止めたい操作はフックで止めます。
出典:Hooks reference
returns a `permissionDecision` of `"deny"` if it contains `rm -rf`https://code.claude.com/docs/en/hooks.md(取得 2026-11-17)
出典:Memory (CLAUDE.md)
To block an action regardless of what Claude decides, use a PreToolUse hookhttps://code.claude.com/docs/en/memory.md(取得 2026-11-17)
複数のフックと、判断が要る場合 読了

同じイベントに当たるフックは並列に動き、権限の判断では最も厳しい結果が採られます。1つでも deny があれば、ツールの呼び出しは止まります。決まったルールでなく判断が要る場合は、Claudeのモデルに判定させるプロンプト型のフックもあります。Agent SDKでも、同じようにフックで危険な操作を止められます。
出典:Agent SDK hooks
For permission decisions, the most restrictive result applies. One hook's `deny` blocks the tool call.https://code.claude.com/docs/en/agent-sdk/hooks.md(取得 2026-11-17)
出典:Agent SDK hooks
Block dangerous operations before they execute, like destructive shell commands or unauthorized file accesshttps://code.claude.com/docs/en/agent-sdk/hooks.md(取得 2026-11-17)
試験のポイント
- 「必ず止める」はPreToolUseのフックで deny。
出典:Hooks reference
permissionDecisionReason: "Destructive command blocked by hook"
https://code.claude.com/docs/en/hooks.md(取得 2026-11-17) - 複数のフックでは、最も厳しい判断が勝つ。
出典:Agent SDK hooks
When an event fires, the matching hooks run in parallel. For permission decisions, the most restrictive result applies.
https://code.claude.com/docs/en/agent-sdk/hooks.md(取得 2026-11-17)