51 lines
1.1 KiB
Plaintext
51 lines
1.1 KiB
Plaintext
=== How to fix it in Apache Commons
|
|
|
|
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 {
|
|
|
|
CommandLine cmd = new CommandLine(command); // Noncompliant
|
|
DefaultExecutor executor = new DefaultExecutor();
|
|
executor.execute(cmd);
|
|
}
|
|
}
|
|
----
|
|
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("/bin/ls");
|
|
allowedCmds.add("/bin/cat");
|
|
|
|
if (allowedCmds.contains(command)){
|
|
CommandLine cmd = new CommandLine(command);
|
|
DefaultExecutor executor = new DefaultExecutor();
|
|
executor.execute(cmd);
|
|
}
|
|
}
|
|
}
|
|
----
|
|
|===
|
|
|
|
=== How does this work?
|
|
|
|
include::../../common/fix/introduction.adoc[]
|
|
|
|
include::../../common/fix/pre-approved-list.adoc[] |