fix: reject unbalanced parentheses in link destinations (#4107)

This commit is contained in:
Bao Nguyen
2026-10-05 09:38:02 -06:00
committed by GitHub
parent 0d2022025d
commit 3363c99921
6 changed files with 27 additions and 1 deletions
+5
View File
@@ -578,6 +578,11 @@ export class _Tokenizer<ParserOutput = string, RendererOutput = string> {
def(src: string): Tokens.Def | undefined {
const cap = this.rules.block.def.exec(src);
if (cap) {
// outside angle brackets a link destination holds only balanced parentheses
if (!this.rules.other.startAngleBracket.test(cap[2]) && findClosingBracket(cap[2], '()') !== -1) {
return;
}
const tag = normalizeLabel(cap[1]).replace(this.rules.other.multipleSpaceGlobal, ' ');
const href = cap[2] ? cap[2].replace(this.rules.other.hrefBrackets, '$1').replace(this.rules.inline.anyPunctuation, '$1') : '';
const title = cap[3] ? cap[3].substring(1, cap[3].length - 1).replace(this.rules.inline.anyPunctuation, '$1') : cap[3];
+2 -1
View File
@@ -153,7 +153,8 @@ export function normalizeLabel(label: string) {
}
export function findClosingBracket(str: string, b: string) {
if (str.indexOf(b[1]) === -1) {
// an opening bracket on its own is still unbalanced, so both have to be absent
if (str.indexOf(b[0]) === -1 && str.indexOf(b[1]) === -1) {
return -1;
}
+5
View File
@@ -0,0 +1,5 @@
<p>[a]: /url(x</p>
<p>[b]: /u)rl</p>
<p>[a] [b] <a href="/url(x">c</a> <a href="(url">d</a> <a href="/url(x)">e</a></p>
+11
View File
@@ -0,0 +1,11 @@
[a]: /url(x
[b]: /u)rl
[c]: /url\(x
[d]: <(url>
[e]: /url(x)
[a] [b] [c] [d] [e]
+2
View File
@@ -5,3 +5,5 @@
<p><a href="foo(bar()">link</a></p>
<p>[link](foo(bar\())</p>
<p>[link](foo(bar)</p>
+2
View File
@@ -5,3 +5,5 @@
[link](foo(bar\())
[link](foo(bar\\())
[link](foo(bar)