I found a security hole in my Codex PreToolUse hook: Bash could modify .env and AGENTS.md
Orchard Hook is a free, open-source PreToolUse security hook for the OpenAI Codex CLI on Apple Silicon Macs. It checks shell commands and file edits immediately before execution. During testing, I found that Bash routes
Orchard Hook is a free, open-source PreToolUse security hook for the OpenAI Codex CLI on Apple Silicon Macs. It checks shell commands and file edits immediately before execution. During testing, I found that Bash routes such as cat > and sed -i could modify protected files including .env, AGENTS.md, Claude Code settings, and package.json scripts without being judged by the same rules as direct editing tools. I fixed this in Release 3.3.2.
I found it myself. It is fixed now.
But think about it for a second. The hole was on my side, the side that had put a guard in place. If you have no guard at all, everything is a hole from the start.
When an AI hands your information to someone outside, there is no malice. It truly believes the instruction is legitimate, and it goes all in, in good faith. That is one way prompt injection can turn into a real external action.
That is why I am building a PreToolUse hook: something that checks a command right before the AI agent runs it, and stops it if it looks dangerous. Even if the AI has been fooled, it is fine as long as it stops at the last step. In AI agent security terms, it is a runtime guardrail.
Porting a Codex PreToolUse security hook to Claude Code
Orchard Hook started on the OpenAI Codex CLI. I then ported it to Claude Code.
Moving the shape was easy. Both Codex and Claude Code have a PreToolUse hook, called before a tool runs.
But when I let the real Claude do ordinary work, the hook stopped all three tasks at the very first step. Claude has different habits from Codex in how it looks for files.
Why shell command pattern matching is not enough
Every time I fixed it, Claude wrote things a little differently. There are almost infinite ways to write a shell command. Shell command validation by pattern matching turns into cat and mouse.
Then I noticed something. 1+1 and 9β7 both give 2. Instead of memorizing expressions to judge them, I should calculate and look at the answer.
Orchard Hook also has a mode that runs the command in a copy of the real project folder and looks at what actually changed. Judging the effect, not the spelling of the command, is the more fundamental fix.
The same file edit bypassed protection through a different tool
While testing the Claude Code port, I noticed something strange. If Claude overwrote a whole file with its Write tool, the hook stopped it. If it did the same thing with cat > in Bash, it went through.
It is like a bank that refuses to lend money to "Tanaka," but lends it if he says his name is "Kobayashi."
When I looked into it, the public Codex version also still had a path. Codex's built-in apply_patch edits were checked, but shell edits such as cat > and sed -i could change these files without being asked:
-
.env(secrets) -
AGENTS.md(instructions for the AI) -
package.jsonscripts (commands that run the next time you run them)
I had let these through because "you can undo it with Git." That was wrong. If you never notice something was planted, there is nothing to undo.
I fixed it right away in Release 3.3.2 and wrote it honestly in the release notes. In the current Release 3.3.3, unrequested changes to AGENTS.md and package.json scripts are held, and so is any change to files in .claude/ or .codex/, whether it comes through apply_patch or through shell edits such as cat > and sed -i. Shell edits to AGENTS.md are held even when requested. The details are in the comparison table.
What enforcement means for an AI agent security hook
What matters more than stopping less often is this:
Does every path that leads to the same result get judged by the same standard?
If another path gets through, that rule has no real force. .env protection and AGENTS.md protection only mean something if Write, apply_patch, cat >, sed -i, and every other route to the same change are held to the same rule.
Claude Code field test: 9 tasks and 59 tool calls
In a field test with Claude Code 2.1.335 and Orchard Hook 3.3.1, Claude completed all 9 tasks. Across 59 tool calls, the hook stopped 4. Three were intentional holds triggered by recursive searches that would read .git; the fourth was a ToolSearch call the hook could not classify.
In a separate six-task set prepared independently and run once per task against 3.3.2, 4 tasks completed and 2 did not. Across 34 tool calls, the hook stopped 6: one intended .git-read hold and five over-stops.
The two unfinished tasks were an offline install from a vendored dependency, and a move, rename, and delete that the user had explicitly asked for. A task with a Japanese file path hit one over-stop but still completed. The requested-deletion problem is still unresolved.
The Claude Code version of Orchard Hook is not public yet.
Codex hook timeout behavior: fail-open vs fail-closed
Once, at passport control on my way out of a country, an officer checked me with a scary face. Then time ran out, and it felt like I just kind of got through. The same thing happens to hooks: what happens when a security hook times out?
In the Claude Kingdom, meaning Claude Code v2.1.295 or later, if the immigration check (the hook) times out, you get taken to a separate room. But only if you set onFailure: "block". That makes the hook fail-closed. If you set nothing, the Claude Kingdom lets you walk through too.
And here is the interesting part. In the Codex CLI version I tested, I could not configure an equivalent fail-closed path. In the Codex Kingdom, if you fumble in front of the officer long enough, you get in when time runs out. In my measurement on Codex CLI 0.171.0-alpha.13.2, when the PreToolUse hook timed out on a shell command, Codex ran the command. In that tested case, the Codex hook timeout was fail-open. File-edit timeouts were outside that measurement.
Who needs an AI agent execution guardrail, and who does not
This tool actually works against you if you don't need it.
You need it if you:
- let the AI run commands automatically without checking each one
- have the AI read ZIPs, repositories, or READMEs from people you don't know
- run the AI where there are
.envfiles, API keys, or production keys - leave the AI working on its own for long stretches
You don't need it if you:
- look at every command the AI suggests before you allow it
- only work with code you wrote yourself
- only run the AI in a throwaway environment with no secrets
Think of it this way. If you only have two drinks, you don't need a hangover supplement. But if you are going to drink tequila, JΓ€ger, gin, and champagne until morning, maybe you do.
Because it is not 100%, it sometimes stops things it didn't need to. If you got stopped at a sobriety checkpoint twice on the way home without having had a drink, you'd be annoyed, right? (In the independent six-task test above, 5 of the 6 holds were over-stops.)
But if handing over permissions means you could die from one critical hit, you should put some clothes on your AI. It listens to your command and heads into an unknown world to complete the mission, completely naked. When an AI is fooled into handing your information to outsiders, there is no malice. It truly believes the instruction is legitimate, and ends up charging in naked.
So the more permissions you give, the more you need to think about where it gets stopped.
In my case: check before opening, check with the hook after opening, stop rewrites of instruction files (MD) with the hook, and watch their size too. That's overkill lol. It's like wearing a hazmat suit every time I go to the convenience store lol.
Still, I don't think I'll catch a cold that easily. And you absolutely do not need to go this far.
How to start: one high-impact Codex project
Even if you want to use it, you don't need it on all your work from the start.
I think a good way is to turn it on only for one Codex project dedicated to high-impact work, and route only the work that handles secrets, sends things outside, changes production, or deletes files through that project. Everything else can stay as it is.
For the detailed setup, ask your AI. It knows your environment better than I do. If you tell it "I want to enable the hook only for this project," it will be quick.
Public version (OpenAI Codex CLI, Apple Silicon macOS only):
https://github.com/shin4141/orchard-agent-guardian/releases/tag/hook-r3.3.3-v1
The Claude Code version is in progress. If you want it, let me know in the comments.
I wrote this in Japanese and translated it into English with help from an AI.
Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.