GitHub Security Advisory: GHSA-5pq7-52mg-hr42
Package: httparty (RUBYGEMS)
Severity: MODERATE (CVSS 6.5)
CWE: CWE-472: External Control of Assumed-Immutable Web Parameter
Vulnerable versions: <= 0.20.0
Patched in: 0.21.0

Summary: httparty has multipart/form-data request tampering vulnerability

Details:
### Impact
I found "multipart/form-data request tampering vulnerability" caused by Content-Disposition "filename" lack of escaping in httparty.

`httparty/lib/httparty/request` > `body.rb` > `def generate_multipart`

https://github.com/jnunemaker/httparty/blob/4416141d37fd71bdba4f37589ec265f55aa446ce/lib/httparty/request/body.rb#L43

By exploiting this problem, the following attacks are possible

* An attack that rewrites the "name" field according to the crafted file name, impersonating (overwriting) another field.
* Attacks that rewrite the filename extension at the time multipart/form-data is generated by tampering with the filename

For example, this vulnerability can be exploited to generate the following Content-Disposition.

> Normal Request example:
> normal input filename: `abc.txt`
> 
> generated normal header in multipart/form-data
> `Content-Disposition: form-data; name="avatar"; filename="abc.txt"`
 
> Malicious Request example
> malicious input filename: `overwrite_name_field_and_extension.sh"; name="foo"; dummy=".txt`
> 
> generated malicious header in multipart/form-data:
> `Content-Disposition: form-data; name="avatar"; filename="overwrite_name_field_and_extension.sh"; name="foo"; dummy=".txt"`

The Abused Header has multiple name ( `avatar` & `foo` ) fields and the "filename" has been rewritten from `*.txt` to `*.sh` .

These problems can result in successful or unsuccessful attacks, depending on the behavior of the parser receiving the request.
I have confirmed that the attack succeeds, at least in the following frameworks

 * Spring (Java)
 * Ktor (Kotlin)
 * Ruby on Rails (Ruby)

The cause of this problem is the lack of escaping of the `"` (Double-Quote) character in Content-Disposition > filename.

WhatWG's HTML spec has an escaping requirement.

https://html.spec.whatwg.org/#multipart-form-data

> For field names and filenames for file fields, the result of the encoding in the previous bullet point must be escaped by replacing any 0x0A (LF) bytes with the byte sequence `%0A`, 0x0D (CR) with `%0D` and 0x22 (") with `%22`. The user agent must not perform any other escapes.



### Patches

As noted at the beginning of this section, encoding must be done as described in the HTML Spec.

https://html.spec.whatwg.org/#multipart-form-data

> For field names and filenames for file fields, the result of the encoding in the previous bullet point must be escaped by replacing any 0x0A (LF) bytes with the byte sequence `%0A`, 0x0D (CR) with `%0D` and 0x22 (") with `%22`. The user agent must not perform any other escapes.

Therefore, it is recommended that Content-Disposition be modified by either of the following

> Before:
> `Content-Disposition: attachment;filename="malicious.sh";dummy=.txt`

> After:
> `Content-Disposition: attachment;filename="%22malicious.sh%22;dummy=.txt"`

https://github.com/jnunemaker/httparty/blob/4416141d37fd71bdba4f37589ec265f55aa446ce/lib/httparty/request/body.rb#L43

```
file_name.gsub('"', '%22')
```

Also,