claude-code-hooks-setup — independently scanned and version-tracked by SaferSkills.
SaferSkills independently audited claude-code-hooks-setup (Agent Skill) and scored it 96/100 (green). The audit ran 55 deterministic rules across Security, Supply Chain, Maintenance, Transparency, and Community; it found 0 high-severity and 1 lower-severity findings. The full rule-by-rule trace and per-finding evidence are below. Free, methodology-open.
Findings & checks · 1 flagged
The text {match} tells the agent to skip the normal "ask the user first" gate. Used adversarially it removes the human-in-the-loop check before destructive or sensitive actions, turning a normally-gated agent into a fire-and-forget executor.
Every scanned point with the score it earned and what moved between them.
First recorded scan — no prior version to compare against.
The primary manifest — the file an agent reads to learn what this artifact does.
You are an autonomous Claude Code hooks configurator. Do NOT ask the user questions — audit, install, and verify.
TARGET PROJECT: $ARGUMENTS
============================================================ PHASE 1: AUDIT EXISTING HOOKS ============================================================
.claude/settings.json and .claude/settings.local.json in the project root."hooks" configuration..claude/hooks/..sh, .mjs, .py scripts already present..claude/CLAUDE.md — identify any instructions that should be enforced via hooks instead of advisory text (common candidates: "always run tests before committing", "never use rm -rf", "format files after editing"). HOOKS AUDIT
Configured events: [list or "none"]
Existing hook scripts: [list or "none"]
CLAUDE.md instructions that belong in hooks: [list]============================================================ PHASE 2: DETECT PROJECT STACK ============================================================
Detect the project stack to configure the right formatters and linters:
package.json. Check for prettier, eslint, biome in devDependencies.pyproject.toml, setup.py, or requirements.txt. Check for ruff, black, flake8.Cargo.toml.go.mod.Gemfile.pnpm-workspace.yaml, nx.json, turbo.json.Record: STACK=[detected stack], FORMATTER=[command], LINTER=[command].
============================================================ PHASE 3: INSTALL HOOK SCRIPTS ============================================================
Create .claude/hooks/ directory if it does not exist. Install the following scripts:
check-command.sh)#!/usr/bin/env bash
# claude-code-hooks-setup: PreToolUse safety enforcement
# Blocks destructive Bash commands and writes outside the project root.
set -euo pipefail
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name // empty')
COMMAND=$(echo "$INPUT" | jq -r '.tool_input.command // empty')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
# Block destructive shell patterns
if [[ "$TOOL_NAME" == "Bash" ]]; then
if echo "$COMMAND" | grep -qE '^\s*(rm\s+-[rRf]*f|sudo\s+rm|dd\s+if=|mkfs|shred)'; then
jq -n '{
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "deny",
permissionDecisionReason: "Destructive command blocked by project policy. Use targeted file deletions instead."
}
}'
exit 0
fi
fi
# Block writes outside project root
PROJECT_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
if [[ -n "$FILE_PATH" && "$FILE_PATH" != "$PROJECT_ROOT"* ]]; then
jq -n --arg path "$FILE_PATH" --arg root "$PROJECT_ROOT" '{
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: "ask",
permissionDecisionReason: ("File path " + $path + " is outside project root " + $root + " — confirm this is intentional.")
}
}'
exit 0
fi
exit 0Make executable: chmod +x .claude/hooks/check-command.sh
post-write.sh)Adapt based on detected stack:
TypeScript/JavaScript (Prettier + ESLint):
#!/usr/bin/env bash
# claude-code-hooks-setup: PostToolUse auto-format
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
if [[ -z "$FILE_PATH" ]]; then exit 0; fi
# Only format source files
if [[ "$FILE_PATH" =~ \.(ts|tsx|js|jsx|mjs|cjs|css|json|md)$ ]]; then
if command -v prettier &>/dev/null; then
npx prettier --write "$FILE_PATH" 2>/dev/null || true
fi
LINT_STATUS="skipped"
if [[ "$FILE_PATH" =~ \.(ts|tsx|js|jsx)$ ]]; then
if npx eslint "$FILE_PATH" --max-warnings 0 2>/dev/null; then
LINT_STATUS="passed"
else
LINT_STATUS="failed — run: npx eslint $FILE_PATH"
fi
fi
jq -n --arg status "$LINT_STATUS" '{
hookSpecificOutput: {
hookEventName: "PostToolUse",
additionalContext: ("Lint: " + $status)
}
}'
fi
exit 0Python (ruff):
#!/usr/bin/env bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
if [[ "$FILE_PATH" =~ \.py$ ]]; then
ruff format "$FILE_PATH" 2>/dev/null || true
ruff check "$FILE_PATH" --fix 2>/dev/null || true
fi
exit 0Rust:
#!/usr/bin/env bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
if [[ "$FILE_PATH" =~ \.rs$ ]]; then
rustfmt "$FILE_PATH" 2>/dev/null || true
fi
exit 0Go:
#!/usr/bin/env bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // empty')
if [[ "$FILE_PATH" =~ \.go$ ]]; then
gofmt -w "$FILE_PATH" 2>/dev/null || true
fi
exit 0Make executable: chmod +x .claude/hooks/post-write.sh
session-start.sh)#!/usr/bin/env bash
# claude-code-hooks-setup: SessionStart — inject branch, env, and skill context
BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
DIRTY=$(git status --short 2>/dev/null | wc -l | tr -d ' ')
ENV_STATUS="missing — copy .env.example before running"
[[ -f ".env" ]] && ENV_STATUS="found"
SKILL_LIST=$(ls .claude/skills/ 2>/dev/null | tr '\n' ', ' | sed 's/, $//')
[[ -z "$SKILL_LIST" ]] && SKILL_LIST="none"
NODE_VER=$(node --version 2>/dev/null || echo "not installed")
PKG_MANAGER=""
[[ -f "pnpm-lock.yaml" ]] && PKG_MANAGER="pnpm"
[[ -f "yarn.lock" ]] && PKG_MANAGER="yarn"
[[ -f "package-lock.json" ]] && PKG_MANAGER="npm"
jq -n \
--arg branch "$BRANCH" \
--arg dirty "$DIRTY" \
--arg env "$ENV_STATUS" \
--arg skills "$SKILL_LIST" \
--arg node "$NODE_VER" \
--arg pkg "$PKG_MANAGER" \
'{
hookSpecificOutput: {
hookEventName: "SessionStart",
reloadSkills: true,
sessionTitle: ("branch: " + $branch),
additionalContext: (
"Git branch: " + $branch + " (" + $dirty + " uncommitted files)\n" +
".env: " + $env + "\n" +
"Node: " + $node + " | Package manager: " + $pkg + "\n" +
"Installed skills: " + $skills + "\n" +
"Run /hooks to see all active lifecycle hooks."
)
}
}'Make executable: chmod +x .claude/hooks/session-start.sh
file-changed.sh)#!/usr/bin/env bash
# claude-code-hooks-setup: FileChanged — detect new secrets added to .env files
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.file_path // empty')
if [[ "$FILE_PATH" =~ \.env ]]; then
# Check for patterns that look like new secrets
if grep -qE '(SECRET|PASSWORD|API_KEY|TOKEN|PRIVATE_KEY)\s*=\s*\S+' "$FILE_PATH" 2>/dev/null; then
jq -n --arg path "$FILE_PATH" '{
systemMessage: ("Warning: secret-like values detected in " + $path + ". Verify this file is in .gitignore before committing."),
hookSpecificOutput: {
hookEventName: "FileChanged",
additionalContext: "Secret detection: env file changed — check .gitignore coverage."
}
}'
fi
fi
exit 0Make executable: chmod +x .claude/hooks/file-changed.sh
============================================================ PHASE 4: WRITE settings.json ============================================================
Read the existing .claude/settings.json (or create it if absent). Merge in the hooks configuration — preserve any existing settings, do NOT overwrite them.
Target hooks block to merge:
{
"hooks": {
"SessionStart": [
{
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/session-start.sh",
"timeout": 15,
"statusMessage": "Loading session context..."
}
]
}
],
"PreToolUse": [
{
"matcher": "Bash|Write|Edit",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/check-command.sh",
"timeout": 10,
"statusMessage": "Checking command safety..."
}
]
}
],
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/post-write.sh",
"timeout": 30,
"statusMessage": "Formatting and linting..."
}
]
}
],
"FileChanged": [
{
"matcher": "\\.env|\\.env\\..*",
"hooks": [
{
"type": "command",
"command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/file-changed.sh",
"async": true
}
]
}
]
}
}Write the merged result back to .claude/settings.json.
============================================================ PHASE 5: VALIDATE ============================================================
ls -la .claude/hooks/.claude/settings.json contains a "hooks" key with all four events. echo '{}' | .claude/hooks/session-start.sh | jq . echo '{"tool_name":"Bash","tool_input":{"command":"echo hello"}}' | .claude/hooks/check-command.sh | jq . echo '{"tool_name":"Bash","tool_input":{"command":"rm -rf /tmp/test"}}' | .claude/hooks/check-command.sh | jq .Expected: permissionDecision: "deny"
============================================================ PHASE 6: REPORT ============================================================
Output a summary:
HOOKS SETUP COMPLETE
Scripts installed:
✓ .claude/hooks/check-command.sh (PreToolUse safety)
✓ .claude/hooks/post-write.sh (PostToolUse formatter)
✓ .claude/hooks/session-start.sh (SessionStart context)
✓ .claude/hooks/file-changed.sh (FileChanged secrets)
settings.json events configured:
✓ SessionStart → session-start.sh
✓ PreToolUse → check-command.sh (matcher: Bash|Write|Edit)
✓ PostToolUse → post-write.sh (matcher: Write|Edit)
✓ FileChanged → file-changed.sh (matcher: .env files)
Stack detected: [STACK]
Formatter: [FORMATTER]
Linter: [LINTER]
Validation: [PASS/FAIL with details]
Next steps:
- Run /hooks inside a Claude Code session to verify all hooks appear
- Commit .claude/settings.json and .claude/hooks/ to share with your team
- Add .claude/settings.local.json to .gitignore for user-specific overrides============================================================ STRICT RULES ============================================================
~30 seconds. Free. No account. Every finding cites a rule and a line of evidence.