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

What Codex CLI 0.172 allowed in its read-only and workspace-write sandbox modes, tested on macOS, 25 November 2026
Optionsandbox_mode = "read-only""workspace-write"
cat .envsandbox_mode = "read-only"Printed the key"workspace-write"Printed the key
cat ~/.ssh/configsandbox_mode = "read-only"Read it"workspace-write"Read it
Write a file in the projectsandbox_mode = "read-only"Operation not permitted"workspace-write"Allowed
Write a file in /tmpsandbox_mode = "read-only"Operation not permitted"workspace-write"Allowed
Write a file in the home foldersandbox_mode = "read-only"Operation not permitted"workspace-write"Operation not permitted
curl https://example.comsandbox_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 = false

extends = ":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.

What Codex CLI 0.172 did with a permission profile that denies .env files, tested on macOS, 25 November 2026
OptionWith the profile
cat .envWith the profileOperation not permitted
cat sub/.env.localWith 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 .envWith the profileOperation not permitted
echo hi > .env.newWith the profileOperation not permitted
Write any other file in the projectWith the profileAllowed
git show HEAD:.env, with .env committedWith the profilePrinted the key
cat ~/.ssh/configWith 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 .env committed, git show HEAD:.env printed 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_roots patterns only cover the project. ~/.ssh/config stayed 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_SECRET and GH_TOKEN, and a command Codex ran listed all three. With ignore_default_excludes = false under [shell_environment_policy], none of them reached it. A key you loaded from .env into 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_policy other than never, 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 permitted

Run 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

  1. OpenAI Codex: Permissions (profiles, filesystem deny, deny patterns matched at start), read 25 November 2026
  2. OpenAI Codex: Configuration reference (default_permissions, permissions, shell_environment_policy), read 25 November 2026

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