An operating system command is built by joining strings at run time

tx0/java:command-from-stringsrecommendedwarningsecurity, cwe-78

What it finds

Runtime.getRuntime().exec("convert " + name) builds an operating system command from a string that is put together at run time. When a part comes from outside, a name with spaces or shell syntax in it changes the command: arguments are split differently, or, with a shell, other commands run (CWE-78). Passing the program and each argument as separate elements of an array or list keeps every value one argument.

Reported are calls of exec on a Runtime and new ProcessBuilder(...) whose command is a concatenation with a part that is not a constant, or a String.format, also as an element of the List.of or Arrays.asList given to ProcessBuilder.

From the rule's source, command_from_strings.rs.

Example from the rule's tests
import java.io.IOException; class Thumbnails {  void make(String name) throws IOException {    Runtime.getRuntime().exec("convert " + name + " thumb.png");tx0 reports: the command is put together from strings at run time    String command = String.format("rm %s", name);    Runtime.getRuntime().exec(command);tx0 reports: the command is put together    Runtime.getRuntime().exec(new String[] {"convert", name, "thumb.png"});    new ProcessBuilder("sh", "-c", "ls " + name);tx0 reports: the command is put together    new ProcessBuilder("ls", name);    Runtime.getRuntime().exec("ls " + "-l");  }}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/java:command-from-strings" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/java:command-from-strings] reason="..."

tx0 explain tx0/java:command-from-strings shows the same in the terminal. Found a false positive? Report it.