Repository navigation
fix: treat \\ as an escape pair in the fast parser - #1042
Merged
motdotla merged 1 commit intoAug 4, 2026
Merged
Conversation
Owner
|
thank you! |
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.
In the
{ fast: true }parser, a double-quoted value whose contents end in an escaped backslash runs past its own closing quote and swallows the rest of the file. Subsequent variables silently disappear.A realistic trigger is a Windows path, since
.envfiles often escape the separators:On
masterthat yieldsWINDIR: '"C:\\Users\\me\\"'— quotes included — because the scan runs to EOF and falls into the unterminated-quote path.API_KEYandPORTsurvive only because nothing later in the file is quoted; add one quoted value below and the lines in between are swallowed instead, as in the first example. Either way there is no error.Cause
The closing-quote scan treats a backslash as escaping only the quote character:
There is no case for
\\, so in the sequence\\"the first backslash is consumed as an ordinary character and the second one is left to pair with the closing quote. The scan skips over the quote that should have ended the value and keeps going until it finds the next quote in the file, or hits EOF and falls into the unterminated-quote path (which is where the stray"inA's value above comes from).The classic regex avoids this by backtracking, so the two parsers disagree.
Fix
Let a backslash consume the next character when it escapes either the quote or another backslash, so the second backslash of a pair can no longer escape the closing quote. Values are unchanged —
\\is still not unescaped, matching the classic parser.Verification
I found this by differentially fuzzing
parse(src)againstparse(src, { fast: true })over generated well-formed.envcontent. Before the change, 15,299 of 400,000 inputs diverged across 21 distinct failure classes; every one traced back to this single cause. After it, 0 of 400,000 diverge.npm testis green (190 pass, 1 pre-existing skip) andnpm run lintis clean. The new test cases fail onmasterand pass with the fix;KEY="a\\b"is included as a control that passes either way.One related case left alone
An odd number of backslashes before the closing quote still diverges, e.g.
WINDIR="C:\Users\me\"(single separators, no escaping):Matching the classic parser there needs more than escape tracking — its regex picks the closing quote using the rest of the line (
\s*(?:#.*)?$), and reproducing that in a forward scan is a bigger change than this one. I left it out rather than half-fix it, and I'm happy to follow up separately if you'd like it addressed.