From c19413b06003ad793e9571604e21d175325f8d87 Mon Sep 17 00:00:00 2001 From: hong4rc Date: Sun, 19 Jul 2026 09:41:20 +0700 Subject: [PATCH] fix: Avoid O(n^2) backtracking in GFM email autolink regexes --- src/rules.ts | 4 ++-- test/specs/redos/quadratic_email_autolink.cjs | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 test/specs/redos/quadratic_email_autolink.cjs diff --git a/src/rules.ts b/src/rules.ts index 6c42de3083..eb1b8d367d 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -473,11 +473,11 @@ const inlineGfm: Record = { delRDelim, url: edit(/^((?:protocol):\/\/|www\.)(?:[a-zA-Z0-9\-]+\.?)+[^\s<]*|^email/) .replace('protocol', _caseInsensitiveProtocol) - .replace('email', /[A-Za-z0-9._+-]+(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/) + .replace('email', /[A-Za-z0-9._+-]{1,64}(@)[a-zA-Z0-9-_]+(?:\.[a-zA-Z0-9-_]*[a-zA-Z0-9])+(?![-_])/) .getRegex(), _backpedal: /(?:[^?!.,:;*_'"~()&]+|\([^)]*\)|&(?![a-zA-Z0-9]+;$)|[?!.,:;*_'"~)]+(?!$))+/, del: /^(~~?)(?=[^\s~])((?:\\[\s\S]|[^\\])*?(?:\\[\s\S]|[^\s~\\]))\1(?=[^~]|$)/, - text: edit(/^(`+|~+|[^`~])(?:(?=[`~])|(?= {2,}\n)|(?=[a-zA-Z0-9.!#$%&'*+\/=?_`{\|}~-]+@)|[\s\S]*?(?:(?=[\\${'a_'.repeat(50000)}

`, + }, + { + markdown: 'a*'.repeat(50000), + html: `

${'aa'.repeat(25000)}

`, + }, +];