Codex CLI rules: prefix_rule examples that block dangerous commands
Short version: put a .rules file in ~/.codex/rules/, write one prefix_rule() per command you want to forbid or prompt for, and test it with codex execpolicy check. Codex marks rules as experimental, so check the official rules page when you upgrade.
Where the file goes
Codex scans a rules/ folder next to each active config layer at startup. For your user, that’s ~/.codex/rules/ (for example ~/.codex/rules/default.rules). Project rules in .codex/rules/ load only when the project’s .codex layer is trusted. Restart Codex after editing.
A starter rules file
The file is Starlark, which reads like Python. We checked every rule below with codex execpolicy check (codex-cli 0.172.0).
# ~/.codex/rules/default.rules (or .codex/rules/*.rules in a trusted project)
# Never force push.
prefix_rule(
pattern = ["git", "push", ["-f", "--force"]],
decision = "forbidden",
justification = "Force push rewrites shared history. Push a new branch instead.",
match = ["git push -f", "git push --force origin main"],
not_match = ["git push origin feat", "git push --force-with-lease"],
)
prefix_rule(
pattern = ["git", "push", "origin", ["-f", "--force"]],
decision = "forbidden",
justification = "Force push rewrites shared history.",
match = ["git push origin --force main"],
)
# Ask before pushing straight to main.
prefix_rule(
pattern = ["git", "push", "origin", ["main", "master"]],
decision = "prompt",
justification = "Push a feature branch and open a PR instead.",
)
# Ask before throwing away work.
prefix_rule(
pattern = ["git", "reset", "--hard"],
decision = "prompt",
justification = "Discards uncommitted changes. Run git stash first.",
)
prefix_rule(
pattern = ["git", "clean"],
decision = "prompt",
justification = "Deletes untracked files. Run git clean -n to preview.",
)
# Recursive deletes of root, home or the whole project.
prefix_rule(
pattern = ["rm", ["-rf", "-fr", "-Rf"], ["/", "~", ".", "..", "*"]],
decision = "forbidden",
justification = "Recursive delete of a root, home or project directory.",
match = ["rm -rf /", "rm -rf ~", "rm -fr ."],
not_match = ["rm -rf dist", "rm -r build"],
)
# Publishing is for humans or CI.
prefix_rule(
pattern = [["npm", "pnpm", "yarn"], "publish"],
decision = "forbidden",
justification = "Releases are cut by a human or tagged CI.",
)
# Routine read-only commands can skip the prompt.
prefix_rule(
pattern = ["git", ["status", "diff", "log"]],
decision = "allow",
)How the fields work
patternis the command prefix as a list of arguments. An inner list like["-f", "--force"]means “any of these at this position”.decisionisallow,promptorforbidden. When several rules match, the strictest wins:forbiddenbeatsprompt, which beatsallow.justificationis shown with prompts and rejections. Forforbidden, put the safer alternative here.matchandnot_matchare inline tests that Codex checks when it loads the file.
The inline tests are worth writing. When we gave a rule for -f a match example of git push --force, the file refused to load:
Error: failed to parse policy at bad.rules
Caused by:
expected every example to match at least one rule. ... unmatched examples: ["git push --force"]Test a command against your rules
codex execpolicy check --rules ~/.codex/rules/default.rules -- git push --force origin main{"matchedRules":[{"prefixRuleMatch":{"matchedPrefix":["git","push","--force"],"decision":"forbidden","justification":"Force push rewrites shared history. Push a new branch instead."}}],"decision":"forbidden"}Add --pretty for readable output, and repeat --rules to combine files.
The limits of prefix matching
- Flag order matters. With the file above,
git push origin main --forceonly matches the “push to main” rule, so it’s aprompt, notforbidden.git push origin feat --forcematches nothing. A prefix rule can’t see a flag at the end of the command. If that matters, use a pattern-based hook in the other tools and keep branch protection on the server (see the force-push guide). - Shell wrappers. According to the docs, when Codex runs
bash -lc "..."with a simple chain of plain words joined by&&,||,;or|, it splits the chain and checks each command. Scripts with redirects,$(...), variables or globs aren’t split. They’re checked as onebash -lccall.codex execpolicy checkchecks exactly the tokens you give it, so test the plain command. - Rules govern approvals, not the sandbox. They decide which commands Codex may run outside the sandbox or must ask about. Keep the sandbox on as well:
# ~/.codex/config.toml
sandbox_mode = "workspace-write"
approval_policy = "on-request"
[sandbox_workspace_write]
network_access = false