Two renderers, one escape

An XSS in Rocket.Chat's markdown parser via nested code and images

Rocket.Chat parsed inline code and image-link markdown as separate passes. Nesting one inside the other let an href value escape its attribute, opening a path to session-stealing script injection.

Hexfield editorial

TargetRocket.Chat chat platform
Bug classStored XSS via markdown nesting
ReportedJuly 28, 2017
Fixed inRocket.Chat 0.58.0
Web
  1. 01

    Inspect

    Nesting an inline code span inside an image link's alt text produced visibly malformed HTML instead of either feature's normal output, with the inline code left unparsed and the href value split across two attribute contexts.

  2. 02

    Hypothesize

    If the second rendering pass misreads where one attribute ends, the attacker-controlled text might land as a new attribute name and, potentially, a new attribute value inside the broken tag.

  3. 03

    Test

    Supplying a value after the URL confirmed a controllable attribute value, and probing character by character mapped the exact escaping rules: no spaces, no closing parenthesis, quotes and ampersands entity-escaped.

  4. 04

    Solve

    Show that a hover event handler in the constrained attribute runs script from a single chat message, then report the parser bug and the readable session storage.

Trigram plot of HTML markup bytes, showing the tight repeating clusters that tag and attribute syntax produce against open text
Markup has a narrow, repetitive trigram signature compared to prose, which is part of why a parser that handles two markdown passes separately can be pushed into malformed output without obviously broken syntax.

Rocket.Chat is an open-source team chat platform, used here as the group’s own day-to-day messaging tool, which is how the bug was first noticed rather than through a planned audit. Rocket.Chat messages support Markdown formatting, and two of its features, inline code spans and image links, turned out not to compose safely when nested inside each other.

The first odd output

Inline code, written as `code`, renders as a <code> element. An image link, written as ![title](http://url.to.image), renders as an anchor wrapping a styled <div> whose background is set to the given URL, with the bracketed text becoming the anchor’s title attribute. Each works as expected alone. Writing them nested, as ![`foo`](http://bar), produced garbled output instead of either expected form, which was the first sign that two separate rendering passes were stepping on each other.

Reading the broken markup

Comparing the HTML each piece of syntax produces alone makes the interaction clear. Inline code renders predictably:

<code class="code-colors inline">s</code>

A plain image link renders as:

<a href="http://bar" title="foo" target="_blank">
  <div class="inline-image" style="background-image: url(http://bar);"></div>
</a>

Nested together, ![`foo`](http://bar) instead produced:

<a href="&lt;a href=" http:="" bar"="" target="_blank" rel="noopener noreferrer">
  http://bar</a>" title="`foo`" target="_blank">
  <div class="inline-image" style="background-image: url(http://bar);"></div>

The inline code span inside the alt text was left unparsed, visible verbatim as `foo` further down the malformed tag, while the href value had been run through a second, separate pass that treated the plain URL http://bar as if it were itself markdown needing autolinking, wrapping it in a complete nested anchor tag and splitting the original attribute in the process. The practical effect: the content that should have been a single href attribute value was now split across two attribute contexts, and the second of those, the attribute name that got invented when the nested anchor’s own href="..." text spilled outside its intended quotes, was directly under the message author’s control.

Why the constraints were not enough

That observation raises one question: if the attribute name is controllable, is the attribute value controllable too? A harmless test value showed that it was, partially. The parser appended a quote entity to every value, refused spaces, escaped double quotes and ampersands, and the markdown syntax itself ruled out a closing parenthesis.

Constraints like these look like a defence, but they are a filter on characters, not on meaning. An attacker who controls the name of an attribute can choose an event handler, and browsers fire handlers such as onmouseover on an anchor without any click. JavaScript also offers more than one string delimiter and its own comment syntax, so a value that cannot contain a double quote or a space can still be valid code. The forced suffix the parser added could be neutralised in the same way. In other words, the escaping protected the HTML attribute syntax while leaving the JavaScript inside it fully expressive.

Proof of execution came from a message that redirected the browser when hovered. A chat client is a good target for hover-triggered code: messages fill most of the screen, and a long, padded message makes an accidental hover almost certain.

What the bug exposed

The impact depended on where the client kept its secrets. Rocket.Chat stored the logged-in user’s ID and login token in localStorage, and mirrored them into cookies without the HttpOnly flag. Any script running in the page could therefore read a full session credential, and a single hovered message could send it to another host as an ordinary-looking image request. Viewing the wrong message was enough to lose the account, and the only visible symptom was a brief reload of an image.

Two independent weaknesses combined here: a parser that produced attribute syntax from user input, and session storage that script could read. Fixing either one would have reduced the bug from account takeover to a nuisance, which is the practical argument for defence in depth in chat software.

Disclosure and fix

The issue was reported to Rocket.Chat’s security team on July 28, 2017, and a fix landed on July 31, 2017, shipping to users in the Rocket.Chat 0.58.0 release on August 16, 2017. The underlying fix separates the two markdown passes so that content already consumed by one renderer, such as an inline code span, cannot be re-interpreted by a later autolinking pass operating on the same string.

What transfers to other challenges

The general lesson is about composition, not about Rocket.Chat specifically: a markdown or templating pipeline with several independent passes over the same string is only as safe as the least safe pass, and nesting one construct inside another is the cheapest way to find where two passes disagree about what has already been escaped. A parser that is individually correct for inline code and individually correct for autolinks can still produce unescaped output when one feeds the other’s result back through a second interpretation step. Testing nested combinations of every pair of supported markdown features, not just each feature alone, is a low-effort way to find this class of bug in similar chat or forum renderers. For a non-text way of telling markup apart from other structured formats at a glance, see binary data visualization.

Questions readers ask

Which two markdown features combined to cause this bug?

Inline code, written between backticks, and image-link syntax, written as an exclamation mark followed by alt text in brackets and a URL in parentheses. Rendered separately each produced safe output. Nesting the inline code syntax inside the image's alt text broke the image renderer's attribute escaping.

What could an attacker control in the resulting markup?

The attribute name and, with some restrictions, its value, inside the malformed anchor tag the parser produced. Spaces, closing parentheses, and the double-quote and ampersand characters were not available, which ruled out a plain onload-style payload but still allowed an event handler such as onmouseover with a single-quoted JavaScript string.

How did the payload reach stored session data?

Rocket.Chat kept the session's user ID and login token in both local storage and in cookies that were not marked HTTP-only, so JavaScript running in the page could read them directly and forward them to an external URL, which is the basis of a session hijacking payload built from the same injection.

How was the payload made harder to notice?

Rather than redirecting the browser, which is immediately visible, the payload replaced the Rocket.Chat logo image already present in the page with a new one pointed at an attacker-controlled URL carrying the stolen tokens as query parameters, so the only visible effect was a brief reload of that one image.

New to capture the flag? Formats, teams and a first-month plan in one place.

Read the CTF guide