簡單配置 Claude Code Hooks — 危險指令攔截 + 任務完成通知
Claude Code 的 Hooks 機制可以在特定事件觸發時自動執行腳本,像是在執行 Bash 指令前先檢查有沒有危險操作、或是在任務完成時跳出桌面通知提醒你回來看結果。
這篇會帶你從零開始設定三個實用的 Hook:
- 攔截危險指令 — 避免 Claude 執行
rm -rf、git push --force等破壞性操作 - 保護敏感檔案 — 防止
.env、金鑰等機密檔案被意外修改 - 任務完成通知 — 透過 Windows 桌面 Toast 通知提醒你任務跑完了
前置準備:安裝 BurntToast 模組
要在 Windows 上實現桌面通知,我們需要用到 PowerShell 的 BurntToast 模組。開啟終端機執行:
powershell -ExecutionPolicy Bypass -Command "Install-Module -Name BurntToast -Force -Scope CurrentUser"
NOTE — 關於 ExecutionPolicy
-ExecutionPolicy Bypass 是為了避免 PowerShell 預設的執行原則阻擋安裝。-Scope CurrentUser 代表只裝在當前使用者,不需要系統管理員權限。
安裝完成後可以先測試一下通知有沒有正常運作:
powershell -Command "Import-Module BurntToast; New-BurntToastNotification -Text 'Test', '通知測試成功'"
如果右下角有跳出通知,就代表安裝成功了。
建立 Hook 腳本
在 ~/.claude/hooks/ 目錄下建立兩個腳本檔案。
1. 攔截危險指令 block-dangerous-commands.sh
這個腳本會在 Claude 執行任何 Bash 指令前被觸發,檢查指令內容是否包含危險操作:
#!/bin/bash
# Claude Code Hook: 攔截危險指令
# 從 stdin 讀取 JSON 格式的 tool input
input=$(cat)
command=$(echo "$input" | jq -r '.tool_input.command // empty')
# 定義危險指令 pattern
dangerous_patterns=(
"rm -rf /"
"rm -rf ~"
"git push.*--force"
"git reset --hard"
"mkfs\."
"> /dev/sda"
"dd if="
":(){ :|:& };:"
)
for pattern in "${dangerous_patterns[@]}"; do
if echo "$command" | grep -qEi "$pattern"; then
echo "BLOCKED: 偵測到危險指令 — $command"
echo "如果你確定要執行,請手動在終端機中操作。"
exit 2
fi
done
exit 0
2. 保護敏感檔案 protect-sensitive-files.sh
這個腳本會在 Claude 嘗試編輯或寫入檔案前觸發,保護機密檔案不被修改:
#!/bin/bash
# Claude Code Hook: 保護敏感檔案
input=$(cat)
file_path=$(echo "$input" | jq -r '.tool_input.file_path // empty')
# 定義受保護的檔案 pattern
protected_patterns=(
"\.env$"
"\.env\."
"credentials"
"secret"
"\.pem$"
"\.key$"
"id_rsa"
"\.ssh/config"
)
for pattern in "${protected_patterns[@]}"; do
if echo "$file_path" | grep -qEi "$pattern"; then
echo "BLOCKED: 嘗試修改受保護的檔案 — $file_path"
echo "如果需要修改敏感檔案,請手動編輯。"
exit 2
fi
done
exit 0
建立完成後,記得給腳本執行權限:
chmod +x ~/.claude/hooks/block-dangerous-commands.sh
chmod +x ~/.claude/hooks/protect-sensitive-files.sh
設定 settings.json
開啟 Claude Code 的設定檔 ~/.claude/settings.json,加入以下 hooks 區塊:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": "\"$HOME\"/.claude/hooks/block-dangerous-commands.sh"
}
]
},
{
"matcher": "Edit|Write",
"hooks": [
{
"type": "command",
"command": "\"$HOME\"/.claude/hooks/protect-sensitive-files.sh"
}
]
}
],
"Stop": [
{
"matcher": "",
"hooks": [
{
"type": "command",
"command": "powershell.exe -Command \"Import-Module BurntToast; New-BurntToastNotification -Text 'Claude Code', '任務完成,請檢查結果'\""
}
]
}
]
}
}
TIP — 各區塊說明
- PreToolUse — 在工具執行「之前」觸發。
matcher用來比對工具名稱,支援正則表達式 - Stop — 在 Claude 完成任務停止時觸發。
matcher留空代表所有情況都會觸發 - Hook 腳本 exit code 為
2時代表「阻擋」該操作,Claude 會看到阻擋訊息並停止執行
運作流程
設定完成後的運作方式:
- 當 Claude 要執行 Bash 指令 → 先跑
block-dangerous-commands.sh檢查 - 當 Claude 要編輯或建立檔案 → 先跑
protect-sensitive-files.sh檢查 - 當 Claude 完成任務停止 → 自動彈出 Windows 桌面通知
如果 Hook 攔截到危險操作,Claude 會收到阻擋訊息並停下來,你可以自行決定是否手動執行。
WARNING — Git Bash 使用者注意
如果你在 Windows 上使用 Git Bash,$HOME 路徑可能和 PowerShell 的不同。確認腳本路徑正確指向 C:/Users/<你的使用者名稱>/.claude/hooks/。
小結
Hooks 是 Claude Code 裡面非常實用的安全機制,幾行設定就能幫你擋掉大部分的風險操作。桌面通知更是讓你可以放心把任務丟給 Claude 跑,完成時自動提醒你回來檢查。