Mutation XSS via copy-and-paste in WYSIWYG editors
Originally disclosed on Mozilla
- Fixed in 3 days
- Paid in 27 days
- Disclosed after 3 days
I've been recently analyzing copying and pasting in browsers and found a bug similar to bug 1599181, but this one allows a mutation XSS on pasting. The webpage needs to follow some specific pattern to be vulnerable but I could find some quite easily.
The root of the issue is exactly the same as it was in bug 1599181, that is serializing of CSS. To recap, if pasted content contains <style> tag, Firefox decides to sanitize it. The stylesheet is pasted verbatim if it doesn't contain any "dangerous" rules (like @import), but it is rewritten if it does. Consider the following example:
<style>
@import'';
@font-face { font-family: 'ab<\/style><img src onerror=alert(1)>'}
</style>
Because of @import, after pasting, the code would be sanitized to:
<style>
@font-face { font-family: "ab</style><img src onerror=alert(1)>"; }
</style>
Please note how <\/style> was transformed to </style>. This doesn't introduce any issue by itself because in the case above, the text node of the stylesheet is modified directly, hence the </style> inside doesn't close the tag. However, if the application does something akin to:
textEditor.innerHTML = clipboardData.innerHTML;
then it is vulnerable to XSS, because the <img> tag would leave the <style> tag. It was my assumption that certain applications might do that and by quick googling I found two WYSIWYG editors who do exactly this. So perhaps there are even more.
Proof of Concept
- Go to https://jsbin.com/xivapasere/1/edit?html,output.
- Press
copy me. - Go to https://rawgit.com/alohaeditor/Aloha-Editor/hotfix/src/demo/boilerplate/ (one of the editors that's vulnerable to this mutation XSS).
- Give Aloha Editor a while to load...
- Go to any
contenteditablecontent. - Paste from clipboard.
- Alert fires!
I don't consider it a bug in Aloha Editor, more of a bug in Firefox itself. To understand why alert fired, have a look at https://rawgit.com/alohaeditor/Aloha-Editor/hotfix/src/plugins/common/paste/lib/paste-plugin.js - line 274. What the editor does is that it paste the content in an invisible <div> (called $clipboard or $CLIPBOARD in the code).
Then, using jQuery, the code is parsed as HTML:
if (typeof content === 'string') {
$content = $('<div>' + content + '</div>');
} else if (content instanceof $) {
$content = $('<div>').append(content);
}
In the PoC content is equal to "<style>@font-face { font-family: \"ab</style><img src onerror=alert(1)>\"; }</style>", leading to an obvious XSS issue.
I think that perhaps Firefox sanitizer could always escape < to \3c in CSS strings to get rid of the issue?
For the record, I've also reported certain copy-and-paste bugs to Chromium and they decided to go a different way altogether and they just delete <style> tag from pasted content by serializing it into inline style attributes. I think that it significantly reduces the risk. Safari also does the same.
Here's the approach pasted from Chromium patch:
This patch follows the same approach as in WebKit [1]:
- First create a dummy document to insert the markup
- Then computes style and layout in the dummy document
- Re-serialize the dummy document as the markup to be inserted. This
reuses the code path that we serialize a selection range into
clipboard, where we need to serialize element computed style into
inline styles so that the element styles are preserved.
- Make sure all style elements are removed before inserting markup
into document
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is known
A medium-severity XSS bug in DOM: Serializers. Mozilla made it public on 13 December 2019 as CVE-2019-17022.
About XSS
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.