{"slug": "show-hn-claude-code-hooks-that-stop-agents-from-reading-secret-values", "title": "Show HN: Claude Code hooks that stop agents from reading secret values", "summary": "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.", "body_md": "Stop your AI agent from reading secret values out of `.env`, `vercel env pull`, `printenv` and 30+ workarounds. **Names are free, values are not.**\n\nFour small, dependency-free Python hooks for [Claude Code](https://code.claude.com/docs/en/hooks):\n\n| Hook | Event | What it does | \n|---|---|---|\n| `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. | \n| `secret-guard.py` | `UserPromptSubmit` | Blocks a prompt that contains a pasted credential (the classic case: an editor line selection from `.env` ). | \n| `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. | \n| `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. | \n\nAn 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.\n\nTelling 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.\n\nA 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`).\n\nBlocking 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.\n\nRequires Python 3.9+ on your `PATH` as `python3`. No packages.\n\n```\ngit clone https://github.com/alittlenerdy/claude-code-safety-hooks.git\nmkdir -p ~/.claude/hooks\ncp claude-code-safety-hooks/hooks/*.py ~/.claude/hooks/\n```\n\nThen merge this into `~/.claude/settings.json` (global) or `.claude/settings.json` (one project). The same block is in [`examples/settings.json`](https://github.com/alittlenerdy/claude-code-safety-hooks/blob/main/examples/settings.json). Use only the hooks you want.\n\n```\n{\n  \"hooks\": {\n    \"UserPromptSubmit\": [\n      { \"hooks\": [ { \"type\": \"command\", \"command\": \"python3 \\\"$HOME/.claude/hooks/secret-guard.py\\\"\", \"timeout\": 10 } ] }\n    ],\n    \"PreToolUse\": [\n      {\n        \"matcher\": \"Bash|Read|Edit|NotebookEdit\",\n        \"hooks\": [ { \"type\": \"command\", \"command\": \"python3 \\\"$HOME/.claude/hooks/secret-exposure-guard.py\\\"\", \"timeout\": 10 } ]\n      }\n    ],\n    \"Stop\": [\n      { \"hooks\": [ { \"type\": \"command\", \"command\": \"python3 \\\"$HOME/.claude/hooks/needs-you-gate.py\\\"\", \"timeout\": 10 } ] }\n    ]\n  }\n}\n```\n\nNotes, checked against the [hooks reference](https://code.claude.com/docs/en/hooks):\n\n- The `PreToolUse` matcher must list`Read` ,`Edit` and`NotebookEdit` as well as`Bash` . Those tools take a file path rather than a shell command, so they leak a dotenv just as`cat` does.\n- `Stop` and`UserPromptSubmit` do not take a matcher.\n- Run `/hooks` inside Claude Code to confirm they loaded.\n\nVerify the install:\n\n```\nHOOKS_DIR=~/.claude/hooks claude-code-safety-hooks/tests/run-hook-tests.sh\n```\n\n- `vercel env pull` and`vercel env get` (use`vercel env ls` )\n- Whole-environment dumps: `printenv` , bare`env` , bare`set` ,`export -p` ,`declare -p`\n- 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`\n- Copiers on a dotenv: `cp` ,`scp` ,`rsync` ,`install` ,`ditto`\n- Matchers that print the matching line, which carries the value: `grep` ,`rg` ,`awk` ,`sed` ,`perl` ,`jq` , and`cut` on any field but 1\n- `git show` ,`git log -p` and`git diff` on a dotenv path, including`HEAD:.env`\n- 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)\n- Evasions: `find -exec cat` ,`| xargs cat` ,`cat < .env.local` ,`python3 -c \"print(open('.env.local').read())\"` ,`node -e \"...readFileSync('.env.local')...\"` , a`while read` loop over a dotenv,`bash -lc \"...\"` wrappers, env-var prefixes, backslash-escaped commands, subshells and chains\n- `echo` or`printf` of a secret-named variable (`$API_KEY` ,`${SLACK_BOT_TOKEN}` )\n- `curl -v` or`--trace` on a request that carries an Authorization header\n- 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`\n- The `Read` ,`Edit` and`NotebookEdit` tools on a dotenv or credential file.`Write` is allowed, because creating a dotenv sends a value out of the session, not in.\n\n**Explicitly allowed** (covered by the self-test, so agents always have a legal move):\n\n```\nvercel env ls                              # names only\ngrep -oE '^[A-Z_]+' .env.local | sort -u   # names only\ngrep -c '^STRIPE_SECRET_KEY=' .env.local   # is it set? 0 or 1\ncut -d= -f1 .env.local                     # names only\nprintenv | cut -d= -f1                     # names only\ncompgen -v                                 # shell variable names\nnode --env-file=.env.local script.js       # loads without printing\nsource .env.local                          # loads without printing\nv=\"$API_KEY\"; echo \"len=${#v} ...${v: -4}\" # fingerprint, not value\ngrep -rn API_KEY src/                      # searching code for a name\n```\n\n**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.\n\nThe human can bypass `secret-exposure-guard` deliberately:\n\n```\nCLAUDE_ALLOW_SECRET_EXPOSURE=1 vercel env pull .env.local\n```\n\nThe 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.\n\nIt works with no configuration. To personalize it:\n\n| Env var | Default | Meaning | \n|---|---|---|\n| `NEEDS_YOU_GATE_USER` | `the user` | Who the agent is handing work to | \n| `NEEDS_YOU_GATE_DECISIONS` | `DECISIONS.md` | Your standing-decisions file, which the agent is told to search first | \n| `NEEDS_YOU_GATE_CONFIG` | `~/.claude/needs-you-gate.json` | Optional JSON config file | \n\nConfig file:\n\n```\n{ \"user\": \"Sam\", \"decisions_path\": \"docs/DECISIONS.md\", \"extra_triggers\": [\"\\\\bover to you\\\\b\"] }\n```\n\nEnvironment 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.\n\n- **Fail open.** Malformed input, an internal error or a missing file allows the call. A guard that breaks sessions gets deleted.\n- **Never echo the secret.** Block messages name the pattern, never the value.`secret-guard` shows at most a three-character masked preview.\n- **Every block names a safe alternative** , and the self-test covers each alternative, so the guard cannot block its own advice.\n\n```\ntests/run-hook-tests.sh\n```\n\nThis 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.\n\nMIT. See [LICENSE](https://github.com/alittlenerdy/claude-code-safety-hooks/blob/main/LICENSE).\n\nBuilt by [Playground Giants](https://playgroundgiants.com) · [@j1mmyhackett](https://x.com/j1mmyhackett)", "url": "https://wpnews.pro/news/show-hn-claude-code-hooks-that-stop-agents-from-reading-secret-values", "canonical_source": "https://github.com/alittlenerdy/claude-code-safety-hooks", "published_at": "2026-12-04 14:07:38+00:00", "updated_at": "2026-12-04 14:17:34.309594+00:00", "lang": "en", "topics": ["ai-agents", "ai-safety", "ai-tools", "developer-tools"], "entities": ["Claude Code", "Anthropic", "claude-code-safety-hooks", "secret-exposure-guard.py", "secret-guard.py", "chrome-secret-redact.py", "needs-you-gate.py", "Vercel"], "also_reported_by": [], "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", "recommendation": null, "alternates": {"html": "https://wpnews.pro/news/show-hn-claude-code-hooks-that-stop-agents-from-reading-secret-values", "markdown": "https://wpnews.pro/news/show-hn-claude-code-hooks-that-stop-agents-from-reading-secret-values.md", "text": "https://wpnews.pro/news/show-hn-claude-code-hooks-that-stop-agents-from-reading-secret-values.txt", "jsonld": "https://wpnews.pro/news/show-hn-claude-code-hooks-that-stop-agents-from-reading-secret-values.jsonld"}}