Skip to content
Prototype pollution Write-up Advanced

EJS - Server Side Prototype Pollution gadgets to RCE

The EJS templating library trusts the prototype of the options object passed to it, so if an application lets an attacker pollute Object.prototype, EJS picks up attacker-controlled options. Setting the client and escapeFunction options causes attacker code to be placed into the compiled template function and evaluated, giving remote code execution.

kevin_mizu Follow

Published on mizu.re

Last month (February 2023), I took a look into NodeJS HTML templating libraries. During my research, I found an interesting Server Side Prototype Pollution (SSPP) gadget in the EJS library which can be leveraged to RCE.

Mizu (@kevin_mizu), on mizu.re

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 EJS reads options from a user-supplied object without protecting against an inherited (polluted) prototype, and the client and escapeFunction options are concatenated into evaluated template source without sanitisation.

Impact

An attacker who can pollute the prototype can execute arbitrary commands on the server, and Express applications using EJS are vulnerable by default once such pollution exists.

What the attacker needed

An app-level prototype pollution bug; app uses EJS via Express res.render

The fix

EJS should not trust inherited properties on the options object, for example by copying only own properties or using null-prototype objects for user data.

How it was found

  1. Pollute Object.prototype with client and escapeFunction values through an application-level prototype pollution bug.
  2. Request a route that calls res.render so EJS compiles a template using the polluted options.
  3. EJS inserts the escapeFunction value into the generated function source and evaluates it, running the attacker's command.

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.

Prototype pollution is when an attacker sneaks extra properties onto a shared base object that many other objects inherit from. EJS, a library that builds HTML pages from templates, reads its settings from such an object, so polluted settings get used. One setting is inserted as code into the template function, which is then run, so the attacker's code executes on the server.

What is Prototype pollution?

Input changes JavaScript's shared object prototype, altering behaviour across the application, sometimes to XSS or code execution.

More by kevin_mizu

View all

4 more disclosed reports, best first.

Similar reports

View all

Same bug class and techniques, clearest first.

Prototype pollution

The lesson behind this report.

Input changes JavaScript's shared object prototype, altering behaviour across the application, sometimes to XSS or code execution.