Repository navigation
Fix quadratic parsing of nested image wikilinks - #1186
Merged
Merged
Conversation
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.
Collaborator
|
It looks good to me, thanks! |
Collaborator
|
For future security (including DoS attacks) vulnerabilities, please fill a private report in the recently enabled option of the 'Security and quality' tab. |
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 |
notriddle
approved these changes
Oct 29, 2026
4 tasks done
This was referenced Nov 2, 2026
This was referenced Nov 2, 2026
Closed
Closed
This was referenced Nov 3, 2026
13 of 15 tasks
This was referenced Nov 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
ENABLE_WIKILINKS,handle_wikilinkbuilds a wikilink's target from the raw text of its body:scan_wikilink_pipewalks it looking for|, thencontains('\0')andfrom_replace_nulswalk it again. Once a wikilink is created,disable_all_linksonly disablesLinkentries, 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 followed by]]× n already took 0.01 s on main at the same size. With footnotes on,[^a]: xfollowed 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 throughOptions::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.txtalready 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_suffixis set, each sample appends a new balanced chunk, so the nesting depth never grows and![[/]]scores as linear even on main.