Skip to content

.env files are not excluded from file discovery, exposing secrets to AI context #20904

@YumaKakuya

Description

@YumaKakuya

When working in a project that has a .env file, opencode's file search discovers it like any other text file. If the file gets read (manually or by the agent exploring the project), its contents — API keys, database URLs, tokens — end up in the conversation context sent to the model.

.gitignore keeps .env out of version control but has no effect on opencode's file discovery. The built-in ignore list in src/file/ignore.ts filters folders like node_modules and files like *.log, but nothing prevents .env files from being listed, searched, and read.

Steps to reproduce:

  1. Create a project with a .env containing a secret (e.g. API_KEY=sk-test-1234)
  2. Open opencode in that directory
  3. Search for files — .env appears in results
  4. Read the file or let the agent discover it during exploration
  5. The secret is now in the conversation context

Expected behavior:

.env and its variants (.env.local, .env.production, etc.) should be excluded from file discovery by default, the same way node_modules and .git are excluded.

Proposed fix:

Add **/.env and **/.env.* to the default ignore patterns in FileIgnore. This covers standard dotenv naming conventions while leaving unrelated files (.envrc, env.ts, environment.yaml) unaffected.

I'd like to submit a PR for this — the change is two patterns in ignore.ts plus tests.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions