Files
ISEP/scripts/test-kbdb-api-wall-guard-bash.sh
T
Leo 0cdb6f2c05 fix(hooks): 讓四支閘認得出「指令位置」跟「heredoc/引號裡的文字」
inkstone/InkStoneCo#23、#56 同一個病:閘對整條指令字串做關鍵字掃描,
把「檔案內容/留言引用裡剛好提到某個關鍵字」當成「真的在執行」,
同時放過包一層讀取指令、或藏在 heredoc body 裡的真動作。

- 新增共用輔助 hooks/lib/strip_heredoc.py:heredoc body 是資料不是指令,
  四支閘(github-contact / main-and-prod-push / stage-before-prod /
  kbdb-api-wall 的 Bash 分支)呼叫前一律先拿掉 body 再比對。
- main-and-prod-push-guard.sh:修掉跟 release-tag-guard.sh 同款的
  「開頭是讀取工具就整條放行」前綴繞過洞;git push 的偵測改成指令位置比對;
  main/master 目標改用單字邊界,不再誤中 "domain" 這種子字串。
- github-contact-guard.sh:拿掉 gh CLI/git push 判準裡「前面隨便一個空白
  就算數」的鬆散邊界,只認真正的指令分隔符。
- kbdb-api-wall-guard.sh:Bash 分支原本引用不存在的 kbdb_cmd_check.py,
  python3 找不到檔案就吃掉錯誤印 "OK",該分支形同虛設——任何
  `wrangler d1 execute` 直打 kbdb 都會被放行。邏輯搬進新檔
  hooks/lib/kbdb_cmd_check.py(shlex 分詞、quote-aware),把 .sh 的
  參照路徑改過去,補回 Bash 分支的 kbdb-sql-ok 逃生口。

四支各補 InkStoneCo#40 §1 要求的三行中文檔頭。

新增四支可重跑測試(scripts/test-*.sh),共 69 條斷言全過,
含 #23/#56 票上實撞的原始形狀(寫 docs/TESTING.md 的 heredoc、
貼引用 #56 敘述的留言、`grep git push`)。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-20 18:51:52 +08:00

42 lines
1.9 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
# kbdb-api-wall-guard 的 Bash 分支測試(inkstone/ISEP#30 群組)
#
# 這支補的是一個真的漏擋:hooks/kbdb-api-wall-guard.sh 的 Bash 分支原本引用
# hooks/kbdb_cmd_check.py(不存在的路徑),python3 找不到檔案就以非零結束,
# 而呼叫端寫成 `... || echo "OK"` —— 於是這個分支永遠印 "OK"Bash 分支形同虛設,
# 任何 `wrangler d1 execute` 直打 kbdb 都會被放行。修法:邏輯搬進
# hooks/lib/kbdb_cmd_check.py(新檔),並把 .sh 裡的參照路徑改過去。
#
# 判準:真的用 wrangler 對 kbdb 這顆 D1 下 execute 要擋;只是提到(commit message、
# heredoc body、grep 搜尋)、或目標不是 kbdb 的 D1,都不准擋。
cd "$(dirname "$0")/.." || exit 1
H=hooks/kbdb-api-wall-guard.sh
PASS=0; FAIL=0
run(){ # $1=want $2=cmd
printf '%s' "{\"tool_name\":\"Bash\",\"tool_input\":{\"command\":$(python3 -c 'import json,sys;print(json.dumps(sys.argv[1]))' "$2")}}" \
| bash "$H" >/dev/null 2>&1
got=$?
if [ "$got" = "$1" ]; then PASS=$((PASS+1)); printf ' ✅ '; else FAIL=$((FAIL+1)); printf ' ❌ '; fi
printf 'want=%s got=%s %.72s\n' "$1" "$got" "$2"
}
w="wrangler"; sub="d1 execute"; db="arcrun-kbdb"
echo "── 該擋:真的直打 kbdb 這顆 D1 執行 SQL ──"
run 2 "$w $sub $db --remote --command \"SELECT 1\""
run 2 "npx $w $sub $db --command \"SELECT 1\""
run 2 "echo start && $w $sub $db --command \"SELECT 1\""
echo "── 不該擋:只是提到、heredoc body、目標不是 kbdb ──"
run 0 "git commit -m \"ran $w $sub $db earlier, see ticket\""
run 0 "$(printf 'cat > note.md <<%sEOF%s\nwe should avoid %s %s %s\nEOF\n' "'" "'" "$w" "$sub" "$db")"
run 0 "grep -rn \"$w $sub\" hooks/"
run 0 "$w $sub some-other-db --command \"SELECT 1\""
run 0 "$w deploy --env stage"
run 0 'echo hello world'
run 0 "$w $sub $db --command \"SELECT 1\" kbdb-sql-ok"
echo
echo "$PASS/$((PASS+FAIL)) 通過"
[ "$FAIL" -eq 0 ]