Can AI coding agents read your .env file?

Yes. Claude Code, Cursor and Codex can usually read an ignored .env file. Learn where credentials leak and how to keep real secrets outside the workspace.

  • AI coding agents
  • .env
A small robot shines a light on a .env file full of hidden values, next to the words Can your agent read this?
A small robot shines a light on a .env file full of hidden values, next to the words Can your agent read this?

Yes. Claude Code, Cursor, Codex and other AI coding agents can usually read the .env files in any project they can inspect.

Adding .env to .gitignore prevents a normal Git commit. It does not hide the file from software running on your laptop, including a coding agent.

Keeping credentials out of Git still matters, but it no longer covers every way a secret can leave your machine.

Why .gitignore does not protect a secret

Suppose a project contains this file:

DATABASE_URL=postgresql://admin:password@production.example/app
PAYMENT_API_KEY=live_value_here

An entry in .gitignore stops git add . from staging the file. The file remains beside the source code. An agent asked to diagnose a database error may inspect it, run a command that loads it, or add temporary logging around the environment. A .claudeignore file doesn’t change that in Claude Code. Our .claudeignore test shows which settings do.

Agents are useful because they have broad access, and that access is the risk. The OWASP Secure Coding with AI Cheat Sheet describes agents that edit files, execute commands, install packages, run tests, and access network services. A workspace available to that agent is part of its trust boundary.

The agent does not need to open .env directly to expose a value. Loaders such as dotenv put file values into the application environment. It can run a program that prints the environment, include a credential in an error report, or send a request to the wrong service after editing a test.

A sandbox doesn’t close this gap by default. Codex’s sandbox let a sandboxed command read a sample .env in every mode we ran.

Where credentials can leave the workspace

A secret may never reach a Git commit and still appear in:

  • terminal output or shell history
  • generated patches and debugging transcripts
  • logs, crash reports, and test snapshots
  • saved chats and support conversations
  • MCP server configuration
  • commands sent to another tool or network service

GitGuardian’s State of Secrets Sprawl 2026 report found that Claude Code co-authored commits leaked secrets at about twice the baseline rate across public GitHub commits. The report also says roughly 28% of incidents in its dataset originated entirely outside code repositories, in collaboration and productivity tools.

A credential can travel through every surface an agent can read, run, or report back through, and repository scanning covers only part of that path. A TruffleHog scan of your Git history, for example, never reads an ignored .env file.

.env file security: check what your coding agent can access

Before starting an agent, inspect the places where working credentials collect:

  • .env, .env.local, and framework-specific environment files
  • JSON, YAML, or TOML configuration used by local tools
  • test fixtures copied from production data
  • cloud CLI profiles and user-level credential files
  • parent directories that fall inside the agent’s filesystem scope
  • logs and saved terminal output

Review the permissions you granted, including shell and network access. A rule that tells an agent not to read a file is weaker than an operating-system or sandbox boundary that prevents the read.

Use fake values for unit tests. If a real integration is unavoidable, use a narrowly scoped development credential that cannot charge customers, delete production data, or administer a cloud account.

For a practical workspace setup, read How to keep .env secrets away from AI coding agents.

Where the Dopbase workflow fits

Dopbase stores secrets by project and environment, outside the application repository. You import the existing .env once and delete the file. The workspace can keep an .env.example containing names and harmless examples:

DATABASE_URL=
PAYMENT_API_KEY=

When a reviewed command needs development values, the client fetches an explicit environment and passes those values to the child process:

dopbase run payment-service/development -- npm test

This design removes the need for a plaintext .env copy beside the code. It also makes the target environment visible in the command.

The agent can still check configuration. A Names only token lets it list projects, environments, secret names and versions, and Dopbase refuses --reveal, run and secret set for that token.

If you launch the coding agent itself with secret values, the agent receives them. If it can edit and execute the program that receives them, it may be able to make that program reveal them. A secrets manager controls storage and delivery, and has no say over what the receiving code does with a value.

Use a smaller trust boundary

Keep routine agent work on fake data. Review changes before running credentialed integration tests, then execute those tests yourself or through a controlled CI job with an approval step.

That way, opening a repository no longer means opening a file full of working credentials.