~ 4 min read

NodeJS Command Injection

share on
Your NodeJS application is vulnerable to Command Injection without you even knowing it. Learn how your NodeJS is at risk with this real-world CVE-2024-21488 example.

One of the biggest culprit with NodeJS Command Injection vulnerabilities is that they are sometimes transparently hidden in your codebase’s third-party application dependencies and can be easily overlooked. Yet, OS Command Injection is a common and a significant security vulnerability that can be exploited by an attacker to execute arbitrary system commands on the host operating system.

NodeJS Command Injection

NodeJS Command Injection vulnerabilities are a type of security vulnerability that is rooted with the ability of attackers to execute arbitrary system commands on the host operating system, made possible due to developers relying on Node.js core module child_process to execute system commands via APIs like exec, execFile, spawn, and fork.

Here’s a NodeJS Command Injection vulnerability example:

const { exec } = require('child_process');
function checkHostAlive(host, callback) {
exec(`ping -c 1 ${host}`, (err, stdout, stderr) => {
if (err) {
callback(err);
return;
}
callback(null, stdout);
});
}
app.post('/status', (req, res) => {
const { host } = req.body;
checkHostAlive(host, (err, result) => {
if (err) {
res.status(500).send('Error checking host status');
}
res.status(200).send(result);
});
});

The checkHostAlive function uses the exec function to execute a system command to ping a host. The host variable is directly interpolated into the command string without any input validation or sanitization. This makes it possible for an attacker to inject arbitrary system commands into the host variable and execute them on the host operating system.

Blind use of Command Injection in NodeJS

The above code snippet is a classic example of a NodeJS Command Injection vulnerability and while it seems trivial, let me show you how your NodeJS application is vulnerable to Command Injection without you even knowing it.

Supposed you need to perform some network utility tasks in your backend application. You might go to the npm registry to find a package for it. One such popular network utility package is network that receives about 65,000 weekly downloads, so fairly common.

One of its usage example is as follows:

var network = require('network');
network.get_public_ip(function(err, ip) {
// should return your public IP address
console.log(err || ip);
})

However, the network package is vulnerable to Command Injection. The mac_address_for function in the package uses the above mentioned exec function from child_process Node.js core module to execute the arp command to get the MAC address for a given IP address.

The ip variable is directly interpolated into a command string without any input validation or sanitization, making it possible for an attacker to inject arbitrary system commands into the ip variable and execute them on the host operating system.

Hence, consider the following proof-of-concept code:

const { mac_address_for } = require('network');
let ifaceName = " $(touch /tmp/success) # \" || touch /tmp/success # ' || touch /tmp/success";
let cb = "{}";
let result = mac_address_for(ifaceName, cb);

When looking at the network npm package source code the insecure coding practice is evident:

exports.mac_address_for = function(nic_name, cb) {
var cmd = 'cat /sys/class/net/' + nic_name + '/address';
trim_exec(cmd, cb);
};
function trim_exec(cmd, cb) {
exec(cmd, function(err, out) {
if (out && out.toString() != '')
cb(null, out.toString().trim())
else
cb(err)
})
}

This vulnerability identified as CVE-2024-21488 shows how developers might unsuspectingly pass user input to a third-party package and unknowingly introduce a Command Injection vulnerability in their NodeJS application.

How to prevent a NodeJS Command Injection vulnerability

What do you think the security fix for this vulnerability is? Is the answer being input validation and sanitization? Surely, validating user input is import and sanitizing user input is a common practice developers take.

The maintainer’s approach to fixing this security vulnerability was the following:

function ensure_valid_nic(str) {
if (str.match(/[^\w]/))
throw new Error("Invalid nic name given: " + str);
}
exports.mac_address_for = function(nic_name, cb) {
ensure_valid_nic(nic_name);
var cmd = 'cat /sys/class/net/' + nic_name + '/address';
trim_exec(cmd, cb);
};

Do you think this is a secure fix? The ensure_valid_nic function checks if the nic_name variable contains any non-word characters and throws an error if it does. This is an attempt to prevent command injection by restricting the characters that can be used in the nic_name variable.

Is this fix sufficient to prevent Command Injection in the network package?

This is when I invite you to get my Node.js Command Injection book and learn how to secure your NodeJS application from Command Injection vulnerabilities and practice secure coding conventions when you build your Node.js application code and your own third-party npm dependencies.


Node.js Security Newsletter

Subscribe to get everything in and around the Node.js security ecosystem, direct to your inbox.

    JavaScript & web security insights, latest security vulnerabilities, hands-on secure code insights, npm ecosystem incidents, Node.js runtime feature updates, Bun and Deno runtime updates, secure coding best practices, malware, malicious packages, and more.