How to stop Codex reading .env files
Codex's read-only and workspace-write sandboxes both let it read .env. A permission profile with a deny entry stops it, tested on Codex CLI 0.172, plus the gaps that remain: git history, your home folder and environment variables.
· 6 min read
Codex’s sandbox does not stop it reading your .env. In Codex CLI 0.172, both read-only and workspace-write limit where Codex may write and keep it off the network, but a cat .env inside either one printed the key, and so did cat ~/.ssh/config. What stops it is a permission profile with a deny entry for the files. With one in place, every read we tried failed with Operation not permitted, and Codex told the model the files were off limits, so it declined to try at all.
We ran each command through codex sandbox on macOS, in a throwaway project with a made-up key, on 25 November 2026. The environment-variable check ran through codex exec; where a point comes from OpenAI’s documentation instead, it says so.
What the two sandbox modes allow
| Option | sandbox_mode = "read-only" | "workspace-write" |
|---|---|---|
| cat .env | sandbox_mode = "read-only"Printed the key | "workspace-write"Printed the key |
| cat ~/.ssh/config | sandbox_mode = "read-only"Read it | "workspace-write"Read it |
| Write a file in the project | sandbox_mode = "read-only"Operation not permitted | "workspace-write"Allowed |
| Write a file in /tmp | sandbox_mode = "read-only"Operation not permitted | "workspace-write"Allowed |
| Write a file in the home folder | sandbox_mode = "read-only"Operation not permitted | "workspace-write"Operation not permitted |
| curl https://example.com | sandbox_mode = "read-only"No network | "workspace-write"No network |
The sandbox is about writes and the network. Reading is open: anything your user account can read, Codex can read too.
The setting that stops it: a permission profile
Codex has named permission profiles. You pick one with default_permissions, build it on a built-in one with extends, and list paths under filesystem with "read", "write" or "deny". Keys under the special ":workspace_roots" table are patterns inside the project; keys directly under filesystem are paths elsewhere. This is the file we tested, with the two extra settings from the gaps below:
# ~/.codex/config.toml
# Top-level keys first: a key written below a [table] header
# belongs to that table.
approval_policy = "never"
default_permissions = "no-env"
[permissions.no-env]
extends = ":workspace"
# Paths outside the project: absolute, or starting with ~
[permissions.no-env.filesystem]
"~/.ssh" = "deny"
# Patterns inside the project you are working in
[permissions.no-env.filesystem.":workspace_roots"]
"**/.env*" = "deny"
"**/*.env" = "deny"
# Keep variables named like KEY, SECRET or TOKEN out of commands
[shell_environment_policy]
ignore_default_excludes = falseextends = ":workspace" keeps what workspace-write gave: writes in the project and the temp folder, nothing elsewhere. Use either default_permissions with [permissions] or sandbox_mode, not both, as the permissions documentation says.
| Option | With the profile |
|---|---|
| cat .env | With the profileOperation not permitted |
| cat sub/.env.local | With the profileOperation not permitted |
| cat config.env (with **/*.env too) | With the profileOperation not permitted |
| grep -r API_KEY . | With the profileOperation not permitted for each file |
| cat a symlink that points at .env | With the profileOperation not permitted |
| echo hi > .env.new | With the profileOperation not permitted |
| Write any other file in the project | With the profileAllowed |
| git show HEAD:.env, with .env committed | With the profilePrinted the key |
| cat ~/.ssh/config | With the profileAllowed, until "~/.ssh" = "deny" was added |
The deny covers writes as well as reads, so Codex also cannot create or overwrite a matching file. In a real codex exec run, asked to print .env and to try several ways, the model answered that the active filesystem policy blocks reads of .env and made no attempt.
What a deny entry does not cover
- Git history. With
.envcommitted,git show HEAD:.envprinted the key under the same profile. Git reads its own object files, not the path you denied. If a secret was ever committed, rotate it. - Your home folder. The
:workspace_rootspatterns only cover the project.~/.ssh/configstayed readable until we added"~/.ssh" = "deny"under[permissions.no-env.filesystem]; the~was understood. Add the folders that hold your own keys and cloud credentials the same way. - Environment variables. By default, variables named like KEY, SECRET or TOKEN reach every command Codex runs. We set
FOO_API_KEY,MY_SECRETandGH_TOKEN, and a command Codex ran listed all three. Withignore_default_excludes = falseunder[shell_environment_policy], none of them reached it. A key you loaded from.envinto your shell is still a key. - Files created later. The documentation says that where Codex expands deny patterns before the sandbox starts, a matching file created afterwards is not added to the list.
- A yes from you. With an
approval_policyother thannever, the model can ask to run a command outside the sandbox. If you approve it, that command can read anything your account can. Read the command before you say yes.
A TOML mistake that breaks the whole file
In TOML, a key written below a table header belongs to that table. We first added approval_policy at the end of the file, under the filesystem table, and Codex refused to start:
Error loading config.toml: data did not match any variant of
untagged enum FilesystemPermissionToml
in `permissions`Put top-level keys such as approval_policy and default_permissions above the first [table].
Check it on your own machine
codex sandbox runs one command under the sandbox settings in your config.toml, without a model. Run it in a project that has a .env:
cd your-project
codex sandbox -- cat .env
# cat: .env: Operation not permittedRun it again after you upgrade Codex or change the file.
We tested Codex CLI 0.172.0 on macOS. Linux and Windows use different sandboxes, so check there with the same commands before relying on it.
More on Codex settings
The Codex config.toml settings covers the rest of the file: approvals, network, rules and requirements.toml. The Codex config checker reads your config.toml in the browser and points out settings like these. Codex hook failed shows what happens when a PreToolUse hook breaks, and how to stop Claude Code reading .env files covers the same test in Claude Code. A deny list stops reads, but it cannot ask anyone: VariantGate’s Codex hook sends risky commands to a person to approve first, as the Codex guide describes.
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