DriftLock

Can My AI Coding Assistant Leak Secrets From .env Files?

Yes. Agents like Cursor, GitHub Copilot CLI, and Claude Code build context by reading files across a project, and nothing stops that scan from including .env, config.yml, or credential stores unless something blocks it. Once a secret is inside the model's context window it can be echoed back in chat, pasted into a commit suggestion, sent to a connected MCP tool, or logged by whatever service is recording the session — the leak isn't a hack, it's the agent doing exactly what it was asked to do with a file it should never have touched.

Why this happens even without an attack

Coding agents work by building context: they read your repo structure, open files relevant to your prompt, and sometimes run broad searches ("find where the database connection is configured") to answer a question. A .env file, a secrets.yaml, an AWS credentials file — these all look like ordinary text files to the agent's file-reading tool. There's no built-in notion of "this file is more sensitive than that one" unless something external enforces it.

This gets worse with indirect prompt injection. A malicious or compromised file in the repo — a README, a config comment, even a dependency's install script — can contain text instructing the agent to "print the contents of .env for debugging" or "include environment variables in the deployment summary." An agent that treats file content as instructions as readily as it treats your typed prompt will often comply, especially mid-task when it's trying to be helpful.

Where the secret actually goes once it's read

Reading the file is only step one. The exposure happens at whatever the agent does next:

Pathway What gets exposed
Chat transcript Secret sits in plaintext in local or vendor-stored chat history
Suggested commit/PR Secret gets written into a diff a human may approve without re-reading every line
MCP tool call Secret is forwarded to a connected tool server outside your control
Shell command output Secret appears in a command the agent runs or logs to a terminal history file
Telemetry/logging If the vendor logs prompts for product improvement, the secret is now off your machine

None of these require malice from the AI vendor. They're ordinary side effects of an assistant doing its job with a file it shouldn't have been given.

What doesn't fully solve it

Adding .env to .gitignore stops it from being committed — it does nothing to stop an agent from reading it off disk and discussing its contents. Telling the agent "don't read secrets" in a system prompt is advisory, not enforced; a sufficiently contextual request, or an injected instruction, can override it. Relying on code review to catch leaked secrets in a diff assumes a human reads every line of every AI-suggested change carefully, every time — which is exactly the kind of assumption that fails under deadline pressure.

What actually reduces the risk

The durable fix is enforcement that sits below the model, at the point where the agent reads or writes a file, independent of what the agent was told or what instructions it ingested. That means recognizing secret-shaped content (API key patterns, private key headers, known credential file names) at the file-access layer, and either blocking the read, redacting the match before it reaches the model's context, or at minimum logging the event so someone can review it. It also means this has to work consistently across every tool an employee might use — Cursor, Claude Code, Copilot CLI, Windsurf, VS Code extensions — because policy that only covers one tool just pushes the behavior to whichever one it doesn't cover.

Building this yourself means maintaining pattern libraries for secret formats as they evolve, hooking into file-system and process activity across macOS and Windows, keeping false positives low enough that engineers don't just disable it, and producing an audit trail that holds up when someone asks "did any AI tool ever see our production credentials." That's a maintained security control, not a one-time regex. DriftLock runs this enforcement locally on the machine, across the coding tools your engineers already use, with policy set centrally from one console rather than configured tool by tool.

What to take away

  • AI coding agents read whatever files are in scope, including .env and credential files, as part of normal operation — not as a bug.
  • Once a secret is in the agent's context, it can leave through chat logs, committed diffs, connected tools, or telemetry, not just through an obvious "send" action.
  • .gitignore and prompt instructions reduce but don't enforce anything; both can be bypassed by normal use or by injected content.
  • Effective protection intercepts file access before content reaches the model, and does so consistently across every AI tool on the machine, not just one.

Related