GitHub advisory · PyPI
Mistune: Quadratic-time DoS in parse_link_href via unbounded parenthesis scan on empty-label links
Description
Fixed in Mistune 3.3.4. Upgrade to 3.3.4 or later.
< 3.3.43.3.4The reported payload was reproduced against the 3.3.3 release tag and verified to scale approximately linearly against the 3.3.4 release tag.
The following report describes the issue before the fix.
parse_link_href in mistune performs an unbounded parenthesis-balancing scan that runs to end-of-document on each unmatched (. Because the empty label [] bypasses the no_close_bracket_before high-water-mark guard, a single small document of the form "[](" * N causes O(N) openers to each drive an O(N) scan, producing O(N²) CPU on the default mistune.html() renderer with no plugins.
parse_link matches the empty label [] (parse_link_label("[](",1) returns ('', 2)), so it reaches the ( branch and calls parse_link_href. That function's scan loop (helpers.py:143-159) advances past characters and increments level on each (, never finding an unescaped ) at level==0, so it runs to EOF and returns None. The cursor then advances only ~2 chars to the next [](. The no_close_bracket_before high-water mark that keeps the balanced-bracket link path linear is never entered because the empty label matches before that branch; _mark_no_link_before only advances past the 2-char label, not the href region.
A single unauthenticated request rendering untrusted markdown pins a worker at 100% CPU for tens of seconds. Measured on default mistune.html, no plugins: 6KB->0.80s, 12KB->3.15s, 24KB->12.5s, 48KB->49.8s (2x input -> ~3.9x time, clean O(n²)). This is the most severe of the mistune quadratic-DoS findings.
import mistune
md = mistune.create_markdown() # default HTML renderer, no plugins
md("[](" * 16000) # ~48KB input stalls a worker ~50s
```…
Manual fix · PyPI
pip install --upgrade "mistune>=3.3.4"Sources