OS commands are security-sensitive. For example, their use has led in the past to the following vulnerabilities:

Applications that execute operating system commands or execute commands that interact with the underlying system should neutralize any externally-provided input used to construct those commands. Failure to do so could allow an attacker to execute unexpected or dangerous commands, potentially leading to loss of confidentiality, integrity or availability.

This rule flags code that specifies the name of the command to run. The goal is to guide security code reviews.

Ask Yourself Whether

(*) You are at risk if you answered yes to any of those questions.

Recommended Secure Coding Practices

Restrict the control given to the user over the executed command:

Restrict which users can have access to the command:

Reduce the damage the command can do:

Questionable Code Example

Runtime.getRuntime().exec(...);  // Questionable. Validate the executed command.

ProcessBuilder pb = new ProcessBuilder(command);  // Questionable.
pb.command(command);  // Questionable.

// === apache.commons ===
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;

CommandLine cmdLine = CommandLine.parse("bad.exe");
DefaultExecutor executor = new DefaultExecutor();
executor.execute(cmdLine); // Questionable

Exceptions

The following code will not raise any issue.

ProcessBuilder pb = new ProcessBuilder();
pb.command();

See