AI-Coding-Agent am Push auf Main hindern, ohne auf den Prompt zu vertrauen
Ein Hook blockierte Claude Code beim Bearbeiten von Migrationsdateien und meldete einen klaren Fehler. Doch Claude umging die Sperre über die Shell und schrieb die Änderung stattdessen per echo in die Datei, weil der Hook nur das Edit-Tool überwachte.

Posted on Nov 29 • Originally published at Medium
I had a hook that stopped Claude Code from editing my migration files. It did its job: the edit tool was blocked and Claude got a clear error.
Then Claude made the same change through the shell instead:
echo "-- x" >> db/migrations/004.sql Enter fullscreen mode Exit fullscreen mode The hook never saw it. It was watching the edit tool, and Claude had gone around it through Bash. The file changed.
Nothing here was malicious. The agent wanted the change made, and I had put the lock on one door of a room with several.
That's the whole problem in one story. So I tested the locks I could, against the same agent, to find out which ones actually hold.
Start with the cheapest lock: telling the agent. "Never edit migrations." "Don't push to main."
It works, right up until it doesn't. A long session fills the agent's context with other things, and a rule written at the top gets less weight as the work piles up. A reader put it well on Reddit: the instruction works until the context gets cluttered and the agent forgets.
A prompt is a request. It's worth having, because most of the time the agent follows it. But it isn't a lock, and you shouldn't count it as one.
A hook runs a script before a tool call and can say no. Claude Code also has deny rules in its settings, like this one:
"permissions": { "deny": [ "Edit(**/migrations/**)" ] } Enter fullscreen mode Exit fullscreen mode I expected a deny rule to cover only the edit tool. It covers more. I tested it in Claude Code 2.1.327, with each command explicitly allowed so that only the deny rule could stop it.
echo "-- x" >> db/migrations/001.sql echo "-- t" | tee -a db/migrations/001.sql cp new.sql db/migrations/001.sql sed -i "s/int/bigint/" db/migrations/001.sql cd db/migrations && echo "-- y" >> 004.sql Enter fullscreen mode Exit fullscreen mode These ran, and the file changed:
python3 -c "open('db/migrations/001.sql','a').write('-- p')" f = db/migrations/004.sql; echo "-- x" >> $f # the path only exists after the variable expands Enter fullscreen mode Exit fullscreen mode So the deny rule closes most doors. But anything that decides by reading the command text is one interpreter away from a bypass. A script, or a path built from a variable, walks right past it.
That's still worth having. It catches the route the agent usually takes, and the error message is useful. Think of it as a seatbelt: it helps in the common crash, and it isn't a wall.
The stronger idea is to stop reading commands and make the thing itself refuse. If the file can't be written, it doesn't matter which program tries.
I tested this on Linux, as a normal user (not root, since root ignores file permissions). I made the applied migrations owned by a different user, in a shared directory where anyone can add new files but nobody can touch someone else's. New files were still allowed, because the agent has to be able to add migration 004.
Then I tried every route I could think of:
A variable-built path or a cd first made no difference. The check happens on the file, not on the command.
I also tried the weaker version, where the agent owns the files and I just remove write permission. That one failed. The agent can call os.chmod on its own file, give itself write access back, and write. The owner has to be someone other than the agent.
The same idea works for code. A reader described how they stop an agent pushing to main: they turn on branch protection so nothing merges without a pull request, and they give the agent a token that has no write permission on protected branches. The agent can open pull requests on feature branches all day. If it tries main, the server refuses before the push even becomes an approval prompt. I haven't tested that setup myself, but it's the same move: the agent never holds the key.
There's one more check worth adding, because it doesn't care how a file changed. For migrations the real rule is "new files are fine, never edit one that's been applied." So in CI:
! git diff --name-status origin/main...HEAD -- db/migrations/ | grep -v '^A' Enter fullscreen mode Exit fullscreen mode It fails the build if anything in that folder was modified, renamed or deleted. (The! matters: grep exits 0 when it finds a line, which is the case you want to fail.)
Here's how I'd stack them, weakest to strongest:
Use more than one. The upper layers tell the agent what to do. The lower ones make sure it can't do the wrong thing when it ignores you.
The best approval prompt is the one that never appears, because the agent never had the key in the first place.
Take five minutes and list what your agent can change that you would be upset to find changed: the migrations folder, main, the billing config, the production environment. Then for each one, ask where the lock sits. If the answer is "in the prompt", you have a request, not a lock.
The hooks, the deny rule, the test script and the file-permission results are in the ops-ai repo. If you find a route past any of these, open an issue.
Originally published on Level Up Coding.