Skip to content

Commit c732dd2

Browse files
authored
fix: prevent quadratic complexity in emStrongLDelim regex (#3906)
1 parent f3a3ec0 commit c732dd2

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

src/Tokenizer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,11 +730,12 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
730730
emStrong(src: string, maskedSrc: string, prevChar = ''): Tokens.Em | Tokens.Strong | undefined {
731731
let match = this.rules.inline.emStrongLDelim.exec(src);
732732
if (!match) return;
733+
if (!match[1] && !match[2] && !match[3] && !match[4]) return;
733734

734735
// _ can't be between two alphanumerics. \p{L}\p{N} includes non-english alphabet/numbers as well
735-
if (match[3] && prevChar.match(this.rules.other.unicodeAlphaNumeric)) return;
736+
if (match[4] && prevChar.match(this.rules.other.unicodeAlphaNumeric)) return;
736737

737-
const nextChar = match[1] || match[2] || '';
738+
const nextChar = match[1] || match[3] || '';
738739

739740
if (!nextChar || !prevChar || this.rules.inline.punctuation.exec(prevChar)) {
740741
// unicode Regex counts emoji as 1 char; spread into array for proper count (used multiple times below)

src/rules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ const blockSkip = edit(/link|precode-code|html/, 'g')
285285
.replace('html', /<(?! )[^<>]*?>/)
286286
.getRegex();
287287

288-
const emStrongLDelimCore = /^(?:\*+(?:((?!\*)punct)|[^\s*]))|^_+(?:((?!_)punct)|([^\s_]))/;
288+
const emStrongLDelimCore = /^(?:\*+(?:((?!\*)punct)|([^\s*]))?)|^_+(?:((?!_)punct)|([^\s_]))?/;
289289

290290
const emStrongLDelim = edit(emStrongLDelimCore, 'u')
291291
.replace(/punct/g, _punctuation)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
markdown: '_'.repeat(10000) + ' a',
3+
html: `<p>${'_'.repeat(10000)} a</p>\n`,
4+
};

0 commit comments

Comments
 (0)