fix: disable dotenv interpolation and harden .env quoting#351
Merged
Conversation
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.
0a5d4c1 to
41b0b42
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to #350. Review found the double-quote fallback was broken —
python-dotenvdoesn't support\$escaping, so values with both'and$were still corrupted.load_dotenv(interpolate=False)— prevents$VARexpansion regardless of quoting\and"(no\$— not needed with interpolation off)monkeypatch.delenvbefore all_persist_to_envcalls to prevent env pollution across testsTest 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 valuetest_persist_to_env_empty_value— empty string round-triptest_persist_to_env_overwrites_quoted_value— overwrite + no line duplicationtest_remove_from_env_file_quoted— removal of new quoted formatdotenv_values()calls useinterpolate=Falsematching production