PTS security notes / Guides

How to stop your AI coding agent reading .env and secrets

By PTS · Published 2026-12-03 · Updated 2026-12-03 · Docs checked December 3, 2026

The usual leak is boring. A .env file sits in the project, a local settings file has an API token, or an agent runs a shell command that can read whatever your user can read. You do not need a new product to shrink that. You need the controls your tools already ship, used on purpose, with a clear view of their limits.

This is a practical pass for Claude Code, Codex and Cursor. Setting names were checked against official docs on December 3, 2026. Check your version before you copy anything into production, and test on a non-production project first.

Start with git, so secrets never leave the laptop

Apiiro found more than 100,000 Claude Code .claude/settings.local.json files in public GitHub repos, with about 1,100 holding secrets, as reported by Ars Technica on Nov 9, 2026. Claude Code writes some "don't ask again" approvals to that local file (permissions).

Add this to .gitignore today:

text
.claude/settings.local.json
.env
.env.*
!.env.example

That stops a commit. It does not stop an agent that is already running in the folder. The next layers are deny rules, ignore files and a sandbox.

Claude Code: deny the files, then turn the sandbox on

In .claude/settings.json (or your user settings in ~/.claude/settings.json), you can deny reads of secret files and some dangerous commands. Anthropic's settings reference shows this pattern (settings reference):

json
{
  "permissions": {
    "deny": [
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(./secrets/**)",
      "Read(./config/credentials.json)",
      "Bash(curl *)"
    ]
  }
}

Two limits matter, and they come from the same docs:

For OS-level enforcement, enable the sandbox (sandboxing):

json
{
  "sandbox": {
    "enabled": true,
    "allowUnsandboxedCommands": false,
    "filesystem": {
      "denyRead": ["./.env", "./.env.*", "~/.ssh", "~/.aws/credentials"]
    }
  }
}

The sandbox is off by default. When it is on, writes are limited, but reads still include most of the machine, including ~/.ssh and ~/.aws/credentials, unless you add filesystem.denyRead. Network starts closed and goes through a proxy. Environment variables are inherited, including secrets already in the process environment.

The sandbox wraps shell commands only. Built-in file tools (Read, Edit, Write, WebFetch), MCP servers, hooks and several helper processes run outside it. Use deny rules for the file tools and the sandbox for the shell, together.

You can also set permissions.disableBypassPermissionsMode to "disable" so no one can enter bypass-permissions mode. With that key set, --dangerously-skip-permissions is ignored (settings reference).

A starter file that combines deny, ask, sandbox and bypass-disabled is in the open-source starter repo.

Codex: sandbox first, network off unless you need it

User defaults live in ~/.codex/config.toml. A cautious local setup is a workspace-write sandbox, approval on request, and no network inside that sandbox (config basics, sandboxing):

toml
sandbox_mode = "workspace-write"
approval_policy = "on-request"

[sandbox_workspace_write]
network_access = false

workspace-write lets the agent edit inside the workspace and run routine local commands inside that boundary. on-request asks when the agent needs to go beyond it. danger-full-access removes the filesystem and network boundaries and should stay off on any machine that holds real credentials.

If an older file still has approval_policy = "untrusted", remove it. Codex and ChatGPT Work no longer support that value, and it can prevent the client from starting (agent approvals and security).

Cursor: ignore files help, and they are not a wall

Create a .cursorignore at the project root, using .gitignore syntax. Cursor uses it to block Agent, Tab and @ mention access to those paths (ignore file).

text
**/.env
**/.env.*
**/credentials.json
**/secrets.json
**/*.pem
**/*.key

Cursor already ignores .env* files by default, along with .gitignore entries. A .cursorignore is still worth adding for credentials, keys and secret folders that are not covered, and for a rule you can see in the repo.

The same docs, and Cursor's agent security page, are clear about the limit: the terminal and MCP tools are not blocked by .cursorignore. Treat ignore files as a context filter, not a security boundary. Keep approval prompts on for terminal commands, and be careful with run modes, which Cursor describes as best-effort guardrails rather than a hard boundary.

A short order of operations

  1. Add the .gitignore block, and check that .env and .claude/settings.local.json are not already in git history.
  2. Put deny rules and a starter sandbox in your Claude Code settings, or the Codex config above, or a .cursorignore if that is your tool.
  3. Run the 15-minute inventory in the same terminal you use for the agent. Confirm the accounts and env names you expect, and only those.
  4. Try a harmless prompt that asks the agent to read .env. You want a denial, a prompt, or a sandbox error, not the file contents.
  5. Rerun when you add an MCP server, a cloud login or a new agent. The permissions audit is a 10-minute companion.

Skip-all-prompts modes, including bypass-permissions or a full-access sandbox with approvals off, belong only inside a throwaway container or VM that holds no production credentials.

Want the checklist and starter files?

The free Agent Blast Radius Checklist is a 9-page worksheet plus these starter files. It lives on Gumroad. This site does not collect email.

This article was drafted with an AI assistant (Grox) and reviewed and published under the PTS name. It is educational, not legal advice. PTS is not affiliated with Anthropic, OpenAI or Cursor.

Get the free checklist

A 9-page worksheet plus starter files for Claude Code and Codex. Download it on Gumroad. This site does not collect email.

Get the free checklist

Keep reading