fix(docs): avoid backtracking in excerpt link destinations (#31884)

This commit is contained in:
Neil
2026-10-22 00:19:14 -07:00
committed by GitHub
parent 9e1ffb2333
commit 7ff3a071c6
2 changed files with 27 additions and 2 deletions
@@ -27,8 +27,8 @@ export function stripSearchExcerptMarkdown(value) {
})
const linkedText = protectedCode.includes(']')
? protectedCode
.replace(/!\[([^\]]*)\]\((?:\\.|[^)])*\)/g, '$1')
.replace(/\[([^\]]+)\]\((?:\\.|[^)])*\)/g, '$1')
.replace(/!\[([^\]]*)\]\((?:\\[\s\S]|[^)\\])*\\?\)/g, '$1')
.replace(/\[([^\]]+)\]\((?:\\[\s\S]|[^)\\])*\\?\)/g, '$1')
.replace(/\[([^\]]+)\]\s*\[[^\]]*\]/g, '$1')
.replace(/\[\[([^|\]]+)\|([^\]]+)\]\]/g, '$2')
.replace(/\[\[([^\]]+)\]\]/g, '$1')
+25
View File
@@ -1,7 +1,32 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { runInNewContext } from 'node:vm'
import { stripSearchExcerptMarkdown } from '../src/components/docs/search-excerpt.mjs'
test('unterminated escaped link destinations do not backtrack exponentially', () => {
for (const prefix of ['![](', '[label](']) {
const input = prefix + '\\('.repeat(32_768)
const result = runInNewContext(
'strip(input)',
{ strip: stripSearchExcerptMarkdown, input },
{
timeout: 250
}
)
assert.equal(result, input.replace(/\\\(/g, '('))
}
})
test('link destinations retain escaped closing parentheses and literal escapes', () => {
for (const prefix of ['![label](', '[label](']) {
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\)b)'), 'label')
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\(b)'), 'label')
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\)'), 'label')
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\\\))'), 'label)')
assert.equal(stripSearchExcerptMarkdown(prefix + 'https://example.com/a\\\nb)'), 'label')
}
})
test('search excerpts omit inline Markdown syntax', () => {
assert.equal(
stripSearchExcerptMarkdown(