Rails ActionView sanitize helper bypass leading to XSS using SVG tag.
The Rails sanitize helper could be bypassed when the svg and use tags were allowed, because a use element could reference a base64-encoded SVG data URI containing an onerror handler. An attacker could then execute script in the context of the application, leading to data theft and actions on behalf of other users.
Originally disclosed on HackerOne
- Fixed in 21 days
- Paid in 21 days
- Disclosed after 46 days
Summary by the program
###Summary
Loofah >= 2.1.0, < 2.19.1 is vulnerable to cross-site scripting via the image/svg+xml media type in data URIs.
###Mitigation
Upgrade to Loofah >= 2.19.1.
###Severity
The Loofah maintainers have evaluated this as Medium Severity 6.1.
###References
CWE - CWE-79: Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting') (4.9) SVG MIME Type (image/svg+xml) is misleading to developers · Issue #266 · w3c/svgwg https://hackerone.com/reports/1694173
The report
In the specific configuration, it was possible to bypass HTML sanitization by using the use tag of the SVG element.
In the index.html.erb:
<%= sanitize "<svg><use href=\"data:image/svg+xml;base64,PHN2ZyBpZD0neCcgeG1sbnM9J2h0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnJyB4bWxuczp4bGluaz0naHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluaycgd2lkdGg9JzEzMzcnIGhlaWdodD0nMTMzNyc+CjxpbWFnZSBocmVmPSIxIiBvbmVycm9yPSJhbGVydCh3aW5kb3cub3JpZ2luKSIgLz4KPC9zdmc+#x\"/></svg>", tags: %w(svg use) %>
use tag allows to embed another base64 encoded SVG containing target XSS payload, base64 after decoding:
<svg id='x' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' width='1337' height='1337'>
<image href="1" onerror="alert(window.origin)" />
</svg>
SVG and use tags had to be allowed either in global configuration config.action_view.sanitized_allowed_tags = ['svg', 'use']
or inline with tags argument of the helper.
Impact
XSS could lead to data theft through the attacker’s ability to manipulate data through their access to the application, and their ability to interact with other users, including performing other malicious attacks, which would appear to originate from a legitimate user. These malicious actions could also result in reputational damage for the business through the impact on customers’ trust.
Attachments

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 allowed the SVG use element to reference a data URI without inspecting the embedded SVG content, so an event handler inside it survived filtering.
Impact
XSS could lead to data theft and let the attacker act as the victim, though only when the svg and use tags were explicitly allowed.
What the attacker needed
Svg and use tags allowed in sanitizer; attacker can supply HTML to sanitize
The fix
Upgrade Loofah to version 2.19.1 or later.
How it was found
- Allow the svg and use tags in the sanitizer configuration or inline.
- Pass an SVG use element whose href is a base64-encoded SVG data URI containing an image with an onerror handler.
- Render the sanitized output in the page so the browser loads the embedded SVG and fires the handler.
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.
A web page can include a small image written as text, called a data URI. The sanitizer is meant to remove dangerous parts of user-supplied HTML, but it did not inspect the image hidden inside the SVG use tag. That hidden image contained a script that runs when it fails to load, so the attacker's code ran in the victim's browser.
What is XSS?
Script an attacker supplies runs in another user's browser, on the target's origin, with that user's session.