Skip to content

OS Command Injection via shelljs.exec()

Description

User controlled data in 'shelljs.exec()' can result in Remote OS Command Execution. This occurs when user input is not properly sanitized and is used to construct an OS command, allowing an attacker to inject malicious commands.

Examples

Insecure Code

javascript
const shell = require('shelljs'); shell.exec('ls ' + req.query.file);

Secure Code

javascript
const shell = require('shelljs'); const allowedCommands = ['ls', 'pwd']; const command = 'ls'; if (allowedCommands.includes(command)) { shell.exec(command); }

Remediation

Validate and sanitize all user input before passing it to 'shelljs.exec()'. Consider using a whitelist approach to only allow specific, expected commands.

Rule Details

FieldValue
IDCODE-0387
CategoryInjection
SeverityCRITICAL
CWECWE-78
ConfidenceHIGH
ImpactHIGH
LikelihoodMEDIUM
ExploitabilityEASY
Tagsos command injection, remote code execution
OWASPA1:2017-Injection, A03:2021-Injection