cd /news/ai-agents/show-hn-claude-code-hooks-that-stop-… · home › topics › ai-agents › article
[ARTICLE · art-181829] src=github.com ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

Show HN: Claude Code hooks that stop agents from reading secret values

A developer released four dependency-free Python hooks for Claude Code, published on GitHub as claude-code-safety-hooks, that block AI agents from reading secret values out of `.env` files, `vercel env pull`, `printenv` and more than 30 workarounds while still allowing environment variable names. The hooks cover four events: `secret-exposure-guard.py` on PreToolUse blocks Bash commands and file reads that would put a secret value into the transcript, `secret-guard.py` on UserPromptSubmit blocks prompts containing pasted credentials, `chrome-secret-redact.py` on PostToolUse redacts secret-looking values from browser-automation output, and `needs-you-gate.py` on Stop makes the agent re-check whether it could finish work itself. The author reported that an earlier guard let through `timeout 60 vercel env pull ... --environment=production` because it stripped only bare wrappers, landing every production value for four projects on disk (deleted within minutes and logged as an incident), and the guard now strips `timeout`, `flock`, `taskset`, `chrt`, `ionice`, `doas`, `chronic`, `unbuffer` and `watch` with their options, covered by six new test cases.

key takeaway

Install the four Claude Code hooks (secret-exposure-guard.py on PreToolUse, secret-guard.py on UserPromptSubmit, chrome-secret-redact.py on PostToolUse, needs-you-gate.py on Stop) to block secret values from entering the transcript; the guard now strips argument-taking wrappers like `timeout 60` tha

read7 min views1 publishedDec 4, 2026
Show HN: Claude Code hooks that stop agents from reading secret values
Image: Michielbdejong (auto-discovered)

Stop your AI agent from reading secret values out of .env, vercel env pull, printenv and 30+ workarounds. Names are free, values are not.

Four small, dependency-free Python hooks for Claude Code:

Hook Event What it does
secret-exposure-guard.py PreToolUse Blocks Bash commands and file reads that would put a secret value into the transcript, and tells the agent the names-only alternative.
secret-guard.py UserPromptSubmit Blocks a prompt that contains a pasted credential (the classic case: an editor line selection from .env ).
chrome-secret-redact.py PostToolUse Redacts secret-looking values (known key prefixes, long hex, and key-shaped tokens on API-key pages) from browser-automation tool output before the model reads it.
needs-you-gate.py Stop When the agent ends a turn by handing work back to you ("Needs you", approval asks, click-through steps), makes it re-check once whether it could do the work itself.

An agent that needs to know which environment variables exist almost never needs their values. But the obvious commands (vercel env pull, cat .env.local, printenv) dump every value into the session transcript. Transcripts persist, and rotating a credential does not scrub history. The cheap fix is to never read the value at all.

Telling the agent "be careful with .env files" does not hold up. It works around the instruction with grep, python -c "open(...)", find -exec cat or a while read loop. These hooks enforce the rule at the tool-call boundary, and every block message hands the agent a safe command, so it can keep working.

A guard that matches vercel env pull has to match it however it is launched. In our own setup an agent ran timeout 60 vercel env pull ... --environment=production to read a few counters, and the guard let it through: it stripped bare wrappers like env or nice, but not a wrapper that takes its own arguments. Every production value for four projects landed on disk (deleted within minutes, nothing printed, logged as an incident). The guard now strips timeout/ gtimeout DURATION, flock LOCK, taskset, chrt, ionice, doas, chronic, unbuffer and watch, including their options and option values ( nice -n 10, timeout -k 5). Six new test cases cover the exact shape plus allow cases (timeout 60 npm run build, timeout 60 vercel env ls).

Blocking shell reads doesn't cover an agent that opens a vendor's API-key page in a browser and reads the page text. It happened twice in one afternoon before chrome-secret-redact.py existed. The redactor rewrites the tool output, so the key never reaches the transcript. It can't redact a screenshot: pair it with a rule never to screenshot key pages.

Requires Python 3.9+ on your PATH as python3. No packages.

git clone https://github.com/alittlenerdy/claude-code-safety-hooks.git
mkdir -p ~/.claude/hooks
cp claude-code-safety-hooks/hooks/*.py ~/.claude/hooks/

Then merge this into ~/.claude/settings.json (global) or .claude/settings.json (one project). The same block is in examples/settings.json. Use only the hooks you want.

{
  "hooks": {
    "UserPromptSubmit": [
      { "hooks": [ { "type": "command", "command": "python3 \"$HOME/.claude/hooks/secret-guard.py\"", "timeout": 10 } ] }
    ],
    "PreToolUse": [
      {
        "matcher": "Bash|Read|Edit|NotebookEdit",
        "hooks": [ { "type": "command", "command": "python3 \"$HOME/.claude/hooks/secret-exposure-guard.py\"", "timeout": 10 } ]
      }
    ],
    "Stop": [
      { "hooks": [ { "type": "command", "command": "python3 \"$HOME/.claude/hooks/needs-you-gate.py\"", "timeout": 10 } ] }
    ]
  }
}

Notes, checked against the hooks reference:

  • The PreToolUse matcher must listRead ,Edit andNotebookEdit as well asBash . Those tools take a file path rather than a shell command, so they leak a dotenv just ascat does.
  • Stop andUserPromptSubmit do not take a matcher.
  • Run /hooks inside Claude Code to confirm they loaded.

Verify the install:

HOOKS_DIR=~/.claude/hooks claude-code-safety-hooks/tests/run-hook-tests.sh
  • vercel env pull andvercel env get (usevercel env ls )
  • Whole-environment dumps: printenv , bareenv , bareset ,export -p ,declare -p
  • Printers on a dotenv (.env ,.env.local ,.env.production at any path):cat ,head ,tail ,less ,more ,bat ,strings ,xxd ,od ,base64 ,nl ,open ,tee ,pbcopy
  • Copiers on a dotenv: cp ,scp ,rsync ,install ,ditto
  • Matchers that print the matching line, which carries the value: grep ,rg ,awk ,sed ,perl ,jq , andcut on any field but 1
  • git show ,git log -p andgit diff on a dotenv path, includingHEAD:.env
  • The same readers on other credential files: ~/.aws/credentials ,.netrc ,.npmrc ,.pgpass ,.pypirc ,.git-credentials ,~/.ssh/id_* ,~/.config/gh/hosts.yml ,~/.kube/config ,*.pem ,*.p12 ,service-account*.json ,client_secret*.json (*.pub keys are allowed)
  • Evasions: find -exec cat ,| xargs cat ,cat < .env.local ,python3 -c "print(open('.env.local').read())" ,node -e "...readFileSync('.env.local')..." , awhile read loop over a dotenv,bash -lc "..." wrappers, env-var prefixes, backslash-escaped commands, subshells and chains
  • echo orprintf of a secret-named variable ($API_KEY ,${SLACK_BOT_TOKEN} )
  • curl -v or--trace on a request that carries an Authorization header
  • Secret managers: supabase secrets list --reveal ,gh secret list --json ...,value ,security find-generic-password -w ,op read ,bw get password ,aws secretsmanager get-secret-value ,aws ssm get-parameter --with-decryption ,gcloud secrets versions access ,kubectl get secret -o yaml|json ,doppler secrets ,heroku config ,stripe config --list
  • The Read ,Edit andNotebookEdit tools on a dotenv or credential file.Write is allowed, because creating a dotenv sends a value out of the session, not in.

Explicitly allowed (covered by the self-test, so agents always have a legal move):

vercel env ls                              # names only
grep -oE '^[A-Z_]+' .env.local | sort -u   # names only
grep -c '^STRIPE_SECRET_KEY=' .env.local   # is it set? 0 or 1
cut -d= -f1 .env.local                     # names only
printenv | cut -d= -f1                     # names only
compgen -v                                 # shell variable names
node --env-file=.env.local script.js       # loads without printing
source .env.local                          # loads without printing
v="$API_KEY"; echo "len=${#v} ...${v: -4}" # fingerprint, not value
grep -rn API_KEY src/                      # searching code for a name

Known gaps, on purpose: grep -r SOME_NAME . across a tree that happens to contain a dotenv looks exactly like a legitimate code search. A guard that fires on every code search gets switched off, so this one stays open. And nothing here can retract a value that has already reached a transcript. If that happens, rotate it.

The human can bypass secret-exposure-guard deliberately:

CLAUDE_ALLOW_SECRET_EXPOSURE=1 vercel env pull .env.local

The hook honors the variable inline, as a prefix anywhere in the command, or exported in the environment Claude Code runs in. The exported form is the only one the path-addressed tools (Read, Edit) can see. Nothing stops an agent from typing the prefix itself. This is a speed bump against accidents, which covers almost every real leak, not a sandbox.

It works with no configuration. To personalize it:

Env var Default Meaning
NEEDS_YOU_GATE_USER the user Who the agent is handing work to
NEEDS_YOU_GATE_DECISIONS DECISIONS.md Your standing-decisions file, which the agent is told to search first
NEEDS_YOU_GATE_CONFIG ~/.claude/needs-you-gate.json Optional JSON config file

Config file:

{ "user": "Sam", "decisions_path": "docs/DECISIONS.md", "extra_triggers": ["\\bover to you\\b"] }

Environment variables override the file. The gate blocks at most once per turn: the second stop (stop_hook_active: true) always goes through, so it cannot loop.

  • Fail open. Malformed input, an internal error or a missing file allows the call. A guard that breaks sessions gets deleted.
  • Never echo the secret. Block messages name the pattern, never the value.secret-guard shows at most a three-character masked preview.
  • Every block names a safe alternative , and the self-test covers each alternative, so the guard cannot block its own advice.
tests/run-hook-tests.sh

This runs every hook's --self-test, then checks that each hook fails open on malformed stdin. Current counts: secret-exposure-guard 136 cases (80 block, 51 allow, 5 end-to-end), secret-guard 15, needs-you-gate 13, chrome-secret-redact 5 (synthetic fixtures). CI runs the same script on Linux and macOS with Python 3.10, 3.12 and 3.13.

MIT. See LICENSE.

Built by Playground Giants · @j1mmyhackett

── more in #ai-agents 4 stories · sorted by recency
── more on @claude code 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/show-hn-claude-code-…] indexed:0 read:7min 2026-12-04 · —