GHSL-2021-021: Remote code execution in ejs
The ejs template engine mixes template data with configuration options when used through Express's render API, allowing an attacker to overwrite internal settings. If an application passes user-controlled query parameters directly to res.render, an attacker can set the outputFunctionName option to inject code and achieve remote code execution.
Originally disclosed on GitHub Security Lab
The Express render API was designed to only pass in template data. By allowing template engine configuration options to be passed through the Express render API directly, downstream users of an Express template engine may inadvertently introduce insecure behavior into their applications with impacts ranging from Cross…
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 report has not been written yet. The Original tab links to the source.
How it worked
Root cause
Likely, ejs does not separate template data from engine configuration options when receiving them through Express's render API, allowing user-controlled input to override sensitive settings such as outputFunctionName.
Impact
Remote code execution could occur in downstream applications that pass user-controlled objects to res.render.
What the attacker needed
Application uses ejs with Express; Application passes user-controlled object to res.render
The fix
The project was declared dead by its maintainers, so no fix was provided; applications should avoid passing user-controlled objects directly to res.render.
How it was found
- Set up an Express application that uses ejs and passes req.query directly to res.render.
- Send a request with a query parameter that sets settings[view options][outputFunctionName] to a malicious value.
- The malicious value injects code that executes a system command, such as creating a file.
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.
Template engines like ejs fill in placeholders in web pages. Express lets you pass data to these engines, but ejs also accepts configuration settings in the same object. If a website passes user input directly, an attacker can change a setting that controls how code is generated, leading to code execution on the server.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.