Skip to content
RCE Disclosed report Intermediate CVE-2019-5413

Code Injection Vulnerability in morgan Package

The morgan logging middleware builds its format string using the Function() constructor, so a format string containing crafted characters can execute arbitrary JavaScript. An attacker who can control the format parameter, for example through a prototype pollution flaw, could run code on the server.

cris_semmle Follow

Originally disclosed on HackerOne

Medium 6.8 View original
  • Triaged in 35 days
  • Fixed in 53 days
  • Disclosed after 3 months

I would like to report a code injection vulnerability in morgan. It allows an attacker to inject arbitrary JS commands in certain situations.

Module

module name: morgan version: 1.9.0 npm page: https://www.npmjs.com/package/morgan

Module Description

HTTP request logger middleware for node.js

Named after Dexter, a show you should not watch until completion.

Module Stats

1,120,329 downloads in the last week

Vulnerability

Vulnerability Description

An attacker can use the format parameter to inject arbitrary commands

Steps To Reproduce:

The basic attack vector looks like this:

JavaScript
var morgan = require('morgan');
var f = morgan('25 \\" + console.log(\'hello!\'); +  //:method :url :status :res[content-length] - :response-time ms');
f({}, {}, function () {
});

However, it is hard to believe that the package is used this way in any application. However, a more interesting attack vector is when combining this vulnerability with a prototype pollution one:

JavaScript
var morgan = require('morgan');
//payload delivered through a prototype pollution attack
Object.prototype[':method :url :status :res[content-length] - :response-time ms'] = '25 \\" + console.log(\'hello!\'); +  //:method :url :status :res[content-length] - :response-time ms';
//benign looking usage of morgan that can be exploited due to the prototype pollution attack
var f = morgan(':method :url :status :res[content-length] - :response-time ms');
f({}, {}, function () {
});

Eval and it's variants like Function() should almost neve be used in such popular packages.

Patch

N/A remove the usage of Function().

Wrap up

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

Impact

If combined with a prototype pollution attack this vulnerability is very serious (RCE). Otherwise, it is very unlikely that the attacker can control the vulnerable format parameter, but not impossible to think.

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 package passes the user-supplied format string into the Function() constructor, which evaluates it as JavaScript code.

Impact

If combined with a prototype pollution attack, this could lead to remote code execution; otherwise, exploitation requires the attacker to control the format parameter, which the report calls very unlikely.

What the attacker needed

Attacker controls the format parameter; or a prototype pollution flaw exists

The fix

The Function() constructor should be removed so the format string is never evaluated as code.

How it was found

  1. Load the morgan module in a Node.js script.
  2. Call morgan with a format string that contains injected JavaScript.
  3. Invoke the returned middleware with dummy request and response objects.
  4. The injected code is intended to run when the middleware is invoked.

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.

morgan is a library that writes web request logs for Node.js applications. It turns the log format text into a JavaScript function using a built-in feature called Function(), which runs text as code. If an attacker can influence that format text, they can slip in their own code and make the server run it.

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.