Skip to content

Fix quadratic parsing of nested image wikilinks - #1186

Merged
notriddle merged 1 commit into
pulldown-cmark:mainfrom
zul5050:nested-image-wikilinks
Oct 29, 2026
Merged

notriddle merged 1 commit into
pulldown-cmark:mainfrom
zul5050:nested-image-wikilinks

Conversation

@zul5050

@zul5050 zul5050 commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

With ENABLE_WIKILINKS, handle_wikilink builds a wikilink's target from the raw text of its body: scan_wikilink_pipe walks it looking for |, then contains('\0') and from_replace_nuls walk it again. Once a wikilink is created, disable_all_links only disables Link entries, so every enclosing ![[ stays live on the wikilink stack. On ![[![[![[…]]]]]] each of those openers then resolves in turn and rescans everything inside it, which is quadratic in the nesting depth. The event count stays tiny, because in the unpiped case each enclosing image replaces its body with a fresh text node, so all of that work is thrown away.

Release build of the CLI with -W -d, on one line of ![[ × n followed by ]] × n:

n input main this branch
25,000 125 KB 0.45 s 0.01 s
50,000 250 KB 1.81 s < 0.01 s
100,000 500 KB 7.26 s < 0.01 s

[[ × n followed by ]] × n already took 0.01 s on main at the same size. With footnotes on, [^a]: x followed by ![[^a] × 50,000 and ] × 50,000 goes from 0.73 s to under 0.01 s. So any service that renders untrusted Markdown with wikilinks enabled, including through Options::all(), can be slowed down this way with a few hundred KB of input. That makes it an algorithmic-complexity DoS.

The fix clears the wikilink stack after a wikilink is created. That applies the rule wikilinks.txt already states for links ("the deepest one takes precedence"), and [[x|![[a]]]] already behaved that way. It does change the output for an image wikilink that contains another wikilink: ![[![[dog.png]]]] used to render <img src="!%5B%5Bdog.png%5D%5D" alt="![[dog.png]]" /> and now renders ![[<img src="dog.png" alt="dog.png" />]]. The new spec example covers that and the piped form, and it fails on main.

To check that nothing else moved, I rendered 300,000 random short inputs built from ![[, [[, ]], |, footnote references, inline images and link reference definitions with both builds, each under wikilinks only, Options::all() and no options. 830 of the 900,000 renders changed, all with wikilinks on and all with a second wikilink opener nested inside an ![[. The full suite passes, along with the simd, serde and no_std jobs.

I didn't add a dos-fuzzer regression. When repeating_pattern_after_suffix is set, each sample appends a new balanced chunk, so the nesting depth never grows and ![[ / ]] scores as linear even on main.

handle_wikilink builds a wikilink's target from the raw text of its
body. After creating one it only disabled `Link` openers, so every
enclosing `![[` stayed on the wikilink stack, resolved in turn and
rescanned all of the text inside it. `![[` x n followed by `]]` x n
took quadratic time.

Clear the wikilink stack once a wikilink is created, so the deepest
one takes precedence, as wikilinks.txt already says for links.
@Martin1887
Martin1887 requested a review from notriddle October 28, 2026 15:42
@Martin1887

Copy link
Copy Markdown
Collaborator

It looks good to me, thanks!

@Martin1887

Copy link
Copy Markdown
Collaborator

For future security (including DoS attacks) vulnerabilities, please fill a private report in the recently enabled option of the 'Security and quality' tab.

@zul5050

zul5050 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Got it, anything like this from me will go through the private report in the 'Security and quality' tab from now on, as the new SECURITY.md describes.

@notriddle
notriddle merged commit 29d6cfa into pulldown-cmark:main Oct 29, 2026
6 checks passed
This was referenced Nov 2, 2026
This was referenced Nov 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants