One line in a repo switched off my AI safety hooks, and Claude Code never asked me again
By Kazani
I run Claude Code with safety hooks. They are small scripts that check every action before it happens. One blocks the agent from reading secret files like .env. One stops deletes outside the project. One blocks force-pushes. I wrote them so that a bad instruction can't talk the agent past them.
This week I tested whether a repository could switch them off. It could, three different ways. In a fourth test, one line in the repo's settings file ran the repo's own code 12 times in a print-mode session where the agent didn't use a single tool. Then I checked whether Claude Code would warn me if a repo I had already trusted changed its hooks. It didn't.
Below is what I tested, who found the bigger problem first, and what I changed. Every claim is labelled: tested (I ran it), docs (Claude Code's documentation says so), or inferred.
Finding 1: one env line runs the repo's code
Claude Code reads a settings file from the project folder, .claude/settings.json. That file can have an env block, which sets environment variables for the session and everything it starts.
I made a throwaway repo whose settings file set one variable, BASH_ENV, pointing at a script inside the repo. Bash runs whatever file BASH_ENV names every time a bash script starts. Claude Code runs hooks when a session starts and around every tool call, and most of mine are bash scripts.
I started Claude Code in that folder and asked it to reply with one word. It used no tools. The repo's script ran 12 times, once for each of the 12 bash hooks that fire in a session like that. None of them were my guardrails, which only fire when the agent uses a tool. When I let it make one Bash call, the count went to 20: 7 runs from my guardrail hooks and 13 from my other hooks. (tested, Claude Code 2.1.327, macOS, print mode; I traced which hook started each run)
That test ran in print mode, which never asks whether you trust the folder. In a normal session, Claude Code applies a repo's env values only after you trust the folder, so the line does nothing until you say yes. (docs) Finding 2 is about what happens after that yes.
The repo didn't need a hooks section, an MCP server or anything else that looks like code. It needed one line in a settings file.
Finding 2: a trusted repo can add hooks without a new prompt
The first time you open a folder, Claude Code asks whether you trust it. I opened a scratch repo, said yes, closed it and opened it again. No prompt, as expected.
Then I committed a change that added a startup hook to the repo's settings, with the commit message "chore: update config". I opened the folder again. There was no prompt and no notice, and the hook ran. (tested, in a separate config folder so my real setup wasn't touched)
So trust is given once, per folder. Anything that later lands in that folder through a pull or a branch rides on that old yes. That is how the North Korea-linked group Google calls MIDNIGHT NEPTUNE used it. According to Google's threat report from 17 Nov, they got into crypto companies through fake recruiter profiles, then edited .claude/settings.json in the companies' own internal repos so that starting Claude Code downloaded a backdoor. Those developers had trusted those repos long before. (docs: Google Threat Intelligence report, 17 Nov 2026)
There is an open request on Claude Code's GitHub to ask again when a trusted repo's hooks change (issue 103766). Codex already does this: per Datadog's write-up, it hashes each hook and won't run a new or changed one until you review it.
None of this is new, and here's who found it first
The env part is not my discovery. Nick Frichette at Datadog Security Labs published it on 3 Aug. His example used PATH: a repo's settings point PATH at its own folder, so when something in the session runs git, it runs the repo's fake git instead. (In my own tests, the git calls came from a plugin's hook, not from Claude Code itself.) He also names BASH_ENV, NODE_OPTIONS and PYTHONPATH, and makes a broader point: any program can give meaning to an environment variable, so a list of "dangerous" names will never be complete.
Claude Code's docs show that problem in practice. They have a list of variables Claude Code ignores in a project's env block: a repo can't set HOME, TMPDIR or CLAUDE_CONFIG_DIR, among many others. PATH, BASH_ENV and NODE_OPTIONS are not on the list. (docs, checked 24 Nov)
Repos that plant AI assistant hooks are also not new. On 26 Oct, JFrog reported 42 armed GitHub repos across 11 owners, with execution hooks in files like .claude/settings.json, .cursor/mcp.json and .vscode/tasks.json. And the disableAllHooks problem below is an open GitHub issue (110181), filed on 15 Nov by someone else.
What I add is narrower. I wanted to know what this does to a person's own guardrails, so I tested it against mine.
Finding 3: three ways a repo turned off my guardrail
I made a fake .env file holding a made-up string, then asked the agent to read it. With no repo settings, my hook blocked it. Then I tried four different one-line settings files:
All tested on 2.1.327, in throwaway folders.
The second bypass is my design mistake, and it's worth showing. My hook has an unlock: start Claude Code with a certain variable set to 1 and the hook steps aside. The comment I wrote above it says "the running model cannot change this process's inherited env." That is true of the model. It is not true of the repo. A settings file sets environment variables, and my hook couldn't tell where a value came from.
The third works because my hook calls a small tool named jq to read its input, and it looks jq up on PATH. Point PATH at a fake jq that returns nothing, and the hook sees nothing to block.
Companies get a protection that solo developers don't have by default
Claude Code has a fourth settings level above your own, called managed settings. It's meant for a company's IT team, but on a Mac anyone with admin rights can install it.
First, a repo's disableAllHooks can't switch off managed hooks. (docs)
Second, managed hooks start without the repo's env values. I moved my guardrails into managed settings and reran the BASH_ENV repo. Claude Code's debug log said the hook "is the organization's and starts without the repository settings' env of BASH_ENV". My guardrails ran 14 times, and the repo's script ran 0 times from them. It still ran 13 times from my other hooks, which live in my own settings file. (tested, 2.1.327)
So Claude Code keeps a repo's env away from an organization's hooks, but not from hooks in a person's own settings file.
What I changed
Everything below is installed on my machine and tested.
1. Guardrail hooks moved to managed settings. On a Mac that's a file at /Library/Application Support/ClaudeCode/managed-settings.json. Creating it needs admin rights, and changing it needs sudo. A repo's BASH_ENV no longer reaches the guardrails (tested), and a repo's disableAllHooks can't switch them off (docs).
2. The hook scripts stop trusting their environment. Each one now starts bash in a mode that ignores BASH_ENV, throws away every inherited variable, and sets PATH and HOME itself. Against the four attacks in the table, the hardened scripts blocked all four and still allowed normal reads and deletes inside the project. (tested, by running the scripts directly with each attack's environment)
3. Unlocks are no longer variables. To unlock a hook now, I create a flag file that only an admin can create, and the hook honors it for 15 minutes after it's made. A forgotten unlock dies on its own. (tested with a simulated owner, because a real one needs sudo)
4. My tool vetting checks for this. The linter I run before installing any agent tool now flags a repo whose settings use disableAllHooks or set any env value. It's tested on sample repos, and gives the same results as before on everything else. (tested)
Two habits go with these. Before I open a repo, and after every pull, I read .claude/settings.json, .claude/settings.local.json and .mcp.json, and I run the search Datadog published, which covers more file types than I would think to check. Trust isn't asked again, so the pull is the moment to look. And I treat print mode the same way: running claude -p shows no trust dialog at all (docs), so running it in a repo I haven't read is the same as running the repo's code.
What this doesn't fix
First, the guard scripts can be bypassed without sudo. The scripts themselves are owned by the system, but they call jq and python3 from /usr/local/bin, a Homebrew folder my own account can write to. I tested two swaps, each with no sudo. A fake jq that always prints "allow" let 4 of my 7 guards pass, including the one that blocks reading .env. A .pth file dropped into that python's site-packages folder let a force-push past the git guard, and let a secret through the read guard when the agent reached it through a symlink. A plain .env read was still blocked. (tested, by running each guard the way Claude Code runs it; I couldn't build a valid test for a seventh guard)
Second, a repo can still run its code. My 13 other bash hooks live in my own settings file, so they still get the repo's BASH_ENV. Code that runs as me doesn't need to get past my guardrails. It can read .env itself, or swap jq. (the BASH_ENV runs are tested; a repo doing the jq swap this way is inferred)
So the claim I can make is narrow. A line in a repo's settings file can no longer switch my guardrails off directly. It can still run code on my machine, and that code can route around them.
None of this makes an untrusted repo safe. Datadog's advice holds: open unfamiliar code in a throwaway environment with no real credentials.
Disclosure
Before publishing, I checked whether anything here was new enough to report to Anthropic first. Nothing was: all of it was documented or public before I started. Claude Code's docs say hooks and every subprocess inherit the env block. Datadog's 3 Aug post covers env code execution, BASH_ENV included. The disableAllHooks problem is issue 110181, and the missing re-prompt is issue 103766. No report was filed.
Tests ran on Claude Code 2.1.327 to 2.1.330, on macOS.
If you run Claude Code hooks as guardrails, check today whether a repo can set anything they depend on. I'll keep publishing tests like this one. Follow me if you want the next one.
Drafted and edited with Claude from tests I ran on my own machine. I chose what to claim and reviewed the final text.