RCE in Ruby using Mustache Templates
A Ruby web application rendered user-controlled Mustache templates server-side, and the template tags were not restricted, so an attacker could chain Ruby methods on objects. By impersonating a PagerDuty webhook, the author used the constantize method to reach ActiveRecord models and read or delete all customer data.
Published on web.archive.org
The Mustache syntax is touted as being “logic-less templates”; however, when client-provided templates are rendered server side, there are some unexpected side effects.
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Not written yet. The AI write-up for this write-up has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely the application rendered user-supplied Mustache templates on the main web app instance without restricting which tags or variables could be used, exposing Ruby objects and method chaining.
Impact
The author could read any record from the database and could also delete all data, though methods needing arguments such as update were blocked by the template validator.
What the attacker needed
Unauthenticated webhook endpoint; ability to impersonate PagerDuty; ActiveSupport present in environment
The fix
The program introduced a safelist of template tags allowed in communication templates, and the author suggests rendering in a sandbox, filtering variables, or using the JavaScript library instead.
How it was found
- Find the unauthenticated PagerDuty webhook endpoint and impersonate the PagerDuty service with arbitrary content.
- Supply a Mustache tag that renders the value of context to confirm Ruby objects are exposed.
- Use method chaining with constantize to reach an arbitrary Ruby class such as Time.
- Create a PagerDuty incident named after an ActiveRecord model and update the template to chain constantize, all and first.
- Invoke the fake webhook and refresh the public page to see a database record returned.
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 write-up has not been written yet. The Original tab links to the source.
A template is a page with blanks that get filled in with data before it is shown. Here the blanks were filled in by the user, and the template engine could also look up and call parts of the Ruby program itself. That let the author reach the database and read or wipe records.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.