From 61c3fcc1799fd21ef52ce4dbf1d5ca0e5f466ea6 Mon Sep 17 00:00:00 2001 From: Manav | Software Developer Date: Sun, 22 Nov 2026 19:17:01 +0530 Subject: [PATCH 1/3] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Limit=20reference=20d?= =?UTF-8?q?efinitions=20to=20999=20code=20points?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- markdown_it/rules_block/reference.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/markdown_it/rules_block/reference.py b/markdown_it/rules_block/reference.py index 175308f9..5548c504 100644 --- a/markdown_it/rules_block/reference.py +++ b/markdown_it/rules_block/reference.py @@ -28,7 +28,8 @@ def reference(state: StateBlock, startLine: int, _endLine: int, silent: bool) -> labelEnd = None pos = 1 - while pos < maximum: + # A 999-code-point label can close at offset 1000 after the opening '['. + while pos < maximum and pos <= 1000: ch = charCodeAt(string, pos) if ch == 0x5B: # /* [ */ return False From ca9ff6840f5e9a7b8123aca539dfc3e5c245f1ec Mon Sep 17 00:00:00 2001 From: Manav | Software Developer Date: Sun, 22 Nov 2026 19:19:39 +0530 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=A7=AA=20TEST:=20Cover=20reference=20?= =?UTF-8?q?label=20length=20boundaries?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_port/test_reference_labels.py | 95 ++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 tests/test_port/test_reference_labels.py diff --git a/tests/test_port/test_reference_labels.py b/tests/test_port/test_reference_labels.py new file mode 100644 index 00000000..8b70f720 --- /dev/null +++ b/tests/test_port/test_reference_labels.py @@ -0,0 +1,95 @@ +import pytest + +from markdown_it import MarkdownIt +from markdown_it.utils import EnvType + + +@pytest.mark.parametrize( + ("label", "expected_key"), + [ + pytest.param("a" * 999, "A" * 999, id="ascii-999"), + pytest.param("a" * 1000, None, id="ascii-1000"), + pytest.param("😀" * 999, "😀" * 999, id="astral-999"), + pytest.param("😀" * 1000, None, id="astral-1000"), + pytest.param("ß" * 999, "SS" * 999, id="casefold-999"), + pytest.param("ß" * 1000, None, id="casefold-1000"), + pytest.param(r"\]" * 499 + "a", r"\]" * 499 + "A", id="escaped-999"), + pytest.param(r"\]" * 500, None, id="escaped-1000"), + pytest.param( + "a" * 498 + "\n" + "b" * 500, + "A" * 498 + " " + "B" * 500, + id="multiline-999", + ), + pytest.param("a" * 499 + "\n" + "b" * 500, None, id="multiline-1000"), + pytest.param("a" + " " * 998, "A", id="whitespace-999"), + pytest.param("a" + " " * 999, None, id="whitespace-1000"), + ], +) +def test_reference_label_codepoint_limit(label, expected_key): + """Count raw Unicode code points, before case or whitespace normalization.""" + source = f"[{label}]: /target\n" + env: EnvType = {} + tokens = MarkdownIt().parse(source, env) + if expected_key is None: + assert env == {} + assert [token.type for token in tokens] == [ + "paragraph_open", + "inline", + "paragraph_close", + ] + assert tokens[1].content == source[:-1] + else: + assert tokens == [] + assert env == { + "references": { + expected_key: { + "title": "", + "href": "/target", + "map": [0, label.count("\n") + 1], + } + } + } + + +@pytest.mark.parametrize("inline_definitions", [False, True]) +@pytest.mark.parametrize("prefix", ["", "> ", "- "]) +@pytest.mark.parametrize("length", [999, 1000]) +def test_reference_label_limit_in_containers(prefix, length, inline_definitions): + """The same boundary applies inside lists and blockquotes, with source maps.""" + md = MarkdownIt("commonmark", {"inline_definitions": inline_definitions}) + env: EnvType = {} + tokens = md.parse(f"{prefix}[{'a' * length}]: /target\n", env) + accepted = length == 999 + assert bool(env.get("references")) is accepted + definitions = [token for token in tokens if token.type == "definition"] + assert len(definitions) == int(accepted and inline_definitions) + if accepted: + assert env["references"]["A" * length]["map"] == [0, 1] + if definitions: + assert definitions[0].map == [0, 1] + + +@pytest.mark.parametrize("inline_definitions", [False, True]) +def test_oversized_label_does_not_create_duplicate_reference(inline_definitions): + """An invalid long label must not affect later definitions or duplicate maps.""" + oversized = "ok" + " " * 998 + source = ( + f"[ok]: /first\n\n[{oversized}]: /discard\n\n" + "[ok]: /second\n\n[short]: /last\n\n[ok] [short]\n" + ) + md = MarkdownIt("commonmark", {"inline_definitions": inline_definitions}) + env: EnvType = {} + tokens = md.parse(source, env) + assert env == { + "references": { + "OK": {"title": "", "href": "/first", "map": [0, 1]}, + "SHORT": {"title": "", "href": "/last", "map": [6, 7]}, + }, + "duplicate_refs": [ + {"title": "", "href": "/second", "label": "OK", "map": [4, 5]} + ], + } + assert any(token.content == f"[{oversized}]: /discard" for token in tokens) + assert md.render(source).endswith( + '

ok short

\n' + ) From abbc3d6b38a6e51379e689ad7a7895b27683f400 Mon Sep 17 00:00:00 2001 From: Manav | Software Developer Date: Sun, 22 Nov 2026 19:21:50 +0530 Subject: [PATCH 3/3] =?UTF-8?q?=F0=9F=93=9A=20DOCS:=20Note=20the=20referen?= =?UTF-8?q?ce-definition=20label=20limit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c411d126..5e74acfb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## Unreleased + +### 🐛 Fixed + +* Reject link-reference definition labels longer than 999 Unicode code points, before case and whitespace normalization, matching CommonMark and markdown-it 15.0.3. + ## 4.3.0 - 2026-11-10 This release fixes two quadratic-time parsing paths that let crafted input stall rendering for minutes (security),