diff --git a/CHANGELOG.md b/CHANGELOG.md index c411d126..efa7d98b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## Unreleased + +### 🐛 Fixed + +* 🐛 Prevent linkification in link or image labels from stalling or raising a parser progress error, restoring the missing markdown-it 14.1.0 progress check ([#470](https://github.com/executablebooks/markdown-it-py/issues/470)). + ## 4.3.0 - 2026-11-10 This release fixes two quadratic-time parsing paths that let crafted input stall rendering for minutes (security), diff --git a/markdown_it/rules_inline/linkify.py b/markdown_it/rules_inline/linkify.py index 3669396e..403391b8 100644 --- a/markdown_it/rules_inline/linkify.py +++ b/markdown_it/rules_inline/linkify.py @@ -36,6 +36,10 @@ def linkify(state: StateInline, silent: bool) -> bool: return False url: str = link.url + # An invalid match must not leave the parser at the same position or earlier. + if len(url) <= len(proto): + return False + # disallow '*' at the end of the link (conflicts with emphasis) url = url.rstrip("*") diff --git a/tests/test_linkify.py b/tests/test_linkify.py index c1905399..85502d20 100644 --- a/tests/test_linkify.py +++ b/tests/test_linkify.py @@ -5,6 +5,54 @@ pytest.importorskip("linkify_it") +@pytest.mark.parametrize( + "scheme,prefix_length", + [("http", 8), ("http", 9), ("https", 9), ("https", 10), ("ftp", 7), ("ftp", 8)], +) +@pytest.mark.parametrize( + "source,expected", + [ + ("{prefix}[{url}", "{prefix}[{link}"), + ("{prefix}[{url}]", "{prefix}[{link}]"), + ( + "{prefix}[{url}](https://example.org)", + '{prefix}{url}', + ), + ( + "{prefix}![{url}](image.png)", + '{prefix}{url}', + ), + ], + ids=["unclosed", "closed", "link", "image"], +) +def test_linkify_in_link_label(scheme, prefix_length, source, expected): + """Link-label lookahead must not stall on a stale pending scheme (#470).""" + md = MarkdownIt("gfm-like") + prefix = "a" * prefix_length + url = f"{scheme}://s://" + link = f'{scheme}://s://' + text = source.format(prefix=prefix, url=url) + expected = expected.format(prefix=prefix, url=url, link=link) + assert md.renderInline(text) == expected + assert md.render(text) == f"

{expected}

\n" + + +@pytest.mark.parametrize("preset", ["commonmark", "gfm-like"]) +def test_link_label_with_linkify_disabled(preset): + """The progress check must not require linkifying ordinary label text.""" + md = MarkdownIt(preset, {"linkify": False}) + text = "abcdefgh[http://s://" + assert md.renderInline(text) == text + + +def test_linkify_inside_emphasis() -> None: + """Preserve normal linkification and trailing emphasis delimiters.""" + md = MarkdownIt("gfm-like") + assert md.renderInline("*https://example.org/*") == ( + 'https://example.org/' + ) + + def test_token_levels(): mdit = MarkdownIt(options_update={"linkify": True}).enable("linkify") tokens = mdit.parse("www.python.org")