Skip to content

Fix: wikilink with pipe separator leaks child node's next chain into sibling content - #1102

Merged
Martin1887 merged 5 commits into
pulldown-cmark:mainfrom
wesc:fix-wikilink-termination-handling
Jun 3, 2026
Merged

Martin1887 merged 5 commits into
pulldown-cmark:mainfrom
wesc:fix-wikilink-termination-handling

Conversation

@wesc

@wesc wesc commented May 26, 2026

Copy link
Copy Markdown
Contributor

handle_wikilink creates a cycle/DAG in the tree when a pipe separator is present, causing exponential event-count growth.

When a wikilink with a pipe separator is parsed (e.g. [[name|display]]), handle_wikilink sets the wikilink node's child pointer to body_node without terminating body_node's next chain. Because body_node is a reused node whose next chain still threads through cur_ix (the closing ] delimiter) and into the content that follows the ]], the EventIter visits those trailing nodes twice: once as children of the wikilink (via the child pointer) and once as siblings (via the wikilink node's own next pointer).

With repeated adversarial patterns like [[[[^(\n|]]]]=]]]]]]]]\n, this causes super-polynomial event-count growth effectively hanging the parser. The fix adds a short chain-termination walk starting from body_node that finds and severs the next link that would otherwise reach cur_ix, ensuring the child subtree is properly bounded before the wikilink node is installed into the tree.

This bug was discovered via fuzzing.

This PR contains two tests for the behavior and a fix. There are no regressions against other tests.

@wesc
wesc force-pushed the fix-wikilink-termination-handling branch from 26c19f1 to 967d260 Compare May 26, 2026 13:52
@Martin1887
Martin1887 requested a review from notriddle May 26, 2026 14:30
Comment thread pulldown-cmark/src/parse.rs Resolved
Comment thread pulldown-cmark/src/parse.rs Resolved
Comment thread pulldown-cmark/specs/wikilinks.txt Outdated
```````````````````````````````` example_wikilinks
[[[[link|]]]]
.
<p>[[<a href="link">]</a>]]</p>

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m confused. Where did the “]” inside the a element come from?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great catch.. I've been staring at these brackets and am falling into a daze. The bug is in the code I submitted.. the expectation is that scan_nodes_to_ix points to the display text, but in this particular example it points to the first closing ]. I will write some more tests.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the correct rendering for:

[[link|]]

Should we produce <p><a href="link"></a></p> or is that considered an error? We expect

[[|Symbol]]

to pass through and render as-is, so should we also expect [[link|]] to render as is?

@notriddle notriddle May 27, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be addressed in a new commit 65e5fd6

Two tests in wikilinks.txt, one for [[[[foo|bar]]]] and [[foo|]]. It's not in wikilinks.txt but I also verified that [[[[foo|]]]] parses as expected.

wesc and others added 2 commits June 2, 2026 09:55
I was confused about this for awhile, and didn't get why that loop was
needed. It turns out, it's needed because the prev link wasn't set up
right. With the prev link set up right, it can be left out.
Comment thread pulldown-cmark/src/parse.rs Outdated
// Terminate the child chain so it does not extend
// past the closing `]]` into sibling content.
let mut scan = body_node;
loop {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been wondering for awhile why this loop was needed, and, finally, I figured it out.

It shouldn't be needed. This code is supposed to take care of fixing the same problem.

https://github.com/wesc/pulldown-cmark/blob/65e5fd6c2e072fbc7d50c6935dd0d0685b159127/pulldown-cmark/src/parse.rs#L1022-L1024

As long as prev is correctly assigned, you shouldn't need that.

@notriddle

Copy link
Copy Markdown
Collaborator

I've pushed a commit that fixes the prev assignment and removes up the unneeded loop.

@Martin1887
Martin1887 merged commit 17b5263 into pulldown-cmark:main Jun 3, 2026
6 checks passed
@wesc
wesc deleted the fix-wikilink-termination-handling branch June 3, 2026 14:02
@zoosky

zoosky commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

@wesc can you be more specific?

this bug was discovered by fuzzing

@wesc

wesc commented Jun 4, 2026

Copy link
Copy Markdown
Contributor Author

@wesc can you be more specific?

this bug was discovered by fuzzing

I used a fuzzer (cargo-fuzz), which injects a large amount of random input into select functions looking for crashes or pathological behavior. I used it on an downstream library (EmbedAnything) which flagged issues there as well as in pulldown-cmark. So I came here to work on a fix. After I opened the PR, I found this: #1076, and also evidence in this repo that others have been fuzzing.

Fuzzing is producing another crash bug: a buffer overflow and access to invalid memory that, in my tests, only crashes when running the fuzzer. This doesn't seem to affect production builds, but that's just luck. I'll open a separate PR for that but I don't understand the fix that AI has provided just yet so I want to get a handle on it first.

It would probably be a good idea to regularly fuzz this library.. cargo fuzz is finding these issues within a few minutes of running so it's a cheap thing to set up.

This was referenced Oct 29, 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.

4 participants