feat: 4-layer secret leak prevention

Merged
SH
5mo ago
+127-0

Summary

Implements the 4-layer secret leak prevention pattern from https://zenn.dev/takna/articles/secret-leak-prevention-4-layer.

  • Layer 1 (design): .gitignore patterns for credential locations (_credentials/, *.local.md, *.secret.md, credentials*.md, **/draft/).
  • Layer 2 (pre-commit): lefthook.yml runs gitleaks git --staged --redact --verbose. Gitleaks config moved to config/gitleaks/config.toml and installed globally at ~/.config/gitleaks/config.toml via home-manager; GITLEAKS_CONFIG env var is set so every repo picks up the same baseline without a per-repo .gitleaks.toml.
  • Layer 3 (push protection): server-side, enable manually per repo at Settings -> Advanced Security -> Secret Protection -> Push protection.
  • Layer 4 (AI hook): config/shared/hooks/secret-guard.sh is a shared PreToolUse hook wired into both Claude Code (config/claude/settings.json) and Codex (config/codex/hooks.json) for Write|Edit|MultiEdit. It extracts content from multiple payload shapes (Claude tool_input.*, Codex tool.input.*, plus MultiEdit edits[].new_string), runs gitleaks against the extracted content, and exits 2 to block on detection. Skips silently if gitleaks/jq are missing so it does not break before the next rebuild.

gitleaks and lefthook added to home-manager/packages/default.nix.

Test plan

  • darwin-rebuild switch --flake . installs gitleaks and lefthook
  • lefthook install wires .git/hooks/pre-commit in this repo
  • echo 'AWS_SECRET_ACCESS_KEY=AKIAIOSFODNN7EXAMPLE' | gitleaks stdin flags
  • Claude Code Write with a fake AWS key in content is blocked by secret-guard.sh
  • Codex Write with the same payload is blocked
  • Clean writes pass through (exit 0)

Summary by cubic

Adds a 4-layer secret leak prevention system across ignore rules, pre-commit checks, server push protection, and editor hooks. Also adds shell coverage for secret-guard.sh and formats the script.

  • New Features

    • Layer 1: .gitignore excludes _credentials/, *.local.md, *.secret.md, credentials*.md, **/draft/.
    • Layer 2: Pre-commit via lefthook runs gitleaks git --staged --redact --verbose; global config at ~/.config/gitleaks/config.toml via home-manager with custom rules and allowlist.
    • Layer 3: Push protection (server-side). Enable per repo in Settings > Advanced Security > Secret Protection.
    • Layer 4: secret-guard.sh hooks Claude Code and Codex Write/Edit/MultiEdit, scans payloads with gitleaks, exits 2 on findings; skips if gitleaks/jq missing.
  • Dependencies

    • Added gitleaks and lefthook to home-manager packages.