← Blog

How to stop AI coding agents from leaking secrets

Why AI coding agents can expose API keys and .env files, what Claude Code, Copilot and Cursor settings really block, and a checklist to keep secrets safe.

Illustration for How to stop AI coding agents from leaking secrets

AI coding agents read your files, run your commands and write commits. That's what makes them useful. It's also why a secret that was "safe enough" on a developer's laptop can end up in a prompt, a log or a pushed branch without anyone noticing.

From my perspective, the question of how to stop AI coding agents from leaking secrets isn't talked about clearly enough. Teams either assume an ignore file handles it, or they ban agents from anything sensitive and lose most of the value. Neither is the answer.

The short answer: keep secrets out of the agent's reach, not just out of its instructions. Deny reads of secret files in the tool's settings, run agents in a sandbox or container with no production credentials, and put secret scanning on every push as the safety net. Today, we'll walk through where secrets leak, what each tool's settings actually block, and a checklist you can adopt this week.

Where Secrets Actually Leak

Most leaks don't look like an attack. They look like an agent being helpful. A few common paths:

  • Reading a secret file into context. The agent opens .env to "understand the configuration," and the key is now part of the conversation sent to the model.
  • Printing the environment. A debugging step runs env or printenv, or echoes a variable, and the output lands in the session.
  • Hardcoding to make it work. The agent can't find a variable, so it pastes the value into a config file or a test fixture to get the build green.
  • Committing the wrong file. A generated .env.local or a credentials JSON gets staged and pushed with everything else.
  • Following someone else's instructions. A README, issue or web page the agent reads contains text written to make it send data somewhere. That's prompt injection, and secrets are exactly what it's after.

The OWASP Top 10 for LLM Applications lists sensitive information disclosure as LLM02:2025, and one of its core recommendations applies directly here: enforce strict access controls, so a process only gets the data it needs.

An agent can't leak a secret it was never able to read.

Know What "Ignore" Really Means

Here's the truth: ignore files and exclusion settings help, but none of the major tools promise they're a hard boundary. Each vendor documents the gaps, and your team should know them before relying on them.

Claude Code

Claude Code uses permission rules, not an ignore file. According to Anthropic's permissions docs, a rule like Read(./.env) denies reading that file, and the docs say plainly that a .claudeignore file has no effect. The same page is direct about the limits: Read deny rules don't apply to a command that reads files without naming them, such as grep -r pattern ., or to a Python or Node script that opens files itself. For a restriction that has to hold, the docs point you to the sandbox. I walked through setting both up in Claude Code permissions and sandboxing.

GitHub Copilot

Copilot has content exclusion, available on Copilot Business and Copilot Enterprise. Repository admins and organization owners can exclude paths, and excluded files won't inform Copilot's suggestions or responses. GitHub made exclusions generally available in the Copilot app and CLI in September 2026. Read the limitations section, though: the docs note that exclusion isn't currently supported in the Edit and Agent modes of Copilot Chat in editors, and that it doesn't apply to symbolic links.

Cursor

Cursor uses a .cursorignore file with .gitignore syntax to keep files away from Agent, Tab and @ mentions. The docs say complete protection isn't guaranteed due to LLM unpredictability, and that the terminal and MCP server tools used by Agent can't block access to ignored files.

Ignore files tell the agent where not to look. They don't stop a shell command from looking for it.

Take Secrets Out of the Workspace

Since no setting is a wall on its own, the most reliable fix is boring: don't keep real secrets where agents work.

  • Use a secrets manager or a local vault, and load values at runtime rather than storing them in files in the repository folder.
  • Give agents development credentials only. Scoped, short-lived, with no access to production data. If one leaks, rotating it is a non-event.
  • Keep production credentials off machines where agents run, sandbox or not. This is the single rule I'd never bend.
  • Commit a .env.example with placeholder values, so the agent has something legitimate to read when it wants to know which variables exist.
  • Watch the environment. Anything exported in the shell the agent uses is visible to every command it runs. Start agents from a clean shell rather than one where you've just exported a cloud admin key.

If you're not sure where your team stands on this and the rest of your AI rollout, the free readiness assessment scores five areas in about three minutes.

Add a Safety Net at Push Time

Prevention fails sometimes. So the last line of defense sits where every change has to pass: the push.

GitHub's push protection blocks pushes that contain recognized secrets before they reach the repository. Push protection for your user account is on by default and stops you pushing secrets to public repositories. For your own repositories, push protection has to be enabled, and for private repositories it requires GitHub Secret Protection. Pair it with a pre-commit secret scanner so problems are caught on the laptop, before a push is even attempted.

The same rule applies whether a person or an agent made the commit. That's the point: it doesn't depend on the agent behaving.

A Checklist to Stop AI Coding Agents Leaking Secrets

Here's a version you can paste into your team's wiki:

Secrets and AI coding agents: team baseline

Settings
- [ ] Claude Code: Read deny rules for .env, .env.*, secrets/** in .claude/settings.json
- [ ] Claude Code: sandbox enabled, with ~/.ssh and ~/.aws in filesystem.denyRead
- [ ] Copilot: content exclusion set for secret paths (Business/Enterprise)
- [ ] Cursor: .cursorignore covers secret paths
- [ ] Nobody relies on ignore files alone (terminal and scripts can still read)

Workspace
- [ ] No real secrets stored in the repository folder
- [ ] Agents use scoped, short-lived development credentials only
- [ ] No production credentials on machines where agents run
- [ ] .env.example with placeholders is committed

Safety net
- [ ] Push protection enabled on every repository
- [ ] Pre-commit secret scanning installed for every engineer
- [ ] Reviewers check for hardcoded values in config, tests and fixtures

Response
- [ ] Any secret seen by an agent or pushed is rotated immediately
- [ ] Incidents reported to a named owner

Plan for the Leak Anyway

Even with all of this in place, assume one will slip through eventually. Make the response automatic: if a secret was read into an agent session, printed in a log or pushed anywhere, rotate it first and investigate second. Don't spend a day working out whether it "really" left the machine.

Write that rule into your AI usage policy so nobody has to make the call under pressure. I covered what else belongs in that policy in AI usage policy for software teams.

Rotating a key takes minutes. Explaining a breach takes months.

Conclusion

Keeping secrets safe with AI coding agents comes down to layers. Deny reads in each tool's settings, keep real credentials out of the workspace, run agents sandboxed and away from production, and catch whatever slips through at push time.

So, whether you're the first engineer on your team trying an agent or the lead rolling one out to everyone, start with the two highest-value steps: no production credentials where agents run, and push protection on every repository.

I hope you found this helpful. If you'd like these guardrails as settings you can drop in rather than write from scratch, there's a ready-to-edit set of AI agent guardrails in the AI-Assisted Development Kit.

Keep the secrets out of reach, and the agent can be as helpful as it likes.