Codex rules let you decide which commands the Codex CLI may run outside the sandbox. You write prefix_rule() entries in a .rules file, usually ~/.codex/rules/default.rules, each with a command prefix and a decision: allow (run without asking), prompt (ask every time) or forbidden (block without asking). When several rules match, the most restrictive wins. Restart Codex after editing, and test a rule with codex execpolicy check. OpenAI marks rules as experimental, so details may change.
Everything here comes from OpenAI's rules documentation.
01Where rules files live
Codex scans a rules/ folder next to each active config layer when it starts:
- Your user rules:
~/.codex/rules/, for example~/.codex/rules/default.rules. They apply in every project. - Project rules:
<repo>/.codex/rules/. These load only when the project's.codex/layer is trusted, so a repository you cloned can't quietly allow commands for itself. - Team Config locations, where your organisation uses them.
When you choose "Yes, and don't ask again for commands that start with ..." in an approval prompt, Codex writes that rule to ~/.codex/rules/default.rules. So if you've used that option, the file already exists and you can review what you've allowed. The approval prompt guide explains each choice.
Codex reads rules at startup, so restart it after changing a file.
02Writing a rule
Rules use Starlark, which looks like Python but can't touch the file system or have other side effects.
# Ask before viewing PRs outside the sandbox.
prefix_rule(
pattern = ["gh", "pr", "view"],
decision = "prompt",
justification = "Viewing PRs is allowed with approval",
match = ["gh pr view 7888", "gh pr view --repo openai/codex"],
not_match = ["gh pr --repo openai/codex view 7888"],
)
pattern (required)The command prefix, one list element per argument. An element can be a string, or a list of alternatives such as ["view", "list"]decisionallow (the default), prompt or forbiddenjustificationA reason Codex may show in the prompt or rejection. For forbidden, suggest an alternativematch, not_matchExample commands Codex checks when it loads the file, to catch mistakesThe pattern must match the start of the command exactly, argument by argument. In the example, gh pr --repo openai/codex view 7888 doesn't match, because --repo sits where view should be.
03A few useful rules
Allow read-only Git commands without prompting:
prefix_rule(pattern = ["git", ["status", "diff", "log"]], decision = "allow")
Always ask before pushing, even if a broader rule allows git:
prefix_rule(pattern = ["git", "push"], decision = "prompt")
Block a command outright and point Codex elsewhere:
prefix_rule(
pattern = ["rm", "-rf"],
decision = "forbidden",
justification = "Don't delete recursively. Ask me first.",
)
04Most restrictive wins
When more than one rule matches, Codex applies the strictest decision: forbidden beats prompt, which beats allow. You can safely keep a broad allow alongside narrow prompt or forbidden rules, and the narrow ones still apply.
05Chained commands and shell wrappers
Codex treats bash -lc, bash -c and their zsh and sh equivalents specially, because one string can hide several commands.
- Simple chains are split. If the script is plain words joined by
&&,||,;or|, Codex splits it into separate commands and checks each one.git add . && rm -rf /becomesgit add .andrm -rf /, so allowinggit adddoesn't let the second part through. - Anything more complex isn't split. Redirection,
$(...)substitutions, environment variables such asFOO=bar, wildcards and control flow make Codex treat the whole thing as one command,["bash", "-lc", "<full script>"], and match your rules against that.
06Test a rule before relying on it
codex execpolicy check --pretty \
--rules ~/.codex/rules/default.rules \
-- gh pr view 7888 --json title,body,comments
It prints JSON with the strictest decision and the rules that matched, including their justifications. Pass --rules more than once to combine files.
07For administrators
Admins can enforce restrictive prefix_rule entries from requirements.toml, which users can't loosen with their own files. See OpenAI's managed configuration docs for the format.
08Rules, the sandbox and hooks
Rules decide what happens when Codex wants to run a command outside the sandbox. Commands the sandbox already allows don't need rules. For when Codex asks in the first place, see approval modes explained. A PermissionRequest hook is the other way to automate decisions, with your own script; see the PermissionRequest guide.
Requests that still reach a prompt can go to your phone. Notifier for Codex sends them to an iPhone or Android app, from Mac, Windows and Linux, so you can approve or reject while away. A forbidden rule blocks the command before any prompt, so it never reaches the phone. Notifier for Codex is independent and not affiliated with OpenAI.
09FAQ
Where is the Codex default.rules file?
~/.codex/rules/default.rules. Codex creates it when you choose "don't ask again" for a command prefix, and you can edit it by hand.
Do Codex project rules in .codex/rules work?
Yes, but only when the project's .codex/ layer is trusted. Otherwise Codex ignores them.
How do I block a command in Codex?
Add a prefix_rule with decision = "forbidden" and a justification suggesting an alternative, then restart Codex.
If I allow git add, can Codex chain a dangerous command after it?
Not in a simple chain. Codex splits &&, ||, ; and | chains and checks each part, and the strictest result wins.