OpenAI Codex CLI
A PreToolUse hook that checks every Codex CLI shell command and apply_patch edit before it runs.
Codex CLI 0.124.0 or later. With 0.117 to 0.123, add [features] codex_hooks = true to ~/.codex/config.toml first.
Connect
shellfirm connect codexshellfirm — connecting Codex CLI integration
/home/dev/.codex/hooks.json:
- add hooks.PreToolUse entry (matcher "Bash|apply_patch") running `shellfirm check --hook codex --config-dir /home/dev/.config/shellfirm`
✓ written
Codex shell commands and apply_patch edits are now checked by shellfirm (PreToolUse hook; dangerous and risky ones are denied, because Codex hooks cannot ask). Codex runs a new or changed hook only after you trust it: start `codex` once and trust it (or open /hooks); `codex exec` skips an untrusted hook. Needs Codex CLI 0.124.0 or later.Trust the hook once
Codex runs a new or changed hook only after you trust it. Start the interactive
codex once: it lists untrusted hooks when it starts (or open /hooks).
codex exec skips an untrusted hook without a warning, so trust it in the
TUI before you rely on it in scripts. Re-running connect after a change to the
hook line (for example a new --config-dir) needs a new trust.
What it installs
The file is ~/.codex/hooks.json ($CODEX_HOME/hooks.json when CODEX_HOME is set):
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash|apply_patch",
"hooks": [
{
"type": "command",
"command": "shellfirm check --hook codex --config-dir /home/dev/.config/shellfirm",
"timeout": 30
}
]
}
]
}
}Bashcalls are checked as shell commands (tool_input.command). A command given as an argv array (["bash", "-lc", "…"]) or undercmd, and the shell tool namesshell,exec_commandandlocal_shell, are read asBash;tool_input.workdir(relative tocwd) is where the command runs.apply_patchcalls are checked file by file: every file the patch adds, updates, deletes or moves to is checked like a write. The worst file decides, whatever the order, and the reason names it. A patch run through the shell (apply_patch '<patch>',apply_patch <<'EOF' … EOF) is judged the same way.- With
CODEX_HOMEset (a relative value is resolved against the hook's current directory),$CODEX_HOME/config.tomlandhooks.jsonare protected like~/.codex/…, and$CODEX_HOME/auth.jsonis a credential file for reads and edits. - A patch run as
applypatch(Codex's other name for it) is judged likeapply_patch, an ANSI-C quoted patch (apply_patch $'…') is decoded, and a patch whose content cannot be seen (apply_patch < p.diff,apply_patch "$(cat p.diff)") is denied, since Codex cannot ask. - Codex rejects unknown top-level keys in
hooks.json, so shellfirm only addshooks(statusanddoctorreport other keys). Your own hooks in the file are kept. [features] hooks = false(formerlycodex_hooks) inconfig.toml(or the project's.codex/config.toml) turns hooks off.connect,statusanddoctorsay so;statusshowshook OFF:
codex (user): hook OFF, file tools yes, fail-closed no, MCP n/a (hooks are turned off in /home/dev/.codex/config.toml ([features] hooks = false): Codex runs no hook (set it to true)) ...What Codex sees
Codex PreToolUse hooks cannot ask (Codex treats ask as invalid hook output), so shellfirm answers deny for both risky and dangerous commands, with the reason, the matched rule and a safer alternative. If the command is intended, run it yourself.
| Command | Answer | Effect |
|---|---|---|
No check matches (ls) | nothing (no output, exit 0) | Codex's own approvals apply |
Risky, below the auto-deny threshold (cat ~/.ssh/id_rsa, Medium) | deny, saying Codex cannot ask | Blocked; the audit log records deny_reason: agent_auto_deny |
At or above the threshold, or deny-listed (git push -f origin main, High) | deny | Blocked |
Real answer for cat ~/.ssh/id_rsa, decoded from hookSpecificOutput.permissionDecisionReason:
shellfirm BLOCKED this command.
[exfil:read_secret_file] [MEDIUM] This reads a private key or a credentials file. Shown on screen it can leak (screen sharing, terminal logs); run by an AI agent, the secret goes into the conversation and on to the model provider.
BLOCKED: Matched risky pattern(s) (severity MEDIUM, below the auto-deny threshold HIGH); confirm before running (Codex cannot ask for confirmation, so shellfirm blocks it; run it yourself if it is intended)
Safe alternative: read only what you need, not the secret itself (An SSH private key: `ssh-keygen -y -f <key>` prints just the public half. A credentials file: a command that hides the secret shows the same, e.g. `aws configure list`, `kubectl config view`, `gh auth status`, `npm config list`.)The JSON around it:
{"hookSpecificOutput":{"hookEventName":"PreToolUse","permissionDecision":"deny","permissionDecisionReason":"shellfirm BLOCKED this command.\n[exfil:read_secret_file] [MEDIUM] ..."}}Edits of agent instruction files (AGENTS.md, CLAUDE.md, ...) are an everyday task: they go ahead, with a note, when every file of the call is one. Overwriting a private key or credential file (~/.ssh/id_rsa, ~/.aws/credentials) is denied (shellfirm-self:credential_file_edit), and deleting one (*** Delete File:) is Critical. Malformed hook input, and a command or path with a NUL byte, is denied (Failing closed).
Input to a running session
Codex runs no PreToolUse hook for write_stdin (input sent to a running
exec_command session): only the command that started the session is checked.
shellfirm sandbox codex covers what runs there.
If shellfirm cannot run
If the hook fails or times out, Codex runs the tool call. shellfirm status
shows fail-closed no for Codex for that reason. shellfirm itself never exits
without a decision for risky input: a crash becomes a deny.
Options
| Option | Effect |
|---|---|
--scope user (default) | ~/.codex/hooks.json |
--scope project | ./.codex/hooks.json, which Codex loads in trusted projects. No personal paths are recorded; backups go to ~/.cache/shellfirm/backups |
--dry-run | Show what would change, write nothing |
--uninstall | Remove shellfirm's entries only |
$ shellfirm connect codex --scope project --dry-run
shellfirm — connecting Codex CLI integration (dry run)
Would create /home/dev/projects/webapp/.codex/hooks.json:
- add hooks.PreToolUse entry (matcher "Bash|apply_patch") running `shellfirm check --hook codex`
Dry run: no changes were made. Run without --dry-run to apply.codex --dangerously-bypass-hook-trust, codex --sandbox danger-full-access (-s, -sdanger-full-access, -c sandbox_mode=danger-full-access, -c default_permissions=':danger-full-access'), codex -c features.hooks=false (in any -c spelling: -cfeatures.hooks=false, features={hooks=false}, profiles.<name>.sandbox_mode=danger-full-access), codex --disable hooks and a fresh CODEX_HOME started by an agent ask first, like the other hook bypasses (see self-protection).
MCP server and sandbox
connect codex installs only the hook. To let Codex also look commands up with shellfirm's MCP tools, register the server: codex mcp add shellfirm -- shellfirm mcp (see Other agents).
The hook reads the command line. To stop what a script does once it runs, turn on Codex's own sandbox with your credential files denied: shellfirm sandbox codex. See Agent sandbox.
Check the integration
Agent integrations:
codex (user): hook yes, file tools yes, fail-closed no, MCP n/a (Codex runs a new or changed hook only after you trust it (the TUI asks at startup, or use /hooks; `codex exec` skips it until then))Remove the hook (uninstall)
$ shellfirm connect codex --uninstall
shellfirm — removing Codex CLI integration
/home/dev/.codex/hooks.json:
- remove 1 shellfirm hook(s) from hooks.PreToolUse
- delete the file (only shellfirm's entries were in it)
✓ removed (backup: /home/dev/.codex/hooks.json.shellfirm-bak-20261119T203029Z)
shellfirm removed from Codex CLI.To see what the hook would have caught in your earlier Codex sessions, run shellfirm audit replay --agent codex (read-only). See Replay agent sessions.