Skip to content

fix: stop the translation regex from backtracking exponentially (#9) - #12

Open
HusnainMaroof wants to merge 1 commit into
zidsa:mainfrom
HusnainMaroof:fix/translation-regex-redos
Open

HusnainMaroof wants to merge 1 commit into
zidsa:mainfrom
HusnainMaroof:fix/translation-regex-redos

Conversation

@HusnainMaroof

Copy link
Copy Markdown

Fixes #9

cc @yazan-albaiz (security maintainer, per SECURITY.md)

Problem

TRANS_RE in src/utils/translations.ts matches the string body with (?:\\.|(?!\1).)*. Both alternatives match a backslash, so an unterminated call such as _("\a\a\a… has exponentially many ways to match. vitrin translations then hangs at 100% CPU.

Measured on main (45e59e4), Node 22:

\a repeats time
20 134 ms
24 1.8 s
26 more than 2 s (each extra \a doubles it)
50,000 (end to end through generateTranslations) still running after 30 s

Fix

A one-line change: the plain-character branch no longer matches a backslash.

-const TRANS_RE = /(?<![A-Za-z0-9_])_\(\s*(['"])((?:\\.|(?!\1).)*)\1\s*\)/g;
+const TRANS_RE = /(?<![A-Za-z0-9_])_\(\s*(['"])((?:\\.|(?!\1)[^\\\n\r\u2028\u2029])*)\1\s*\)/g;
  • \\. always starts with \ and the other branch never does, so each character can be consumed one way only, and matching is linear
  • [^\\\n\r\u2028\u2029] excludes the same line terminators that . did, so a message still cannot span a line break, as before

Behaviour

Normal templates give exactly the same output as before. The one change: a backslash always escapes the next character. So _("a\") is now an unterminated string rather than the message a\.

I checked this against Jinja2 3.1.6 with Babel. Jinja raises a TemplateSyntaxError on _("a\"), so no working theme contains it. Escaped quotes such as _("He said \"hi\"") and _('It\'s') extract exactly as Babel extracts them.

Verification

  • ReDoS analyzer: recheck reports the old pattern as vulnerable (exponential) and the new one as safe. I also scanned the other 18 regex literals in src/, and none are flagged
  • Linear growth: 12 attack shapes from 10⁴ to 10⁶ repetitions all grow about 10× per 10× of input. The issue's payload with 50,000 repeats now finishes generateTranslations in about 30 ms
  • Same output: on a 1 MB realistic template (20,204 messages), the output is identical to the old regex, and the speed is the same (about 3 ms)
  • Tests: added 3 tests to tests/unit/utils/translations.test.ts:
    • a backslash before the closing quote is an escape (this fails on main)
    • a message does not span a line break
    • a ReDoS guard with 25,000 escapes that must finish in under 2 s (this hangs on main)
  • npx vitest run tests/unit/utils/translations.test.ts: 14/14 pass (11 before)
  • pnpm build passes
  • The full vitest run has the same 4 failing suites as on main (they are unrelated), and none are new

Not changed

Jinja allows a message across a line break (_("multi\nline")), and Babel extracts it, but TRANS_RE has always skipped those. I kept that behaviour so this PR fixes only #9. I'm happy to open a separate issue for it if you want.

)

In TRANS_RE the string body was (?:\\.|(?!\1).)*. Both alternatives
match a backslash, so an unterminated call such as _("\a\a\a... has
exponentially many ways to match, and extraction hangs (n=26 takes
over 2 s, and each extra \a doubles it).

The plain-character branch no longer matches a backslash, and it still
excludes the same line terminators that "." excluded. Every character
can be consumed one way only, so matching is linear. A backslash now
always escapes the next character, which is how Jinja reads string
literals.

Adds tests for the escape behaviour, for line breaks, and a ReDoS guard.

Fixes zidsa#9

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.

Catastrophic Backtracking in Translation Regex

1 participant