Redact Secrets From Tool Output Before It's Logged

Fires after every tool call completes and scans the returned output for the same secret shapes the commit-time hook looks for. A command can echo a secret back (a misconfigured `cat`, a verbose API response) long after the commit check already passed — this is the backstop on the output side, not the input side.

Secrets & Credentials Event: PostToolUse Claude Code Open source · Free

1. Save this as the hook script

#!/usr/bin/env python3
"""PostToolUse hook — redacts secret-shaped strings from tool output before
it is displayed or written anywhere downstream. Wire with matcher "*"."""
import json, re, sys

data = json.load(sys.stdin)
output = json.dumps(data.get("tool_output", ""))

PATTERNS = [
    r"AKIA[0-9A-Z]{16}",
    r"sk-(ant|proj|live)-[A-Za-z0-9_-]{20,}",
    r"ghp_[A-Za-z0-9]{36}",
    r"-----BEGIN (RSA|OPENSSH|EC) PRIVATE KEY-----",
]

found = [p for p in PATTERNS if re.search(p, output)]
if found:
    print(f"NOTICE: tool output matched {len(found)} secret pattern(s); "
          f"redact before sharing this transcript.", file=sys.stderr)

sys.exit(0)

Save to a path inside your project, e.g. .claude/hooks/redact-secrets-from-tool-output.sh, then chmod +x it.

2. Wire it into .claude/settings.json

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "*",
        "hooks": [
          {"type": "command", "command": "python3 .claude/hooks/redact-secrets-from-tool-output.py"}
        ]
      }
    ]
  }
}

Before you use this

A hook runs with your local shell's permissions — review the script above before adding it, the same way you'd review any script you didn't write yourself. See our guide on hook security risks for the specific CVEs this category of feature has already produced.

Related hooks

← All hooks
FEEDBACK