GFM autolink extension can crash the process or hang it on small untrusted Markdown
With the autolink extension on, a paragraph of email addresses can blow the stack and kill the whole process, and a link ending in lots of closing brackets hangs it.
- comrak
- Affected
<= 0.54.0Patched0.55.0
Published · 2 min read
Summary
With the GFM autolink extension enabled, two paths in src/parser/autolink.rs scale badly on ordinary untrusted Markdown. One overflows the stack and aborts the whole process, the other is quadratic in CPU. The extension is off in comrak's bare defaults but on in the usual GFM/GitHub-style setup, including the commonmarker gem, so both are reachable wherever comrak renders user-submitted Markdown.
Email autolinking recurses without a depth limit
process_email_autolinks (autolink.rs:12) matches the first email in the text, copies everything after it into a new node with remain.to_string(), then calls itself on that remainder (autolink.rs:102). A paragraph of N bare emails recurses N deep. cmark-gfm does the same work in a loop, so it doesn't have this problem.
A Rust stack overflow is an abort, not a catchable panic, so this kills the process. The threshold depends on stack size: about 30,000 emails (~210 KB) on the default 8 MB main thread, but only about 2,000 (~14 KB) on a 512 KB worker thread of the kind a server pool uses.
use comrak::{markdown_to_html, Options};
fn main() {
let md = "a@b.co ".repeat(30_000);
let mut o = Options::default();
o.extension.autolink = true;
markdown_to_html(&md, &o); // thread has overflowed its stack -> SIGABRT
}
The per-level copy also makes it quadratic in time on the way to the crash.
Trailing parens are quadratic
autolink_delim (autolink.rs:310) strips trailing punctuation off a detected URL. For every trailing ) it recounts every parenthesis in the link from the start (the loop at autolink.rs:360), then drops one byte and repeats, so N trailing ) cost O(N^2).
Input is "http://a.b" + ")".repeat(n). Release build, autolink = true: 64 KB = 0.9s, 128 KB = 3.4s, 256 KB = 13.6s, a clean 4x per doubling. The same URL with trailing . stays linear, which isolates the paren branch. www. links hit the same code.
Impact
Availability only. Any endpoint rendering untrusted Markdown with autolink on can be crashed outright, or pinned single-threaded, by well under a megabyte. The crash is the worse of the two, since the caller can't catch it.