Codex config.toml: the settings that decide what Codex may do
The config.toml settings that decide what Codex may run, write, read and reach, tested on Codex CLI 0.172: approval_policy, sandbox_mode, permission profiles, network, environment variables, hooks, rules and requirements.toml.
· 8 min read
Codex reads one file, ~/.codex/config.toml, to decide what it may do on your machine, and most of it is about the commands it runs. The short version, from our tests on Codex CLI 0.172: the sandbox controls writes and network, not reads. In both built-in sandbox modes Codex could read .env and ~/.ssh/config. A permission profile with deny entries is what stopped that. Environment variables named like API_KEY reached every command until we changed one setting, and a hook only blocked a command when it exited with code 2 or answered deny.
Below is what each setting controls and what it did when we ran it. We used codex sandbox, which runs one command under your config the way Codex does, and codex exec for whole turns, against a throwaway project. Rows marked “docs” come from the Codex documentation instead.
What each setting controls
| Option | Controls | What happened | From |
|---|---|---|---|
| sandbox_mode | ControlsWrites and network for the commands Codex runs | What happenedread-only: no writes anywhere, no network. workspace-write: writes in the project and /tmp, not in your home folder, and no network. Both let a command read .env and ~/.ssh/config. | Fromtested |
| [sandbox_workspace_write] | ControlsNetwork inside workspace-write | What happenednetwork_access = true let curl reach example.com in a Codex session. codex sandbox ignored it, so test network in a real session. | Fromtested |
| default_permissions + [permissions.*] | ControlsNamed profiles: reads, writes and network per path | What happenedA profile that extends ":workspace" with "**/.env*" = "deny" stopped cat, grep -r and a symlink with "Operation not permitted", and Codex told the model, which then declined to try. "~/.ssh" = "deny" worked for the home folder. network.enabled = true opened the network. | Fromtested |
| [shell_environment_policy] | ControlsEnvironment variables passed to commands | What happenedBy default FOO_API_KEY, MY_SECRET and GH_TOKEN all reached a command. With ignore_default_excludes = false, names containing KEY, SECRET or TOKEN were removed. | Fromtested |
| [[hooks.PreToolUse]] | ControlsA program that sees each tool call first | What happenedOnly exit code 2 or a JSON deny blocked. A hook that exited 1, timed out, printed junk, answered "ask", was missing or was not trusted yet let the command run. | Fromtested |
| approval_policy | ControlsWhen Codex stops to ask you | What happenedon-request lets the model ask to run something outside the sandbox; never asks nobody, so whatever needs to leave the sandbox fails. untrusted is no longer supported and on-failure is deprecated. | Fromdocs |
| rules (~/.codex/rules) | ControlsAllow, prompt or forbid commands by prefix | What happenedprefix_rule entries in .rules files. Every rule that matches counts and the strictest wins: forbidden, then prompt, then allow. | Fromdocs |
| [projects.*] trust_level | ControlsWhether a repository's own .codex/ folder loads | What happenedAn untrusted project's .codex/config.toml, hooks and rules are skipped. trust_level = "trusted" under [projects."<path>"] loads them. | Fromdocs |
| requirements.toml | ControlsWhat a machine's admin allows, above your config | What happened/etc/codex/requirements.toml on macOS and Linux can limit approval policies, sandbox modes and permission profiles, and pin settings people cannot change. | Fromdocs |
Every approval_policy value, tested one by one, and why codex exec never asks, are in Codex approval_policy explained.
The sandbox is about writes, not reads
sandbox_mode = "read-only" sounds like the safe choice, and for changes it is: nothing in the project, /tmp or your home folder could be written. But “read-only” means Codex may read everything. In our run it printed .env and read ~/.ssh/config in both read-only and workspace-write. Network was off in both until we turned it on.
Permission profiles are the newer way to say the same things per path. You name a profile, base it on :read-only or :workspace, and add "deny" for paths. The docs say to use either a profile or sandbox_mode, not both. A deny covered every way we tried to read the file, and also writing a new .env.new. How to stop Codex reading .env files has the full run, and the gaps a deny leaves, such as git show HEAD:.env.
A starting config for a laptop
This file loaded in Codex CLI 0.172, and the checks under it gave the results shown. Replace the hook command with your own, or leave the hook out.
# ~/.codex/config.toml
# Top-level keys first: a key written under a [table] belongs to it.
approval_policy = "on-request"
default_permissions = "project"
[permissions.project]
extends = ":workspace"
# Outside the project: folders Codex should not read.
[permissions.project.filesystem]
"~/.ssh" = "deny"
"~/.aws" = "deny"
"~/.config/gh" = "deny"
# Inside the project: files Codex should not read or write.
[permissions.project.filesystem.":workspace_roots"]
"**/.env*" = "deny"
"**/*.pem" = "deny"
# Keep variables named like KEY, SECRET or TOKEN out of commands.
[shell_environment_policy]
ignore_default_excludes = false
# A hook that sees every tool call. Only exit 2 blocks.
[[hooks.PreToolUse]]
matcher = ".*"
[[hooks.PreToolUse.hooks]]
type = "command"
command = "/usr/local/bin/my-hook"
timeout = 120# Run one command the way Codex would, with your config:
codex sandbox -- sh -c 'cat .env; echo x > ~/x.txt; curl -sI https://example.com'
# Expected with the config above:
# cat: .env: Operation not permitted
# sh: /Users/you/x.txt: Operation not permitted
# (curl fails: no network)codex sandbox ignored network_access = true under [sandbox_workspace_write], while a Codex session honoured it. A profile’s network.enabled = true worked in both. Test network settings in a session.
One TOML mistake that breaks the whole file
In TOML every key belongs to the table above it. We appended approval_policy to the end of a file that ended with a filesystem table, and Codex refused to start, because it read the line as a path rule. Put top-level settings at the top.
[permissions.project.filesystem.":workspace_roots"]
"**/.env*" = "deny"
approval_policy = "never" # read as a path rule, not a setting
# Error loading config.toml: data did not match any variant of
# untagged enum FilesystemPermissionToml in `permissions`Hooks and rules
- Hooks fail open. In our runs, a PreToolUse hook that exited 1, ran past its timeout, printed something that was not JSON, answered
"ask", pointed at a missing file or had not been trusted under/hookslet the command run. Codex hook failed has each case and how to make a hook fail closed. - Rules match command prefixes. A
prefix_rulesays allow, prompt or forbidden for commands that start with given words, and the strictest match wins. They do not see what a script does once it runs, so a forbiddenrmdoes not stoppython cleanup.py. - Project settings need trust. A repository can ship its own
.codex/config.toml, hooks and rules, and none of them load until you trust the project. A team policy that lives only there is skipped on a fresh clone.
Check your own file
Paste your config into the Codex config checker to see unknown keys, secrets written into the file, variables passed to commands and hooks that are switched off. The Codex permissions builder writes a config and rules file from a few choices.
None of these settings can ask a teammate before a deploy, a payment or a database change. That is what VariantGate’s Codex hook does: it sends the command to an inbox and blocks it unless a person approves in time. Setup is in the Codex guide.
Codex changes quickly. These results are from version 0.172.0; run the checks again after an upgrade.
Sources
Give your agents somewhere to ask.
Free for 100 requests a month. Sign in with your email and have a first request waiting a few minutes from now.
Start free