Skip to content
RCE Disclosed report Advanced

Child process environment injection via prototype pollution

Prototype pollution can set the NODE_OPTIONS environment variable for child processes, allowing injection of arbitrary JavaScript or local files. An attacker who can exploit a prototype pollution vulnerability could execute code in spawned child processes.

coreyfarrell Follow

Originally disclosed on HackerOne

  • Disclosed after 46 days

Summary: prototype pollution causes polluted system environment for child processes.

Description: This can be used to inject arbitrary --require flags to node.js child processes or in the case of current node.js versions it can be used to inject arbitrary JavaScript to child processes.

In practice this would require exploiting an existing prototype pollution vulnerability, https://www.npmjs.com/advisories/1164 for example could allow remote attack if an untrusted handlebars template were processed before spawning child processes.

Steps To Reproduce:

The following code demonstrates that prototype injection is reflected in the environment of child_process spawns.

JavaScript
'use strict';

const {spawnSync} = require('child_process');

// Prototype injection entered directly here for demonstration purposes, normally would be
// accomplished by exploiting a vulnerable npm module, https://www.npmjs.com/advisories/1164
// for example.
({}).__proto__.NODE_OPTIONS = '--require=./malicious-code.js';

// This will execute `./malicious-code.js` before running `subprocess.js`
console.log(spawnSync(process.execPath, ['subprocess.js']).stdout.toString());

// Current versions of node.js can run arbitrary code without needing the malicious-code.js
// to be on the destination file system:
({}).__proto__.NODE_OPTIONS = `--experimental-loader="data:text/javascript,console.log('injection');"`;

// The child process will print `injection` before running subprocess.js
console.log(spawnSync(process.execPath, ['subprocess.js']).stdout.toString());

Creating this script along with a subprocess.js and malicious-code.js that each perform a console.log will demonstrate the effectiveness of this prototype pollution.

Impact

Successful prototype injection on version of node.js which supports --experimental-loader can run any JavaScript code in child processes. Older versions of node.js can only be caused to run arbitrary code that is on the local file system.

This could also be used as a DoS attack if NODE_OPTIONS were set to --bad-flag.

The original report as published on HackerOne, by coreyfarrell. 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

Likely, the child_process module copies inherited properties from the global object prototype into the environment of spawned processes, so a polluted prototype can inject environment variables like NODE_OPTIONS.

Impact

Successful prototype injection on versions of Node.js supporting --experimental-loader can run any JavaScript code in child processes; older versions can only run arbitrary code already on the local file system, and setting NODE_OPTIONS to an invalid flag could cause a denial of service.

What the attacker needed

An existing prototype pollution vulnerability; ability to spawn child processes

How it was found

  1. Set a property on the global object prototype to define NODE_OPTIONS with a --require flag pointing to a malicious file.
  2. Spawn a child process using spawnSync with the Node.js executable and a script.
  3. Observe that the malicious file is executed before the script.
  4. Set NODE_OPTIONS to use --experimental-loader with a data URL containing JavaScript code.
  5. Spawn another child process and observe that the injected JavaScript runs.

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.

Prototype pollution is a bug where an attacker can add properties to a base object that all other objects inherit from. This report shows that if an attacker can do that, they can also set an environment variable called NODE_OPTIONS, which Node.js reads when starting child processes. That lets them run their own code inside those child processes.

What is RCE?

Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.

More from Node.js

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.