-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash-secret-output-detector.sh
More file actions
executable file
·68 lines (57 loc) · 2.13 KB
/
Copy pathbash-secret-output-detector.sh
File metadata and controls
executable file
·68 lines (57 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/bash
# bash-secret-output-detector.sh — Warn when Bash output contains secrets
#
# Solves: Bash command output containing API keys, tokens, or credentials
# enters the LLM context window and gets sent to the API provider
# (#39882). PostToolUse hook that scans stdout for secret patterns.
#
# How it works: PostToolUse hook on Bash that checks command stdout for
# patterns matching API keys, tokens, passwords, and connection strings.
# Emits a systemMessage warning the model to ignore/not repeat secrets.
#
# TRIGGER: PostToolUse
# MATCHER: "Bash"
#
# Usage:
# {
# "hooks": {
# "PostToolUse": [{
# "matcher": "Bash",
# "hooks": [{ "type": "command", "command": "~/.claude/hooks/bash-secret-output-detector.sh" }]
# }]
# }
# }
INPUT=$(cat)
STDOUT=$(echo "$INPUT" | jq -r '.tool_result.stdout // empty' 2>/dev/null)
[ -z "$STDOUT" ] && exit 0
# Secret patterns (high-confidence, low false-positive)
FOUND=""
# AWS keys
if echo "$STDOUT" | grep -qE 'AKIA[0-9A-Z]{16}'; then
FOUND="${FOUND}AWS access key, "
fi
# Generic API keys/tokens (long hex/base64 strings after key= or token=)
if echo "$STDOUT" | grep -qiE '(api[_-]?key|api[_-]?secret|auth[_-]?token|access[_-]?token|secret[_-]?key)\s*[:=]\s*\S{20,}'; then
FOUND="${FOUND}API key/token, "
fi
# Connection strings with passwords
if echo "$STDOUT" | grep -qiE '(mysql|postgres|mongodb|redis)://[^:]+:[^@]+@'; then
FOUND="${FOUND}database connection string, "
fi
# Private keys
if echo "$STDOUT" | grep -qE '-----BEGIN (RSA |EC |OPENSSH )?PRIVATE KEY-----'; then
FOUND="${FOUND}private key, "
fi
# JWT tokens
if echo "$STDOUT" | grep -qE 'eyJ[A-Za-z0-9_-]{10,}\.eyJ[A-Za-z0-9_-]{10,}\.[A-Za-z0-9_-]+'; then
FOUND="${FOUND}JWT token, "
fi
# GitHub/GitLab tokens
if echo "$STDOUT" | grep -qE '(ghp|gho|ghs|ghr|glpat)_[A-Za-z0-9]{30,}'; then
FOUND="${FOUND}GitHub/GitLab token, "
fi
if [ -n "$FOUND" ]; then
FOUND="${FOUND%, }"
echo "{\"systemMessage\":\"⚠ SECRET DETECTED in command output: ${FOUND}. Do NOT repeat, log, or include these values in any output. They are now in context but should be treated as redacted.\"}"
fi
exit 0