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

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
This commit is contained in:
Tzong Hao Chen
2026-03-04 08:30:23 -07:00
committed by GitHub
parent 5b6faee492
commit 46fb9b806f
2 changed files with 5 additions and 1 deletions
+1 -1
View File
@@ -380,7 +380,7 @@ const tag = edit(
const _inlineLabel = /(?:\[(?:\\[\s\S]|[^\[\]\\])*\]|\\[\s\S]|`+[^`]*?`+(?!`)|[^\[\]\\`])*?/;
const link = edit(/^!?\[(label)\]\(\s*(href)(?:(?:[ \t]*(?:\n[ \t]*)?)(title))?\s*\)/)
const link = edit(/^!?\[(label)\]\(\s*(href)(?:(?:[ \t]+(?:\n[ \t]*)?|\n[ \t]*)(title))?\s*\)/)
.replace('label', _inlineLabel)
.replace('href', /<(?:\\.|[^\n<>\\])+>|[^ \t\n\x00-\x1f]*/)
.replace('title', /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/)
+4
View File
@@ -0,0 +1,4 @@
module.exports = {
markdown: 'a[b](c'.repeat(1000),
html: `<p>${'a[b](c'.repeat(1000)}</p>\n`,
};