From 78a82a0c4a04abdc96e0448e9863fd8fe0ba9d04 Mon Sep 17 00:00:00 2001 From: Nash Aschenbath Date: Tue, 24 Nov 2026 20:00:58 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Reject=20escaped=20contro?= =?UTF-8?q?ls=20in=20link=20destinations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 6 ++++ markdown_it/helpers/parse_link_destination.py | 10 +++++- .../test_link_destination_controls.py | 36 +++++++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/test_port/test_link_destination_controls.py diff --git a/CHANGELOG.md b/CHANGELOG.md index c411d126..18a4c888 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Change Log +## Unreleased + +### 🐛 Fixed + +* Reject backslash-prefixed line endings in angle-wrapped link destinations and ASCII controls in bare destinations. + ## 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/helpers/parse_link_destination.py b/markdown_it/helpers/parse_link_destination.py index c98323c0..7f5bb26e 100644 --- a/markdown_it/helpers/parse_link_destination.py +++ b/markdown_it/helpers/parse_link_destination.py @@ -33,6 +33,8 @@ def parseLinkDestination(string: str, pos: int, maximum: int) -> _Result: return result if code == 0x5C and pos + 1 < maximum: # \ + if charCodeAt(string, pos + 1) == 0x0A: + return result pos += 2 continue @@ -55,8 +57,14 @@ def parseLinkDestination(string: str, pos: int, maximum: int) -> _Result: break if code == 0x5C and pos + 1 < maximum: - if charCodeAt(string, pos + 1) == 0x20: + next_code = charCodeAt(string, pos + 1) + if next_code == 0x20: break + if next_code is not None and (next_code < 0x20 or next_code == 0x7F): + # A control character cannot be escaped into a destination. + # Keep the literal backslash, then let the control end it. + pos += 1 + continue pos += 2 continue diff --git a/tests/test_port/test_link_destination_controls.py b/tests/test_port/test_link_destination_controls.py new file mode 100644 index 00000000..325ac6c8 --- /dev/null +++ b/tests/test_port/test_link_destination_controls.py @@ -0,0 +1,36 @@ +from itertools import product + +from markdown_it import MarkdownIt + + +def test_angle_destination_cannot_escape_a_line_ending() -> None: + for prefix, newline in product(("", "!"), ("\n", "\r\n", "\r")): + source = prefix + "[link]()" + result = MarkdownIt("commonmark").render(source) + assert " None: + for prefix, control in product(("", "!"), ("\n", "\t", "\x01", "\x7f")): + source = prefix + "[link](foo\\" + control + "bar)" + result = MarkdownIt("commonmark").render(source) + assert " None: + for separator in ("\n", "\t"): + source = "[link](foo\\" + separator + ")" + assert ( + MarkdownIt("commonmark").render(source) + == '

link

\n' + ) + + +def test_punctuation_escapes_and_tabs_in_angle_destinations_still_work() -> None: + md = MarkdownIt("commonmark") + assert md.render(r"[link](a\(b\))") == '

link

\n' + assert ( + md.render("[link]()") == '

link

\n' + )