From c6a25bb2a9d14f839cf2fd9abe4e4c7c3eece25b Mon Sep 17 00:00:00 2001 From: spokodev Date: Fri, 11 Sep 2026 23:38:17 +0100 Subject: [PATCH] fix: avoid O(n^2) scanning in reflinkSearch (#4090) Co-authored-by: Claude Opus 5 (1M context) --- src/rules.ts | 27 +++++++++++++++++-- test/specs/redos/quadratic_reflink_search.cjs | 18 +++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/specs/redos/quadratic_reflink_search.cjs diff --git a/src/rules.ts b/src/rules.ts index 7d847035..af30f961 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -466,9 +466,32 @@ const nolink = edit(/^!?\[(ref)\](?:\[\])?/) .replace('ref', _blockLabel) .getRegex(); +// `reflink` and `nolink` are anchored, so the tokenizer tries each of them at a +// single position. `reflinkSearch` drops the anchors and runs with the global +// flag, which makes every '[' in the source a start position. A label crosses a +// bracket only by escaping it, and nothing caps how often it does, so a +// candidate that can never match still scans to the end of the source and the +// whole pass costs O(n^2). +// +// The labels below bound that scan. `_boundedInlineLabel` limits how many +// escapes, code spans and nested brackets a link text may hold, leaving runs of +// ordinary characters unbounded so link text of any length is still found. +// `_boundedBlockLabel` limits the label itself to 999 items, which is every +// label CommonMark allows: "A link label can have at most 999 characters +// inside the square brackets." +const _boundedBlockLabel = /(?!\s*\])(?:\\[\s\S]|[^\[\]\\]){1,999}/; +const _boundedInlineLabel = edit(/(?:[^\[\]\\`]*(?:\[(?:brackets|\\[\s\S]|[^\[\]\\])*\]|\\[\s\S]|`+(?!`)[^`]*?`+(?!`)|``+(?=\]))){0,999}?[^\[\]\\`]*?/) + .replace('brackets', _inlineLabelBrackets) + .getRegex(); + const reflinkSearch = edit('reflink|nolink(?!\\()', 'g') - .replace('reflink', reflink) - .replace('nolink', nolink) + .replace('reflink', edit(/^!?\[(label)\]\[(ref)\]/) + .replace('label', _boundedInlineLabel) + .replace('ref', _boundedBlockLabel) + .getRegex()) + .replace('nolink', edit(/^!?\[(ref)\](?:\[\])?/) + .replace('ref', _boundedBlockLabel) + .getRegex()) .getRegex(); const _caseInsensitiveProtocol = /[hH][tT][tT][pP][sS]?|[fF][tT][pP]/; diff --git a/test/specs/redos/quadratic_reflink_search.cjs b/test/specs/redos/quadratic_reflink_search.cjs new file mode 100644 index 00000000..1c69bb1f --- /dev/null +++ b/test/specs/redos/quadratic_reflink_search.cjs @@ -0,0 +1,18 @@ +// Regression: `reflinkSearch` is unanchored and runs with the global flag, so +// every '[' in the source is a start position. A label crosses a bracket only +// by escaping it, and nothing capped how often it could, so a candidate that +// can never match still scanned to the end of the source. 188 KB of the first +// shape took 21s to parse with default options. +const escapedBracket = 18000; +const escapedPair = 14000; + +module.exports = [ + { + markdown: '[' + '\t\\['.repeat(escapedBracket) + '[', + html: `

[${'\t['.repeat(escapedBracket)}[

`, + }, + { + markdown: '[' + '\t\\[\\]'.repeat(escapedPair) + '[', + html: `

[${'\t[]'.repeat(escapedPair)}[

`, + }, +];