Static analysis (SAST) check · Java

Runtime exec concatenation (Java)

A command line is assembled by concatenation and passed to Runtime.exec.

Why it matters

Any operand that comes from outside the program can add arguments, and with a shell in the command, whole commands.

How to fix it

Use ProcessBuilder with each argument as its own list element.

Example

The rule's own test fixtures, verbatim: the test suite requires the rule to report the first and not the second.

Vulnerable: reported (Java)
public class Bad {
    public Process archive(String directory) throws Exception {
        return Runtime.getRuntime().exec("tar czf backup.tgz " + directory);
    }
}
Fixed: not reported (Java)
public class Ok {
    public Process archive(String directory) throws Exception {
        return new ProcessBuilder("tar", "czf", "backup.tgz", directory).start();
    }
}

What TrustIvan reads

Runs on repository scans of Java source once static analysis is turned on (SAST_RULES and the separately built analyzer image); it is off by default.

Weakness classes and references

Terms: SAST (static application security testing), Taint analysis, CWE (Common Weakness Enumeration), OWASP Top 10

All static analysis (SAST) checks · The whole catalogue · How this works in TrustIvan · Documentation

See TrustIvan on your own estate.

Book thirty minutes with an engineer and bring an image, a repository or a cloud account, or start free on the hosted service.