Files
system-dev-template/template/.claude/hooks/pre-write-guard.sh
T
Leo 39783cccc8 feat: 接關 hook + SDD 強制 hook(實作 wishlist 兩項)
§1 接關機制(雙保險):
- session-start-recall.sh:SessionStart 自動注入 status 重點 + 快照核實提醒
- /wiki-recall:fallback 命令,hook 失效時手動接關

§2 軟規範 → 硬攔截:
- sdd-guard.sh:動 code 檔但無 SDD → exit 2 擋(/sdd-check 自動版)
- pre-write-guard.sh:專案自訂禁令骨架(預設停用)
- settings.json:掛 SessionStart + PreToolUse

配套:install.sh 下載 hooks/settings(settings 比照 CLAUDE.md 不覆蓋);
README/CLAUDE.md 補文件 + 誠實限制聲明。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-15 16:04:58 +08:00

53 lines
1.7 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# PreToolUse hook 範本骨架 —— 專案自訂禁令
# wishlist §2 可選:讓使用者自訂專案禁令(例:「KBDB 禁動表」「某目錄唯讀」)。
#
# 預設不啟用。要用時:
# 1. 在下面 FORBIDDEN_PATTERNS 填入禁改的路徑/檔名 pattern
# 2. 到 .claude/settings.json 的 PreToolUse 加掛這支
#
# 掛在 PreToolUsematcher: Write|Edit)。stdin 收到 JSON{ tool_name, tool_input: { file_path } }
# 命中禁令 → exit 2 擋。
#
# 誠實限制:只擋直接寫檔。bash 繞道、helper 間接改動擋不到。留痕可審 ≠ 技術防偽。
set -euo pipefail
# ── 專案自訂:禁改的 pattern(一行一個,case glob 語法)──────
# 範例(已註解,啟用前請改成自己的):
# "*/db/schema.sql" # 禁手改 schema
# "*/migrations/*" # migration 一旦建立不可改
FORBIDDEN_PATTERNS=(
# "*/your/protected/path/*"
)
# 沒設任何禁令 → 直接放行(骨架預設狀態)
[ ${#FORBIDDEN_PATTERNS[@]} -eq 0 ] && exit 0
INPUT=$(cat)
if command -v jq >/dev/null 2>&1; then
FILE_PATH=$(printf '%s' "$INPUT" | jq -r '.tool_input.file_path // empty')
else
FILE_PATH=$(printf '%s' "$INPUT" | grep -o '"file_path"[[:space:]]*:[[:space:]]*"[^"]*"' | head -1 | sed 's/.*"file_path"[[:space:]]*:[[:space:]]*"//;s/"$//')
fi
[ -z "$FILE_PATH" ] && exit 0
for pattern in "${FORBIDDEN_PATTERNS[@]}"; do
# shellcheck disable=SC2254
case "$FILE_PATH" in
$pattern)
cat >&2 <<EOF
🚫 專案禁令攔截:$FILE_PATH 命中禁改規則($pattern)。
這是本專案 .claude/hooks/pre-write-guard.sh 設定的硬底線。
要動 → 先和負責人確認,並更新禁令設定。
EOF
exit 2
;;
esac
done
exit 0