
## Review A dedicated reviewer checked the rule description successfully for: - [ ] logical errors and incorrect information - [ ] information gaps and missing content - [ ] text style and tone - [ ] PR summary and labels follow [the guidelines](https://github.com/SonarSource/rspec/#to-modify-an-existing-rule)
45 lines
1.1 KiB
Plaintext
45 lines
1.1 KiB
Plaintext
== How to fix it in Java SE
|
|
|
|
=== Code examples
|
|
|
|
include::../../common/fix/code-rationale.adoc[]
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
Runtime r = Runtime.getRuntime();
|
|
String userInput = request.getParameter("example");
|
|
|
|
if (userInput != null) {
|
|
String[] envs = {userInput};
|
|
r.exec("/path/to/example", userInput);
|
|
} else{
|
|
r.exec("/path/to/example");
|
|
}
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
Runtime r = Runtime.getRuntime();
|
|
String userInput = request.getParameter("example");
|
|
|
|
if (userInput != null && userInput.matches("^[a-zA-Z0-9]*$")) {
|
|
String[] envs = {"ENV_VAR=%s".format(userInput)};
|
|
r.exec("/path/to/example", envs);
|
|
} else {
|
|
r.exec("/path/to/example");
|
|
}
|
|
}
|
|
----
|
|
|
|
=== How does this work?
|
|
|
|
include::../../common/fix/pre-approved-list.adoc[]
|
|
|