Browse Source

merge: fix/hook-stdin-json — hooks read stdin JSON

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
0xDarkMatter 5 days ago
parent
commit
2b51b342e8
3 changed files with 31 additions and 4 deletions
  1. 10 3
      hooks/dangerous-cmd-warn.sh
  2. 10 1
      hooks/enforce-uv.sh
  3. 11 0
      hooks/pre-commit-lint.sh

+ 10 - 3
hooks/dangerous-cmd-warn.sh

@@ -18,10 +18,17 @@
 #   2 = block with message (dangerous command detected)
 
 INPUT="$1"
-
-if [[ -z "$INPUT" ]]; then
-  exit 0
+# Modern Claude Code delivers the tool call as JSON on stdin
+# ({"tool_input":{"command":"..."}}); older configs pass it as $TOOL_INPUT/$1.
+# Support both so the hook works regardless of harness version.
+if [[ -z "$INPUT" && ! -t 0 ]]; then
+  RAW="$(cat 2>/dev/null)"
+  if [[ -n "$RAW" ]] && command -v jq >/dev/null 2>&1; then
+    INPUT="$(printf '%s' "$RAW" | jq -r '.tool_input.command // .tool_input // empty' 2>/dev/null)"
+  fi
+  [[ -z "$INPUT" ]] && INPUT="$RAW"
 fi
+[[ -z "$INPUT" ]] && exit 0
 
 # -------------------------------------------------------------------
 # Dangerous patterns and their risk descriptions

+ 10 - 1
hooks/enforce-uv.sh

@@ -28,7 +28,16 @@
 #   - Honors ENFORCE_UV=0 to disable for a single command or session.
 
 INPUT="$1"
-
+# Modern Claude Code delivers the tool call as JSON on stdin
+# ({"tool_input":{"command":"..."}}); older configs pass it as $TOOL_INPUT/$1.
+# Support both so the hook works regardless of harness version.
+if [[ -z "$INPUT" && ! -t 0 ]]; then
+  RAW="$(cat 2>/dev/null)"
+  if [[ -n "$RAW" ]] && command -v jq >/dev/null 2>&1; then
+    INPUT="$(printf '%s' "$RAW" | jq -r '.tool_input.command // .tool_input // empty' 2>/dev/null)"
+  fi
+  [[ -z "$INPUT" ]] && INPUT="$RAW"
+fi
 [[ -z "$INPUT" ]] && exit 0
 [[ "$ENFORCE_UV" == "0" ]] && exit 0
 

+ 11 - 0
hooks/pre-commit-lint.sh

@@ -14,6 +14,17 @@
 # }
 
 INPUT="$1"
+# Modern Claude Code delivers the tool call as JSON on stdin
+# ({"tool_input":{"command":"..."}}); older configs pass it as $TOOL_INPUT/$1.
+# Support both so the hook works regardless of harness version.
+if [[ -z "$INPUT" && ! -t 0 ]]; then
+  RAW="$(cat 2>/dev/null)"
+  if [[ -n "$RAW" ]] && command -v jq >/dev/null 2>&1; then
+    INPUT="$(printf '%s' "$RAW" | jq -r '.tool_input.command // .tool_input // empty' 2>/dev/null)"
+  fi
+  [[ -z "$INPUT" ]] && INPUT="$RAW"
+fi
+[[ -z "$INPUT" ]] && exit 0
 
 # Only trigger on git commit commands
 if ! echo "$INPUT" | grep -qE 'git\s+commit'; then