PTS security notes / Questions

Can AI coding agents read my .env file and SSH keys?

By PTS · Published December 5, 2026 · Docs checked December 5, 2026

Short answer

Yes, by default. In Claude Code, file reads inside your project need no approval, and even with its sandbox on, commands can read most of the machine, including ~/.ssh, unless you deny it. Codex and Cursor each have controls for this, and each leaves a gap you should know about.

What each tool can read by default

Anything an agent reads becomes part of what the model sees. A secret in a file it opens is a secret in the conversation.

Block it in Claude Code

Deny the files you know about. Then turn on the sandbox for the shell, and deny the credential folders there too. Put this in your project's .claude/settings.json. The ./ paths in the sandbox block resolve from the project root in that file, but from ~/.claude in your user settings:

json
{
  "permissions": {
    "deny": [
      "Read(./.env)",
      "Read(./.env.*)",
      "Read(./secrets/**)",
      "Read(~/.ssh/**)",
      "Read(~/.aws/**)"
    ]
  },
  "sandbox": {
    "enabled": true,
    "allowUnsandboxedCommands": false,
    "filesystem": {
      "denyRead": ["./.env", "./.env.*", "~/.ssh", "~/.aws/credentials"]
    }
  }
}

Two limits from Anthropic's docs:

Block it in Codex

Codex permission profiles can deny reads with exact paths or globs. This profile extends the built-in workspace profile and denies .env files and your SSH folder (permissions):

toml
default_permissions = "project-edit"

[permissions.project-edit]
extends = ":workspace"

[permissions.project-edit.filesystem]
glob_scan_max_depth = 3
"~/.ssh" = "deny"

[permissions.project-edit.filesystem.":workspace_roots"]
"**/*.env" = "deny"

One caveat from the docs: on Linux, WSL and native Windows, glob matches are enumerated when each sandboxed process starts. A long-running shell, script or dev server can still read a matching .env created after it started. Start a fresh process after you add secrets files.

Block it in Cursor

Add a .cursorignore at the project root, and add global ignore patterns in your user settings so you do not repeat it per project (ignore file):

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

Then set Read access to Workspace, so reads outside the project ask first, and keep the sandbox on for shell commands.

Test it in two minutes

  1. Put a fake value in a new .env, such as FAKE_KEY=not-a-real-secret. Never use a real one for a test.
  2. Ask the agent to print the contents of .env.
  3. You want a denial, a prompt or a sandbox error, not the value.
  4. Repeat with cat ~/.ssh/config, and with a command that reads without naming the file, like grep -r FAKE_KEY .. That last one is where deny rules alone fail.

If a real secret was exposed

Rotate it. Revoking and reissuing the key is the only fix that works after the fact. Then check that .env and .claude/settings.local.json are in .gitignore and not in your git history. The free Claude Code settings scanner checks pasted settings for saved secrets, wide allow rules and missing guardrails in your browser, and the command-line version never prints the secrets it finds. The full walkthrough covers that and the per-tool settings in more depth, and the 15-minute inventory shows what your agent can reach right now.

Quick follow-ups

Does a deny rule fully stop Claude Code reading .env?

No. Anthropic's docs say read deny rules are not a complete block. They cover built-in file tools and some Bash commands, but not a script that opens the file itself. Add the sandbox and keep secrets out of reach.

Do I need to worry about ~/.ssh if I only work in one project?

Yes. Anthropic's docs say the sandbox's default read policy includes credential files like ~/.ssh and ~/.aws/credentials. Add filesystem.denyRead entries, or run the agent in a container that never mounts them.

Is .cursorignore enough?

It helps, but Cursor's docs say the terminal and MCP tools cannot be blocked by it. Pair it with the sandbox, Read access set to Workspace, and approval prompts for commands.

Sources

See also the glossary and the other questions.

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

Wallet side of things? Get Don't Get Drained, also free. Or get both in one checkout.

Related guides

More questions