Guide

How to keep .env files away from AI coding agents

To keep a .env file away from an AI coding agent, use the tool’s own deny or ignore setting, then assume it is only a partial barrier and keep real secrets out of the project folder and the shell environment. The settings differ by tool, and several vendors say plainly what theirs does not cover.

The facts below come from each tool’s documentation, linked on its page under /agents. Behavior changes between versions, so check the linked source for your release.

Why does this matter?

A coding agent works on your project directory, and an env file usually sits in it. Anything the agent reads can be sent to its model provider, written into a transcript, or exposed through prompt injection. Aider’s documentation says it sends the files you add to the chat plus a repository map to the model, so a secret in those files leaves your machine (/agents/aider). Disclosed attacks have used injected text to steer agents toward environment variables, including API keys, as covered in why agents should never see your API keys.

How do I hide .env from Claude Code?

Claude Code restricts file reads with permissions.deny rules in settings.json. A .claudeignore file has no effect.

  1. Open .claude/settings.json in the project, or ~/.claude/settings.json for every project.
  2. Add deny rules under permissions:
{
  "permissions": {
    "deny": ["Read(./.env)", "Read(./.env.*)", "Read(./secrets/**)"]
  }
}
  1. Know the limits. Anthropic describes Read rules as a best-effort application to built-in file tools, and says a Bash deny rule “isn’t a security boundary”. Built-in read-only commands such as cat run without a prompt, so cat .env is a separate path from the file tool. Anthropic recommends pairing deny rules with the sandbox when a restriction must hold. Sources: permissions and settings.

Claude Code’s documentation does not describe automatic .env loading in its environment-variables reference. Variables come from your shell or the env key of a settings file (/agents/claude-code).

How do I hide .env from Cursor?

Cursor uses a .cursorignore file in the project root, in gitignore syntax. It blocks access in Agent, Tab and @ references. Files in .gitignore and a built-in list that includes .env* are ignored unless you re-include them with !.

The limit is stated in Cursor’s own documentation: terminal commands and MCP server tools used by the Agent cannot be blocked by .cursorignore, and complete protection is not guaranteed. Source: ignore files. See /agents/cursor.

How do I hide .env from Aider?

Aider reads .env files on purpose. It loads them from your home directory, the git root and the current directory, with later files taking priority, and you can name another with --env-file. That is where it expects your provider keys. To keep project files out of the repository map and the chat, add a .aiderignore file (gitignore syntax). Sources: dotenv docs and the FAQ. See /agents/aider.

How do I hide .env from Gemini CLI?

Gemini CLI also loads .env. It searches from the current directory upward until it finds one, reaches the project root or the home directory, then falls back to ~/.env. A .geminiignore file in the project root excludes matching files from tools that support it, such as the @ command. Google states only tools that respect the file honor it. By default, host variables matching names such as *TOKEN*, *SECRET*, *KEY* and *PASSWORD* are redacted before an MCP server is started. Sources: gemini-ignore and configuration. See /agents/gemini-cli.

How do I hide .env from Cline?

Cline offers .clineignore, but its docs describe it as a context filter, not an access-control boundary. It filters the initial file listing, automatic context gathering and search results, while explicit @ mentions and shell commands can still read ignored files. For enforcement, Cline’s docs point to a PreToolUse hook script that blocks reads, edits and commands targeting matching paths; hooks are disabled in CLI --yolo mode. Source: clineignore. See /agents/cline.

What about Codex, Copilot and others?

  • OpenAI Codex CLI. The documentation reviewed does not describe a way to deny reads of specific files. It does let you control which environment variables reach commands through shell_environment_policy, and the documented default does not strip names containing KEY, SECRET or TOKEN. Set inherit = "none" or add exclude filters if you do not want secrets in the commands it runs. Source: advanced config. See /agents/codex.
  • GitHub Copilot in VS Code. GitHub’s content exclusion page lists Agent and Edit modes in VS Code as unsupported, so an excluded file can still be read there. Source: content exclusion. See /agents/github-copilot.
  • Windsurf. Paths matched by .devinignore (the legacy .codeiumignore is still read) cannot be viewed, edited or created. Source: windsurf-ignore. See /agents/windsurf.
  • Zed. always_deny patterns under agent.tool_permissions take precedence over other rules. Source: tool permissions. See /agents/zed.

What should I do beyond ignore files?

Ignore files stop accidental reads. For a secret you cannot afford to leak, remove it from the agent’s reach.

  1. Keep real keys out of the project folder. Use a development key in the project and keep production keys in a secrets store.
  2. Do not export secrets in the shell that launches the agent. A variable in the environment is inherited by commands the agent runs.
  3. Use the tool’s sandbox or approval mode so file and command access is limited and visible.
  4. Scope and rotate the keys that remain. If an agent could have read a key, rotate it: see how to rotate API keys.
  5. Review project-level config before you open a repository. Disclosed issues in the dataset, such as CVE-2025-61260 in older Codex CLI releases and the Cursor MCPoison issue, involved configuration files that came with a repository.

The broader approach is covered in giving AI coding agents API access safely.

Where does BotPassword fit?

With BotPassword, a project does not need a plaintext .env for secrets at all: an agent asks for an outcome, such as running a command with a credential, and BotPassword injects the value into that process and returns redacted output. The env to BotPassword tool helps move values out of an existing file.

Frequently asked questions

Can AI coding agents read my .env file?

By default most can read any file in the project folder. Each tool has its own ignore or deny mechanism, and several vendors state those mechanisms do not cover every path, such as terminal commands.

Does adding .env to .gitignore stop an agent reading it?

Not in general. Git ignore rules govern git, and tools differ in whether they honor them. Cursor, for example, ignores files in .gitignore for its Agent, Tab and @ references, while Claude Code needs explicit deny rules.

Is an ignore file a security boundary?

The vendors say it is not. Cline's docs describe .clineignore as a context filter, and Cursor says its ignore file cannot block terminal or MCP tools.

What is the safest way to keep secrets from an agent?

Keep the value out of the project folder and the agent's environment, and hand it only to the command that needs it. Ignore files reduce accidental exposure but do not remove the secret from your machine.

More guides

Give agents access. Keep the keys.

BotPassword is in early access. Request an invite and we'll email you when your spot opens.