Files
ISEP/hooks/tests/sdd-guard.test.sh
T
Leo 87186585d6 fix(hooks): sdd-guard.sh 修「解析失敗仍照擋、且訊息洩漏 /nonexistent」(InkStoneCo#22)
症狀(總管 2026-08-12 實撞):寫暫存腳本進 scratchpad
(/private/tmp/.../scratchpad/foo.py)被 sdd-guard.sh 攔下,訊息印出字面的
「/nonexistent/3-specs/ 下找不到任何 SDD」。

兩個洞:
- 洞 A:scratchpad 不在任何 git repo 裡,卻被當成「repo 裡的 code 變動」誤判
  需要 SDD。改成先問 path_in_git_worktree()(見 hooks/lib/path-resolve.sh):
  不在任何 git repo 裡 → SDD 天生管不到,直接放行,不必先猜專案根。
  這個檢查放在 $_root 的 case 分岔之前、對兩邊都適用——第一版只放進「專案外」
  分支,被本次新增的 hooks/tests/sdd-guard.test.sh 抓到一個不對稱漏洞(cwd 剛好
  等於 scratchpad 祖先目錄時會漏判),改成統一檢查後修掉。
- 洞 B:舊版用內部 sentinel `/nonexistent/3-specs` 重用既有的擋下路徑,但這個
  假路徑被直接印進使用者看到的訊息。改用 RESOLVED 旗標記解析成不成功,訊息
  改用人話描述原因,不洩漏假路徑。

fail-closed / fail-open 的判準(票上明確要求回答,不能各憑運氣):
真的落在某個 git repo 裡、但那個 repo 沒有 3-specs(或沒有 active SDD)→
仍然 fail-closed(擋)。理由:這道閘存在的目的就是防止「沒有 SDD 卻能動
code」,把「判斷不出來」直接放行,等於把環境跑歪(cwd 被切走、
$CLAUDE_PROJECT_DIR 沒設)悄悄變成「這道閘關掉了、且沒人知道」——silent
bypass 的代價遠高於多打一次確認。#22 紅線亦明寫「不要把閘改成解析失敗就
放行」。

順手修的殘留 cwd 依賴:SPECS_DIR 的預設值原本是相對路徑
「system-dev/docs/3-specs」,專案內迴圈找不到時會被拿去跟 hook 執行當下的
cwd 兜;改成絕對路徑 $_root/system-dev/docs/3-specs。

同時修 ADR-0001(ISEP 自建 wiki):標題與內文原本會讓人誤解成「ISEP plugin
裝到哪個 repo,就會在那裡自建一份 wiki」,但實際查證(marketplace.json 只宣告
hooks/commands/skills、README 明文排除 wiki/docs、hooks 一律用
${CLAUDE_PLUGIN_ROOT} 讀自己不是寫別處)並非如此——那份 wiki 只是 ISEP 這個
repo自己的開發歷史,跟裝 plugin 無關。唯一真的會在某 repo 建 wiki 的
scripts/install.sh 是 system-dev-template 的獨立安裝器殘留,要手動執行,
作用對象是 cwd 不是「plugin 裝到的地方」——這多半是誤解的真正來源,已在
ADR 的「常見誤解」段說明。

驗證:
- 造出 08-12 原始事故情境(cwd=InkStoneCo、CLAUDE_PROJECT_DIR 未設、寫
  scratchpad),修前擋(印 /nonexistent)、修後放行——實測輸出見票留言。
- 造出「真的在 git repo 裡但沒有 3-specs」情境,修後仍擋、訊息不含
  /nonexistent。
- 新增 hooks/tests/sdd-guard.test.sh:8 案例全過(洞 A/洞 B/fail-open
  陷阱/單一活性違反/恰好一份 active/改文件放行)。
- 既有六套 scripts/test-*.sh 全過,無退步。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-20 21:03:14 +08:00

91 lines
4.2 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.
#!/usr/bin/env bash
# sdd-guard.sh 的迴歸測試(inkstone/InkStoneCo#22)。
#
# 涵蓋兩個洞:
# 洞 A — scratchpad/任何不在 git repo 裡的暫存檔被誤判成「code 變動」而擋下。
# 洞 B — 真的解析失敗(fail-closed)時,訊息裡印出內部 sentinel `/nonexistent`。
# 以及既有行為不能退步:單一活性違反仍擋、恰好 1 份 active 仍放行、
# 「dirname 還沒建立」不可被誤判成「不在 repo 裡」(新邏輯自己可能引入的 fail-open 陷阱)。
#
# 用法:hooks/tests/sdd-guard.test.sh [hooks/sdd-guard.sh 的路徑]
# 🔴 全程在一個乾淨的 TMP 底下建假 repo,跑完自己清;不動任何真 repo。
set -u
HOOK="${1:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/sdd-guard.sh}"
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"' EXIT
PASS=0; FAIL=0
mk() { # mk <file_path> -> JSON on stdout
python3 -c "import json,sys;print(json.dumps({'tool_name':'Write','tool_input':{'file_path':sys.argv[1],'content':'x'}}))" "$1"
}
t() { # t <期望 exit code> <說明> <file_path> [額外檢查關鍵字]
local want="$1" desc="$2" path="$3" must_not_contain="${4:-}"
local out rc
out=$(mk "$path" | "$HOOK" 2>&1)
rc=$?
local ok=1
[ "$rc" -eq "$want" ] || ok=0
if [ -n "$must_not_contain" ] && printf '%s' "$out" | grep -qF "$must_not_contain"; then
ok=0
fi
if [ "$ok" -eq 1 ]; then
echo " ✅ $desc"; PASS=$((PASS+1))
else
echo " ❌ $desc —— 期望 exit=$want,實得 exit=$rc"
[ -n "$must_not_contain" ] && echo " (且訊息不該含「$must_not_contain」)"
echo " 輸出:$out" | head -3
FAIL=$((FAIL+1))
fi
}
# ── 準備:一個真的沒有 3-specs 的 git repo(模擬「真的解析失敗」)──
REPO_NO_SDD="$TMP/repo-no-sdd"
mkdir -p "$REPO_NO_SDD/src"
git init -q "$REPO_NO_SDD"
# ── 準備:一個有 1 份 active SDD 的 git repo ──
REPO_ONE_ACTIVE="$TMP/repo-one-active"
mkdir -p "$REPO_ONE_ACTIVE/system-dev/docs/3-specs/x" "$REPO_ONE_ACTIVE/src"
git init -q "$REPO_ONE_ACTIVE"
printf -- '---\nstatus: active\n---\n# X\n' > "$REPO_ONE_ACTIVE/system-dev/docs/3-specs/x/design.md"
# ── 準備:一個有 2 份 active SDD 的 git repo(單一活性違反)──
REPO_MULTI="$TMP/repo-multi-active"
mkdir -p "$REPO_MULTI/system-dev/docs/3-specs/a" "$REPO_MULTI/system-dev/docs/3-specs/b" "$REPO_MULTI/src"
git init -q "$REPO_MULTI"
printf -- '---\nstatus: active\n---\n# A\n' > "$REPO_MULTI/system-dev/docs/3-specs/a/design.md"
printf -- '---\nstatus: active\n---\n# B\n' > "$REPO_MULTI/system-dev/docs/3-specs/b/design.md"
# ── 準備:scratchpad 風格的暫存區(不在任何 git repo 裡)──
SCRATCH="$TMP/private/tmp/claude-fake-session/scratchpad"
mkdir -p "$SCRATCH"
# 讓 $_rootCLAUDE_PROJECT_DIR 或 pwd)刻意跟這些假 repo 對不上,
# 逼所有案例都走「專案外的路徑」那個分支——這正是 #22 實撞的情境(cwd 跑歪/
# CLAUDE_PROJECT_DIR 沒設,路徑不落在 $_root 底下)。
unset CLAUDE_PROJECT_DIR
cd "$TMP"
echo "── 洞 A:不在任何 git repo 裡的路徑,SDD 管不到,該放行 ──"
t 0 "scratchpad 暫存 .py(本票原始事故)" "$SCRATCH/fix-project-settings.py"
t 0 "scratchpad 巢狀更深" "$SCRATCH/nested/deep/tmp.js"
echo "── 洞 B:真的解析失敗(repo 存在但沒有 3-specs)仍要 fail-closed,但訊息不准洩漏內部假路徑 ──"
t 2 "真 repo 沒有 3-specs → 仍擋" "$REPO_NO_SDD/src/foo.py"
t 2 "上面那筆的訊息不准出現 /nonexistent" "$REPO_NO_SDD/src/foo.py" "/nonexistent"
echo "── fail-open 陷阱:新檔案要建在還沒建立的子目錄下,不可被誤判成「不在 repo 裡」──"
t 2 "真 repo、目標子目錄還沒建立 → 仍擋(不能因為 dirname 不存在就放行)" "$REPO_NO_SDD/brand-new/not-yet/bar.py"
echo "── 既有行為不能退步 ──"
t 0 "只有 1 份 active SDD,改 code 檔 → 放行" "$REPO_ONE_ACTIVE/src/x.py"
t 2 "2 份 active SDD(單一活性違反)→ 擋" "$REPO_MULTI/src/x.py"
t 0 "改 .md 文件(非 code 檔)→ 放行,即使找不到 3-specs" "$REPO_NO_SDD/README.md"
echo
echo "結果:通過 $PASS 失敗 $FAIL"
[ "$FAIL" -eq 0 ] || exit 1