Repository navigation
Improve Perf: fail inline links early on deep parentheses in destination or title - #985
heberholanda wants to merge 1 commit into
Conversation
| else if (c == '(' && enclosingCharacter == '(') | ||
| { | ||
| // An unescaped ( is not allowed in a title enclosed in parentheses | ||
| break; |
There was a problem hiding this comment.
This makes a valid link reference definition disappear when the next line is text that starts with ( and has another ( inside:
[a]: /u
(see (x) for more)
[a]main (same as commonmark.js): <p>(see (x) for more)</p> and <p><a href="/u">a</a></p>
this PR: <p>[a]: /u\n(see (x) for more)</p> and <p>[a]</p>
Before, the title parsed as see (x, and TryParseLinkReferenceDefinition went to the "title followed by text" case, which keeps the definition and drops the title. Now the title fails, so it goes to the else { return false; } next to it.
I ran 60k random inputs against commonmark.js: 952 go from right to wrong with this PR. With #986 applied on top (it keeps the definition when a title on the next line does not close) these are all gone, so #986 should go in first, or that part of it could be included here. A test with the input above would guard it.
There was a problem hiding this comment.
Thanks for catching this, and for the commonmark.js comparison. You're right: this is a regression from this PR.
[a]: /u
(see (x) for more)
[a]On main the title parses as see (x and the definition is kept through the "title followed by text" path. With this PR, TryParseTitle now rejects the unescaped ( (correct per spec), so the definition falls into the else { return false; } branch and is lost. That branch already drops definitions on main when a title on the next line doesn't close ([foo]: /url\n(title), which is what #986 fixes; this PR just sends more inputs down that path.
I applied #986 on top of this branch locally: your example, [a]: /u\n(t (x) y)\n\n[a] and [foo]: /url\n(title all keep the definition and match commonmark.js, and [a]: /u (ok) still gets its title.
So this PR shouldn't go in before #986. My plan:
- Once Fix link definition lost to an unclosed title on the next line #986 is merged, rebase this PR on top of it.
- Add your input as a regression test (
[a]: /u\n(see (x) for more)\n\n[a]→<p>(see (x) for more)</p>\n<p><a href="/u">a</a></p>), with and without trivia tracking.
If you'd rather have this PR stand on its own, I can include the same fallback here instead. Let me know which you prefer.
There was a problem hiding this comment.
Thanks for checking it. Rebasing on #986 sounds best to me: one fallback in one place. That is xoofx's call though.
Problem
Parsing an inline link is quadratic for inputs with many unclosed
](openers:"[a](" × N + ")": for each](,LinkHelper.TryParseUrlcounts the nested(of the following openers and scans all the way to the end of the text before failing."[a](u (" × N + ")": the destination is fine, butTryParseTitleaccepts unescaped(inside a title enclosed in parentheses, so every attempt again scans to the end of the text.On current
main, rendering 100,000 characters of either input takes 1–2 seconds. Found while hardening a wiki that renders user content.Fix
Two small changes in
LinkHelper, both following the CommonMark spec and cmark:manual_scan_link_url_2), soTryParseUrlnow fails as soon as more than 32 unescaped(are open. Autolinks (isAutoLink) are not affected.(...)), including a(or)character only if it is backslash-escaped". cmark's scanner does the same ([(] (escaped_char|[^()\x00])* [)]).TryParseTitlenow fails on an unescaped(in a title enclosed in parentheses.The same change is applied to
TryParseUrlTriviaandTryParseTitleTrivia. With both limits, each failed attempt stops after a bounded number of characters, so parsing becomes linear.Measurements
Markdown.ToHtmlwith the default pipeline, best of 3 runs, .NET 9, Release:"[a](" × N + ")""[a](u (" × N + ")"Tests
TestLinkHelper.TestUrlNestedParenthesisLimit: 32 levels are still accepted, 33 are rejected (TryParseUrlandTryParseUrlTrivia).TestLinkHelper.TestTitleUnescapedParenthesis: escaped parentheses in a(...)title are still accepted, an unescaped(is rejected, and(inside a quoted title is still accepted (TryParseTitleandTryParseTitleTrivia).MiscTests.UnclosedInlineLinksAreNotQuadratic: both inputs at 100,000 characters render the expected HTML.The two
TestLinkHelpertests fail without the fix. The whole suite passes on net8.0 and net10.0 (7579 passed, 1 skipped), including the CommonMark spec and roundtrip tests.