Hook ベース権限モデル
CLAUDE.md上級
Claude Code hooks(PreToolUse、PostToolUse、Notification)はCLAUDE.mdルールを超えてポリシーをプログラムで適用する方法を提供します。このパターンは権限レイヤーの構築方法を示します:保護パスへの書き込みブロック、破壊的コマンドの承認要求、監査のための全ファイル変更記録、機密操作の通知。ガードレールが提案ではなく強制されるべきチーム環境に最適です。
hooks権限セキュリティガードレールポリシー
パターンコード
# Hook-Based Permission Model
## .claude/settings.json — Hook Configuration
```json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Write|Edit",
"command": "node .claude/hooks/check-write-permission.js"
},
{
"matcher": "Bash",
"command": "node .claude/hooks/check-bash-command.js"
}
],
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "node .claude/hooks/log-file-change.js"
}
],
"Notification": [
{
"command": "node .claude/hooks/notify-slack.js"
}
]
}
}
```
## .claude/hooks/check-write-permission.js
```javascript
// Block writes to protected paths
const PROTECTED = [
/^\.env/,
/^src\/config\/production/,
/^migrations\//,
/package-lock\.json/,
];
const input = JSON.parse(require("fs").readFileSync("/dev/stdin", "utf8"));
const filePath = input.tool_input?.file_path || "";
const blocked = PROTECTED.some((re) => re.test(filePath));
if (blocked) {
console.log(JSON.stringify({
decision: "block",
reason: `Protected path: ${filePath}. Manual edit required.`,
}));
} else {
console.log(JSON.stringify({ decision: "approve" }));
}
```
## .claude/hooks/check-bash-command.js
```javascript
// Block destructive commands
const BLOCKED_PATTERNS = [
/rm\s+-rf/,
/git\s+push\s+--force/,
/git\s+reset\s+--hard/,
/DROP\s+TABLE/i,
/DELETE\s+FROM\s+\w+\s*;/i,
];
const input = JSON.parse(require("fs").readFileSync("/dev/stdin", "utf8"));
const cmd = input.tool_input?.command || "";
const blocked = BLOCKED_PATTERNS.some((re) => re.test(cmd));
if (blocked) {
console.log(JSON.stringify({
decision: "block",
reason: "Destructive command detected. Requires manual execution.",
}));
} else {
console.log(JSON.stringify({ decision: "approve" }));
}
```
このパターンをプロジェクト設定にコピーして適用してください。
実行プレビュー
Hook ベース権限モデル
Hook ベース権限モデルについて
Claude Codeパターンは、複雑な開発シナリオに対応するための実証済みアーキテクチャ設計とワークフロー構造です。Hook ベース権限モデルは上級レベルのCLAUDE.mdパターンで、プロジェクトに合わせて応用できるテスト済みの再現可能なアプローチを提供し、より効率的で一貫した成果を実現します。