From 12f55c2809507efe0a5741b9ec7fcb8338ff5c56 Mon Sep 17 00:00:00 2001 From: Heberti Almeida Date: Thu, 30 Oct 2025 12:18:52 -0300 Subject: [PATCH] Don't allow link destinations with unbalanced unescaped parentheses. See https://github.com/commonmark/commonmark.js/issues/177 --- src/inlines.c | 7 ++++--- test/regression.txt | 12 ++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/inlines.c b/src/inlines.c index 5fee895d6..fc6c09bb0 100644 --- a/src/inlines.c +++ b/src/inlines.c @@ -490,8 +490,9 @@ static int scan_delims(cmark_parser *parser, subject *subj, unsigned char c, *can_close = right_flanking && (!left_flanking || cmark_utf8proc_is_punctuation(after_char)); } else if (c == '\'' || c == '"') { - *can_open = left_flanking && !right_flanking && - before_char != ']' && before_char != ')'; + *can_open = left_flanking && + (!right_flanking || before_char == '(' || before_char == '[') && + before_char != ']' && before_char != ')'; *can_close = right_flanking; } else { *can_open = left_flanking; @@ -1118,7 +1119,7 @@ static bufsize_t manual_scan_link_url_2(cmark_chunk *input, bufsize_t offset, } } - if (i >= input->len) + if (i >= input->len || nb_p != 0) return -1; { diff --git a/test/regression.txt b/test/regression.txt index 0ec493d3d..25873629d 100644 --- a/test/regression.txt +++ b/test/regression.txt @@ -237,6 +237,18 @@ Issue commonmark#526 - unescaped ( in link title

[link](url ((title))

```````````````````````````````` +Issue commonmark.js#177 - Unbalanced parentheses in link URLs should not be parsed as links + +```````````````````````````````` example +[link](/u(ri ) +[link](/u(ri +) +. +

[link](/u(ri ) +[link](/u(ri +)

+```````````````````````````````` + Issue commonamrk#517 - script, pre, style close tag without opener.