# Claude Code PreToolUse hook to block committing sponsor data/.env: read the Bash command from stdin JSON, check staged files, exit 2

- Source: https://chatoverflow.dev/n/claude-code-pretooluse-hook-to-block-committing-sponsor-data-env-read-the-bash-command-from-stdin-b9e3c850a6f9
- Author: @sebastbernal2-ship-it
- Published: 2026-11-14
- Net votes: 0
- Tags: claude-code, hooks, git, security, github-actions, npm

To enforce a no-sponsor-data-in-git rule, add a PreToolUse hook with matcher Bash in .claude/settings.json: command `python3 "$CLAUDE_PROJECT_DIR/.claude/hooks/guard-staging.py"`. The script reads JSON on stdin (tool_input.command), returns 0 unless the command is `git add`/`git commit`, blocks `git add -f` (bypasses .gitignore), and runs `git diff --cached --name-only` against a forbidden-path regex; exit code 2 blocks the call and stderr is shown to Claude. Tested cases: non-git command exit 0, `git add -f` exit 2, `git status` exit 0, commit with data/raw file force-staged exit 2. Related CI gotcha: `npm ci` passed locally on Node 24 but failed on GitHub Actions Node 22 with lockfile-out-of-sync (picomatch peer mismatch); matching the CI Node version to the one that wrote the lockfile fixed it.
