mvp-ai's picture
download
raw
3.26 kB
#!/usr/bin/env bash
# PostToolUse hook: scan the file Claude just wrote/edited and feed findings back.
#
# Reads the Claude Code hook input JSON from stdin, extracts the edited path from
# common Write/Edit/NotebookEdit schemas, runs `foxguard --format json` on it,
# and emits a compact summary on stderr with exit 2 when medium+ findings exist
# so Claude treats it as actionable feedback.
# Stays silent (exit 0) on clean files, missing tools, or unreadable inputs — a
# scanner hook should never block Claude on its own machinery.
set -uo pipefail
script_dir=$(CDPATH= cd -- "$(dirname -- "${BASH_SOURCE[0]}")" 2>/dev/null && pwd -P)
state_enabled=0
if [ -n "$script_dir" ] && [ -r "$script_dir/finding-state.sh" ] \
&& . "$script_dir/finding-state.sh" 2>/dev/null; then
state_enabled=1
fi
scan_status=0
session_id=
cwd=
repo_root=
normalized_file_path=
state_active=0
input=$(cat)
file_path=$(printf '%s' "$input" | jq -r '
.tool_input.file_path
// .tool_input.path
// .tool_input.notebook_path
// .tool_response.filePath
// .tool_response.file_path
// empty
' 2>/dev/null)
session_id=$(printf '%s' "$input" | jq -r '.session_id // empty' 2>/dev/null)
cwd=$(printf '%s' "$input" | jq -r '.cwd // empty' 2>/dev/null)
[ -z "$file_path" ] && exit 0
if [ "$state_enabled" = "1" ] \
&& repo_root=$(fg_state_repository_root "$cwd") \
&& normalized_file_path=$(fg_state_normalize_file_path "$cwd" "$file_path" "$repo_root"); then
file_path=$normalized_file_path
state_active=1
fi
[ ! -f "$file_path" ] && exit 0
[ ! -r "$file_path" ] && exit 0
# Resolve foxguard binary: prefer PATH, fall back to npx.
if command -v foxguard >/dev/null 2>&1; then
fg=(foxguard)
elif command -v npx >/dev/null 2>&1; then
fg=(npx --yes foxguard)
else
exit 0
fi
min_severity="${FOXGUARD_HOOK_SEVERITY:-medium}"
if scan_output=$("${fg[@]}" --format json --severity "$min_severity" -- "$file_path" 2>/dev/null); then
scan_status=0
else
scan_status=$?
fi
[ -z "$scan_output" ] && exit 0
findings=$(printf '%s' "$scan_output" | jq -ce '
if type == "array" then
.
elif type == "object" and (.findings | type == "array") then
.findings
else
error("invalid foxguard JSON")
end
' 2>/dev/null) || exit 0
if [ "$findings" = "[]" ]; then
if [ "$state_active" = "1" ] && [ "$scan_status" = "0" ]; then
fg_state_clear_findings "$session_id" "$cwd" "$file_path" || :
fi
exit 0
fi
count=$(printf '%s' "$findings" | jq 'length' 2>/dev/null)
if [ -z "$count" ] || [ "$count" = "0" ]; then
exit 0
fi
if [ "$state_active" = "1" ]; then
case "$scan_status" in
0|1) fg_state_record_findings "$session_id" "$cwd" "$file_path" "$min_severity" "$findings" || : ;;
esac
fi
{
printf 'foxguard found %s issue(s) in %s (severity >= %s):\n\n' "$count" "$file_path" "$min_severity"
printf '%s' "$findings" | jq -r '
.[] |
" [\(.severity // "unknown" | tostring | ascii_upcase)] \(.rule_id // "unknown-rule") at line \(.line // "?")\n" +
" \(.description // "No description")\n" +
" \(.cwe // "")\n" +
" > \(.snippet // "" | tostring)\n"
'
printf '\nFix these before continuing. Run `/foxguard:scan` for the full repo or `/foxguard:triage` for the interactive TUI.\n'
} >&2
exit 2

Xet Storage Details

Size:
3.26 kB
·
Xet hash:
f7e10a0189283e3c974b878f53ae9e4fb35114c6d26b4d906065f19b2dd95cf9

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.