Skip to content

fix: prevent ReDoS in inline link regex title group#3902

Merged
UziTech merged 1 commit intomarkedjs:masterfrom
tzonghao:fix/link-regex-redos
Mar 4, 2026
Merged

fix: prevent ReDoS in inline link regex title group#3902
UziTech merged 1 commit intomarkedjs:masterfrom
tzonghao:fix/link-regex-redos

Conversation

@tzonghao
Copy link
Contributor

@tzonghao tzonghao commented Feb 24, 2026

The title separator in the link regex used [ \t]* which allowed the title group to be probed at every backtrack position of the greedy href group. On long single-line input containing [text]( patterns without a nearby closing ), this produced O(n²) per regex call and O(n³) in the full inline tokenizer.

Change [ \t]* to [ \t]+|\n to require actual whitespace before the title. This matches CommonMark spec requirements and eliminates the backtracking cascade.

Before: 18K input takes ~36 seconds (event loop blocked)
After: 18K input takes ~45ms

Marked version:

Markdown flavor: Markdown.pl|CommonMark|GitHub Flavored Markdown|n/a

Description

  • Fixes #### (if fixing a known issue; otherwise, describe issue using the following format)

Contributor

  • Test(s) exist to ensure functionality and minimize regression (if no tests added, list tests covering this PR); or,
  • 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.

The title separator in the link regex used [ \t]* which allowed the
title group to be probed at every backtrack position of the greedy href
group. On long single-line input containing [text]( patterns without a
nearby closing ), this produced O(n²) per regex call and O(n³) in the
full inline tokenizer.

Change [ \t]* to [ \t]+|\n to require actual whitespace before the
title. This matches CommonMark spec requirements and eliminates the
backtracking cascade.

Before: 18K input takes ~36 seconds (event loop blocked)
After:  18K input takes ~45ms
Copilot AI review requested due to automatic review settings February 24, 2026 15:14
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @tzonghao, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical performance vulnerability (ReDoS) within the Markdown parser's inline link processing. By refining the regular expression used for link titles, it prevents excessive backtracking on certain inputs, leading to a substantial improvement in parsing efficiency and system stability when encountering malformed Markdown links.

Highlights

  • ReDoS Vulnerability Fix: Addressed a Regular Expression Denial of Service (ReDoS) vulnerability in the inline link regex's title group, which previously allowed for O(n²) per regex call and O(n³) in the full inline tokenizer with specific malformed inputs.
  • Regex Optimization: Modified the link regex in src/rules.ts to change [ \t]* to [ \t]+|\n, requiring actual whitespace before the title and eliminating the backtracking cascade that caused the performance issue.
  • Performance Improvement: Demonstrated a significant performance improvement, reducing processing time for an 18K problematic input from approximately 36 seconds to about 45 milliseconds.
  • New Test Case: Introduced a new test file, test/specs/redos/cubic_link_title.cjs, to specifically target and verify the fix for the cubic ReDoS issue in link titles.
Changelog
  • src/rules.ts
    • Modified the link regular expression to prevent a ReDoS vulnerability by adjusting the whitespace matching for link titles.
  • test/specs/redos/cubic_link_title.cjs
    • Added a new test case to specifically target and verify the fix for the cubic ReDoS issue in inline link titles.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses a critical Regular Expression Denial of Service (ReDoS) vulnerability in the inline link regex. The modification to the regex ensures stricter adherence to the CommonMark specification for title separators, preventing excessive backtracking. The addition of a dedicated test case for this specific ReDoS scenario is a valuable improvement, enhancing the robustness and reliability of the parser.

@vercel
Copy link

vercel bot commented Feb 24, 2026

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

A member of the Team first needs to authorize it.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a ReDoS (Regular Expression Denial of Service) vulnerability in the inline link regex pattern by changing the title separator from [ \t]* to [ \t]+|\n. This prevents catastrophic backtracking on malicious input while maintaining CommonMark specification compliance.

Changes:

  • Modified the inline link regex to require actual whitespace (at least one space/tab or a newline) before attempting to match the title group, eliminating O(n²) backtracking per regex call
  • Added a ReDoS test case that validates the fix by testing 1000 repeated a[b](c patterns without closing parentheses

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/rules.ts Updated link regex pattern to change title separator from [ \t]* to `[ \t]+
test/specs/redos/cubic_link_title.cjs Added test case with 1000 repeated incomplete link patterns to verify the ReDoS vulnerability is fixed

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@UziTech UziTech left a comment

Choose a reason for hiding this comment

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

Thanks! 💯

@vercel
Copy link

vercel bot commented Feb 28, 2026

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

Project Deployment Actions Updated (UTC)
marked-website Ready Ready Preview, Comment Feb 28, 2026 3:36pm

Request Review

@UziTech UziTech merged commit 46fb9b8 into markedjs:master Mar 4, 2026
11 of 12 checks passed
github-actions bot pushed a commit that referenced this pull request Mar 4, 2026
## [17.0.4](v17.0.3...v17.0.4) (2026-03-04)

### Bug Fixes

* prevent ReDoS in inline link regex title group ([#3902](#3902)) ([46fb9b8](46fb9b8))
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.

4 participants