Skip to content

fix: .env loading no longer overrides already-set environment variables - #252

Open
mattpodwysocki wants to merge 3 commits into
mainfrom
fix/dotenv-no-override
Open

fix: .env loading no longer overrides already-set environment variables#252
mattpodwysocki wants to merge 3 commits into
mainfrom
fix/dotenv-no-override

Conversation

@mattpodwysocki

Copy link
Copy Markdown
Contributor

Summary

  • .env loading previously applied every key onto process.env with override semantics, so a .env file present in the working directory could take precedence over variables already set by the host process (e.g. MAPBOX_API_ENDPOINT, MAPBOX_ACCESS_TOKEN).
  • Extracted the loading logic into src/utils/loadDotEnv.ts and changed it to skip any key that's already set, matching the intent of Node's own process.loadEnvFile(). Already-set keys are now reported in the startup log message and the config.load_env tracing span instead of being silently skipped.

Test plan

  • npx vitest run — all tests pass, including new regression tests in test/utils/loadDotEnv.test.ts covering override precedence
  • npm run build succeeds
  • CHANGELOG.md updated

🤖 Generated with Claude Code

A malicious/updated .env in the project working directory could override
MAPBOX_API_ENDPOINT after the MCP host (Claude Desktop, VS Code, etc.) had
already set it correctly, while the host-injected MAPBOX_ACCESS_TOKEN
survived and kept being sent to the overridden endpoint -- exfiltrating it
to an attacker-controlled host with no error surfaced. .env loading now
skips any key already present in process.env, matching the intent of
Node's own loadEnvFile(), and reports skipped keys in the startup log/span
instead of silently dropping them.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@mattpodwysocki
mattpodwysocki requested a review from a team as a code owner August 25, 2026 16:00
mattpodwysocki and others added 2 commits August 25, 2026 12:01
No code behavior change -- just wording in comments, the changelog entry,
and test fixture values.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant