The 10 000 Handlebars Hack How Email Templates Led To Server Takeover
A Shopify app allowed shop owners to customise email templates with Handlebars, and user input was evaluated server-side. An attacker could inject a template that executed arbitrary code on the server, leading to full server compromise.
Published on infosecwriteups.com
While studying advanced template injection techniques, I came across one of the most fascinating bug bounty stories I’ve ever encountered. Researcher Mahmoud’s journey to discover a Handlebars template injection vulnerability in Shopify’s Return Magic app reads like a digital detective novel, complete with…
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 used Handlebars, a template engine that allows function calls, to render user-controlled email templates without sandboxing, so template expressions could reach JavaScript's Function constructor and execute code.
Impact
Attackers could run any system commands, read environment variables and credentials, and take over the application server, with potential lateral movement.
What the attacker needed
Access to edit email templates in the app
The fix
The app switched from Handlebars to the logic-less Mustache engine, and Handlebars later disabled constructor access.
How it was found
- Enter a simple template expression in an email template and receive a test email showing an object.
- Realise the engine is Handlebars, not Mustache, because function calls are possible.
- Use template helpers to overwrite Object.prototype.toString so it returns executable code.
- Call the Function constructor, which now uses the poisoned toString to run the code.
- Read environment variables to confirm code execution.
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 engine lets you put placeholders like {{name}} in a document, and the server fills them in. Here, the engine was too powerful: it let the placeholder run JavaScript code. So someone editing an email template could make the server run their own commands.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.