cc-safe-setup 29.6.0 → 29.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/COOKBOOK.md +70 -0
- package/README.md +43 -4
- package/TROUBLESHOOTING.md +30 -0
- package/examples/api-rate-limit-tracker.sh +51 -0
- package/examples/auto-answer-question.sh +67 -0
- package/examples/auto-approve-readonly-tools.sh +10 -0
- package/examples/aws-production-guard.sh +40 -0
- package/examples/banned-command-guard.sh +48 -0
- package/examples/bash-heuristic-approver.sh +59 -0
- package/examples/block-database-wipe.sh +1 -1
- package/examples/classifier-fallback-allow.sh +70 -0
- package/examples/commit-message-check.sh +8 -1
- package/examples/commit-message-quality.sh +35 -0
- package/examples/credential-exfil-guard.sh +12 -0
- package/examples/cwd-reminder.sh +37 -0
- package/examples/dependency-install-guard.sh +84 -0
- package/examples/deploy-guard.sh +1 -1
- package/examples/detect-mixed-indentation.sh +33 -0
- package/examples/disk-space-check.sh +42 -0
- package/examples/docker-dangerous-guard.sh +47 -0
- package/examples/dockerfile-lint.sh +58 -0
- package/examples/edit-always-allow.sh +53 -0
- package/examples/env-file-gitignore-check.sh +39 -0
- package/examples/env-source-guard.sh +1 -1
- package/examples/git-stash-before-danger.sh +58 -0
- package/examples/github-actions-guard.sh +49 -0
- package/examples/gitignore-auto-add.sh +30 -0
- package/examples/go-vet-after-edit.sh +33 -0
- package/examples/hook-tamper-guard.sh +67 -0
- package/examples/kubernetes-guard.sh +2 -1
- package/examples/large-file-write-guard.sh +40 -0
- package/examples/main-branch-warn.sh +40 -0
- package/examples/max-edit-size-guard.sh +9 -15
- package/examples/mcp-server-guard.sh +70 -0
- package/examples/multiline-command-approver.sh +89 -0
- package/examples/no-base64-exfil.sh +27 -0
- package/examples/no-debug-commit.sh +60 -0
- package/examples/no-exposed-port-in-dockerfile.sh +32 -0
- package/examples/no-fixme-ship.sh +41 -0
- package/examples/no-hardcoded-ip.sh +26 -0
- package/examples/no-http-in-code.sh +19 -0
- package/examples/no-push-without-tests.sh +33 -0
- package/examples/no-self-signed-cert.sh +19 -0
- package/examples/no-star-import-python.sh +28 -0
- package/examples/no-wget-piped-bash.sh +22 -0
- package/examples/node-version-check.sh +40 -0
- package/examples/npm-publish-guard.sh +5 -2
- package/examples/output-token-env-check.sh +44 -0
- package/examples/package-lock-frozen.sh +25 -0
- package/examples/pip-venv-required.sh +40 -0
- package/examples/port-conflict-check.sh +62 -0
- package/examples/prefer-builtin-tools.sh +33 -0
- package/examples/python-import-check.sh +52 -0
- package/examples/python-ruff-on-edit.sh +51 -0
- package/examples/quoted-flag-approver.sh +51 -0
- package/examples/react-key-warn.sh +32 -0
- package/examples/rm-safety-net.sh +9 -0
- package/examples/rust-clippy-after-edit.sh +37 -0
- package/examples/session-quota-tracker.sh +44 -0
- package/examples/session-start-safety-check.sh +60 -0
- package/examples/session-summary-stop.sh +49 -0
- package/examples/session-time-limit.sh +34 -0
- package/examples/temp-file-cleanup.sh +41 -0
- package/examples/test-before-push.sh +8 -1
- package/examples/test-coverage-reminder.sh +49 -0
- package/examples/test-exit-code-verify.sh +60 -0
- package/examples/tool-file-logger.sh +46 -0
- package/examples/typescript-lint-on-edit.sh +61 -0
- package/examples/typescript-strict-check.sh +35 -0
- package/examples/uncommitted-changes-stop.sh +16 -0
- package/examples/uncommitted-discard-guard.sh +72 -0
- package/examples/worktree-unmerged-guard.sh +13 -3
- package/examples/yaml-syntax-check.sh +50 -0
- package/index.mjs +3 -0
- package/package.json +2 -2
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# output-token-env-check.sh — Warn if max output tokens is not configured
|
|
3
|
+
#
|
|
4
|
+
# Solves: "Response exceeded 32000 output token maximum" error
|
|
5
|
+
# (#24055 — 80 reactions)
|
|
6
|
+
#
|
|
7
|
+
# Claude Code defaults to 32,000 max output tokens. For complex tasks,
|
|
8
|
+
# this is often not enough. Setting CLAUDE_CODE_MAX_OUTPUT_TOKENS
|
|
9
|
+
# prevents the error, but many users don't know about this env var.
|
|
10
|
+
#
|
|
11
|
+
# This hook checks on session start (Notification/Stop) and warns
|
|
12
|
+
# if the env var is not set or is set to the default 32000.
|
|
13
|
+
#
|
|
14
|
+
# TRIGGER: Notification
|
|
15
|
+
# MATCHER: ""
|
|
16
|
+
#
|
|
17
|
+
# Usage:
|
|
18
|
+
# {
|
|
19
|
+
# "hooks": {
|
|
20
|
+
# "Notification": [{
|
|
21
|
+
# "matcher": "",
|
|
22
|
+
# "hooks": [{ "type": "command", "command": "~/.claude/hooks/output-token-env-check.sh" }]
|
|
23
|
+
# }]
|
|
24
|
+
# }
|
|
25
|
+
# }
|
|
26
|
+
|
|
27
|
+
# Only run once per session (check if we already warned)
|
|
28
|
+
MARKER="/tmp/cc-output-token-warned-$$"
|
|
29
|
+
[ -f "$MARKER" ] && exit 0
|
|
30
|
+
|
|
31
|
+
MAX_TOKENS="${CLAUDE_CODE_MAX_OUTPUT_TOKENS:-}"
|
|
32
|
+
|
|
33
|
+
if [ -z "$MAX_TOKENS" ]; then
|
|
34
|
+
echo "TIP: CLAUDE_CODE_MAX_OUTPUT_TOKENS is not set (default: 32,000)." >&2
|
|
35
|
+
echo " For complex tasks, set it higher to avoid truncated responses:" >&2
|
|
36
|
+
echo " export CLAUDE_CODE_MAX_OUTPUT_TOKENS=128000" >&2
|
|
37
|
+
touch "$MARKER"
|
|
38
|
+
elif [ "$MAX_TOKENS" -le 32000 ] 2>/dev/null; then
|
|
39
|
+
echo "TIP: CLAUDE_CODE_MAX_OUTPUT_TOKENS=$MAX_TOKENS (low for complex tasks)." >&2
|
|
40
|
+
echo " Consider: export CLAUDE_CODE_MAX_OUTPUT_TOKENS=128000" >&2
|
|
41
|
+
touch "$MARKER"
|
|
42
|
+
fi
|
|
43
|
+
|
|
44
|
+
exit 0
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# package-lock-frozen.sh — Block modifications to lockfiles
|
|
3
|
+
#
|
|
4
|
+
# Prevents: Unintended lockfile changes that cause merge conflicts
|
|
5
|
+
# and dependency drift. Claude should use npm ci, not npm install.
|
|
6
|
+
#
|
|
7
|
+
# Blocks: Edit/Write to package-lock.json, yarn.lock, pnpm-lock.yaml
|
|
8
|
+
#
|
|
9
|
+
# TRIGGER: PreToolUse
|
|
10
|
+
# MATCHER: "Edit|Write"
|
|
11
|
+
|
|
12
|
+
INPUT=$(cat)
|
|
13
|
+
FILE=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.filePath // empty' 2>/dev/null)
|
|
14
|
+
[ -z "$FILE" ] && exit 0
|
|
15
|
+
|
|
16
|
+
BASENAME=$(basename "$FILE")
|
|
17
|
+
case "$BASENAME" in
|
|
18
|
+
package-lock.json|yarn.lock|pnpm-lock.yaml|Cargo.lock|poetry.lock|Gemfile.lock|composer.lock)
|
|
19
|
+
echo "BLOCKED: Direct modification of lockfile '$BASENAME'." >&2
|
|
20
|
+
echo " Use the package manager to update dependencies instead." >&2
|
|
21
|
+
exit 2
|
|
22
|
+
;;
|
|
23
|
+
esac
|
|
24
|
+
|
|
25
|
+
exit 0
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# pip-venv-required.sh — Block pip install outside of a virtual environment
|
|
3
|
+
#
|
|
4
|
+
# Prevents: System-wide pip install that can break the OS Python.
|
|
5
|
+
# Only allows pip install when a virtualenv is active.
|
|
6
|
+
#
|
|
7
|
+
# TRIGGER: PreToolUse
|
|
8
|
+
# MATCHER: "Bash"
|
|
9
|
+
#
|
|
10
|
+
# Usage:
|
|
11
|
+
# {
|
|
12
|
+
# "hooks": {
|
|
13
|
+
# "PreToolUse": [{
|
|
14
|
+
# "matcher": "Bash",
|
|
15
|
+
# "hooks": [{ "type": "command", "command": "~/.claude/hooks/pip-venv-required.sh" }]
|
|
16
|
+
# }]
|
|
17
|
+
# }
|
|
18
|
+
# }
|
|
19
|
+
|
|
20
|
+
INPUT=$(cat)
|
|
21
|
+
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null)
|
|
22
|
+
[ -z "$COMMAND" ] && exit 0
|
|
23
|
+
|
|
24
|
+
# Only check pip install commands
|
|
25
|
+
echo "$COMMAND" | grep -qE '^\s*(pip|pip3)\s+install' || exit 0
|
|
26
|
+
|
|
27
|
+
# Allow if -r requirements.txt (deterministic install)
|
|
28
|
+
echo "$COMMAND" | grep -qE 'pip3?\s+install\s+-r' && exit 0
|
|
29
|
+
|
|
30
|
+
# Allow if --user flag (user-level, not system)
|
|
31
|
+
echo "$COMMAND" | grep -qE 'pip3?\s+install\s+.*--user' && exit 0
|
|
32
|
+
|
|
33
|
+
# Check if virtualenv is active
|
|
34
|
+
if [ -z "$VIRTUAL_ENV" ] && [ -z "$CONDA_DEFAULT_ENV" ]; then
|
|
35
|
+
echo "BLOCKED: pip install outside of virtual environment." >&2
|
|
36
|
+
echo " Activate a venv first: python3 -m venv .venv && source .venv/bin/activate" >&2
|
|
37
|
+
exit 2
|
|
38
|
+
fi
|
|
39
|
+
|
|
40
|
+
exit 0
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# port-conflict-check.sh — Warn before starting a server on an occupied port
|
|
3
|
+
#
|
|
4
|
+
# Prevents: "EADDRINUSE" errors that confuse Claude into debugging
|
|
5
|
+
# phantom issues. Detects port conflicts before they happen.
|
|
6
|
+
#
|
|
7
|
+
# Detects: npm start, npm run dev, python -m http.server, node server.js,
|
|
8
|
+
# next dev, vite, etc.
|
|
9
|
+
#
|
|
10
|
+
# TRIGGER: PreToolUse
|
|
11
|
+
# MATCHER: "Bash"
|
|
12
|
+
#
|
|
13
|
+
# Usage:
|
|
14
|
+
# {
|
|
15
|
+
# "hooks": {
|
|
16
|
+
# "PreToolUse": [{
|
|
17
|
+
# "matcher": "Bash",
|
|
18
|
+
# "hooks": [{ "type": "command", "command": "~/.claude/hooks/port-conflict-check.sh" }]
|
|
19
|
+
# }]
|
|
20
|
+
# }
|
|
21
|
+
# }
|
|
22
|
+
|
|
23
|
+
INPUT=$(cat)
|
|
24
|
+
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null)
|
|
25
|
+
[ -z "$COMMAND" ] && exit 0
|
|
26
|
+
|
|
27
|
+
# Detect server-starting commands
|
|
28
|
+
echo "$COMMAND" | grep -qiE '(npm\s+(start|run\s+dev)|npx\s+(next|vite|nuxt)|python.*http\.server|node\s+.*server|flask\s+run|uvicorn|gunicorn|rails\s+s)' || exit 0
|
|
29
|
+
|
|
30
|
+
# Try to extract port from command
|
|
31
|
+
PORT=""
|
|
32
|
+
if echo "$COMMAND" | grep -qE '\-\-port[= ]+([0-9]+)'; then
|
|
33
|
+
PORT=$(echo "$COMMAND" | grep -oE '\-\-port[= ]+([0-9]+)' | grep -oE '[0-9]+')
|
|
34
|
+
elif echo "$COMMAND" | grep -qE '\-p[= ]+([0-9]+)'; then
|
|
35
|
+
PORT=$(echo "$COMMAND" | grep -oE '\-p[= ]+([0-9]+)' | grep -oE '[0-9]+')
|
|
36
|
+
fi
|
|
37
|
+
|
|
38
|
+
# Common default ports
|
|
39
|
+
if [ -z "$PORT" ]; then
|
|
40
|
+
if echo "$COMMAND" | grep -qiE 'next|vite|nuxt'; then PORT=3000
|
|
41
|
+
elif echo "$COMMAND" | grep -qiE 'flask|django'; then PORT=5000
|
|
42
|
+
elif echo "$COMMAND" | grep -qiE 'rails'; then PORT=3000
|
|
43
|
+
elif echo "$COMMAND" | grep -qiE 'http\.server'; then PORT=8000
|
|
44
|
+
else PORT=3000
|
|
45
|
+
fi
|
|
46
|
+
fi
|
|
47
|
+
|
|
48
|
+
# Check if port is in use
|
|
49
|
+
if command -v ss >/dev/null 2>&1; then
|
|
50
|
+
if ss -tlnp 2>/dev/null | grep -q ":${PORT} "; then
|
|
51
|
+
PID=$(ss -tlnp 2>/dev/null | grep ":${PORT} " | grep -oP 'pid=\K[0-9]+' | head -1)
|
|
52
|
+
echo "WARNING: Port $PORT is already in use (PID: ${PID:-unknown})." >&2
|
|
53
|
+
echo " Kill it: kill $PID or use a different port." >&2
|
|
54
|
+
fi
|
|
55
|
+
elif command -v lsof >/dev/null 2>&1; then
|
|
56
|
+
if lsof -i ":${PORT}" -sTCP:LISTEN >/dev/null 2>&1; then
|
|
57
|
+
echo "WARNING: Port $PORT is already in use." >&2
|
|
58
|
+
echo " Check: lsof -i :$PORT" >&2
|
|
59
|
+
fi
|
|
60
|
+
fi
|
|
61
|
+
|
|
62
|
+
exit 0
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# prefer-builtin-tools.sh — Deny bash commands that have dedicated built-in tool equivalents
|
|
3
|
+
# PreToolUse hook (matcher: Bash)
|
|
4
|
+
# Solves: https://github.com/anthropics/claude-code/issues/19649 (48+ reactions)
|
|
5
|
+
#
|
|
6
|
+
# Claude Code has built-in Read, Edit, Grep, Glob tools that are faster and safer
|
|
7
|
+
# than bash equivalents. But Claude often reaches for sed, grep, cat instead.
|
|
8
|
+
# This hook denies those commands with a pointer to the correct built-in tool.
|
|
9
|
+
|
|
10
|
+
INPUT=$(cat)
|
|
11
|
+
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null)
|
|
12
|
+
[ -z "$COMMAND" ] && exit 0
|
|
13
|
+
|
|
14
|
+
# Check all segments of piped/chained commands
|
|
15
|
+
while IFS= read -r segment; do
|
|
16
|
+
cmd=$(echo "$segment" | sed 's/^[[:space:]]*//' | sed 's/^[A-Za-z_][A-Za-z_0-9]*=[^ ]* //')
|
|
17
|
+
base=$(basename "$(echo "$cmd" | awk '{print $1}')" 2>/dev/null)
|
|
18
|
+
case "$base" in
|
|
19
|
+
cat) msg="Use the Read tool to read files, or Write to create them" ;;
|
|
20
|
+
head|tail) msg="Use the Read tool with offset/limit parameters" ;;
|
|
21
|
+
sed) msg="Use the Edit tool for modifications, or Read for viewing line ranges" ;;
|
|
22
|
+
awk) msg="Use Read, Grep, or Edit tools instead" ;;
|
|
23
|
+
grep|rg) msg="Use the built-in Grep tool (supports -A/-B/-C context, glob filters, output_mode)" ;;
|
|
24
|
+
find) msg="Use the built-in Glob tool for file pattern matching" ;;
|
|
25
|
+
*) continue ;;
|
|
26
|
+
esac
|
|
27
|
+
cat <<EOF
|
|
28
|
+
{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"Do not use \`$base\`. $msg"}}
|
|
29
|
+
EOF
|
|
30
|
+
exit 0
|
|
31
|
+
done < <(echo "$COMMAND" | tr '|' '\n' | sed 's/[;&]\{1,2\}/\n/g')
|
|
32
|
+
|
|
33
|
+
exit 0
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# python-import-check.sh — Detect unused imports in Python files
|
|
3
|
+
#
|
|
4
|
+
# Prevents: Unused imports that trigger linter warnings and add
|
|
5
|
+
# unnecessary dependencies. Claude often adds imports
|
|
6
|
+
# during development and forgets to clean up.
|
|
7
|
+
#
|
|
8
|
+
# TRIGGER: PostToolUse
|
|
9
|
+
# MATCHER: "Write|Edit"
|
|
10
|
+
|
|
11
|
+
INPUT=$(cat)
|
|
12
|
+
FILE=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty' 2>/dev/null)
|
|
13
|
+
[ -z "$FILE" ] && exit 0
|
|
14
|
+
|
|
15
|
+
case "$FILE" in
|
|
16
|
+
*.py) ;;
|
|
17
|
+
*) exit 0 ;;
|
|
18
|
+
esac
|
|
19
|
+
|
|
20
|
+
[ ! -f "$FILE" ] && exit 0
|
|
21
|
+
|
|
22
|
+
# Quick check: find import lines and see if the imported name appears elsewhere
|
|
23
|
+
python3 -c "
|
|
24
|
+
import re, sys
|
|
25
|
+
|
|
26
|
+
with open('$FILE') as f:
|
|
27
|
+
content = f.read()
|
|
28
|
+
lines = content.split('\n')
|
|
29
|
+
|
|
30
|
+
imports = []
|
|
31
|
+
for line in lines:
|
|
32
|
+
m = re.match(r'^import\s+(\w+)', line)
|
|
33
|
+
if m: imports.append(m.group(1))
|
|
34
|
+
m = re.match(r'^from\s+\S+\s+import\s+(.+)', line)
|
|
35
|
+
if m:
|
|
36
|
+
for name in m.group(1).split(','):
|
|
37
|
+
name = name.strip().split(' as ')[-1].strip()
|
|
38
|
+
if name and name != '*':
|
|
39
|
+
imports.append(name)
|
|
40
|
+
|
|
41
|
+
unused = []
|
|
42
|
+
for imp in imports:
|
|
43
|
+
# Count occurrences (excluding the import line itself)
|
|
44
|
+
count = len(re.findall(r'\b' + re.escape(imp) + r'\b', content))
|
|
45
|
+
if count <= 1: # Only appears in the import line
|
|
46
|
+
unused.append(imp)
|
|
47
|
+
|
|
48
|
+
if unused:
|
|
49
|
+
print(f'Possibly unused imports in $FILE: {', '.join(unused[:5])}', file=sys.stderr)
|
|
50
|
+
" 2>&1
|
|
51
|
+
|
|
52
|
+
exit 0
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# python-ruff-on-edit.sh — Run ruff lint after editing Python files
|
|
3
|
+
#
|
|
4
|
+
# TRIGGER: PostToolUse
|
|
5
|
+
# MATCHER: Edit
|
|
6
|
+
#
|
|
7
|
+
# Best with the v2.1.85 "if" field to avoid running on non-Python edits:
|
|
8
|
+
#
|
|
9
|
+
# {
|
|
10
|
+
# "hooks": {
|
|
11
|
+
# "PostToolUse": [{
|
|
12
|
+
# "matcher": "Edit",
|
|
13
|
+
# "hooks": [{
|
|
14
|
+
# "type": "command",
|
|
15
|
+
# "if": "Edit(*.py)",
|
|
16
|
+
# "command": "~/.claude/hooks/python-ruff-on-edit.sh"
|
|
17
|
+
# }]
|
|
18
|
+
# }]
|
|
19
|
+
# }
|
|
20
|
+
# }
|
|
21
|
+
#
|
|
22
|
+
# Without "if", the hook runs after every Edit and checks internally.
|
|
23
|
+
|
|
24
|
+
INPUT=$(cat)
|
|
25
|
+
FILE=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty' 2>/dev/null)
|
|
26
|
+
|
|
27
|
+
# Skip non-Python files (redundant with "if" field, kept for backward compat)
|
|
28
|
+
[[ "$FILE" != *.py ]] && exit 0
|
|
29
|
+
[ ! -f "$FILE" ] && exit 0
|
|
30
|
+
|
|
31
|
+
# Prefer ruff, fall back to flake8, then pylint
|
|
32
|
+
if command -v ruff &>/dev/null; then
|
|
33
|
+
ISSUES=$(ruff check "$FILE" --quiet 2>/dev/null)
|
|
34
|
+
elif command -v flake8 &>/dev/null; then
|
|
35
|
+
ISSUES=$(flake8 "$FILE" --max-line-length=120 2>/dev/null)
|
|
36
|
+
elif command -v pylint &>/dev/null; then
|
|
37
|
+
ISSUES=$(pylint "$FILE" --errors-only --score=no 2>/dev/null)
|
|
38
|
+
else
|
|
39
|
+
exit 0 # No linter available
|
|
40
|
+
fi
|
|
41
|
+
|
|
42
|
+
if [ -n "$ISSUES" ]; then
|
|
43
|
+
COUNT=$(echo "$ISSUES" | wc -l)
|
|
44
|
+
echo "⚠ Lint: $COUNT issue(s) in $(basename "$FILE")" >&2
|
|
45
|
+
echo "$ISSUES" | head -5 >&2
|
|
46
|
+
if [ "$COUNT" -gt 5 ]; then
|
|
47
|
+
echo " ... and $((COUNT - 5)) more" >&2
|
|
48
|
+
fi
|
|
49
|
+
fi
|
|
50
|
+
|
|
51
|
+
exit 0
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# quoted-flag-approver.sh — Auto-approve commands with quoted flag values
|
|
3
|
+
#
|
|
4
|
+
# Solves: "Command contains quoted characters in flag names" false positives
|
|
5
|
+
# (#27957 — 70 reactions, breaks agentic workflows)
|
|
6
|
+
#
|
|
7
|
+
# After a Claude Code update, normal commands like:
|
|
8
|
+
# git commit -m "fix bug"
|
|
9
|
+
# bun run build --flag "value"
|
|
10
|
+
# trigger a confirmation prompt even when they match allowlist patterns.
|
|
11
|
+
#
|
|
12
|
+
# This PermissionRequest hook auto-approves these prompts when:
|
|
13
|
+
# 1. The base command is in a safe list
|
|
14
|
+
# 2. The only "issue" is quoted characters in flag values
|
|
15
|
+
#
|
|
16
|
+
# TRIGGER: PermissionRequest
|
|
17
|
+
# MATCHER: ""
|
|
18
|
+
#
|
|
19
|
+
# Usage:
|
|
20
|
+
# {
|
|
21
|
+
# "hooks": {
|
|
22
|
+
# "PermissionRequest": [{
|
|
23
|
+
# "matcher": "",
|
|
24
|
+
# "hooks": [{ "type": "command", "command": "~/.claude/hooks/quoted-flag-approver.sh" }]
|
|
25
|
+
# }]
|
|
26
|
+
# }
|
|
27
|
+
# }
|
|
28
|
+
|
|
29
|
+
INPUT=$(cat)
|
|
30
|
+
|
|
31
|
+
# Only handle "quoted characters in flag names" prompts
|
|
32
|
+
MESSAGE=$(echo "$INPUT" | jq -r '.message // empty' 2>/dev/null)
|
|
33
|
+
echo "$MESSAGE" | grep -qi "quoted characters in flag" || exit 0
|
|
34
|
+
|
|
35
|
+
# Extract the command being checked
|
|
36
|
+
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null)
|
|
37
|
+
[ -z "$COMMAND" ] && exit 0
|
|
38
|
+
|
|
39
|
+
# Safe base commands — customize for your project
|
|
40
|
+
SAFE_COMMANDS="git|npm|npx|bun|yarn|pnpm|docker|make|cargo|go|pip|python3|node|tsc|eslint|prettier|jest|vitest|pytest|curl|wget|rsync|tar|zip|unzip|cp|mv|mkdir|cat|echo|grep|find|ls|chmod|sed|awk"
|
|
41
|
+
|
|
42
|
+
# Extract base command (first word, ignoring env vars and path)
|
|
43
|
+
BASE_CMD=$(echo "$COMMAND" | sed 's/^[A-Z_]*=[^ ]* //' | awk '{print $1}' | sed 's|.*/||')
|
|
44
|
+
|
|
45
|
+
if echo "$BASE_CMD" | grep -qE "^($SAFE_COMMANDS)$"; then
|
|
46
|
+
echo '{"permissionDecision":"allow"}'
|
|
47
|
+
exit 0
|
|
48
|
+
fi
|
|
49
|
+
|
|
50
|
+
# Unknown command — let the prompt through
|
|
51
|
+
exit 0
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# react-key-warn.sh — Warn about missing key props in JSX lists
|
|
3
|
+
#
|
|
4
|
+
# Prevents: "Each child in a list should have a unique key prop" errors.
|
|
5
|
+
# Claude often generates .map() without key props.
|
|
6
|
+
#
|
|
7
|
+
# TRIGGER: PostToolUse
|
|
8
|
+
# MATCHER: "Write|Edit"
|
|
9
|
+
|
|
10
|
+
INPUT=$(cat)
|
|
11
|
+
FILE=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty' 2>/dev/null)
|
|
12
|
+
[ -z "$FILE" ] && exit 0
|
|
13
|
+
|
|
14
|
+
case "$FILE" in
|
|
15
|
+
*.jsx|*.tsx) ;;
|
|
16
|
+
*) exit 0 ;;
|
|
17
|
+
esac
|
|
18
|
+
|
|
19
|
+
[ ! -f "$FILE" ] && exit 0
|
|
20
|
+
|
|
21
|
+
# Check for .map( without key= in the return
|
|
22
|
+
if grep -qE '\.map\s*\(' "$FILE"; then
|
|
23
|
+
# Count map calls and key props
|
|
24
|
+
MAPS=$(grep -c '\.map\s*(' "$FILE" 2>/dev/null || echo 0)
|
|
25
|
+
KEYS=$(grep -c 'key=' "$FILE" 2>/dev/null || echo 0)
|
|
26
|
+
if [ "$MAPS" -gt "$KEYS" ]; then
|
|
27
|
+
echo "WARNING: $FILE has $MAPS .map() calls but only $KEYS key= props." >&2
|
|
28
|
+
echo " Add key props to list items to avoid React warnings." >&2
|
|
29
|
+
fi
|
|
30
|
+
fi
|
|
31
|
+
|
|
32
|
+
exit 0
|
|
@@ -27,6 +27,9 @@
|
|
|
27
27
|
# }]
|
|
28
28
|
# }
|
|
29
29
|
# }
|
|
30
|
+
#
|
|
31
|
+
# Note: This hook checks rm, find -delete, and shred. Do NOT add an "if" field
|
|
32
|
+
# (v2.1.85) because "if" only supports one pattern and would miss the others.
|
|
30
33
|
|
|
31
34
|
INPUT=$(cat)
|
|
32
35
|
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty' 2>/dev/null)
|
|
@@ -41,6 +44,12 @@ if echo "$COMMAND" | grep -qE '^\s*(sudo\s+)?rm\s'; then
|
|
|
41
44
|
# Extract the target (last argument after flags)
|
|
42
45
|
TARGET=$(echo "$COMMAND" | grep -oP 'rm\s+[^;|&]*' | awk '{print $NF}')
|
|
43
46
|
|
|
47
|
+
# Block path traversal early
|
|
48
|
+
if echo "$TARGET" | grep -qF '..'; then
|
|
49
|
+
echo "BLOCKED: path traversal detected in rm target" >&2
|
|
50
|
+
exit 2
|
|
51
|
+
fi
|
|
52
|
+
|
|
44
53
|
# Allow safe targets
|
|
45
54
|
if echo "$TARGET" | grep -qE "^(\./)?(${SAFE_TARGETS})(/|$)"; then
|
|
46
55
|
exit 0
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# rust-clippy-after-edit.sh — Run cargo clippy after editing Rust files
|
|
3
|
+
#
|
|
4
|
+
# Prevents: Common Rust anti-patterns and potential bugs.
|
|
5
|
+
# Clippy catches: needless borrows, inefficient patterns,
|
|
6
|
+
# suspicious operations.
|
|
7
|
+
#
|
|
8
|
+
# TRIGGER: PostToolUse
|
|
9
|
+
# MATCHER: "Write|Edit"
|
|
10
|
+
|
|
11
|
+
INPUT=$(cat)
|
|
12
|
+
FILE=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty' 2>/dev/null)
|
|
13
|
+
[ -z "$FILE" ] && exit 0
|
|
14
|
+
|
|
15
|
+
case "$FILE" in
|
|
16
|
+
*.rs) ;;
|
|
17
|
+
*) exit 0 ;;
|
|
18
|
+
esac
|
|
19
|
+
|
|
20
|
+
[ ! -f "$FILE" ] && exit 0
|
|
21
|
+
|
|
22
|
+
# Find Cargo.toml
|
|
23
|
+
DIR=$(dirname "$FILE")
|
|
24
|
+
while [ "$DIR" != "/" ]; do
|
|
25
|
+
[ -f "$DIR/Cargo.toml" ] && break
|
|
26
|
+
DIR=$(dirname "$DIR")
|
|
27
|
+
done
|
|
28
|
+
|
|
29
|
+
if [ -f "$DIR/Cargo.toml" ] && command -v cargo >/dev/null 2>&1; then
|
|
30
|
+
WARNINGS=$(cd "$DIR" && cargo clippy --quiet 2>&1 | grep "^warning" | head -3)
|
|
31
|
+
if [ -n "$WARNINGS" ]; then
|
|
32
|
+
echo "Clippy warnings:" >&2
|
|
33
|
+
echo "$WARNINGS" | sed 's/^/ /' >&2
|
|
34
|
+
fi
|
|
35
|
+
fi
|
|
36
|
+
|
|
37
|
+
exit 0
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# session-quota-tracker.sh — Track cumulative tool calls per session
|
|
3
|
+
#
|
|
4
|
+
# Solves: Token consumption spiraling without warning (#23706, #38335)
|
|
5
|
+
# Users hit Max plan limits unexpectedly. This hook tracks
|
|
6
|
+
# tool call count per session and warns at thresholds.
|
|
7
|
+
#
|
|
8
|
+
# Tracks: cumulative tool calls in a session file
|
|
9
|
+
# Warns at: 50, 100, 200, 500 tool calls
|
|
10
|
+
#
|
|
11
|
+
# TRIGGER: PostToolUse
|
|
12
|
+
# MATCHER: ""
|
|
13
|
+
#
|
|
14
|
+
# Usage:
|
|
15
|
+
# {
|
|
16
|
+
# "hooks": {
|
|
17
|
+
# "PostToolUse": [{
|
|
18
|
+
# "matcher": "",
|
|
19
|
+
# "hooks": [{ "type": "command", "command": "~/.claude/hooks/session-quota-tracker.sh" }]
|
|
20
|
+
# }]
|
|
21
|
+
# }
|
|
22
|
+
# }
|
|
23
|
+
|
|
24
|
+
# Session tracking file
|
|
25
|
+
SESSION_FILE="/tmp/cc-quota-tracker-$$"
|
|
26
|
+
|
|
27
|
+
# Increment counter
|
|
28
|
+
if [ -f "$SESSION_FILE" ]; then
|
|
29
|
+
COUNT=$(cat "$SESSION_FILE")
|
|
30
|
+
COUNT=$((COUNT + 1))
|
|
31
|
+
else
|
|
32
|
+
COUNT=1
|
|
33
|
+
fi
|
|
34
|
+
echo "$COUNT" > "$SESSION_FILE"
|
|
35
|
+
|
|
36
|
+
# Warn at thresholds
|
|
37
|
+
case "$COUNT" in
|
|
38
|
+
50) echo "[Session: 50 tool calls. Consider saving work.]" >&2 ;;
|
|
39
|
+
100) echo "[Session: 100 tool calls. Token usage may be high.]" >&2 ;;
|
|
40
|
+
200) echo "[Session: 200 tool calls. Check your usage dashboard.]" >&2 ;;
|
|
41
|
+
500) echo "[Session: 500 tool calls. Consider starting a new session.]" >&2 ;;
|
|
42
|
+
esac
|
|
43
|
+
|
|
44
|
+
exit 0
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# session-start-safety-check.sh — Warn about uncommitted changes on session start
|
|
3
|
+
#
|
|
4
|
+
# Solves: Claude Code running destructive git commands on session startup
|
|
5
|
+
# that destroy uncommitted work (#34327, #39394)
|
|
6
|
+
#
|
|
7
|
+
# How it works:
|
|
8
|
+
# On SessionStart, checks for:
|
|
9
|
+
# 1. Uncommitted changes (modified/new files)
|
|
10
|
+
# 2. Unpushed commits
|
|
11
|
+
# 3. Stashed changes that may need attention
|
|
12
|
+
#
|
|
13
|
+
# Prints warnings but does NOT block (exit 0 always).
|
|
14
|
+
# The goal is awareness, not prevention.
|
|
15
|
+
#
|
|
16
|
+
# TRIGGER: SessionStart
|
|
17
|
+
# MATCHER: ""
|
|
18
|
+
#
|
|
19
|
+
# Usage:
|
|
20
|
+
# {
|
|
21
|
+
# "hooks": {
|
|
22
|
+
# "SessionStart": [{
|
|
23
|
+
# "hooks": [{ "type": "command", "command": "~/.claude/hooks/session-start-safety-check.sh" }]
|
|
24
|
+
# }]
|
|
25
|
+
# }
|
|
26
|
+
# }
|
|
27
|
+
|
|
28
|
+
# Only run in git repos
|
|
29
|
+
git rev-parse --git-dir > /dev/null 2>&1 || exit 0
|
|
30
|
+
|
|
31
|
+
WARNINGS=0
|
|
32
|
+
|
|
33
|
+
# Check for uncommitted changes
|
|
34
|
+
CHANGES=$(git status --porcelain 2>/dev/null | wc -l)
|
|
35
|
+
if [ "$CHANGES" -gt 0 ]; then
|
|
36
|
+
echo "⚠ WARNING: $CHANGES uncommitted changes detected." >&2
|
|
37
|
+
echo " Consider: git stash (before destructive operations)" >&2
|
|
38
|
+
WARNINGS=$((WARNINGS + 1))
|
|
39
|
+
fi
|
|
40
|
+
|
|
41
|
+
# Check for unpushed commits
|
|
42
|
+
UNPUSHED=$(git log --oneline @{upstream}..HEAD 2>/dev/null | wc -l)
|
|
43
|
+
if [ "$UNPUSHED" -gt 0 ]; then
|
|
44
|
+
echo "⚠ WARNING: $UNPUSHED unpushed commits." >&2
|
|
45
|
+
echo " Consider: git push (to protect against local data loss)" >&2
|
|
46
|
+
WARNINGS=$((WARNINGS + 1))
|
|
47
|
+
fi
|
|
48
|
+
|
|
49
|
+
# Check for stashes
|
|
50
|
+
STASHES=$(git stash list 2>/dev/null | wc -l)
|
|
51
|
+
if [ "$STASHES" -gt 0 ]; then
|
|
52
|
+
echo "ℹ NOTE: $STASHES stashed changes exist." >&2
|
|
53
|
+
echo " Review: git stash list" >&2
|
|
54
|
+
fi
|
|
55
|
+
|
|
56
|
+
if [ "$WARNINGS" -eq 0 ]; then
|
|
57
|
+
echo "✓ Working tree clean, all commits pushed." >&2
|
|
58
|
+
fi
|
|
59
|
+
|
|
60
|
+
exit 0
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# session-summary-stop.sh — Print session change summary on stop
|
|
3
|
+
#
|
|
4
|
+
# Solves: No quick way to see what Claude changed during a session.
|
|
5
|
+
# Git diff shows code changes but not the full picture.
|
|
6
|
+
#
|
|
7
|
+
# How it works: Stop hook that runs `git diff --stat` and outputs
|
|
8
|
+
# a summary of all modified files since the session started.
|
|
9
|
+
#
|
|
10
|
+
# Usage: Add to settings.json as a Stop hook
|
|
11
|
+
#
|
|
12
|
+
# {
|
|
13
|
+
# "hooks": {
|
|
14
|
+
# "Stop": [{
|
|
15
|
+
# "matcher": "",
|
|
16
|
+
# "hooks": [{ "type": "command", "command": "~/.claude/hooks/session-summary-stop.sh" }]
|
|
17
|
+
# }]
|
|
18
|
+
# }
|
|
19
|
+
# }
|
|
20
|
+
|
|
21
|
+
INPUT=$(cat)
|
|
22
|
+
|
|
23
|
+
# Only run if in a git repo
|
|
24
|
+
if ! git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
|
|
25
|
+
exit 0
|
|
26
|
+
fi
|
|
27
|
+
|
|
28
|
+
# Get change summary
|
|
29
|
+
CHANGES=$(git diff --stat HEAD 2>/dev/null)
|
|
30
|
+
STAGED=$(git diff --cached --stat 2>/dev/null)
|
|
31
|
+
UNTRACKED=$(git ls-files --others --exclude-standard 2>/dev/null | wc -l)
|
|
32
|
+
|
|
33
|
+
if [ -n "$CHANGES" ] || [ -n "$STAGED" ] || [ "$UNTRACKED" -gt 0 ]; then
|
|
34
|
+
echo "--- Session Change Summary ---" >&2
|
|
35
|
+
if [ -n "$CHANGES" ]; then
|
|
36
|
+
echo "Modified:" >&2
|
|
37
|
+
echo "$CHANGES" | head -20 >&2
|
|
38
|
+
fi
|
|
39
|
+
if [ -n "$STAGED" ]; then
|
|
40
|
+
echo "Staged:" >&2
|
|
41
|
+
echo "$STAGED" | head -10 >&2
|
|
42
|
+
fi
|
|
43
|
+
if [ "$UNTRACKED" -gt 0 ]; then
|
|
44
|
+
echo "Untracked files: $UNTRACKED" >&2
|
|
45
|
+
fi
|
|
46
|
+
echo "---" >&2
|
|
47
|
+
fi
|
|
48
|
+
|
|
49
|
+
exit 0
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
# session-time-limit.sh — Warn when session exceeds time limit
|
|
3
|
+
#
|
|
4
|
+
# Prevents: Unbounded autonomous sessions that consume excessive tokens.
|
|
5
|
+
# Default: warn at 2 hours, configurable via CC_SESSION_LIMIT_HOURS.
|
|
6
|
+
#
|
|
7
|
+
# TRIGGER: PostToolUse
|
|
8
|
+
# MATCHER: ""
|
|
9
|
+
|
|
10
|
+
INPUT=$(cat)
|
|
11
|
+
|
|
12
|
+
# Track session start
|
|
13
|
+
MARKER="/tmp/cc-session-start-$$"
|
|
14
|
+
NOW=$(date +%s)
|
|
15
|
+
|
|
16
|
+
if [ ! -f "$MARKER" ]; then
|
|
17
|
+
echo "$NOW" > "$MARKER"
|
|
18
|
+
exit 0
|
|
19
|
+
fi
|
|
20
|
+
|
|
21
|
+
START=$(cat "$MARKER")
|
|
22
|
+
ELAPSED=$(( (NOW - START) / 60 )) # minutes
|
|
23
|
+
LIMIT_HOURS="${CC_SESSION_LIMIT_HOURS:-2}"
|
|
24
|
+
LIMIT_MIN=$((LIMIT_HOURS * 60))
|
|
25
|
+
WARN_MIN=$((LIMIT_MIN * 3 / 4)) # warn at 75%
|
|
26
|
+
|
|
27
|
+
if [ "$ELAPSED" -ge "$LIMIT_MIN" ]; then
|
|
28
|
+
echo "SESSION TIME LIMIT: ${ELAPSED}min elapsed (limit: ${LIMIT_HOURS}h)." >&2
|
|
29
|
+
echo " Consider saving work and starting a new session." >&2
|
|
30
|
+
elif [ "$ELAPSED" -ge "$WARN_MIN" ]; then
|
|
31
|
+
echo "[Session: ${ELAPSED}min / ${LIMIT_HOURS}h limit]" >&2
|
|
32
|
+
fi
|
|
33
|
+
|
|
34
|
+
exit 0
|