Skip to content
RCE Disclosed report Intermediate CVE-2020-8141

Code Injection Vulnerability in dot Package

The dot templating library compiles templates using the Function() constructor, which allows arbitrary JavaScript execution if an attacker controls the template or can pollute Object.prototype. An attacker could achieve code injection or remote code execution under those conditions.

cris_semmle Follow

Originally disclosed on HackerOne

High 7.4 View original
  • Triaged in 35 days
  • Fixed in 4 months
  • Disclosed after 8 months

I would like to report a code injection vulnerability in dot. It allows attackers to execute arbitrary JS code, especially when combined with a prototype pollution attack.

Module

module name: dot version: 1.1.2 npm page: https://www.npmjs.com/package/dot

Module Description

Created in search of the fastest and concise JavaScript templating function with emphasis on performance under V8 and nodejs. It shows great performance for both nodejs and browsers.

doT.js is fast, small and has no dependencies.

Module Stats

76,838 downloads in the last week

Vulnerability

Vulnerability Description

dot uses Function() to compile templates. this can be exploited by the attacker if she can control the template or if she can control the value set on Object.prototype.

Steps To Reproduce:

a) The basic attack vector

JavaScript
var doT = require("dot");
var tempFn = doT.template("<h1>Here is a sample template " +
    "{{=console.log(23)}}</h1>");
tempFn({})

b) in combination with a prototype pollution attack

  • create a folder "resources" and inside that a file called "mytemplate.dot" with the following content:
HTML
<h1>Here is a sample template</h1>
  • in the folder containing the resources folder, create and execute the following js file
JavaScript
var doT = require("dot");
// prototype pollution attack vector
Object.prototype.templateSettings = {varname:"a,b,c,d,x=console.log(25)"};
// benign looking template compilation + application
var dots = require("dot").process({path: "./resources"});
dots.mytemplate();

Even though the template compilation + application looks safe, due to the prototype pollution, the attacker can execute arbitrary commands.

Patch

N/A remove Function() call

Wrap up

  • I contacted the maintainer to let them know: N
  • I opened an issue in the related repository: N

Impact

The attacker can achieve code injection/RCE if she can control the template or if she can set arbitrary properties on Object.prototype. Using Function() with runtime computed values is rarely safe.

The original report as published on HackerOne, by cris_semmle. 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 library uses the Function() constructor to compile templates, which executes arbitrary code when template content or inherited settings are attacker-controlled.

Impact

The attacker can achieve code injection or remote code execution if they can control the template or set arbitrary properties on Object.prototype.

What the attacker needed

Attacker controls template content; or attacker can pollute Object.prototype

The fix

The Function() call should be removed to prevent code execution.

How it was found

  1. Require the dot module in a Node.js script.
  2. Call doT.template() with a template string that contains a JavaScript expression.
  3. Invoke the returned template function with an empty object to trigger execution.
  4. For the prototype pollution variant, create a resources folder with a template file.
  5. Set a property on Object.prototype to inject malicious template settings.
  6. Call doT.process() on the resources folder and then invoke the compiled template to execute code.

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.

The dot library turns templates into JavaScript functions using a built-in feature called Function(), which runs any code it is given. If an attacker can either supply the template text or sneak a setting onto a shared object that all JavaScript objects inherit from, they can make the library run their own code. This is dangerous because it can lead to running commands 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.

More by cris_semmle

View all

8 more disclosed reports, best first.

More from Node.js third-party modules

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.