← Java / Spring · Contents

JAVA-036 / Beginner

OS command injection (Runtime.exec string concatenation)

A03:2021 Injection · CWE-78 OS Command Injection

Why is this a problem?

; rm -rf / is executed.

How to fix it

java
// Argument array, no shell, input allowlist
if (!host.matches("^[A-Za-z0-9.-]{1,253}$")) throw new IllegalArgumentException();
new ProcessBuilder("ping", "-c", "1", host).start();

Further reading