mirror of
https://github.com/markedjs/marked.git
synced 2026-11-30 16:48:05 +01:00
fix: reject unbalanced parentheses in link destinations (#4107)
This commit is contained in:
@@ -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
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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>
|
||||
@@ -0,0 +1,11 @@
|
||||
[a]: /url(x
|
||||
|
||||
[b]: /u)rl
|
||||
|
||||
[c]: /url\(x
|
||||
|
||||
[d]: <(url>
|
||||
|
||||
[e]: /url(x)
|
||||
|
||||
[a] [b] [c] [d] [e]
|
||||
@@ -5,3 +5,5 @@
|
||||
<p><a href="foo(bar()">link</a></p>
|
||||
|
||||
<p>[link](foo(bar\())</p>
|
||||
|
||||
<p>[link](foo(bar)</p>
|
||||
|
||||
@@ -5,3 +5,5 @@
|
||||
[link](foo(bar\())
|
||||
|
||||
[link](foo(bar\\())
|
||||
|
||||
[link](foo(bar)
|
||||
|
||||
Reference in New Issue
Block a user