Repository navigation
fix: stop the translation regex from backtracking exponentially (#9) - #12
Open
HusnainMaroof wants to merge 1 commit into
Open
HusnainMaroof wants to merge 1 commit into
HusnainMaroof wants to merge 1 commit into
Conversation
) 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]>
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.
Fixes #9
cc @yazan-albaiz (security maintainer, per
SECURITY.md)Problem
TRANS_REinsrc/utils/translations.tsmatches 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 translationsthen hangs at 100% CPU.Measured on
main(45e59e4), Node 22:\arepeats\adoubles it)generateTranslations)Fix
A one-line change: the plain-character branch no longer matches a backslash.
\\.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 beforeBehaviour
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 messagea\.I checked this against Jinja2 3.1.6 with Babel. Jinja raises a
TemplateSyntaxErroron_("a\"), so no working theme contains it. Escaped quotes such as_("He said \"hi\"")and_('It\'s')extract exactly as Babel extracts them.Verification
recheckreports the old pattern as vulnerable (exponential) and the new one as safe. I also scanned the other 18 regex literals insrc/, and none are flaggedgenerateTranslationsin about 30 mstests/unit/utils/translations.test.ts:main)main)npx vitest run tests/unit/utils/translations.test.ts: 14/14 pass (11 before)pnpm buildpassesvitest runhas the same 4 failing suites as onmain(they are unrelated), and none are newNot changed
Jinja allows a message across a line break (
_("multi\nline")), and Babel extracts it, butTRANS_REhas 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.