From 0d2022025d3cd44750a3ffbb77e2da60635589ee Mon Sep 17 00:00:00 2001 From: spokodev Date: Mon, 5 Oct 2026 16:36:37 +0100 Subject: [PATCH] fix: avoid cubic backtracking on unicode whitespace in a link destination (#4106) Co-authored-by: Claude Opus 5 (1M context) --- src/rules.ts | 2 +- test/specs/redos/cubic_link_unicode_space.cjs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 test/specs/redos/cubic_link_unicode_space.cjs diff --git a/src/rules.ts b/src/rules.ts index a707f42c..42824c6e 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -457,7 +457,7 @@ const _inlineLabel = edit(/(?:\[(?:brackets|\\[\s\S]|[^\[\]\\])*\]|\\[\s\S]|`+(? .replace('brackets', _inlineLabelBrackets) .getRegex(); -const link = edit(/^!?\[(label)\]\(\s*(href)(?:(?:[ \t]+(?:\n[ \t]*)?|\n[ \t]*)(title))?\s*\)/) +const link = edit(/^!?\[(label)\]\([ \t\n]*(href)(?:(?:[ \t]+(?:\n[ \t]*)?|\n[ \t]*)(title))?[ \t\n]*\)/) .replace('label', _inlineLabel) .replace('href', /<(?:\\.|[^\n<>\\])+>|[^ \t\n\x00-\x1f]+|(?=\))/) .replace('title', /"(?:\\"?|[^"\\])*"|'(?:\\'?|[^'\\])*'|\((?:\\\)?|[^)\\])*\)/) diff --git a/test/specs/redos/cubic_link_unicode_space.cjs b/test/specs/redos/cubic_link_unicode_space.cjs new file mode 100644 index 00000000..5df17609 --- /dev/null +++ b/test/specs/redos/cubic_link_unicode_space.cjs @@ -0,0 +1,8 @@ +// Same shape as quadratic_link_empty_href, with the ASCII space replaced by U+00A0. +// The gap before a link destination used to be \s*, which also matches unicode +// whitespace, while the destination class only excluded ASCII space, tab and newline. +// A run of unicode whitespace could therefore be split between the two in n ways. +module.exports = { + markdown: '[](' + ' '.repeat(50000), + html: `

[](${' '.repeat(50000)}

`, +};