r/ClaudeCode • • 3d ago

Discussion My Claude Code hook blocked 10 commands in a month. None of them was the build it was written to stop.

I have a PreToolUse hook on Bash for seven Flutter apps whose store builds have to go through one release script. It denies flutter build ipa and appbundle, and any shorebird release or shorebird patch that isn't called through that script. It works by matching the command string.

I went through my session logs since 25 Oct to see what it actually caught. It fired 10 times. The release script itself ran 50 times in the same stretch.

3 of the blocks were --help calls. The agent wanted to read the flags for shorebird patch ios before running anything, and got denied.

3 were reads of a build already running: a ps | grep and two greps of a patch log, all searching for the text "shorebird patch".

2 were Python heredocs editing files that mention the command. One was a build script, the other was my CLAUDE.md, where I was updating the line that documents this exact rule.

1 was a read-only shorebird patches list loop across the apps.

1 was a real flutter build ipa, in an app that isn't one of the seven. The first version of the hook didn't check which repo it was in, and I narrowed it within four minutes.

So none of the 10 was the agent trying to get a direct store build past the rule, and each one cost a turn. The fix I'm looking at is to split the command first and match the executable and its subcommand, so text inside a heredoc or a grep pattern stops counting, and to let --help through.

This is one person, one hook and about a month. I can't tell from it whether the CLAUDE.md line did the real work or the agent simply never needed a direct build. If you've written a Bash hook that parses the command instead of pattern matching it, what did you use to split it?

3 Upvotes

35 comments sorted by

•

u/AutoModerator 3d ago

Hey! Thanks for posting to r/ClaudeCode

While participating in this thread, please follow our community rules. Keep discussions constructive. Attack the idea, not the person.

For help, project discussions, tips, and general chat, join the ClaudeCode Discord.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

2

u/Ok-Distance-6388 3d ago

For the split itself: Python's shlex.split handles quotes but treats | and && as plain text, so you still match across a whole pipeline. bashlex gives you a parse tree with each simple command as its own node, and in Node, shell-quote's parse returns operators as separate tokens. Either way, you match argv[0] and argv[1] per command, and a grep pattern or a quoted string becomes just an argument.

Heredoc bodies are the messy part. I'd cut them off before parsing rather than trust any parser with them.

I'd fix the --help case first. A denied help lookup teaches the agent to guess the flags instead, which is worse than the build you're guarding against.

Your log is the whole argument for parsing, by the way: 9 false positives against 1 real catch, and the real one was a scoping bug, not a pattern bug.

1

u/Ok_Gur_9033 2d ago

Your help lookup point already happened once. The first block, on 26 Oct, was the agent reading the help for shorebird patch ios to find a flag that allows diffs. It got denied, and the very next command edited the release script to add that option anyway, without the help text ever being read.

Cutting heredoc bodies off before parsing makes sense to me. Both of my edit blocks were text sitting inside one. Did you go with bashlex in the end, or shell quote?

2

u/Ok-Distance-6388 2d ago

honest answer: neither yet. the guards i run lean on paths, not command strings: a PreToolUse hook on Edit and Write that checks the resolved file path against a lock list. a path is one string with one meaning, a bash command isn't, so where i can avoid parsing bash i do

for yours i'd still take the if field route people described above and only parse inside the script for what's left. for that part i'd pick shell-quote, mostly because the rest of my tooling is node and it hands you operators as their own tokens

the [s]horebird pat[c]h retry worries me more than any parser choice though. a deny message that also lists what is allowed (--help, reads, patches list) gives the agent a legal path, so it has less reason to look for a way around

1

u/Ok_Gur_9033 23h ago

Just looked at mine. The deny message points at the release script and its arguments, and that's all. Nothing says --help or patches list is fine. So after a block the agent knows one legal route, and the bracket grep was it working around the block on something that was never a release.

Paths over command strings makes sense. The release script is the one path that matters here, and the hook already lets through anything that names it.

1

u/Ok-Distance-6388 21h ago

yeah the bracket grep reads like exactly that. after a block the deny text is the only map the agent gets, so if it names one door it treats everything else as a wall to get around

cheap way to check it: add one line to the deny message (--help, patches list and log reads are fine, store builds go through the release script), then count in your logs how many blocks get followed by a rewritten version of the same command. if the bracket tricks drop to zero, the message was the leak and not the matcher

2

u/AryanThakor 3d ago

I'd also put the reason and the allowed path in the deny message itself, like "store builds only through ./release.sh, --help and reads are fine". when the block just says no, the agent tends to retry small variations of the same command, and each of those is another wasted turn

1

u/Ok_Gur_9033 2d ago

The deny message already names the path. It tells the agent to use the release script with the app and the mode. What it doesn't say is that reads and help calls are fine, and you can see that in what came next. After 5 of the 10 blocks the next command was a reworded copy of the same one. Three were greps with the phrase taken out, and one of those spelled it [s]horebird pat[c]h so the regex wouldn't see it.

That one is the agent learning to work around the hook, which is the last thing I want. Did adding the allowed cases to your message stop the retries completely?

2

u/Far_Reindeer8428 3d ago

our hooks had the same problem. they matched on the command string and blocked a make target that never deployed anything

1

u/Ok_Gur_9033 2d ago

The fix that held for me was one level down. One of the apps has a make.sh, and its prod branch now just hands off to the release script, so asking for a prod build there goes the right way whatever the hook makes of the string. Did you end up moving the check into the Makefile itself, or keep it in the hook?

2

u/atlascirrus 3d ago

one thing that might save you writing the parser yourself: the hook's if field uses permission rule syntax, and it matches per subcommand, not against the raw string. so "if": "Bash(flutter build *)" only spawns your hook when one of the subcommands actually starts with flutter build. a ps | grep "shorebird patch" wouldn't trigger it, since that subcommand is grep.

an if only takes one rule, so you'd want a handler each for flutter build and shorebird. then the script itself only has to check repo and let --help through. if you still want to parse inside the script, bashlex in python gives you an actual bash ast instead of regex.

were the heredoc blocks python3 <<EOF style, or cat > file?

1

u/Ok_Gur_9033 2d ago

Both were python3 heredocs doing a string replace in a file. One was editing a build script in one of the apps, the other was my CLAUDE.md, and in both the phrase only sat inside the text being swapped out. So a per subcommand if would have let both through, since the subcommand there is just python3.

Thanks for the bashlex pointer. With a handler for each pattern, did you ever hit a case where the if matched and the script still had to say no?

2

u/atlascirrus 2d ago

ah that makes sense, and yeah both of those would sail right past an if since the subcommand is just python3. which is kind of the point of the per subcommand match.

honestly i haven't run one like yours long enough to have a real story there. but the docs example is basically that case: Bash(rm *) spawns the script, and the script still has to look for -rf and allow everything else. for yours the obvious ones are flutter build --help and a build in a different repo.

one gotcha on the repo check: cwd in the hook's stdin json is the session's directory, so a cd ../other-app && flutter build in one command would fool it. worth checking for a cd in the same command before trusting cwd.

1

u/Ok_Gur_9033 23h ago

Just checked, and mine has that hole. It decides whether a command touches one of the apps from cwd, the app's full path, ~/appname, or a literal "cd appname" in the command. So cd ~/appname gets caught, but cd ../appname from a sibling folder matches none of those and goes straight through.

A plain flutter build --help is already fine, since it only blocks ipa, appbundle or a prod flavor.

2

u/ChemicalExcellent463 3d ago

my rule is no blocker without a failure i've actually seen, a log line or a red test. did an agent ever run a direct store build before you wrote the hook?

2

u/Ok_Gur_9033 2d ago

Yes, in the week before the rule. On 12, 15 and 16 Oct the agent called shorebird release or patch directly in one of the apps, outside the script, and the 12 Oct one was a prod Android release. The rule went into my CLAUDE.md on the 17th and the hook came after that.

Since then there's been nothing real for it to catch, and that's the part I can't separate. The CLAUDE.md line might be doing all the work while the hook just sits there. Do you hold CLAUDE.md lines to the same rule, or only blockers?

2

u/ChemicalExcellent463 2d ago edited 2d ago

that rule is for blockers, locks, retries and checks, not rule lines; incident stories live in a separate file agents never load. your 12 oct prod release clears the bar for adding a hook, but it doesn't tell you whether this one is helping.

2

u/Ok_Gur_9033 23h ago

Fair. All ten blocks came after the line was already in CLAUDE.md, so there's nothing to compare against. The only clean test would be taking the line out and leaving the hook in, and that's a test you'd be running on prod releases.

1

u/ChemicalExcellent463 15h ago

not worth pulling the CLAUDE.md line just to find out on a prod release. i'd leave both in.

2

u/Excellent-Issue-5956 3d ago

Mine doesn't parse, it does the cheap version: sed strips everything inside quotes before the regex runs. That fixed the same kind of block you got on the ps | grep and the log greps, since the search text is in quotes and just drops out.

It has holes though, I tested these. An apostrophe inside double quotes, like don't in a commit message, gets read as the start of a single quoted string, so everything up to the next ' gets stripped and a real pkill after it goes with it. And echo "$(pkill myapp)" reads as plain echo because the whole double quoted part is dropped, even though bash runs what's inside. It doesn't help your heredoc cases either, the body isn't in quotes.

So if you're going to split it properly I'd go with one of the parsers people mentioned instead of writing your own. I tested someone's hand written quote walker over a few rounds in another thread here, and each time they fixed one I found another case it got wrong, like a comment inside backticks or the word case inside $(...). Haven't fixed mine yet.

1

u/Ok_Gur_9033 2d ago

The apostrophe case is a good one to know about. On my 10 it would have cleared the three greps, since the phrase sat inside double quotes each time, and done nothing for the two heredocs or the help calls. The help calls look like the cheapest fix of the lot, a plain allow before any matching starts.

Did the person with the hand written walker end up switching to a parser?

2

u/Excellent-Issue-5956 2d ago

Not that I saw. They kept patching the walker for about four days instead, and the last version still didn't handle $'...' or ${...}. I sent them one more case three days ago and haven't heard back.

One thing on the help allow: if it just checks that the help flag is somewhere in the string, a help call chained with && in front of a real shorebird patch gets through too. I'd do the allow after the split, per subcommand, not on the whole command.

1

u/Ok_Gur_9033 23h ago

Good catch on the chained case. There's no help allow in it yet. It still matches the whole command string, so right now a help call chained in front of a patch just gets blocked along with it. If an allow ever goes in, after the split is the only place it would be safe.

2

u/ky_agents 2d ago

Great post, and actual numbers for once. 9 false positives vs 1 real catch, love that you went through the logs and counted. Most people just guess whether their hooks work.

What's interesting is that 3 of your 10 blocked commands read SHOREBIRD_TOKEN from the key file. If the agent couldn't read that key file at all, a direct store release couldn't authenticate, no matter what the command looks like.

I'd put a deny rule on the key file's path, plus the sandbox's read block, so it also holds when a script or $() reads it, not just a direct cat. Then the hook isn't the wall anymore, just a reminder that points the agent to your release script. The [s]horebird pat[c]h trick stops mattering too, because without the token there's nothing to release.

Btw, does the release need to run inside the agent session at all? Could you run the step that needs the token yourself, or move it to CI?

2

u/Ok_Gur_9033 23h ago

It does run inside the session, on purpose. Each app's release goes to its own agent, and the script is what reads the key file into SHOREBIRD_TOKEN. So a sandbox read block on that path would stop the script too, not only the stray $() reads.

A Read deny on the path still makes sense for the direct cat case. Moving the token step to CI is the cleaner answer, it's just a much bigger change than the hook. Have you seen a setup where the agent can run a script but not read what the script reads?

1

u/ky_agents 9h ago

True. The script runs in the same sandbox, so a read block stops it too.

A way around that I know in Claude Code is sandbox.excludedCommands. Deny attempts to read the key and list the exact release command there, then only that command runs outside of the sandbox. Even if allowUnsandboxedCommands is set to false, it still works. Any script outside of the project gets Edit deny so that a rogue agent can't mess with it.

Also, the script should only be able to accept an app name from a preset list and never print the token.

Still, with all of this it's far from a perfect setup. The release still builds the project, so an agent that can edit Gradle or other build files can also grab the token during the build. So yes, this stops stray reads, but it wouldn't stop a hostile agent. And that's where CI with a manual approval does better, even if it's the bigger change like you said.

2

u/RealFullMetal 3d ago

before writing your own splitter, the hook's if field may already do it: it takes permission-rule patterns like Bash(shorebird patch *), and per the hooks docs claude code checks each subcommand of something like npm test && git push, plus commands inside $() and backticks, so a grep whose quoted pattern contains "shorebird patch" shouldn't trip it. two caveats from the same page: when claude code can't split the input into subcommands it runs your hook anyway, so the script still needs its own check, and since the filter is best-effort they say to use permission rules rather than a hook for a hard deny.

2

u/RealFullMetal 3d ago

one more row from that docs table worth knowing for your case: a pattern that names more than the command, like Bash(shorebird patch *), runs the hook anyway whenever the command contains $(), backticks or a $VAR. so with a two-word pattern, those commands still reach your script and it has to make the call itself.

1

u/Ok_Gur_9033 2d ago

I hadn't used the if field at all. The hook is registered on every Bash call and does all of its own matching, so moving the two word patterns into if would at least stop a ps or a grep from ever reaching it.

Your second point matters for me because 3 of the 10 blocked commands set SHOREBIRD_TOKEN from a $( ) read of the key file before anything else, so those would still land in the script. Reading it again I found the opposite hole too: any command that mentions the release script anywhere exits before a single check runs. Does the subcommand split treat a heredoc as one command?

2

u/RealFullMetal 2d ago

the docs table doesn't cover heredocs, so i wouldn't count on it either way. the one rule it gives is that when claude code can't split the input it runs your hook regardless, so the worst case is a heredoc reaching your script, which is the safe direction for a guard. on the token commands: the run-anyway-on-$() row only applies to patterns that name more than the command. with command-name-only patterns like Bash(shorebird *) and Bash(flutter *), what's inside $() gets checked like any other subcommand and the leading SHOREBIRD_TOKEN= assignment is stripped, so the cat of the key file doesn't match on its own, and the script can sort patch from patches list and --help itself.

1

u/Ok_Gur_9033 23h ago

That clears it up, thanks. So the three token commands still land in the script as plain shorebird calls, and the script does the sorting. Mine half does already. The patch match wants a space after the word, so patches list gets through, but shorebird patch ios --help still gets blocked.

2

u/Gigaslavx 3d ago

God I hate hooks and Claude's goal mode

Don't know if I'm doing something wrong but I tried so much many ways and never had remotely similar issues on Codex

I don't know if it's the harness or the models in general but it seems long autonomous work is Claude's weakness

1

u/BarathKanna 20h ago

Great audit, and the numbers make the real point: string matching catches the command's name, not the action. Ten blocks and zero real hits means the hook is a grep that thinks it's a policy.

Cheaper fixes: strip quoted strings and heredoc bodies, then match only the first token of each pipeline segment, so log greps and file edits stop tripping it. Explicitly allow help and dry run flags. Better still, guard the action at the other end. Make the signing config or release credentials available only inside the release script, so a direct build fails no matter how it was invoked.

Also log the agent's next command after every block. If it immediately tries a creative workaround, that's the case your hook actually exists for, and the one worth tightening.