Skip to content

refactor(dotenv): mirror motdotla/dotenv parser rewrite#501

Merged
v1rtl merged 3 commits into
masterfrom
rewrite-dotenv
May 16, 2026
Merged

refactor(dotenv): mirror motdotla/dotenv parser rewrite#501
v1rtl merged 3 commits into
masterfrom
rewrite-dotenv

Conversation

@v1rtl
Copy link
Copy Markdown
Member

@v1rtl v1rtl commented May 16, 2026

Summary

Mirror the parser rewrite that upstream motdotla/dotenv shipped in v16, in a compact form. Replaces the old line-by-line ^\s*([\w.-]+)\s*=\s*(.*)?\s*$ regex — flagged by CodeQL as a polynomial-time / ReDoS-prone pattern — with the upstream single multiline LINE regex.

Changes

packages/dotenv/src/index.ts

  • New LINE regex handles export prefix, KEY= and KEY: separators, quoted (', \", `) and unquoted values, and trailing inline # comments — all in one pass with no catastrophic backtracking.
  • Normalize \r\n?\n once, then loop with LINE.exec().
  • Strip surrounding matching quotes via a single replace; expand \n and \r escapes only inside double quotes (parity with upstream).
  • Added __proto__ / constructor / prototype guard so a malicious .env cannot pollute the prototype chain (small extra hardening on top of upstream).
  • config() gained override and processEnv options, matching upstream's API surface.

packages/dotenv/src/types.ts

  • Added override and processEnv to DotenvConfigOptions.
  • Marked DotenvParseOptions.debug deprecated — the new parser has no concept of an "unmatched line" so per-line debug warnings are gone.

tests/modules/dotenv.test.ts

  • Replaced the obsolete debug test with coverage for the rewrite: ignored non-pair lines, inline comments, export prefix, backtick quotes, __proto__ pollution safety, override, and custom processEnv.

Verification

  • `pnpm vitest run tests/modules/dotenv.test.ts` — 25/25 passing
  • Full suite via pre-commit hook — 805 passed, 3 skipped
  • `pnpm --filter @tinyhttp/dotenv build` — clean

Notes

Existing behavior is preserved for every case covered by the previous test suite. The `debug` option on `parse()` is now a no-op but kept for backwards compatibility.

v1rtl added 3 commits May 17, 2026 02:11
Replace the polynomial-time line-by-line regex with the upstream
single multiline LINE regex used since dotenv v16. Eliminates the
ReDoS-style code scanning alert and brings parity for export prefix,
backtick quotes, inline comments, and \r escape expansion.

Also adds override and processEnv config options, and a
__proto__/constructor/prototype guard in parse().
Brings branch coverage on packages/dotenv/src/index.ts back to 100%.
@v1rtl v1rtl merged commit 4738547 into master May 16, 2026
12 of 13 checks passed
@v1rtl v1rtl deleted the rewrite-dotenv branch May 16, 2026 23:18
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