fix: Avoid O(n^2) backtracking in GFM email autolink regexes#4024
Open
hong4rc wants to merge 1 commit into
Open
fix: Avoid O(n^2) backtracking in GFM email autolink regexes#4024hong4rc wants to merge 1 commit into
hong4rc wants to merge 1 commit into
Conversation
|
@hong4rc is attempting to deploy a commit to the MarkedJS Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Member
|
While it is correct that email addresses can't have a local name longer than 64 characters, marked is not trying to parse valid email addresses. The goal of marked with GFM is to copy GitHub's markdown parsing. For GitHub there is no limit to this length. example: |
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.
Same shape of bug as #4013, this time in the GFM email autolink path.
The
urlrule (used for autolinking bare emails) and thetextrule (used to find where the next inline token starts) each retry at every character position in the input, and each one contains an unbounded[A-Za-z0-9._+-]+run that scans forward looking for an@. When the input has a long run of local-part-looking characters with no@anywhere in it, every one of those retries rescans the same tail of the string, so tokenizing goes quadratic in the length of that run.The fix bounds both runs to
{1,64}instead of+, matching RFC 5321's 64-character limit on the email local part. That caps how far each scan can walk, so tokenizing stays linear, and it doesn't lose any valid input — a local part longer than 64 characters was never a valid email to begin with.Checked with the full spec suite (1753/1753 at the 18.0.6 base this branch started from, still all green after rebasing onto current master) plus a new
test/specs/redos/quadratic_email_autolink.cjscase that's red before the fix and green after. Timing-wise,parse('a_'.repeat(32000))went from ~2420ms to ~33ms.While in there I noticed the sibling
<email>-bracket autolink rule around rules.ts:379 has the same unbounded-run shape — didn't touch it here since it's a separate code path, but it's a plausible follow-up.