mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: avoid O(n^2) scanning in reflinkSearch (#4090)
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
a43c064837
commit
c6a25bb2a9
+25
-2
@@ -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]/;
|
||||
|
||||
@@ -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: `<p>[${'\t['.repeat(escapedBracket)}[</p>`,
|
||||
},
|
||||
{
|
||||
markdown: '[' + '\t\\[\\]'.repeat(escapedPair) + '[',
|
||||
html: `<p>[${'\t[]'.repeat(escapedPair)}[</p>`,
|
||||
},
|
||||
];
|
||||
Reference in New Issue
Block a user