Skip to content

fix: treat \\ as an escape pair in the fast parser - #1042

Merged
motdotla merged 1 commit into
motdotla:masterfrom
Jaybhade:fix/fast-parser-escaped-backslash
Aug 4, 2026
Merged

motdotla merged 1 commit into
motdotla:masterfrom
Jaybhade:fix/fast-parser-escaped-backslash

Conversation

@Jaybhade

@Jaybhade Jaybhade commented Aug 4, 2026

Copy link
Copy Markdown
Contributor

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.

const src = 'A="\\\\"\nB=plain\nC="quoted"\nD=last\n'

dotenv.parse(src)                   // { A: '\\\\', B: 'plain', C: 'quoted', D: 'last' }
dotenv.parse(src, { fast: true })   // { A: '\\\\"\nB=plain\nC=',                D: 'last' }
//                                     ^ B and C are gone

A realistic trigger is a Windows path, since .env files often escape the separators:

WINDIR="C:\\Users\\me\\"
API_KEY=secret
PORT=3000

On master that yields WINDIR: '"C:\\Users\\me\\"' — quotes included — because the scan runs to EOF and falls into the unterminated-quote path. API_KEY and PORT survive 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:

if (cc === 92 /* \ */ && j + 1 < len && str.charCodeAt(j + 1) === quote) {
  j += 2
}

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 " in A'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) against parse(src, { fast: true }) over generated well-formed .env content. 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 test is green (190 pass, 1 pre-existing skip) and npm run lint is clean. The new test cases fail on master and 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):

dotenv.parse('K="a\\"\nN=ok\n')                 // { K: 'a\\', N: 'ok' }
dotenv.parse('K="a\\"\nN=ok\n', { fast: true }) // { K: '"a\\"', N: 'ok' }

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.

@motdotla

motdotla commented Aug 4, 2026

Copy link
Copy Markdown
Owner

thank you!

@motdotla
motdotla merged commit 2fc7eac into motdotla:master Aug 4, 2026
4 checks passed
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.

2 participants