[freespace] Command Injection due to Lack of Sanitization
The freespace npm module passes a user-supplied mount point directly into a shell command without proper sanitisation, allowing an attacker to inject and execute arbitrary commands on Unix systems. This could lead to information gathering, system enumeration, and further execution of scripts or binaries.
- Disclosed after 2 months
I would like to report Command Injection in the freespace module.
It allows an attacker to inject and execute shell commands on Unix based systems.
Module
module name: freespace
version: 1.0.4
npm page: https://www.npmjs.com/package/freespace
Module Description
A library that tells you how much free disk space you have.
- Works on all platform.
- No dependencies and no native libraries.
Module Stats
[26] weekly downloads
Vulnerability
Vulnerability Description
The library offers a function that allows a developer to pass in a disk-label (Windows) or a mount-point (Linux) and returns space details about the same. However, due to lack of proper sanitization of the parameter to that function, injection of multiple commands using delimiters such as ; and && will cause the shell to interpret and execute each command between those characters.
This code is vulnerable on the Unix platform and not on Windows. The Windows variant also directly uses the input without sanitization, however, uses only the first character of the input parameter, which prevents injection.
This is the vulnerable code in index.js of the library:
exports.check = function(driveOrMount, callback) {
return new Promise(function(resolve, reject) {
let cb = function(err, stdout, stderr) { ... };
if (!driveOrMountRegex.test(driveOrMount)) {
let err = new Error(DRIVE_STRING_ERROR);
if (callback) callback(err);
return reject(err);
}
if (process.platform === 'win32') {
driveOrMount = driveOrMount.charAt(0).toLowerCase();
cp.exec(`fsutil volume diskfree ${driveOrMount}:`, {}, cb);
} else {
cp.exec(`df -P ${driveOrMount} | awk 'NR==2 {print $4}'`, {}, cb); // vulnerability - 'driveOrMount' is being used directly without sanitization
}
}).then(function(bytes) { return bytes })
.catch(function(err) { return Promise.reject(err) });
};
Steps To Reproduce:
- Create test directory:
mkdir freespace-pocandcdinto it - Install the library with NPM:
npm install freespace - Create an output directory, I am using
/tmp- which is initially empty - Create a file
test.jscontaining the following:
const freespace = require('freespace');
freespace.check('/ ; touch /tmp/semicolon_file')
.then(bytes => {
console.log(bytes);
});
freespace.check('/ && touch /tmp/ampersand_file')
.then(bytes => {
console.log(bytes);
});
- Run the code:
node test.js - List the output directory - in my case,
ls /tmp - You will see that the files
semicolon_fileandampersand_filehave been created, indicating that the commands were injected and executed

Patch
All parameters should be sanitized before being sent as shell command arguments. The developer has implemented a REGEX check before shell command calls in the line if (!driveOrMountRegex.test(driveOrMount)) which does not cover these special cases. This REGEX can be used to whitelist/blacklist characters and either produce an error or strip them out and continue execution.
Supporting Material/References:
- Operating System Version: Ubuntu 18.04.4 LTS
- NodeJS Version: v14.7.0
- NPM Version: 6.14.7
Wrap up
- I contacted the maintainer to let them know: N
- I opened an issue in the related repository: N
Thank you for your time.
Impact
Command Injection can lead to information gathering, system enumeration and further execution of scripts/binaries.
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
Impact
An attacker could execute arbitrary commands on Unix systems, leading to information gathering, system enumeration, and further execution of scripts or binaries.
What the attacker needed
Unix-based system; attacker controls the mount point argument
The fix
All parameters should be sanitised before being sent as shell command arguments, for example by using the existing regex to whitelist or blacklist characters.
How it was found
- Create a test directory and install the freespace library.
- Create a JavaScript file that calls freespace.check with a mount point containing a command separator and a touch command.
- Run the JavaScript file with Node.js.
- List the output directory to see the created files, confirming command execution.
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.
The library builds a command for the operating system by inserting whatever text you give it. Because it does not check for special characters like semicolons, an attacker can add extra commands that the system will run. This lets them run their own code on the server.
What is RCE?
Running the attacker's code on the target's servers: through command or code injection, unsafe deserialisation or template injection.