fix: prevent quadratic complexity in emStrongLDelim regex (#3906)

This commit is contained in:
Sam.An
2026-03-20 09:07:53 -06:00
committed by GitHub
parent f3a3ec05cb
commit c732dd2221
3 changed files with 8 additions and 3 deletions
+3 -2
View File
@@ -730,11 +730,12 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
emStrong(src: string, maskedSrc: string, prevChar = ''): Tokens.Em | Tokens.Strong | undefined {
let match = this.rules.inline.emStrongLDelim.exec(src);
if (!match) return;
if (!match[1] && !match[2] && !match[3] && !match[4]) return;
// _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
if (match[3] && prevChar.match(this.rules.other.unicodeAlphaNumeric)) return;
if (match[4] && prevChar.match(this.rules.other.unicodeAlphaNumeric)) return;
const nextChar = match[1] || match[2] || '';
const nextChar = match[1] || match[3] || '';
if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
// unicode Regex counts emoji as 1 char; spread into array for proper count (used multiple times below)
+1 -1
View File
@@ -285,7 +285,7 @@ const blockSkip = edit(/link|precode-code|html/, 'g')
.replace('html', /<(?! )[^<>]*?>/)
.getRegex();
const emStrongLDelimCore = /^(?:\*+(?:((?!\*)punct)|[^\s*]))|^_+(?:((?!_)punct)|([^\s_]))/;
const emStrongLDelimCore = /^(?:\*+(?:((?!\*)punct)|([^\s*]))?)|^_+(?:((?!_)punct)|([^\s_]))?/;
const emStrongLDelim = edit(emStrongLDelimCore, 'u')
.replace(/punct/g, _punctuation)
@@ -0,0 +1,4 @@
module.exports = {
markdown: '_'.repeat(10000) + ' a',
html: `<p>${'_'.repeat(10000)} a</p>\n`,
};