=== 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 = "/find")
    public void find(@RequestParam("filename") String filename) throws IOException {

        Runtime.getRuntime().exec("/usr/bin/find . -iname " + filename); // Noncompliant
    }
}
----
h| Compliant solution
|
[source,java]
----
@Controller
public class ExampleController
{
    @GetMapping(value = "/find")
    public void find(@RequestParam("filename") String filename) throws IOException {

        String cmd1[] = new String[] {"/usr/bin/find", ".", "-iname", filename};
        Process proc = Runtime.getRuntime().exec(cmd1); // Compliant
    }
}
----
|===

++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[]


Here `java.lang.Runtime.exec(String[] cmdarray)` takes care of escaping the passed arguments and internally
creates a single string given to the operating system to be executed.
