Repository navigation
Fix: wikilink with pipe separator leaks child node's next chain into sibling content - #1102
Conversation
26c19f1 to
967d260
Compare
| ```````````````````````````````` example_wikilinks | ||
| [[[[link|]]]] | ||
| . | ||
| <p>[[<a href="link">]</a>]]</p> |
There was a problem hiding this comment.
I’m confused. Where did the “]” inside the a element come from?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
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.
| // Terminate the child chain so it does not extend | ||
| // past the closing `]]` into sibling content. | ||
| let mut scan = body_node; | ||
| loop { |
There was a problem hiding this comment.
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.
As long as prev is correctly assigned, you shouldn't need that.
|
I've pushed a commit that fixes the |
|
@wesc can you be more specific?
|
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. |
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.