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>
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
#!/bin/sh
|
||||
# 管什麼: subagent 推 gitea main 要先有總管戳記;wrangler 部署到非 stage 環境要 leo 手動解保險。
|
||||
# 為什麼: leo 2026-08-10——「subagent 推 main 你確認,推 prod 我確認」;規則存在但沒機制驗證有沒有照做,於是閘長在機器上。
|
||||
# 誤觸時怎麼關: 讀取/查狀態/演練一律放行;真要推 main,總管看過 commit 後 `git rev-parse --show-toplevel > /tmp/.main-push-ok`;真要打 stage,指令裡帶 staging/--env stage 字樣。
|
||||
# main-and-prod-push-guard.sh — PreToolUse(Bash):**兩層手動確認閘**
|
||||
#
|
||||
# 🔴 立這道閘的來由(leo 2026-08-10):
|
||||
@@ -34,9 +37,23 @@ except Exception: print("")
|
||||
|
||||
[ -z "$CMD" ] && exit 0
|
||||
|
||||
# 🔴 2026-08-20(inkstone/InkStoneCo#23、#56):heredoc 的 body 是資料,不是指令。
|
||||
# 直接對整條指令字串(含 heredoc 內文)做關鍵字掃描,會把「檔案內容/留言引用裡
|
||||
# 剛好提到 git push」當成「真的在推」——同一天兩起實撞:寫 docs/TESTING.md 的
|
||||
# 一行範例文字、貼一則引用 #56 敘述的 Gitea 留言,都被這樣擋下。
|
||||
# 拿掉 heredoc body 再比對,判準不變,範圍變準。失敗就退回原字串(fail-open
|
||||
# 在「這支有沒有幫上忙」,不是「放行與否」——見 hooks/lib/strip_heredoc.py 檔頭)。
|
||||
CMD=$(printf '%s' "$CMD" | python3 "$(dirname "$0")/lib/strip_heredoc.py" 2>/dev/null || printf '%s' "$CMD")
|
||||
|
||||
# ── 先放行明確不發佈的動作(讀取、查狀態、寫本地版控、演練)──────────────
|
||||
# 關鍵字出現在 commit 訊息、在 sed/grep 的參數裡,都不是「執行」。
|
||||
# 🔴 2026-08-20 修正(release-tag-guard.sh/stage-before-prod-guard.sh 已修過同款洞):
|
||||
# 舊版「CMD 開頭是讀取工具就整條放行」本身是穿牆路——
|
||||
# echo x && git push gitea HEAD:main
|
||||
# 開頭是 echo,照 glob 前綴比對會整條 exit 0,後面真的在推 main 完全不看。
|
||||
# ⇒ 改成「先看有沒有出現危險關鍵字,出現了就不吃開頭豁免」,交給下面的
|
||||
# 「指令位置」判準去判斷它是不是真的在執行。
|
||||
case "$CMD" in
|
||||
*"git push"*|*"wrangler deploy"*|*"wrangler publish"*|*"wrangler versions deploy"*) ;;
|
||||
sed\ *|cat\ *|grep\ *|head\ *|tail\ *|wc\ *|less\ *|ls\ *|awk\ *|rg\ *|echo\ *) exit 0 ;;
|
||||
*"git commit"*|*"git add"*|*"git tag"*|*"git stash"*) exit 0 ;;
|
||||
*"git status"*|*"git log"*|*"git diff"*|*"git show"*|*"git branch"*) exit 0 ;;
|
||||
@@ -91,73 +108,78 @@ stamp_ok() {
|
||||
return 0
|
||||
}
|
||||
|
||||
if true; then
|
||||
case "$CMD" in
|
||||
*"git push"*)
|
||||
# 只擋打到 main/master 的;推自己的 feature 分支照常放行
|
||||
case "$CMD" in
|
||||
# 🔴 2026-08-12 拿掉 `push -u` / `push --set-upstream` 這兩個條件。
|
||||
# 它們本來是想抓「沒寫分支的 push」,但實際抓到的是
|
||||
# `git push -u gitea fix/xxx`——**subagent 發表自己分支的標準動作**
|
||||
# (第一次推當然要 -u)。⇒ 舊版等於「agent 永遠推不出自己的分支」,
|
||||
# 而 leo 2026-08-12 的設計是「主線禁止動,大家都走 PR」,推分支是那條路的第一步。
|
||||
# 08-12 當天四張 PR 全是繞成 `git push gitea a:a` 才推出去的。
|
||||
# `*main*`/`*master*` 兩條照舊——真正該擋的是目標分支,不是有沒有帶旗標。
|
||||
*main*|*master*)
|
||||
stamp_ok && exit 0
|
||||
# reworked 2026-08-20 (inkstone/InkStoneCo#23, #56): 'does this command contain
|
||||
# git push' moved from a whole-string substring test to a command-position test --
|
||||
# same pattern already validated on release-tag-guard.sh: the phrase only counts
|
||||
# at the start of the string, or right after ; & | ( ` && ||. A heredoc body was
|
||||
# already stripped above; this closes the remaining gap where the phrase shows up
|
||||
# quoted inside the *same* command line (e.g. a python -c call whose string argument
|
||||
# discusses git push as prose).
|
||||
if printf '%s' "$CMD" | grep -qE '(^|[;&|(`]|&&|\|\|)[[:space:]]*git([[:space:]]+[^;&|]*)?[[:space:]]+push([[:space:]]|$)'; then
|
||||
# only block pushes that target main/master; pushing your own feature branch is fine
|
||||
#
|
||||
# 2026-08-12: dropped the `push -u` / `push --set-upstream` conditions -- those were
|
||||
# meant to catch "push with no branch named", but what they actually caught was
|
||||
# `git push -u gitea fix/xxx`, the normal first-push-of-a-branch shape for a
|
||||
# subagent. The target branch is what should be gated, not whether a flag is present.
|
||||
# 2026-08-20: match the target branch on a word boundary, not a bare substring --
|
||||
# a glob like *main* also matches "domain" (d-o-**m-a-i-n**), e.g. a push to
|
||||
# `fix/custom-domain-setup` would have false-positived.
|
||||
if printf '%s' "$CMD" | grep -qE '(^|[^A-Za-z])(main|master)([^A-Za-z]|$)'; then
|
||||
stamp_ok && exit 0
|
||||
|
||||
# ── 擋下的同時,把「誰想推什麼」留成一份請求(leo 2026-08-12)───────────
|
||||
#
|
||||
# leo 原話:「**它會問你的意見,所以每個你叫起來的 subagent 都有名字。**」
|
||||
#
|
||||
# 做得到的與做不到的,先講清楚:
|
||||
# ❌ **做不到「同步問總管」**——hook 跑在子 session 自己的行程裡,總管在另一個行程。
|
||||
# 要同步問只能 block 等一個檔案出現,那會把 subagent 掛死在那裡。
|
||||
# ✅ **做得到「當場擋 + 留下原始請求」**:總管在自己的迴圈裡讀這個目錄,
|
||||
# 看到的是 repo/分支/逐筆 commit 的**原始資料**,不是 subagent 的散文轉述。
|
||||
# ——這才是名字真正值錢的地方:**不是判斷你是誰,是留下是誰要求的**。
|
||||
#
|
||||
# 🔴 身分的方向刻意不改:**沒有名字不等於總管**(那是 fail-open——
|
||||
# 子 session 繼承環境變數,把名字拿掉就升格了)。放行的唯一憑證仍然只有
|
||||
# 那枚綁 repo、用完即丟的戳記。名字只是署名,不是權限。
|
||||
# 📌 名字只在 `claude -p` 起的子 session 上可靠(乾淨的環境邊界);
|
||||
# Agent tool 起的 subagent 與總管同一個行程、共用環境 ⇒ 那條路上名字塞不進也擦不掉。
|
||||
# 而改子 repo code 的正路本來就是 `claude -p`,所以夠用。
|
||||
_who="${CLAUDE_AGENT_NAME:-未署名}"
|
||||
_hookdir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) || _hookdir=""
|
||||
_reqdir="${_hookdir%/hooks}/pending-main-push"
|
||||
if [ -n "$_hookdir" ] && mkdir -p "$_reqdir" 2>/dev/null; then
|
||||
_root=$(git rev-parse --show-toplevel 2>/dev/null || printf 'unknown')
|
||||
# 檔名只用 ASCII(`未署名` 之類會被 tr 打成一排 dash,看不出是誰)
|
||||
_slugwho=$(printf '%s' "${CLAUDE_AGENT_NAME:-unnamed}" | tr -c 'A-Za-z0-9._-' '-')
|
||||
case "$_slugwho" in *[!-]*) : ;; *) _slugwho=unnamed ;; esac
|
||||
# ⚠️ 先 printf 再 tr:`basename` 會帶一個換行,直接餵 tr 會變成結尾多一根 dash
|
||||
_slugrepo=$(printf '%s' "$(basename "$_root")" | tr -c 'A-Za-z0-9._-' '-')
|
||||
_slug="${_slugwho}--${_slugrepo}"
|
||||
# ⚠️ 這幾行刻意用 `printf '%s\n' "整句"`,不要把內容寫進 printf 的格式字串裡。
|
||||
# 2026-08-12 實撞:格式字串裡同時有反引號與 %s 時,那幾行整行不見(而前後行都在),
|
||||
# ——**寫完當場肉眼檢查產出的檔案才發現**,hook 自己不會叫。內容一律當資料傳。
|
||||
_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || printf '?')
|
||||
_when=$(date '+%Y-%m-%d %H:%M:%S' 2>/dev/null || printf '?')
|
||||
_fence='```'
|
||||
{
|
||||
printf '%s\n\n' "# 推 main 的請求:$_who"
|
||||
printf '%s\n' "- repo:$_root"
|
||||
printf '%s\n' "- 分支:$_branch"
|
||||
printf '%s\n\n' "- 時間:$_when"
|
||||
printf '%s\n' "- 它想跑的指令:"
|
||||
printf '%s\n%s\n%s\n\n' "$_fence" "$CMD" "$_fence"
|
||||
printf '%s\n\n%s\n' "## 還沒推上去的 commit(原始資料,不是轉述)" "$_fence"
|
||||
git log --oneline '@{upstream}..HEAD' 2>/dev/null \
|
||||
|| git log --oneline -20 2>/dev/null \
|
||||
|| printf '(列不出來)\n'
|
||||
printf '%s\n\n%s\n\n%s\n' "$_fence" "## 改了哪些檔" "$_fence"
|
||||
git diff --stat '@{upstream}..HEAD' 2>/dev/null | tail -40 || printf '(列不出來)\n'
|
||||
printf '%s\n\n---\n%s\n' "$_fence" "總管裁完請刪掉這個檔——留著代表「還沒裁」。"
|
||||
} > "$_reqdir/$_slug.md" 2>/dev/null || true
|
||||
fi
|
||||
# ── 擋下的同時,把「誰想推什麼」留成一份請求(leo 2026-08-12)───────────
|
||||
#
|
||||
# leo 原話:「**它會問你的意見,所以每個你叫起來的 subagent 都有名字。**」
|
||||
#
|
||||
# 做得到的與做不到的,先講清楚:
|
||||
# ❌ **做不到「同步問總管」**——hook 跑在子 session 自己的行程裡,總管在另一個行程。
|
||||
# 要同步問只能 block 等一個檔案出現,那會把 subagent 掛死在那裡。
|
||||
# ✅ **做得到「當場擋 + 留下原始請求」**:總管在自己的迴圈裡讀這個目錄,
|
||||
# 看到的是 repo/分支/逐筆 commit 的**原始資料**,不是 subagent 的散文轉述。
|
||||
# ——這才是名字真正值錢的地方:**不是判斷你是誰,是留下是誰要求的**。
|
||||
#
|
||||
# 🔴 身分的方向刻意不改:**沒有名字不等於總管**(那是 fail-open——
|
||||
# 子 session 繼承環境變數,把名字拿掉就升格了)。放行的唯一憑證仍然只有
|
||||
# 那枚綁 repo、用完即丟的戳記。名字只是署名,不是權限。
|
||||
# 📌 名字只在 `claude -p` 起的子 session 上可靠(乾淨的環境邊界);
|
||||
# Agent tool 起的 subagent 與總管同一個行程、共用環境 ⇒ 那條路上名字塞不進也擦不掉。
|
||||
# 而改子 repo code 的正路本來就是 `claude -p`,所以夠用。
|
||||
_who="${CLAUDE_AGENT_NAME:-未署名}"
|
||||
_hookdir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) || _hookdir=""
|
||||
_reqdir="${_hookdir%/hooks}/pending-main-push"
|
||||
if [ -n "$_hookdir" ] && mkdir -p "$_reqdir" 2>/dev/null; then
|
||||
_root=$(git rev-parse --show-toplevel 2>/dev/null || printf 'unknown')
|
||||
# 檔名只用 ASCII(`未署名` 之類會被 tr 打成一排 dash,看不出是誰)
|
||||
_slugwho=$(printf '%s' "${CLAUDE_AGENT_NAME:-unnamed}" | tr -c 'A-Za-z0-9._-' '-')
|
||||
case "$_slugwho" in *[!-]*) : ;; *) _slugwho=unnamed ;; esac
|
||||
# ⚠️ 先 printf 再 tr:`basename` 會帶一個換行,直接餵 tr 會變成結尾多一根 dash
|
||||
_slugrepo=$(printf '%s' "$(basename "$_root")" | tr -c 'A-Za-z0-9._-' '-')
|
||||
_slug="${_slugwho}--${_slugrepo}"
|
||||
# ⚠️ 這幾行刻意用 `printf '%s\n' "整句"`,不要把內容寫進 printf 的格式字串裡。
|
||||
# 2026-08-12 實撞:格式字串裡同時有反引號與 %s 時,那幾行整行不見(而前後行都在),
|
||||
# ——**寫完當場肉眼檢查產出的檔案才發現**,hook 自己不會叫。內容一律當資料傳。
|
||||
_branch=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || printf '?')
|
||||
_when=$(date '+%Y-%m-%d %H:%M:%S' 2>/dev/null || printf '?')
|
||||
_fence='```'
|
||||
{
|
||||
printf '%s\n\n' "# 推 main 的請求:$_who"
|
||||
printf '%s\n' "- repo:$_root"
|
||||
printf '%s\n' "- 分支:$_branch"
|
||||
printf '%s\n\n' "- 時間:$_when"
|
||||
printf '%s\n' "- 它想跑的指令:"
|
||||
printf '%s\n%s\n%s\n\n' "$_fence" "$CMD" "$_fence"
|
||||
printf '%s\n\n%s\n' "## 還沒推上去的 commit(原始資料,不是轉述)" "$_fence"
|
||||
git log --oneline '@{upstream}..HEAD' 2>/dev/null \
|
||||
|| git log --oneline -20 2>/dev/null \
|
||||
|| printf '(列不出來)\n'
|
||||
printf '%s\n\n%s\n\n%s\n' "$_fence" "## 改了哪些檔" "$_fence"
|
||||
git diff --stat '@{upstream}..HEAD' 2>/dev/null | tail -40 || printf '(列不出來)\n'
|
||||
printf '%s\n\n---\n%s\n' "$_fence" "總管裁完請刪掉這個檔——留著代表「還沒裁」。"
|
||||
} > "$_reqdir/$_slug.md" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
cat >&2 <<'MSG'
|
||||
cat >&2 <<'MSG'
|
||||
🚫 推 main 要先有「總管決定了」的戳記(leo 2026-08-10 立)
|
||||
|
||||
leo 原話:
|
||||
@@ -196,11 +218,8 @@ leo 原話:
|
||||
|
||||
【真的該推 main 的例外】不存在。交回總管,一句話的事。
|
||||
MSG
|
||||
exit 2
|
||||
;;
|
||||
esac
|
||||
;;
|
||||
esac
|
||||
exit 2
|
||||
fi
|
||||
fi
|
||||
|
||||
# ── ② prod 部署要 leo 親手解保險 ────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user