zn-gate for Codex CLI
Install zn's hooks for Codex CLI: they block reads of .env files with values and credential files, scrub tool output and block secrets pasted into prompts.
You need Node.js 18 or later. Paste each line in a terminal and press Enter after each one:
npm install -g zn-gate
zn hooks install codex
On Windows PowerShell with the default settings, npm, npx, zn and zn-gate stop with "running scripts is disabled on this system". Add .cmd to each of them (npm.cmd, npx.cmd, zn.cmd, zn-gate.cmd), or use Command Prompt. In PowerShell the two lines above become:
npm.cmd install -g zn-gate
zn.cmd hooks install codex
On macOS and Linux, if npm install -g fails with EACCES, follow npm's guide to fixing permission errors.
Fix it without administrator rights:
mkdir -p ~/.npm-global && npm config set prefix ~/.npm-global
echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.zshrc
Bash users: write to ~/.bashrc instead of ~/.zshrc.
Then open a new terminal and run npx -y zn-gate init (in PowerShell: npx.cmd -y zn-gate init). A Node.js version manager such as nvm also avoids the problem.
Then open Codex and approve the new hooks with /hooks. Updating later? See Upgrading zn-gate.
What you get
- Reads of
.envfiles with values, credential files and~/.znare blocked, in shell commands and in patches (apply_patch). - Tool output is scrubbed before the model sees it. Codex replaces the result with its scrubbed text, so the model gets text instead of the structured result.
- A secret pasted into a prompt is blocked.
- For a command that uses a
__ZN_<NAME>__marker, Codex is told the exactzn run -- ...line to use. - zn checks MCP tool calls before they run, and answers Codex's approval request with a deny when its check would deny the call. It never answers Allow, so Codex shows no Approve button for a call zn blocks.
- At the start of a session, and of a subagent, zn adds a short reminder for the model (zn protects
.envfiles and the vault; usezn runfor commands that need a secret). It never blocks and checks nothing.
Final answer review (Codex)
zn also reads the model's final answer before you see it, and looks for a secret value or for a __ZN_ placeholder that zn did not see earlier in the session.
- Default: warn. You see a message from zn. The model does not see it, so the answer stays as it is.
"stopReview": "block". Codex asks the model to rewrite a final answer that contains a secret value. zn asks at most once per answer, and never when Codex is already continuing because of a Stop hook."replaceToolOutputOnSecret": true(off by default). When a tool result holds a real secret value, not only a__ZN_marker, zn adds its stop text and Codex replaces the result with that text. Codex's documentation says the turn then carries on. Checked on Codex CLI 0.163.0: the model still received the scrubbed copy of the output, not the stop text.
Both settings go under hooks in ~/.zn/config.json. Environment variables do not turn them on:
{
"hooks": {
"stopReview": "block",
"replaceToolOutputOnSecret": true
}
}
Run zn-gate check afterwards. Codex 0.163.0 writes each final answer to its session log three times, and zn counts every copy before it decides that a placeholder is unseen.
Limits
- Codex runs a call or prompt unchecked when a hook times out, crashes or prints something it cannot read. To close that gap, zn answers with a deny and exit code 2 a few seconds before Codex's time limit and on any unexpected error, for tool calls, approval requests and prompts, and the hook launcher blocks the call when Node.js is missing. Codex itself calls its hooks "a guardrail, not an enforcement boundary".
- If another tool has a
PreToolUseorPermissionRequesthook in your Codex folder or the project's.codexfolder, Codex runs it at the same time and it can change a command after zn has checked it. A hook set to run in the background (async) cannot block anything, and[hooks.state]withenabled = falseswitches a hook off.zn-gate checkreports Codex as not protected in these cases. - Codex's own session log keeps the original output.
- On Windows, the hook command was run by hand from PowerShell and from Command Prompt, not in our automated tests.
Keep your .env values in the vault
The agent cannot read .env files with real values. Move the values into zn-gate's encrypted vault, and the file keeps markers such as __ZN_STRIPE_KEY__:
zn unlock # creates the vault the first time
zn env shadow .env
After this your own app also reads the markers: start it with zn-gate's run command once you allow it. env shadow tells you how to undo it. More: the vault.
Check that it works
In a new terminal, run zn-gate check (in PowerShell: zn-gate.cmd check). It should show OK for Codex CLI. What the output means.
On Windows PowerShell, if the "running scripts is disabled" message still appears, see Install for the other ways to fix it.
Remove zn-gate
Run these in your own terminal. Remove the hooks first, then the program:
zn hooks uninstall codex
npm uninstall -g zn-gate
In PowerShell, use zn.cmd hooks uninstall codex and npm.cmd uninstall -g zn-gate.
Keep this order. If you remove the program first, the hooks in ~/.codex/hooks.json point to a file that no longer exists: Codex shows each hook as failed and the calls run unchecked. If that already happened, run npx -y zn-gate hooks uninstall codex to remove the hooks. Your vault stays in the .zn folder of your home folder until you delete it yourself.
Further reading
- Protect Claude Code, Codex CLI, Cursor or OpenCode with one command: what the one-command setup changes in each agent, how to check it and how to remove it.