Claude Hookbook

Hooks / safety

Secret scan on changed files

Runs gitleaks on every file Claude edits and warns when something looks like a secret.

PostToolUseEdit|Writereview riskneeds manual check

What it does

Fires after Edit/Write. The script pipes the changed file through gitleaks stdin mode and, on findings, exits 2 so Claude sees the warning after the edit and can offer to fix it. Starts warn-only on purpose: blocking on every edit gets noisy fast.

When it runs

EventPostToolUse
MatcherEdit|Write
Scopeproject
Platformsmacos, linux
Timeout30s
Requiresjq gitleaks
Failure modewarn
Verified againstClaude Code hooks reference, event/matcher schema checked 2026-09-10
Test statusneeds manual check - run the test steps below before relying on it

Config

Merge this into .claude/settings.json - or use the generator to combine hooks.

{
  "hooks": {
    "PostToolUse": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "${CLAUDE_PROJECT_DIR}/.claude/hooks/secret-scan-changed-files.sh",
            "timeout": 30
          }
        ],
        "matcher": "Edit|Write"
      }
    ]
  }
}

Script

Save as .claude/hooks/secret-scan-changed-files.sh and chmod +x it.

#!/bin/bash
# secret-scan-changed-files: warn when an edit introduces something secret-shaped.
set -u
file=$(jq -r '.tool_input.file_path // empty')
[ -z "$file" ] && exit 0
[ -f "$file" ] || exit 0
command -v gitleaks >/dev/null 2>&1 || exit 0
if ! gitleaks stdin --no-banner < "$file" >/dev/null 2>&1; then
  echo "gitleaks found a possible secret in $file - review before committing." >&2
  exit 2
fi
exit 0

Install

  1. Install gitleaks (brew install gitleaks / see gitleaks.io) and jq.
  2. Save the script to .claude/hooks/secret-scan-changed-files.sh and chmod +x it.
  3. Add the config fragment to .claude/settings.json.

Test it

Sources

Related hooks

Protect .env and secret files

Blocks Claude from writing to .env files, keys, and credential files - unless you say otherwise.

PreToolUseEdit|Writeblocking riskneeds manual check

Block commits that contain secrets

Intercepts git commit commands and runs gitleaks on staged changes first.

PreToolUseBashblocking riskneeds manual check

Pro Pack (later)

Framework bundles, cross-platform scripts, tested blocking policies, and a test harness. Free recipes and the generator stay free.

Waitlist opens soon.