EN

Language

Our editions

Written for each audience, not translated word for word.

How to keep API keys out of coding assistants: a four-layer checklist

How-toBy Lucas Matías García TorresUpdated Data checked 5 min read

Reviewed on . How we work.

Leer en español

A dark mechanical keyboard with three edge-lit glass tiles floating above it

Short answer

To keep API keys out of coding assistants, do not rely on ignore files alone: Cursor, GitHub Copilot and Claude Code all document gaps, such as the agent's terminal, Edit and Agent modes or scripts that open files themselves. Keep secrets out of the project, add exclusion rules for each assistant, start agents from a clean shell with sandboxing where it exists, and turn on GitHub push protection and pre-commit scanning. If a key leaks, rotate it before anything else.

Prices and limits change often: check the vendor's page before you pay. How we make money.

To keep API keys out of coding assistants you need more than an ignore file. Assistants read your project to help, and agents also run commands, so a key in a .env file, a config file or your shell environment can end up in a prompt sent to the provider, in a log, or in a commit an agent pushes. Every vendor we checked says its exclusion feature has gaps. This checklist works in four layers: keep secrets out of the project, tell each assistant what not to read, limit what commands can see, and catch leaks before they reach GitHub. We checked the documentation of Claude Code, Cursor, GitHub Copilot and Gemini CLI, GitHub's push protection and OWASP's guidance on October 28, 2026.

Why assistants see your keys

A coding assistant reads the files you open, the files you mention and the results of its own searches. An agent goes further: it runs commands such as cat or printenv, and their output becomes part of the conversation. It can also commit and push. Each vendor's exclusion feature covers only some of those paths:

  • Cursor says complete protection "isn't guaranteed due to LLM unpredictability", and that the terminal and MCP tools its agent uses cannot block access to ignored files.
  • GitHub Copilot's content exclusion is not supported in the Edit and Agent modes of Copilot Chat in VS Code and other editors.
  • Claude Code's deny rules do not cover scripts that open files by themselves.

So the safest key is one that is not sitting in the project at all.

Step 1: keep secrets out of the project

  • Load keys at runtime from a secrets manager or your operating system's keychain, not from a file in the repository. OWASP's Secrets Management Cheat Sheet warns that environment variables "are generally accessible to all processes and may be included in logs or system dumps", and does not recommend them unless the other methods are not possible.
  • If you must use a .env file, list it in .gitignore, commit a .env.example with fake values instead, and keep production keys off the machine where the agent runs.
  • Give development keys the least privilege they need, as OWASP recommends: separate keys per project, the narrowest permissions the provider offers and a spending limit where there is one.

Step 2: tell each assistant what not to read

Table: how to exclude files in four coding assistants, and what each method does not cover (vendor documentation, checked October 28, 2026)

Assistant How to exclude files What it does not cover
Claude Code Read deny rules in settings.json, such as Read(./.env), Read(./.env.*) or Read(./secrets/**); a .claudeignore file has no effect Commands that read files without naming them, such as grep -r, and scripts that open files themselves; the sandbox enforces access at operating system level
Cursor A .cursorignore file in gitignore syntax, plus a global ignore list in user settings; files in .gitignore are ignored by default The terminal and MCP tools used by the agent; Cursor says protection is not guaranteed
GitHub Copilot Content exclusion set by repository administrators or organization owners, on Copilot Business and Enterprise only Edit and Agent modes of Copilot Chat in VS Code and other editors, chat in Xcode and Eclipse, third-party MCP servers, symlinks and indirect information from the editor
Gemini CLI A .geminiignore file following .gitignore conventions; restart the session after changing it Tools that do not support the file and other services such as Git

In Claude Code, the deny rules go under permissions in your settings file:

{
  "permissions": {
    "deny": ["Read(./.env)", "Read(./.env.*)", "Read(./secrets/**)"]
  }
}

Anthropic's documentation says Read deny rules apply to Claude's built-in file tools, to file commands it recognises in the shell, such as cat, head, tail, sed and tee, and to the targets of redirections. For the same files in Cursor and Gemini CLI, add these lines to .cursorignore or .geminiignore, and to Cursor's global ignore list if you want them excluded in every project:

.env
.env.*
secrets/
*.pem
*.key

If you use Claude Code or Cursor daily, our guides to how to use Claude Code and Claude Code vs Cursor cover the settings files these rules live in.

Step 3: limit what commands can see

  • Start the assistant from a clean shell. If your terminal exports production keys, every command the agent runs inherits them.
  • Turn on sandboxing where it exists. Claude Code's sandbox applies operating system restrictions to the filesystem and network access of shell commands and their child processes, which also stops the scripts that deny rules miss.
  • Keep approvals on for commands that print configuration, such as env, printenv or cat on a config file, and read what a command does before you approve it.
  • Write the rule down for the agent too. A line such as "never read or print .env files" in your AGENTS.md helps, as our guide to AGENTS.md explains, but an instruction is not enforcement, so keep the technical layers.

Step 4: catch leaks before they reach GitHub

  • Check GitHub's push protection for users. GitHub says it is on by default for your account on GitHub.com and stops you from pushing secrets to public repositories.
  • Turn on push protection for your repositories. It needs GitHub Secret Protection, is off by default and blocks pushes containing secrets before they reach the repository. Anyone with write access can bypass a block by giving a reason, and the bypass creates an alert.
  • Scan before you commit. OWASP recommends "secrets detection at the developer level" before a commit or pull request, for example with a pre-commit hook.
  • Review what the agent commits. Read the diff of every agent commit before you push; our guide to code review tools compares tools that help with that review.

If a key leaks anyway

  1. Rotate it first. OWASP's incident response advice is that a new secret must be quickly created and put into use; revoke the old one at the provider.
  2. Check usage and billing for calls you did not make.
  3. Then clean up. Removing the key from the latest commit does not remove it from history or from copies others already made, which is why rotation comes first.

Keys can also leak on purpose: a malicious file or web page can tell an agent to read and send them, a risk explained in our guide to prompt injection.

How we checked

On October 28, 2026 we read Claude Code's settings and permissions documentation, Cursor's ignore file documentation, GitHub's documentation on Copilot content exclusion and on push protection, Gemini CLI's page on ignoring files and OWASP's Secrets Management Cheat Sheet. We did not test how each tool behaves with real keys; the limits in the table are the ones the vendors document.

What we checked

  • Claude Code's settings documentation shows a permissions example with the deny rules Read(./.env) and Read(./.env.*) to stop Claude Code reading .env files. (Anthropic (Claude Code docs), )
  • Read and Edit deny rules apply to Claude's built-in file tools, to file commands Claude Code recognises in Bash such as cat, head, tail, sed and tee, and to redirection targets; they do not apply to commands that read files without naming them, such as grep -r, or to subprocesses such as Python or Node scripts that open files themselves; a .claudeignore file has no effect; sandboxing gives operating system level enforcement for shell commands and their child processes. (Anthropic (Claude Code docs), )
  • Cursor blocks files listed in .cursorignore from code accessible by Agent and Tab and from @ mention references; the terminal and MCP server tools used by Agent cannot block access; complete protection isn't guaranteed due to LLM unpredictability; a global ignore list in user settings is empty by default; files in .gitignore are ignored automatically. (Cursor, )
  • Copilot content exclusion is available to organizations with Copilot Business or Copilot Enterprise and is set by repository administrators, organization owners and enterprise owners; in VS Code it is supported for chat but not for Edit and Agent modes, and chat in Xcode and Eclipse is not supported; it is not guaranteed to be respected by third-party MCP servers, Copilot may use semantic information provided indirectly by the IDE, and it does not apply to symlinks or remote filesystems. (GitHub Docs, )
  • Paths in .geminiignore are excluded by the Gemini CLI tools that support the feature while other services such as Git still see them; the file follows .gitignore conventions and changes apply after restarting the session. (Google (Gemini CLI docs), )
  • Push protection blocks pushes containing secrets before they reach the repository; push protection for repositories requires GitHub Secret Protection and is disabled by default, and anyone with write access can bypass a block by giving a reason, which creates an alert; push protection for users is enabled by default on GitHub.com and stops you pushing secrets to public repositories. (GitHub Docs, )
  • OWASP's Secrets Management Cheat Sheet recommends least privilege, secrets detection at the developer level before a commit or pull request and quick rotation after a leak, and warns that environment variables are generally accessible to all processes and may be included in logs or system dumps, so it does not recommend them unless other methods are not possible. (OWASP Cheat Sheet Series, )

What may change

  • Coding assistants change how they handle ignored files with new releases, and GitHub's content exclusion is in public preview on some surfaces. This page is reviewed every quarter.

Frequently asked questions

Does .cursorignore stop Cursor from reading my .env file?

Partly. Cursor blocks files in .cursorignore from the agent, Tab and @ mentions, and ignores files in .gitignore by default, but its documentation says the agent's terminal and MCP tools cannot block access and that complete protection is not guaranteed.

How do I stop Claude Code reading .env files?

Add Read deny rules such as Read(./.env) and Read(./.env.*) under permissions in settings.json. They cover Claude's file tools and shell commands like cat, but not scripts that open files themselves, so enable the sandbox for operating system level enforcement.

What is GitHub push protection?

It is a secret scanning feature that blocks pushes containing secrets before they reach the repository. For your account it is on by default for public repositories on GitHub.com; for repositories it needs GitHub Secret Protection and is off by default.

Sources

  1. Claude Code settings: settings files and a permissions example that stops Claude Code reading .env files, Anthropic (Claude Code docs). Accessed October 28, 2026.
  2. Configure permissions (Claude Code): Read and Edit deny rules, what they cover, .claudeignore and sandboxing, Anthropic (Claude Code docs). Accessed October 28, 2026.
  3. Ignore file (Cursor documentation): .cursorignore, global ignore list, default ignores and limits, Cursor. Accessed October 28, 2026.
  4. Content exclusion for GitHub Copilot: availability by tool, limitations and data sent to GitHub, GitHub Docs. Accessed October 28, 2026.
  5. Ignoring files (Gemini CLI documentation): the .geminiignore file, Google (Gemini CLI docs). Accessed October 28, 2026.
  6. Push protection (GitHub Docs): push protection for repositories and for users, supported secrets and bypasses, GitHub Docs. Accessed October 28, 2026.
  7. Secrets Management Cheat Sheet, OWASP Cheat Sheet Series. Accessed October 28, 2026.

Spotted an error or an outdated price? Tell us and we will fix it.

Change history

  • : First published. Checked against the documentation of Claude Code, Cursor, GitHub Copilot, Gemini CLI and GitHub push protection, and OWASP's Secrets Management Cheat Sheet, on October 28, 2026.

Next review: .

Part of our Coding Assistants guide.

Other topics