Skip to content

fix: avoid quadratic time on long local part before @ in GFM text - #4225

Merged
UziTech merged 1 commit into
markedjs:masterfrom
shuishui313:fix/gfm-email-text-redos
Nov 16, 2026
Merged

UziTech merged 1 commit into
markedjs:masterfrom
shuishui313:fix/gfm-email-text-redos

Conversation

@shuishui313

Copy link
Copy Markdown
Contributor

Marked version: 18.2.0 (master @ 9a26ae9)

Markdown flavor: GitHub Flavored Markdown

Description

With GFM enabled (the default), a long run of email local-part characters followed by @ but no valid domain is parsed in quadratic time:

marked.parse('a'.repeat(80000) + '@'); // ~14 s before, ~13 ms after

Why

When the url (extended email autolink) rule fails at the start of such a run, inlineText stops after a single character because of the (?=[a-zA-Z0-9.!#$%&'*+\/=?_{|}~-]+@)lookahead. The lexer then retriesurlandtextat the next character, and each try scans to the@` again, so the run is consumed one character per O(n) step.

Fix

Add a leading alternative to the GFM text rule that consumes a whole [A-Za-z0-9.+-]+ run when it is followed directly by @. If the email autolink did not match at the start of that run, it cannot match at any later position in it either (every suffix ends at the same @ and domain), so taking the run in one token gives the same links with linear work. _ is left out of the run so emphasis delimiters are still handled as before.

One visible difference: in awww.example.com@, the mid-word www. was previously autolinked (a<a href="http://www.example.com@">…</a>) only because the lexer re-entered url at every character. It now stays plain text, which matches the GFM rule that a www autolink must start at a line start, after whitespace, or after *, _, ~ or (.

Checked unchanged output for: foo bar@baz.com, xa@b.co, a!b@c.co, foo.bar@x.com and qux@y.org, a_b@c.com, mailto:a@b.co, x mailto:a@b.co, foo@.

Contributor

  • Test(s) exist to ensure functionality and minimize regression: added test/specs/redos/quadratic_email_at.cjs. test:specs (2005 passing) and test:unit (243 passing) pass locally.
  • no tests required for this PR.
  • If submitting new feature, it has been documented in the appropriate places.

Committer

In most cases, this should be a different person than the contributor.

@vercel

vercel Bot commented Nov 16, 2026

Copy link
Copy Markdown

@shuishui313 is attempting to deploy a commit to the MarkedJS Team on Vercel.

A member of the Team first needs to authorize it.

@vercel

vercel Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
marked-website Ready Ready Preview Nov 16, 2026 7:22am UTC

Request Review

@UziTech UziTech left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! 💯

@UziTech
UziTech merged commit c6b22e8 into markedjs:master Nov 16, 2026
8 checks passed
github-actions Bot pushed a commit that referenced this pull request Nov 24, 2026
# [18.3.0](v18.2.1...v18.3.0) (2026-11-24)

### Bug Fixes

* allow trailing tabs after closing code fences ([#4241](#4241)) ([1e11902](1e11902))
* avoid quadratic scan on adjacent inline links ([#4274](#4274)) ([f5ffd22](f5ffd22))
* avoid quadratic time on long local part before @ in GFM text ([#4225](#4225)) ([c6b22e8](c6b22e8))
* check own properties on extensions.renderers in Parser ([#4271](#4271)) ([c572307](c572307))
* honor the CLI end-of-options marker ([#4230](#4230)) ([a1ddc72](a1ddc72))
* keep an indented code block after a list item paragraph ([#4198](#4198)) ([b9e3824](b9e3824))
* preserve literal entities in image alt text ([#4232](#4232)) ([04f1c6e](04f1c6e))
* reject CLI options missing their values ([#4261](#4261)) ([502e658](502e658))
* skip missing optional childTokens properties in walkTokens ([#4262](#4262)) ([283f74f](283f74f))
* wait for CLI error output before exiting ([#4263](#4263)) ([6c2a5ab](6c2a5ab))

### Features

* add inBlockquote to lexer state ([#4222](#4222)) ([201bfce](201bfce)), closes [#4184](#4184) [#4221](#4221)

### Performance Improvements

* avoid scanning table bodies for header-only decisions ([#4213](#4213)) ([a0925cc](a0925cc))
* skip paragraph lookahead for initial quoted code ([#4233](#4233)) ([171f177](171f177))

This branch was successfully deployed

1 active deployment
Preview — a414ed37 Deployed Nov 16, 2026 by vercel[bot]
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.

3 participants