Can Cursor or Claude Code read your .env file? How to block it

Ship ItChecked against the official source

The short answer

Cursor's docs say Cursor automatically ignores files in .gitignore and its default ignore list, which includes .env* files. But the terminal and MCP server tools used by Agent cannot block access to code governed by .cursorignore, and Cursor says complete protection isn't guaranteed due to LLM unpredictability. In Claude Code, a .claudeignore file has no effect: to block Claude's file tools from reading a file, add a Read deny rule for its path, such as Read(./.env).

What to do

  1. In Cursor, check that your .env files are covered by .gitignore or the default ignore list. You can add more files with .cursorignore.
  2. In Claude Code, add a Read deny rule for your .env file, such as Read(./.env).
  3. Ask your AI tool: "Add a Read deny rule for .env and .env.* to my Claude Code settings (in Cursor: check they're in .cursorignore), and never print or echo my keys in the terminal."

Good to know

  • Claude Code's Read deny rules also cover common file commands like cat, but not arbitrary scripts that open files themselves. For OS-level blocking, Claude Code points to its sandbox.
  • If a key was already exposed, rotate it.

Source

Checked on 10 Oct 2026. Prices, limits and settings change, so check the source before you rely on them.

Related