2022-08-05 17:10:47 +02:00
|
|
|
=== How to fix it in Apache Commons
|
|
|
|
|
|
|
|
include::../../common/fix/code-rationale.adoc[]
|
|
|
|
|
2022-10-18 16:03:10 +02:00
|
|
|
==== Noncompliant code example
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
2022-08-05 17:10:47 +02:00
|
|
|
----
|
|
|
|
@Controller
|
|
|
|
public class ExampleController
|
|
|
|
{
|
|
|
|
@GetMapping(value = "/find")
|
|
|
|
public void find(@RequestParam("filename") String filename) throws IOException {
|
|
|
|
|
2022-09-15 10:28:08 +02:00
|
|
|
CommandLine cmd = new CommandLine("/usr/bin/find . -iname " + filename);
|
2022-08-05 17:10:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
2022-09-15 10:28:08 +02:00
|
|
|
|
|
|
|
==== Compliant solution
|
|
|
|
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
2022-08-05 17:10:47 +02:00
|
|
|
----
|
|
|
|
@Controller
|
|
|
|
public class ExampleController
|
|
|
|
{
|
|
|
|
@GetMapping(value = "/find")
|
|
|
|
public void find(@RequestParam("filename") String filename) throws IOException {
|
|
|
|
|
|
|
|
CommandLine cmd = new CommandLine("/usr/bin/find");
|
|
|
|
cmd.addArguments(new String[] {"/usr/bin/find", ".", "-iname", filename});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
=== How does this work?
|
|
|
|
|
|
|
|
include::../../common/fix/introduction.adoc[]
|
|
|
|
|
|
|
|
Here `org.apache.commons.exec.CommandLine.addArguments(String[] addArguments)` takes care of escaping the passed arguments and internally
|
|
|
|
creates a single string given to the operating system to be executed.
|