2023-03-02 18:07:54 +01:00

47 lines
1.2 KiB
Plaintext

=== How to fix it in Java SE
include::../../common/fix/code-rationale.adoc[]
[cols="a"]
|===
h| Non-compliant code example
|
[source,java]
----
@Controller
public class ExampleController
{
@GetMapping(value = "/exec")
public void exec(@RequestParam("command") String command) throws IOException {
Runtime.getRuntime().exec(command); // Noncompliant
}
}
----
h| Compliant solution
|
[source,java]
----
@Controller
public class ExampleController
{
@GetMapping(value = "/exec")
public void exec(@RequestParam("command") String command) throws IOException {
List<String> allowedCmds = new ArrayList<String>();
allowedCmds.add("/usr/bin/ls");
allowedCmds.add("/usr/bin/cat");
if (allowedCmds.contains(command)){
Process proc = Runtime.getRuntime().exec(command);
}
}
}
----
|===
++java.lang.Runtime++ is sometimes used over ++java.lang.ProcessBuilder++ due to ease of use. Flexibility in methods often introduces security issues as edge cases are easily missed. The compliant solution logic is also applied to ++java.lang.ProcessBuilder++.
=== How does this work?
include::../../common/fix/introduction.adoc[]
include::../../common/fix/pre-approved-list.adoc[]