GitHub advisory · PyPI
Mistune: Quadratic-time DoS in inline image parsing (precedence_scan) bypasses not-is-image guards
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.
mistune's inline image parsing reaches precedence_scan for ![ openers because the two guards that keep the link path linear are both gated behind not is_image. A small default-renderer document of the form "![" * N + "x" + "]" * N produces O(N²) CPU with no plugins.
For ![ openers, is_image is True, so both linearizing guards are skipped: if not is_image and pos <= state.no_link_before (inline_parser.py:141) and has_nested_link = not is_image and _label_contains_link(...) (:171). Execution reaches precedence_scan (:179) which runs sc.search(state.src, mark_pos, end_pos) over the whole image body — O(n) per outer opener, x O(n) openers -> O(n²). max_image_depth never fires because the failing scans never produce an image token (image_depth stays 0).
A single unauthenticated request pins a worker at 100% CPU. Measured on default mistune.html, no plugins: 6KB->0.28s, 12KB->1.07s, 24KB->4.20s, 48KB->15.8s (2x input -> ~3.8x time).
import mistune
md = mistune.create_markdown() # default, no plugins
md("![" * 16000 + "x" + "]" * 16000)
"!["*N + "x" + "]"*N (no plugins). Guard: none on input size. Bypass proof: no global cap.not is_image guards at inline_parser.py:141 (no_link_before) and :171 (_label_contains_link). Guard: high-water mark + nested-link short-circuit. Bypass proof: is_image is True for ![, so BOTH guards are gated out; max_image_depth (:checked after) measured flat…Manual fix · PyPI
pip install --upgrade "mistune>=3.3.4"Sources