swiftapplications

Apple security for developers

Why .env files are a risk on a developer Mac (and what to use instead)

How .env files leak keys on a Mac: other apps, AI coding agents, backups and Git. Deny-list them for agents, and move keys to the Keychain or a vault.

Updated 8 October 2026 3 min read

The short answer

.env files are plain text that every program running as you can read, including AI coding agents working in the project. They spread into backups and Git history. Keep real keys out of them: load keys from the Keychain or a vault at run time, tell your agent not to read them, and rotate any key that has ever been committed.

How .env files leak

  • Other programs. Editor extensions, npm install scripts and helper tools all run as you.
  • AI coding agents. An agent reading your project to answer a question can read .env too, and quote it in a prompt or a log.
  • Backups and sync. Time Machine copies it; iCloud Drive syncs it if the project lives in Desktop or Documents.
  • Git. One missing .gitignore line and it’s in history, even after you delete the file.

Tell your agent not to read them

Claude Code reads permission rules from .claude/settings.json in the project; deny rules stop it reading matching files:

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

It helps, but it’s a rule in one tool. The key is still a file on disk.

What to use instead

  • The Keychain, read at run time with security find-generic-password -w (how).
  • A password manager’s CLI that injects secrets into one command, such as 1Password’s op run.
  • A secrets vault for agents, so agents use keys without the keys being written anywhere they can read.

The safe way to do this

MoltenRock

The encrypted vault for your API keys and AI agents

Your keys sealed by your Mac’s Secure Enclave, handed only to the local agents you approve, never written to a .env file. Free for Mac.

Get MoltenRock, free

Questions developers ask

Is it safe to commit .env.example?

Yes, if it holds only names and placeholder values. Never real keys.

Can AI coding agents read my .env file?

If it’s in the folder they work in and nothing forbids it, yes. Use your agent’s deny rules and keep real keys elsewhere.

Does .gitignore protect my .env file?

Only from future commits. It doesn’t remove it from history, backups or other programs.