Skip to content

fix: disable dotenv interpolation and harden .env quoting#351

Merged
bicced merged 2 commits intomainfrom
fix/env-credential-quoting-v2
Mar 6, 2026
Merged

fix: disable dotenv interpolation and harden .env quoting#351
bicced merged 2 commits intomainfrom
fix/env-credential-quoting-v2

Conversation

@bicced
Copy link
Contributor

@bicced bicced commented Mar 6, 2026

Summary

Follow-up to #350. Review found the double-quote fallback was broken — python-dotenv doesn't support \$ escaping, so values with both ' and $ were still corrupted.

  • Read side: load_dotenv(interpolate=False) — prevents $VAR expansion regardless of quoting
  • Write side: single-quoted by default; double-quoted fallback only escapes \ and " (no \$ — not needed with interpolation off)
  • Test hygiene: monkeypatch.delenv before all _persist_to_env calls to prevent env pollution across tests

Test plan

  • test_persist_to_env_single_quote_and_dollar' + $ combined (was failing before this fix)
  • test_persist_to_env_all_special_chars', ", $, \, # all in one value
  • test_persist_to_env_empty_value — empty string round-trip
  • test_persist_to_env_overwrites_quoted_value — overwrite + no line duplication
  • test_remove_from_env_file_quoted — removal of new quoted format
  • All dotenv_values() calls use interpolate=False matching production
  • All 99 credential tests pass
  • Full suite: 1760 passed

bicced added 2 commits March 6, 2026 18:59
The previous fix (single-quoting values) was necessary but insufficient:
python-dotenv does not support \$ escaping in double-quoted strings, so
values containing both ' and $ were still corrupted on reload.

Defense is now layered:
1. Write side: values are single-quoted (no interpolation, no escapes).
   Falls back to double-quoted with \\ and \" escaping for the rare
   case of embedded single quotes.
2. Read side: load_dotenv(interpolate=False) prevents $VAR expansion
   regardless of quoting style.

Also fixes test env pollution (monkeypatch.delenv before _persist_to_env
calls) and adds coverage for all edge cases: combined ' + $, all special
chars together, empty values, overwrite of quoted entries, and removal
of quoted entries.
Remove extraneous f-prefixes from strings without placeholders in
loop.py and sort imports in test_loop.py. These are pre-existing
issues from PR #348 caught by CI's ruff 0.15.5.
@bicced bicced force-pushed the fix/env-credential-quoting-v2 branch from 0a5d4c1 to 41b0b42 Compare March 6, 2026 10:59
@bicced bicced merged commit eadb47d into main Mar 6, 2026
3 checks passed
@bicced bicced deleted the fix/env-credential-quoting-v2 branch March 6, 2026 11:03
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