Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
10 changes: 9 additions & 1 deletion markdown_it/helpers/parse_link_destination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
36 changes: 36 additions & 0 deletions tests/test_port/test_link_destination_controls.py
Original file line number Diff line number Diff line change
@@ -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](<foo\\" + newline + "bar>)"
result = MarkdownIt("commonmark").render(source)
assert "<a " not in result, source
assert "<img " not in result, source


def test_bare_destination_cannot_escape_ascii_controls() -> None:
for prefix, control in product(("", "!"), ("\n", "\t", "\x01", "\x7f")):
source = prefix + "[link](foo\\" + control + "bar)"
result = MarkdownIt("commonmark").render(source)
assert "<a " not in result, source
assert "<img " not in result, source


def test_literal_backslash_can_precede_a_trailing_separator() -> None:
for separator in ("\n", "\t"):
source = "[link](foo\\" + separator + ")"
assert (
MarkdownIt("commonmark").render(source)
== '<p><a href="foo%5C">link</a></p>\n'
)


def test_punctuation_escapes_and_tabs_in_angle_destinations_still_work() -> None:
md = MarkdownIt("commonmark")
assert md.render(r"[link](a\(b\))") == '<p><a href="a(b)">link</a></p>\n'
assert (
md.render("[link](<foo\\\tbar>)") == '<p><a href="foo%5C%09bar">link</a></p>\n'
)
Loading