A short story about an XSS in chat.mozilla.org (CVE-2021-21320)
The usercontent iframe used for file previews accepted a postMessage whose origin was taken from a URL parameter, and it rendered the message's blob without checking its content type. An attacker could send an HTML blob to execute script in the chat.mozilla.org origin, though the victim had to open the previewed image in a new tab.
Published on gccybermonks.com
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.
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.
Context
chat.mozilla.org is a webchat maintained by Matrix.org. The client is open source, and the researcher began by reading the source rather than probing the live application blindly. The feature that mattered was file upload: after a file is chosen, the application shows a preview of the uploaded image or file. That preview is rendered inside an iframe, and the normal flow is that the user clicks the upload button, the file is turned into a blob, and the blob content is sent to the server at upload time.
The interesting part of the design is how the preview iframe receives its content. The application sends a postMessage to the iframe to create the blob, and the same iframe is used to display the preview. That message channel, and the way the iframe decided whether to trust a message, is where the bug lived.
Setup
No accounts, roles or special tooling are described beyond reading the open source client and running a proof of concept in a browser. The relevant code is in matrix-react-sdk at tag v3.14.0, file src/usercontent/index.js. The proof of concept is an HTML page that opens the chat's usercontent endpoint and posts a message into it.
Finding it
The researcher tried several things against the upload functionality itself without success. Attention moved to the preview mechanism: the preview is an iframe, and the parent page communicates with it through postMessage. Reading the iframe-side code showed that the origin check was not a constant. It was read from the query string of the iframe's own URL:
const params = window.location.search.substring(1).split('&');
let lockOrigin;
for (let i = 0; i < params.length; ++i) {
const parts = params[i].split('=');
if (parts[0] === 'origin') lockOrigin = decodeURIComponent(parts[1]);
}
The origin parameter of the iframe URL becomes lockOrigin, the value that incoming messages are compared against. Because the attacker controls the URL used to open the iframe, the attacker also controls the value the iframe will accept as a trusted sender. The check that follows is therefore only as strong as the URL:
window.onmessage = function(e) {
if (e.origin === lockOrigin) {
if (e.data.blob) remoteRender(e);
else remoteSetTint(e);
}
};
If the message carries a blob, it is passed to remoteRender. The message handler does not inspect the blob's content-type before the content is turned into an object URL:
a.href = window.URL.createObjectURL(data.blob);
That is the core of the XSS: attacker-supplied bytes, with an attacker-chosen MIME type, become an object URL inside the chat's origin, which the user can open.
Exploitation
Step 1: satisfy the origin check with the URL
The iframe accepts messages whose origin equals the origin query parameter of its own URL. The proof of concept opens the iframe with that parameter set to the attacker's own domain, so messages sent from the attacker page pass the comparison. The URL used is:
In the payload this is built from document.domain of the page hosting the exploit, so the encoded origin matches wherever the exploit is served from.
Step 2: send a blob with an HTML content type
The attacker page posts a message containing a Blob whose content is a script and whose type is text/html. The iframe's handler sees e.data.blob, the origin matches, and the blob is turned into an object URL. Once the user opens that URL in a new tab, the script executes in the context of chat.mozilla.org.
Step 3: deal with the download attribute
The one friction point is user interaction. The anchor used for the preview carries a download attribute:
a.download = data.download;
Because of that attribute, a plain left click downloads the blob instead of navigating to it. The researcher's workaround is to make the preview itself instruct the user: an image saying "open this image in a new tab" is supplied through the imgSrc field of the same message, and the code assigns it directly:
img.src = data.imgSrc;
The user then right-clicks the image and opens it in a new tab, which navigates to the blob URL and runs the HTML.
Step 4: the full proof of concept
The final page opens the iframe on click, waits, then posts the blob and the instruction image:
<html>
<head>
</head>
<body>
<center>Click anywhere in the page</center>
<script>
let x;
onclick = () => {
x = open("https://chat.mozilla.org/usercontent/?origin=https%3a%2f%2f"+document.domain);
setTimeout(() => {
const payload = new Blob(["<script>alert(1337)<\/script>"], { type : "text/html" });
x.postMessage({ blob: payload, imgSrc: "https://i.imgur.com/CMq55u9.png", auto: true}, "*");
}, 2500);
}
</script>
</body>
</html>
The click handler opens the usercontent endpoint with the attacker origin in the query string. After a 2.5 second delay, it posts an object with three fields: blob (the HTML payload), imgSrc (the instruction image) and auto. The message target origin is *, which is fine here because the receiving side performs its own check, and that check is the one the attacker has already satisfied through the URL.
Here you can see the PoC video: https://www.youtube.com/watch?v=nmio2rTn38Q
Root cause
The iframe that renders user content took its trusted origin from user input: the origin query parameter of its own URL. Any page that can get the victim to open that URL with its own origin in the parameter can then send messages the iframe will accept. The second half of the mistake is that the accepted message's blob was converted to an object URL without validating its content-type, so HTML supplied by the attacker is rendered as HTML in the chat's origin. The download attribute on the anchor is the only thing standing between the payload and automatic execution, and it is bypassed by a right-click and "open in new tab", which the attacker can prompt for with the preview image.
Impact
An attacker who can get a victim to load the proof of concept page and follow the instruction can execute JavaScript in the origin of chat.mozilla.org. The report does not describe what an attacker could reach with that execution, such as session data or other users' messages, so the practical reach beyond script execution in the chat origin is not stated. The attack requires user interaction: a click on the attacker page, and a right-click followed by opening the image in a new tab.
Fix
The vulnerability was fixed on 2021-03-01, before the bounty was awarded on 2021-03-08. The report does not describe the change itself.
Timeline
| Date | Event |
|---|---|
| 2021-02-13 | Vulnerability reported |
| 2021-02-19 | Mozilla assigned a CVE ID |
| 2021-03-01 | Vulnerability fixed |
| 2021-03-08 | $500 bounty awarded |
Variants to test
- Look for
postMessagehandlers that comparee.originagainst a value read fromlocation.search,location.hashor a referrer. A positive result is that a page you control, opened with its own origin in that parameter, has its messages accepted. - Look for
createObjectURLcalls on blobs received throughpostMessage, especially where the blob type is taken from the message. A positive result is that HTML you supply renders and executes in the target origin. - Check whether preview or sandbox iframes carry a
downloadattribute or similar navigation blocker. A positive result is that a right-click, open in new tab, or a direct navigation reaches the blob URL. - Check whether the message handler accepts extra fields that set element attributes, such as an image source, which can be used to display attacker-controlled instructions to the victim.
- Test whether the iframe URL can be opened cross-origin without a user gesture, which would remove the first click from the chain.
What happened, in plain words
A security researcher found a bug in chat.mozilla.org, the web chat that Mozilla runs and that is built and maintained by the people behind Matrix.org. The bug let an attacker run their own JavaScript code inside the chat website's own identity, in the browser of a user who clicked the page and opened the preview image in a new tab. This kind of bug is called cross-site scripting, or XSS for short.
To understand why that matters, imagine a bank teller who only checks whether the person at the counter is wearing the bank's uniform. If anyone can buy that uniform, the check is worthless. In a browser, a website's identity is called its origin: the combination of protocol, hostname and port, such as https://chat.mozilla.org. Code that runs in that origin is treated by the browser as if the website itself wrote it. It can read the page, act as the logged-in user, and talk to the site's servers with the user's credentials. XSS is the trick of getting your own code to run as if it came from the site.
The researcher found three XSS bugs in Mozilla products during a month of bug hunting with friends at a house they called @duph0use. This post covers only one of them. The bug was reported on 2021-02-13, Mozilla assigned it a CVE identifier on 2021-02-19, it was fixed on 2021-03-01, and a $500 bounty was paid on 2021-03-08. A CVE is a public catalogue number for a known vulnerability, so people can refer to the same bug unambiguously. A bounty is money a company pays a researcher for reporting a security problem responsibly.
Reading the source instead of guessing
chat.mozilla.org is a web chat: you open it in a browser, log in, and exchange messages with other people. The program that runs in your browser for this is called the client. The client is open source, which means its code is published for anyone to read. The researcher started by reading that code rather than poking at the live site blindly. This is a common and efficient approach: the source tells you exactly how the site is built, so you can spot weak spots before you try anything.
The feature that turned out to matter was file upload. When you pick a file to send in the chat, the application shows you a preview of the image or file after the upload. That preview is not drawn directly on the main page. It is rendered inside an iframe.
An iframe is a page inside a page. Think of it as a small television embedded in a wall: the wall is the main page, and the television shows a separate document with its own address. Browsers treat the iframe's content as a separate browsing context, which is exactly why sites use iframes for showing content they do not fully trust, such as files uploaded by users.
The normal flow is simple: the user clicks the upload button, the chosen file is turned into a blob, and the blob's content is sent to the server when the upload happens. A blob (binary large object) is a chunk of raw data that the browser holds in memory and can treat like a file, without it existing on disk. It can be text, an image, anything, and it carries a label saying what type it is, such as text/html or image/png.
The interesting design decision was how the preview iframe received its content. The main page sends a postMessage to the iframe to create the blob, and the same iframe is used to display the preview.
postMessage is the browser's built-in way for two windows or frames to talk to each other, even when they come from different sites. One side calls postMessage with some data and a target origin, and the other side receives it through an onmessage handler. Because messages can come from anywhere, the receiving side is supposed to check who sent them. That check, and the way the iframe decided whether to trust a message, is where the bug lived.
Finding the flaw
The researcher tried several things against the upload functionality itself and got nowhere. Attention then moved to the preview mechanism. The preview is an iframe, and the parent page talks to it through postMessage. Reading the code that runs inside the iframe showed that the origin check was not a fixed value baked into the program. It was read from the query string of the iframe's own URL.
The query string is the part of a URL after the question mark, used to pass parameters, for example ?origin=https%3a%2f%2fexample.com. Here is the code that reads it:
const params = window.location.search.substring(1).split('&');
let lockOrigin;
for (let i = 0; i < params.length; ++i) {
const parts = params[i].split('=');
if (parts[0] === 'origin') lockOrigin = decodeURIComponent(parts[1]);
}
Line by line:
const params = window.location.search.substring(1).split('&');takes the query string of the current page (window.location.search), drops the leading?withsubstring(1), and splits the rest on&so eachname=valuepair becomes one item in an array calledparams.let lockOrigin;declares a variable that will hold the origin the iframe decides to trust. It starts out empty.- The
forloop walks through every parameter in the array, one at a time. const parts = params[i].split('=');splits the current parameter on=, soorigin=somethingbecomes['origin', 'something'].if (parts[0] === 'origin') lockOrigin = decodeURIComponent(parts[1]);checks whether the parameter's name isorigin. If it is, the value is URL-decoded (so%3abecomes:) and stored inlockOrigin.
The origin parameter of the iframe URL becomes lockOrigin, the value that incoming messages are compared against. Because the attacker controls the URL used to open the iframe, the attacker also controls the value the iframe will accept as a trusted sender. The check that follows is therefore only as strong as the URL:
window.onmessage = function(e) {
if (e.origin === lockOrigin) {
if (e.data.blob) remoteRender(e);
else remoteSetTint(e);
}
};
Line by line:
window.onmessage = function(e) {registers a function that the browser calls whenever a message arrives at this window. The message object is namede.if (e.origin === lockOrigin) {compares the origin of the sender (e.origin, filled in by the browser, not by the sender) with the value read from the URL. If they match, the message is treated as trusted.if (e.data.blob) remoteRender(e);checks whether the message carries a field calledblob. If it does, the whole message is passed to a function calledremoteRender.else remoteSetTint(e);otherwise, the message is passed toremoteSetTint, which handles the other kind of message.
If the message carries a blob, it is passed to remoteRender. The message handler does not inspect the blob's content-type before the content is turned into an object URL:
a.href = window.URL.createObjectURL(data.blob);
Line by line:
a.href = ...sets the destination of a link element, nameda, to whatever comes after the equals sign.window.URL.createObjectURL(data.blob)asks the browser to make a temporary URL that points at the blob's data. The browser hands back something likeblob:https://chat.mozilla.org/..., which is a real address the browser can navigate to.
That is the core of the XSS: attacker-supplied bytes, with an attacker-chosen MIME type, become an object URL inside the chat's origin, which the user can open. A MIME type is the label that tells the browser what kind of content something is, such as text/html for a web page or image/png for a picture. If the browser is told a blob is text/html, it will render it as a web page, scripts and all, and because the blob URL belongs to chat.mozilla.org, those scripts run with the chat's identity.
Exploitation
Step 1: satisfy the origin check with the URL
The iframe accepts messages whose origin equals the origin query parameter of its own URL. The proof of concept opens the iframe with that parameter set to the attacker's own domain, so messages sent from the attacker page pass the comparison. The URL used is:
In the payload this is built from document.domain of the page hosting the exploit, so the encoded origin matches wherever the exploit is served from. document.domain is a property the browser exposes that holds the hostname of the current page. The %3a%2f%2f parts are URL-encoded colons and slashes, so https:// can travel safely inside a query string without confusing the URL parser.
Step 2: send a blob with an HTML content type
The attacker page posts a message containing a Blob whose content is a script and whose type is text/html. The iframe's handler sees e.data.blob, the origin matches, and the blob is turned into an object URL. Once the user opens that URL in a new tab, the script executes in the context of chat.mozilla.org.
Step 3: deal with the download attribute
The one friction point is user interaction. The anchor used for the preview carries a download attribute:
a.download = data.download;
Line by line:
a.download = ...sets thedownloadattribute on the link element. When a link has this attribute, clicking it makes the browser save the target as a file instead of opening it as a page.data.downloadis the value taken from the incoming message, so the message controls the suggested file name.
Because of that attribute, a plain left click downloads the blob instead of navigating to it. The researcher's workaround is to make the preview itself instruct the user: an image saying "open this image in a new tab" is supplied through the imgSrc field of the same message, and the code assigns it directly:
img.src = data.imgSrc;
Line by line:
img.src = ...sets the address of an image element, namedimg, to whatever comes after the equals sign.data.imgSrcis the value taken from the incoming message, so the message controls which picture is shown.
The user then right-clicks the image and opens it in a new tab, which navigates to the blob URL and runs the HTML. A right-click and "open in new tab" is a normal browser action that bypasses the download behaviour, because the browser navigates to the address directly instead of following the link's download instruction.
Step 4: the full proof of concept
The final page opens the iframe on click, waits, then posts the blob and the instruction image:
<html>
<head>
</head>
<body>
<center>Click anywhere in the page</center>
<script>
let x;
onclick = () => {
x = open("https://chat.mozilla.org/usercontent/?origin=https%3a%2f%2f"+document.domain);
setTimeout(() => {
const payload = new Blob(["<script>alert(1337)<\/script>"], { type : "text/html" });
x.postMessage({ blob: payload, imgSrc: "https://i.imgur.com/CMq55u9.png", auto: true}, "*");
}, 2500);
}
</script>
</body>
</html>
Line by line:
<center>Click anywhere in the page</center>shows a short instruction to the victim. The whole page is a lure: the victim is told to click, and the click starts the attack.let x;declares a variable that will hold a reference to the new window.onclick = () => {runs the following code whenever the victim clicks anywhere on the page.x = open("https://chat.mozilla.org/usercontent/?origin=https%3a%2f%2f"+document.domain);opens a new window at the chat'susercontentendpoint, with theoriginparameter set to the attacker's own domain. The+document.domainpart glues the current page's hostname onto the end, so the encoded origin matches wherever the exploit is hosted.setTimeout(() => {waits before sending the message, so the new window has time to load.const payload = new Blob(["<script>alert(1337)<\/script>"], { type : "text/html" });builds a blob whose content is a tiny HTML page containing a script that shows an alert box with the number 1337. The type is set totext/html, so the browser will treat it as a web page. The\/inside the string is just an escaped slash, so the HTML parser does not see a closing script tag too early.x.postMessage({ blob: payload, imgSrc: "https://i.imgur.com/CMq55u9.png", auto: true}, "*");sends a message to the new window. The message has three fields:blob(the HTML payload),imgSrc(the instruction image) andauto. The second argument,"*", means "deliver this to whatever origin the target window has". That is fine here because the receiving side performs its own check, and that check is the one the attacker has already satisfied through the URL.}, 2500);closes the timeout, so the message is sent 2.5 seconds after the click.}closes the click handler.
The click handler opens the usercontent endpoint with the attacker origin in the query string. After a 2.5 second delay, it posts an object with three fields: blob (the HTML payload), imgSrc (the instruction image) and auto. The message target origin is *, which is fine here because the receiving side performs its own check, and that check is the one the attacker has already satisfied through the URL.
Here you can see the PoC video: https://www.youtube.com/watch?v=nmio2rTn38Q
Root cause
The iframe that renders user content took its trusted origin from user input: the origin query parameter of its own URL. Any page that can get the victim to open that URL with its own origin in the parameter can then send messages the iframe will accept. The second half of the mistake is that the accepted message's blob was converted to an object URL without validating its content-type, so HTML supplied by the attacker is rendered as HTML in the chat's origin. The download attribute on the anchor is the only thing standing between the payload and automatic execution, and it is bypassed by a right-click and "open in new tab", which the attacker can prompt for with the preview image.
Impact
An attacker who can get a victim to load the proof of concept page and follow the instruction can execute JavaScript in the origin of chat.mozilla.org. The report does not describe what an attacker could reach with that execution, such as session data or other users' messages, so the practical reach beyond script execution in the chat origin is not stated. The attack requires user interaction: a click on the attacker page, and a right-click followed by opening the image in a new tab.
Fix
The vulnerability was fixed on 2021-03-01, before the bounty was awarded on 2021-03-08. The report does not describe the change itself.
Timeline
| Date | Event |
|---|---|
| 2021-02-13 | Vulnerability reported |
| 2021-02-19 | Mozilla assigned a CVE ID |
| 2021-03-01 | Vulnerability fixed |
| 2021-03-08 | $500 bounty awarded |
Variants to test
- Look for
postMessagehandlers that comparee.originagainst a value read fromlocation.search,location.hashor a referrer. A positive result is that a page you control, opened with its own origin in that parameter, has its messages accepted. - Look for
createObjectURLcalls on blobs received throughpostMessage, especially where the blob type is taken from the message. A positive result is that HTML you supply renders and executes in the target origin. - Check whether preview or sandbox iframes carry a
downloadattribute or similar navigation blocker. A positive result is that a right-click, open in new tab, or a direct navigation reaches the blob URL. - Check whether the message handler accepts extra fields that set element attributes, such as an image source, which can be used to display attacker-controlled instructions to the victim.
- Test whether the iframe URL can be opened cross-origin without a user gesture, which would remove the first click from the chain.
Glossary
- XSS (cross-site scripting): a bug where an attacker gets their own code to run inside another website's origin, so the browser treats it as the site's own code.
- Origin: the browser's identity for a website, made of protocol, hostname and port, such as
https://chat.mozilla.org. - CVE: a public catalogue number assigned to a known vulnerability so people can refer to it unambiguously.
- Bounty: money a company pays a researcher for reporting a security problem responsibly.
- Open source: software whose code is published for anyone to read.
- Client: the program that runs in your browser and talks to a server.
- iframe: a page embedded inside another page, with its own address and its own browsing context.
- Blob: a chunk of raw data the browser holds in memory and can treat like a file, carrying a label saying what type it is.
- MIME type / content-type: the label that tells the browser what kind of content something is, such as
text/htmlorimage/png. - postMessage: the browser's built-in way for windows and frames to send messages to each other, even across sites.
- onmessage: the handler a window registers to receive messages sent with
postMessage. - Query string: the part of a URL after the question mark, used to pass parameters.
- URL encoding: replacing characters such as
:and/with%3aand%2fso they can travel safely inside a URL. - createObjectURL: a browser function that makes a temporary URL pointing at a blob's data.
- download attribute: an attribute on a link that makes the browser save the target as a file instead of opening it.
- document.domain: a browser property holding the hostname of the current page.
- Proof of concept (PoC): a small demonstration that shows a bug is real and can be triggered.
- User interaction: a step where the victim has to do something, such as click, for the attack to work.
- Root cause: the underlying mistake in the code that makes the bug possible.
- Fix: the change made to the software to close the vulnerability.