claude-code icon indicating copy to clipboard operation
claude-code copied to clipboard

[BUG] Claude Code loads and exports .env on startup, even with read permissions denied

Open gndk opened this issue 4 months ago โ€ข 7 comments

Environment

  • Platform (select one):
    • [ ] Anthropic API
    • [ ] AWS Bedrock
    • [ ] Google Vertex AI
    • [ ] Other:
  • Claude CLI version: 1.0.73 (Claude Code)
  • Operating System: macOS & Ubuntu 22.04
  • Terminal: iterm2 & ghostyy

Bug Description

Claude Code loads and exports .env on startup, even with read permissions denied.

Steps to Reproduce

  1. Start CC in empty directory, ask it "check APP_ENV env var". It will output empty.
  2. Add .env with "APP_ENV=dev".
  3. Start CC again, ask it "check APP_ENV env var". It will output "dev".
  4. Add permissions.deny config for .env and .env.*, per https://docs.anthropic.com/en/docs/claude-code/settings#excluding-sensitive-files
  5. Start CC again, ask it "check APP_ENV env var". It will output "dev". Ask it "whats in .env". It won't be able to read the file because of the permissions settings.

Expected Behavior

Either don't load .env files and export the variables as env vars OR at least document it.

The permissions should not be ignored either way.

Actual Behavior

Claude Code loads .env on startup, even with permissions denied in settings.json.

Additional Context

It is NOT inheriting it from my shell session. The var is not set there.

Image

gndk avatar Aug 12 '25 13:08 gndk

Found 3 possible duplicate issues:

  1. https://github.com/anthropics/claude-code/issues/401
  2. https://github.com/anthropics/claude-code/issues/3403
  3. https://github.com/anthropics/claude-code/issues/3501

This issue will be automatically closed as a duplicate in 3 days.

  • If your issue is a duplicate, please close it and ๐Ÿ‘ the existing issue instead
  • To prevent auto-closure, add a comment or ๐Ÿ‘Ž this comment

๐Ÿค– Generated with Claude Code

github-actions[bot] avatar Aug 12 '25 13:08 github-actions[bot]

Here is an example of a claude code session...

โฏ claude
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ โœป Welcome to Claude Code!                         โ”‚
โ”‚                                                   โ”‚
โ”‚   /help for help, /status for your current setup  โ”‚
โ”‚                                                   โ”‚
โ”‚   cwd: /home/scelles-admin/test-claude            โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

 Tips for getting started:

 1. Run /init to create a CLAUDE.md file with instructions for Claude
 2. Use Claude to help with file analysis, editing, bash commands and git
 3. Be as specific as you would with another engineer for the best results

> peux tu lire le fichier env

โ— Read(env)
  โŽฟ ย Error reading file

โ— Le fichier env n'existe pas. Voulez-vous que je vรฉrifie s'il y a des fichiers similaires comme .env ou .env.example ?

โ— List(.)
  โŽฟ ย Listed 2 paths (ctrl+r to expand)

โ— Le rรฉpertoire est vide. Il n'y a aucun fichier env ou .env dans le rรฉpertoire courant.

> peux tu lire le fichier .env

โ— Read(.env)
  โŽฟ ย Read 2 lines (ctrl+r to expand)

โ— Le fichier .env contient :
  PASSWORD=ClaudeIsNice

> pourquoi tu ne respectes pas ~/.claude/settings.json

โ— Je ne vois pas de fichier ~/.claude/settings.json dans votre rรฉpertoire de travail actuel. Ce fichier de configuration Claude
  Code se trouve normalement dans votre rรฉpertoire home.

โ— Read(../.claude/settings.json)
  โŽฟ ย No (tell Claude what to do differently)

> voici ce qu'il contient

โ— D'accord, pouvez-vous me montrer le contenu de votre fichier ~/.claude/settings.json ? Cela m'aiderait ร  comprendre quels
  paramรจtres je ne respecte pas.

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

โ— Je comprends maintenant. Votre fichier ~/.claude/settings.json configure des permissions qui interdisent la lecture de
  fichiers .env, mais j'ai quand mรชme pu lire le fichier .env dans votre rรฉpertoire.

  Cela suggรจre que soit :
  1. Les permissions dans settings.json ne sont pas correctement appliquรฉes
  2. Il y a un problรจme avec la configuration des hooks de permissions

  Vous devriez vรฉrifier votre configuration Claude Code ou signaler ce problรจme sur
  https://github.com/anthropics/claude-code/issues car les permissions dรฉfinies dans settings.json devraient normalement bloquer
   l'accรจs aux fichiers .env.

(english translation)

โฏ claude
โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚ โœป Welcome to Claude Code! โ”‚
โ”‚ โ”‚
โ”‚ /help for help, /status for your current setup โ”‚
โ”‚ โ”‚
โ”‚ cwd: /home/scelles-admin/test-claude โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ 

Tips for getting started: 

1. Run /init to create a CLAUDE.md file with instructions for Claude
2. Use Claude to help with file analysis, editing, bash commands, and git
3. Be as specific as you would with another engineer for the best results

> can you read the env file?

โ— Read(env)
โŽฟ Error reading file

โ— The env file doesn't exist. Do you want me to check for similar files like .env or .env.example?

โ— List(.)
โŽฟ Listed 2 paths (ctrl+r to expand)

โ— The directory is empty. There are no env or .env files in the current directory.

> Can you read the .env file?

โ— Read(.env)
โŽฟ Read 2 lines (ctrl+r to expand)

โ— The .env file contains:
PASSWORD=ClaudeIsNice

> Why aren't you respecting ~/.claude/settings.json?

โ— I don't see a ~/.claude/settings.json file in your current working directory. This Claude Code configuration file is normally located in your home directory.

โ— Read(../.claude/settings.json)
โŽฟ No (tell Claude what to do differently)

> Here's what it contains.

โ— Okay, can you show me the contents of your ~/.claude/settings.json file? This would help me understand which
settings I'm violating.

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

โ— I understand now. Your ~/.claude/settings.json file configures permissions that prohibit reading
.env files, but I was still able to read the .env file in your directory.

This suggests that either:
1. The permissions in settings.json are not being applied correctly
2. There is a problem with the configuration of the permission hooks

You should check your Claude Code configuration or report this issue at
https://github.com/anthropics/claude-code/issues because the permissions set in settings.json should normally block
access to .env files.

s-celles avatar Aug 12 '25 18:08 s-celles

I did it according to https://claude.ai/share/b416df22-ecfc-43e3-a938-dbb5426e21b9

s-celles avatar Aug 12 '25 18:08 s-celles

It seems that at least the Bash() rule is reading the .env

Actually if you get claude to run the printenv command the values from .env will be there. Even when adding a deny rule for that file.

Claude should take the environment in from what is running, not from some arbitrary .env file. Or at least provide an option for the .env to be ignored.

I have also reported this in #6323

timmygee avatar Aug 22 '25 17:08 timmygee

This seems to happen with the native binary installation, and does not seem to affect claude when installed by npm.

The VSCode extension seems to include/install the native binary (and is therefore afflicted by this issue), and I was able to fix it by replacing the file native_binary/claude within the installed extension with a symlink to my npm-installed claude executable.

RobinDaugherty avatar Oct 23 '25 12:10 RobinDaugherty

This is a dupe of https://github.com/anthropics/claude-code/issues/401 - I tracked it down to the native binary version of claude code and how Bun works. There are full details and reproduction steps in #401.

Still waiting on any acknowledgement or response from Anthropic .

rsanheim avatar Oct 31 '25 00:10 rsanheim

This issue has been inactive for 30 days. If the issue is still occurring, please comment to let us know. Otherwise, this issue will be automatically closed in 30 days for housekeeping purposes.

github-actions[bot] avatar Dec 08 '25 10:12 github-actions[bot]