Skip to content
XSS Write-up Intermediate

XSS via postMessage in chat.mozilla.org

The user content preview iframe in chat.mozilla.org validated postMessage origins using user-controlled data, allowing an attacker to send a malicious blob that was rendered without content-type checking. An attacker could execute JavaScript in the context of the chat application after tricking the victim into opening the preview in a new tab.

Guilherme Keerok Follow

Published on keerok.github.io

Bounty$500 View original

In the last month, some friends and I have founded @duph0use, a house where we spent the last month doing bug bounties, researching, and working. At some point during the time I was there, I started searching for bugs in Mozilla, which led me to find 3 XSSes.

Guilherme Keerok (@k33r0k), on keerok.github.io

The full write-up lives on its author's site

It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 4 min read.

Context

While working out of @duph0use, a house some friends and I founded to spend a month doing bug bounties, research and work, I started hunting in Mozilla. That effort produced three XSSes; this post covers one of them.

The target is chat.mozilla.org, a webchat maintained by Matrix.org. The chat client is open source, so I went through the source and found a file upload feature. My attempts against the upload itself went nowhere, but the same feature also previewed images and files after upload, and that preview turned out to be the interesting part.

Previews are rendered inside an iframe. In the normal flow a user clicks the upload button, picks a file, the file is turned into a blob, and on upload the content of that blob is sent to the server. The application creates the blob by sending a postMessage to the iframe, and that same iframe is what displays the image preview.

Setup

The work was source review of the open source Matrix React SDK, specifically src/usercontent/index.js at tag v3.14.0, followed by a proof of concept.

Finding it

Reading src/usercontent/index.js in matrix-react-sdk v3.14.0, the validation of the postMessage origin was done through user input:

https://github.com/matrix-org/matrix-react-sdk/blob/v3.14.0/src/usercontent/index.js#L1

The next part of the code received the postMessage and passed its data to remoteRender:

https://github.com/matrix-org/matrix-react-sdk/blob/v3.14.0/src/usercontent/index.js#L47

On line 49 the message took a blob supplied by the user (e.data.blob) and, without checking the content-type, passed its content to createObjectURL on line 22:

https://github.com/matrix-org/matrix-react-sdk/blob/v3.14.0/src/usercontent/index.js#L22

That combination is what made the attack work: the postMessage origin validation was done through user input, and the blob content was turned into an object URL without any content-type validation.

Exploitation

The one awkward part of this XSS is that it needs user interaction, caused by the download attribute on line 19:

https://github.com/matrix-org/matrix-react-sdk/blob/v3.14.0/src/usercontent/index.js#L19

It is still trivial to satisfy. The victim has to right-click the image and open it in another tab. To prompt exactly that, I made a simple image saying "open this image in a new tab" and passed it through postMessage in the imgSrc attribute (line 40):

https://github.com/matrix-org/matrix-react-sdk/blob/v3.14.0/src/usercontent/index.js#L40

Step 1: deliver a message to the preview iframe

Send a postMessage to the iframe that renders user content, with a blob whose content is the payload. Because the origin check is derived from user input, the message is accepted.

Step 2: blob becomes an object URL

The handler on line 49 reads e.data.blob and passes it to createObjectURL on line 22 without verifying the content-type. The blob content is now reachable as a URL.

Step 3: show the instruction image

The imgSrc attribute (line 40) carries an image telling the victim to open it in a new tab, so the interaction the download attribute forces is easy to obtain.

Or watch on YouTube
From the original on keerok.github.io

Step 4: victim opens the image in a new tab

The victim right-clicks the preview and opens it in another tab.

This is my final PoC:

Here you can see the PoC video: https://www.youtube.com/watch?v=nmio2rTn38Q

Root cause

The iframe that renders user content trusted a postMessage whose origin validation was built from user input, so the check did not establish who sent the message. The receiving code then took e.data.blob and handed its content to createObjectURL without checking the content-type, so arbitrary content became an object URL. The download attribute on line 19 is what forces the extra click, but it only delays execution rather than preventing it.

Impact

An attacker can execute script, provided the victim performs the right-click and open-in-new-tab step. The report does not describe what an attacker could reach with that execution beyond the XSS itself.

Fix

The vulnerability was fixed on 2021-03-01. The report does not describe the code change.

Timeline

  • 2021-02-13 - Reported the vulnerability
  • 2021-02-19 - Mozilla assigned a CVE ID
  • 2021-03-01 - Fixed the vulnerability
  • 2021-03-08 - $500 Bounty awarded

Variants to test

  1. Look for postMessage listeners in preview or sandbox iframes where the origin check compares against a value taken from the message or from user-controlled state. A positive result is a message from an unrelated origin being processed.
  2. Look for createObjectURL calls on message data with no content-type check. A positive result is a blob of your choosing becoming a URL that renders as a document.
  3. Look for download attributes or similar gates on preview elements. A positive result is a preview that only needs a right-click and open-in-new-tab to load attacker content as a top-level page.
  4. Look for imgSrc-style attributes set from message data. A positive result is attacker-chosen image content rendered inside the preview iframe, which is useful for social engineering the required click.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 12 min read.

What happened, in plain words

A security researcher spent a month working with friends at a shared house they founded for bug bounty work, research and general work. During that time he hunted for bugs in Mozilla products and found three cross-site scripting (XSS) issues. This article is about one of them.

The bug was in chat.mozilla.org, a web chat service maintained by Matrix.org. The chat client is open source, meaning anyone can read its code. While reading that code, the researcher found a feature that lets users upload files and then shows a preview of the uploaded image or file. The upload itself resisted his attempts, but the preview turned out to be the weak spot.

In simple terms, the preview was built in a way that let an attacker slip content of their choosing into the preview and get it to run as a script. That is what XSS means: an attacker gets their own code to run as if it were part of the trusted website, so the browser treats it as legitimate. The catch was that the victim had to right-click the preview image and choose to open it in a new tab. That is a small amount of user interaction, and the researcher made it easy by showing an image that literally told the victim to do it.

Mozilla paid a 500 USD bounty for the report. The timeline at the end shows the dates.

Background: the pieces involved

Before walking through the bug, here are the concepts you need. Each is explained from zero.

What an iframe is

A web page can embed another web page inside itself using an element called an iframe (inline frame). Think of it as a small window cut into a wall: through it you see a different room, but the wall (the outer page) and the room (the inner page) are separate. Websites use iframes to show content from elsewhere, such as a video player, an advertisement, or, in this case, a preview of a file the user uploaded.

What a blob is

A blob (binary large object) is a chunk of raw data that a browser holds in memory, as if it were a file, without it necessarily existing on disk. When you pick a file to upload, the browser can read it into a blob, and then send that blob's contents to a server. You can think of a blob as a temporary container for bytes that the page can pass around.

What postMessage is

Web pages sometimes need to talk to each other, especially when one is embedded in an iframe inside the other. Browsers provide a mechanism called postMessage for this: one page sends a message (some data) to another page, and the receiving page listens for it. Because messages can come from anywhere on the internet, the receiver is supposed to check who sent the message. That check is called origin validation. The origin is the combination of scheme, host and port, roughly the site's address, for example https://chat.mozilla.org. If the receiver does not verify the origin properly, it may accept messages from an attacker's website and act on them.

What createObjectURL is

createObjectURL is a browser function that takes a blob and gives back a URL (a web address) that points to that blob's contents. The URL points to that blob's contents and only works inside the browser that created it. It is a way to make in-memory data addressable as if it were a file on a web server. If the blob contains HTML or JavaScript, and the browser later loads that URL as a page, the code inside can run.

What a content-type is

Every piece of content on the web has a content-type (also called a MIME type), a label that says what kind of thing it is: image/png, text/html, application/pdf, and so on. Browsers use this label to decide how to display or handle the content. If a site takes user-supplied bytes and turns them into a URL without checking the content-type, it may end up serving HTML or JavaScript where it expected an image.

What the download attribute is

On a link (an <a> element), the download attribute tells the browser that clicking the link should download the target rather than navigate to it. It is a hint that changes the default behaviour. In this bug, the download attribute on line 19 caused the need for user interaction: the victim had to right-click the image and open it in another tab.

What a proof of concept (PoC) is

A proof of concept is a small demonstration that a vulnerability is real: the minimum steps or code needed to trigger it. Researchers build a PoC so the vendor can reproduce the issue and so the report is convincing.

The target and the feature

The target is chat.mozilla.org, a web chat maintained by Matrix.org. The chat client is open source, so the researcher read its source code. He found a file upload feature. He tried several different things against the upload itself but did not get anywhere. However, the same feature also showed a preview of images and files after upload, and that preview was interesting.

The preview was rendered inside an iframe. In the normal flow, a regular user clicks the upload button, picks a file, the file is turned into a blob, and at the moment of upload the content of that blob is sent to the server. What made the attack possible was that the application created the blob by sending a postMessage to the iframe, and that same iframe was used to preview the images.

Finding it

The researcher read the file src/usercontent/index.js in the open source Matrix React SDK, at version tag v3.14.0. The validation of the postMessage origin was done through user input:

https://github.com/matrix-org/matrix-react-sdk/blob/v3.14.0/src/usercontent/index.js#L1

In plain words: the code that was supposed to check who sent a message was basing that check on something the user (or attacker) could supply. A check that the sender controls is not a check at all. Imagine a bouncer at a door who asks each visitor "are you on the list?" and believes whatever the visitor answers. That is roughly what was happening here.

The next part of the code received the postMessage and passed its data to a function called remoteRender:

https://github.com/matrix-org/matrix-react-sdk/blob/v3.14.0/src/usercontent/index.js#L47

So the message data was handed to code that renders it. Rendering means turning data into something visible or executable in the page. If the data is not checked, whatever the sender put in the message gets rendered.

On line 49, the message took a blob supplied by the user (e.data.blob) and, without checking the content-type, passed its content to createObjectURL on line 22:

https://github.com/matrix-org/matrix-react-sdk/blob/v3.14.0/src/usercontent/index.js#L22

Line by line in plain words:

  • e.data.blob means: take the blob field out of the message that arrived. The sender chooses what goes in that field.
  • The code does not ask "what kind of content is this?" (no content-type check). It just takes the bytes.
  • createObjectURL turns those bytes into a URL. Now the attacker's bytes have a web address inside the chat site's origin.

That combination is what made the attack work: an attacker-controlled message reaches the iframe, the origin check does not actually constrain the sender, and the blob content is turned into an object URL without any type validation.

Exploitation

The one awkward part of this XSS is that it needs user interaction, caused by the download attribute on line 19:

https://github.com/matrix-org/matrix-react-sdk/blob/v3.14.0/src/usercontent/index.js#L19

Because of that attribute, the browser treats the blob URL as something to download, not something to open as a page. The victim has to right-click the image and open it in another tab. That extra click is the user interaction. It is still trivial to satisfy.

To prompt exactly that, the researcher made a simple image saying "open this image in a new tab" and passed it through postMessage in the imgSrc attribute (line 40):

https://github.com/matrix-org/matrix-react-sdk/blob/v3.14.0/src/usercontent/index.js#L40

The researcher passed his instruction image via postMessage in the imgSrc attribute. By putting his instruction image there, the attacker makes the preview display a picture that tells the victim what to do. This is social engineering: manipulating a person rather than a machine.

Step 1: deliver a message to the preview iframe

Send a postMessage to the iframe that renders user content, with a blob whose content is the payload. Because the origin check is derived from user input, the message is accepted. In everyday terms: the attacker posts a parcel to the preview window, and the window's guard asks the parcel who sent it, then believes the answer written on the parcel.

Step 2: blob becomes an object URL

The handler on line 49 reads e.data.blob and passes it to createObjectURL on line 22 without verifying the content-type. The blob content is now reachable as a URL. In everyday terms: the parcel's contents are put on a shelf and given an address label, without anyone checking whether the contents are safe.

Step 3: show the instruction image

The imgSrc attribute (line 40) carries an image telling the victim to open it in a new tab, so the interaction the download attribute forces is easy to obtain.

Or watch on YouTube
From the original on keerok.github.io

This image is the instruction image the researcher created: a simple picture that says "open this image in a new tab", shown inside the preview so the victim knows what to click.

Step 4: victim opens the image in a new tab

The victim right-clicks the preview and opens it in another tab. In everyday terms: the shelf's address label is opened in a fresh browser tab, and because the address belongs to the chat site, the browser runs the attacker's code as if it were the chat site's own code.

This is the final PoC:

Here you can see the PoC video: https://www.youtube.com/watch?v=nmio2rTn38Q

Root cause

The iframe that renders user content trusted a postMessage whose origin validation was built from user input, so the check did not establish who sent the message. The receiving code then took e.data.blob and handed its content to createObjectURL without checking the content-type, so arbitrary content became an object URL. The download attribute on line 19 is what forces the extra click, but it only delays execution rather than preventing it.

In plain words, two mistakes stacked on top of each other: a guard that believed the visitor's own answer, and a shelf that accepted any contents without a label check. Either one alone might have been survivable; together they let an attacker run code in the chat site.

Impact

An attacker can execute script, provided the victim performs the right-click and open-in-new-tab step. The report does not describe what an attacker could reach with that execution beyond the XSS itself.

Fix

The vulnerability was fixed on 2021-03-01. The report does not describe the code change.

Timeline

  • 2021-02-13 - Reported the vulnerability
  • 2021-02-19 - Mozilla assigned a CVE ID
  • 2021-03-01 - Fixed the vulnerability
  • 2021-03-08 - $500 Bounty awarded

Variants to test

If you want to look for similar bugs yourself, here are the patterns to search for. Each is a starting point, not a guarantee.

  1. Look for postMessage listeners in preview or sandbox iframes where the origin check compares against a value taken from the message or from user-controlled state. A positive result is a message from an unrelated origin being processed.
  2. Look for createObjectURL calls on message data with no content-type check. A positive result is a blob of your choosing becoming a URL that renders as a document.
  3. Look for download attributes or similar gates on preview elements. A positive result is a preview that only needs a right-click and open-in-new-tab to load attacker content as a top-level page.
  4. Look for imgSrc-style attributes set from message data. A positive result is attacker-chosen image content rendered inside the preview iframe, which is useful for social engineering the required click.

Glossary

  • XSS (cross-site scripting): a vulnerability where an attacker gets their own code to run inside a trusted website, so the browser treats it as part of that site.
  • Bug bounty: a programme where organisations pay researchers for reporting security vulnerabilities.
  • Open source: software whose source code is publicly available for anyone to read.
  • iframe: an element that embeds one web page inside another.
  • Blob: a chunk of raw data held in the browser's memory, treated like a file.
  • postMessage: a browser mechanism that lets one page send a message to another, especially across iframes.
  • Origin: the combination of scheme, host and port that identifies a site, for example https://chat.mozilla.org.
  • Origin validation: checking that a message really came from the site it claims to come from.
  • createObjectURL: a browser function that turns a blob into a URL pointing at its contents.
  • Content-type (MIME type): a label saying what kind of content something is, such as image/png or text/html.
  • download attribute: an attribute on a link that tells the browser to download the target instead of navigating to it.
  • remoteRender: the function that received the postMessage data.
  • imgSrc: the attribute that tells an image element which image to display.
  • PoC (proof of concept): a minimal demonstration that a vulnerability is real.
  • User interaction: a step the victim must perform for an attack to work, such as a click.
  • Social engineering: manipulating a person into doing something, rather than exploiting a machine.
  • CVE ID: a public identifier assigned to a known vulnerability.
  • Bounty: the money paid for a valid vulnerability report.
  • Same-origin: belonging to the same scheme, host and port, so the browser treats the content as part of the same site.
  • Top-level document: the page shown in a browser tab, as opposed to content embedded inside an iframe.

More by Guilherme Keerok

View all

3 more disclosed reports, best first.

More from Mozilla

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Cross-site scripting (XSS)

The lesson behind this report.

Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.