Security: OpenCode automatically reads .env files from project/running directories #575

Closed
opened 2026-02-16 17:27:24 -05:00 by yindo · 2 comments
Owner

Originally created by @Heratiki on GitHub (Jul 9, 2025).

OpenCode automatically reads and uses environment variables from .env files in the current project directory without explicit user consent. This creates a security risk where sensitive API keys and credentials can be inadvertently exposed to the AI service.
Steps to Reproduce

Create a project directory with a .env file containing API keys (e.g., OPENROUTER_API_KEY=sk-...)
Run opencode in that directory
OpenCode automatically detects and uses the API key from the .env file
The tool switches to using that provider without user confirmation

Expected Behavior

OpenCode should not automatically read .env files from project directories
If environment variable detection is desired, it should be opt-in with clear user consent
Users should be warned when credentials are being read from project files

Actual Behavior

OpenCode silently reads .env files and uses found credentials
No warning or confirmation is provided to the user
API keys are used without explicit authorization

Security Implications

Developers may unknowingly expose client/project API keys to AI services
.env files often contain sensitive credentials not intended for AI tool usage
This could lead to unintended billing or quota usage on accounts
Violates principle of least privilege and explicit consent

Environment

OpenCode version: 0.2.13
Platform: WSL2/Linux

Originally created by @Heratiki on GitHub (Jul 9, 2025). OpenCode automatically reads and uses environment variables from .env files in the current project directory without explicit user consent. This creates a security risk where sensitive API keys and credentials can be inadvertently exposed to the AI service. Steps to Reproduce Create a project directory with a .env file containing API keys (e.g., OPENROUTER_API_KEY=sk-...) Run opencode in that directory OpenCode automatically detects and uses the API key from the .env file The tool switches to using that provider without user confirmation Expected Behavior OpenCode should not automatically read .env files from project directories If environment variable detection is desired, it should be opt-in with clear user consent Users should be warned when credentials are being read from project files Actual Behavior OpenCode silently reads .env files and uses found credentials No warning or confirmation is provided to the user API keys are used without explicit authorization Security Implications Developers may unknowingly expose client/project API keys to AI services .env files often contain sensitive credentials not intended for AI tool usage This could lead to unintended billing or quota usage on accounts Violates principle of least privilege and explicit consent Environment OpenCode version: 0.2.13 Platform: WSL2/Linux
yindo closed this issue 2026-02-16 17:27:24 -05:00
Author
Owner

@thdxr commented on GitHub (Jul 9, 2025):

the idea behind .env files is it's a standard that tools read and autoload - you can see this with tools like docker compose, bun, air and frameworks like nextjs

the moment we remove this we will get people asking us to add it back in so we're unlikely to remove it, sorry

@thdxr commented on GitHub (Jul 9, 2025): the idea behind .env files is it's a standard that tools read and autoload - you can see this with tools like docker compose, bun, air and frameworks like nextjs the moment we remove this we will get people asking us to add it back in so we're unlikely to remove it, sorry
Author
Owner

@Heratiki commented on GitHub (Jul 9, 2025):

Understandable, but this is a security issue as it's not specifically described as this will happen nor is the user warned. If the source folder that opencode is working in uses a .env file for it's own purposes it should not default to using .env over other settings or at least alert the user.

@Heratiki commented on GitHub (Jul 9, 2025): Understandable, but this is a security issue as it's not specifically described as this will happen nor is the user warned. If the source folder that opencode is working in uses a .env file for it's own purposes it should not default to using .env over other settings or at least alert the user.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: anomalyco/opencode#575