Skip to content

Commit

Permalink
SONARJAVA-4383 jakarta support for S5122, S2254, S6437, S1168 (#4476)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardo-pilastri-sonarsource authored Oct 6, 2023
1 parent 1e111ce commit a7fa130
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public void javaCheckTestSources() throws Exception {
* No differences would mean that we find the same issues with and without the bytecode and libraries
*/
String differences = Files.readString(pathFor(TARGET_ACTUAL + PROJECT_KEY + "-no-binaries_differences"));
assertThat(differences).isEqualTo("Issues differences: 3294");
assertThat(differences).isEqualTo("Issues differences: 3299");
}

private static Path pathFor(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1160,7 +1160,7 @@
{
"ruleKey": "S2254",
"hasTruePositives": true,
"falseNegatives": 0,
"falseNegatives": 1,
"falsePositives": 0
},
{
Expand Down Expand Up @@ -2108,7 +2108,7 @@
{
"ruleKey": "S5122",
"hasTruePositives": true,
"falseNegatives": 15,
"falseNegatives": 18,
"falsePositives": 0
},
{
Expand Down Expand Up @@ -2846,7 +2846,7 @@
{
"ruleKey": "S6437",
"hasTruePositives": true,
"falseNegatives": 56,
"falseNegatives": 57,
"falsePositives": 0
},
{
Expand Down
6 changes: 6 additions & 0 deletions java-checks-test-sources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,12 @@
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>6.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
15 changes: 11 additions & 4 deletions java-checks-test-sources/src/main/java/checks/CORSCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se

resp.getWriter().write("response");
}

protected void doGetJakarta(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) {
resp.setHeader("Access-Control-Allow-Origin", "*"); // Noncompliant [[sc=10;ec=19]]
resp.setHeader("Access-control-allow-Origin", "*"); // Noncompliant [[sc=10;ec=19]]
resp.addHeader("Access-Control-Allow-Origin", "*"); // Noncompliant [[sc=10;ec=19]]
}

// === Spring MVC Controller annotation ===
@CrossOrigin(origins = "*") // Noncompliant [[sc=4;ec=15]] {{Make sure that enabling CORS is safe here.}}
@RequestMapping("")
Expand Down Expand Up @@ -160,19 +167,19 @@ class Local {
public CorsFilter corsFilter4() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*"); // Noncompliant [[secondary=164,165]]
config.addAllowedOrigin("*"); // Noncompliant [[secondary=+1,+2]]
config.applyPermitDefaultValues();
config.applyPermitDefaultValues();
config.addAllowedOrigin("*"); // Noncompliant [[secondary=164,165]]
config.addAllowedOrigin("*"); // Noncompliant [[secondary=-2,-1]]
return new CorsFilter(source);
}
}
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
CorsConfiguration config = new CorsConfiguration();
config.addAllowedOrigin("*"); // Noncompliant [[secondary=173,174]]
config.addAllowedOrigin("*"); // Noncompliant [[secondary=+1,+2]]
config.applyPermitDefaultValues();
config.applyPermitDefaultValues();
config.addAllowedOrigin("*"); // Noncompliant [[secondary=173,174]]
config.addAllowedOrigin("*"); // Noncompliant [[secondary=-2,-1]]
return new CorsFilter(source);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ public class GetRequestedSessionIdCheck extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sessionId = request.getRequestedSessionId(); // Noncompliant [[sc=32;ec=53]] {{Remove use of this unsecured "getRequestedSessionId()" method}}
}
protected void doPostJakarta(jakarta.servlet.http.HttpServletRequest request) {
String sessionId = request.getRequestedSessionId(); // Noncompliant
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ public int[] bark() {
return null;
}

@jakarta.annotation.Nullable
public int[] jakartaArr() { return null; }

int[] qix(){
takeLambda(a -> {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class HardCodedCredentialsShouldNotBeUsedCheck {
private static char[] secretCharArrayField = new char[]{0xC, 0xA, 0xF, 0xE};
private static CharSequence secretCharSequenceField = "Hello, World!".subSequence(0, 12);

public static void nonCompliant(byte[] message, boolean condition, Charset encoding, SignatureAlgorithm paremSignatureAlgorithm) throws ServletException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, UnsupportedEncodingException {
public static void nonCompliant(byte[] message, boolean condition, Charset encoding, SignatureAlgorithm paremSignatureAlgorithm) throws ServletException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException, UnsupportedEncodingException, jakarta.servlet.ServletException {
String effectivelyConstantString = "s3cr37";
byte[] key = effectivelyConstantString.getBytes();

Expand Down Expand Up @@ -69,6 +69,8 @@ public static void nonCompliant(byte[] message, boolean condition, Charset encod
String concatenatedPassword = "abc" + true + ":" + 12 + ":" + 43L + ":" + 'a' + ":" + 0.2f + ":" + 0.2d;
request.login("user", concatenatedPassword); // Noncompliant [[sc=27;ec=47;secondary=-1]]

jakarta.servlet.http.HttpServletRequest requestJakarta = new jakarta.servlet.http.HttpServletRequestWrapper(null);
requestJakarta.login("user", "password"); // Noncompliant
KeyStore store = KeyStore.getInstance(null);

store.getKey("", new char[]{0xC, 0xA, 0xF, 0xE}); // Noncompliant
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
public class CORSCheck extends IssuableSubscriptionVisitor {

private static final MethodMatchers SET_ADD_HEADER_MATCHER = MethodMatchers.create()
.ofTypes("javax.servlet.http.HttpServletResponse")
.ofTypes("javax.servlet.http.HttpServletResponse", "jakarta.servlet.http.HttpServletResponse")
.names("setHeader", "addHeader")
.withAnyParameters()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class GetRequestedSessionIdCheck extends AbstractMethodDetection {
@Override
protected MethodMatchers getMethodInvocationMatchers() {
return MethodMatchers.create()
.ofTypes("javax.servlet.http.HttpServletRequest")
.ofTypes("javax.servlet.http.HttpServletRequest", "jakarta.servlet.http.HttpServletRequest")
.names("getRequestedSessionId")
.addWithoutParametersMatcher()
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
{"cls":"io.vertx.ext.auth.oauth2.providers.StripeAuth","name":"create","args":["io.vertx.core.Vertx","java.lang.String","java.lang.String"],"indices":[2]},
{"cls":"io.vertx.ext.auth.oauth2.providers.TwitterAuth","name":"create","args":["io.vertx.core.Vertx","java.lang.String","java.lang.String","io.vertx.core.http.HttpClientOptions"],"indices":[2]},
{"cls":"io.vertx.ext.auth.oauth2.providers.TwitterAuth","name":"create","args":["io.vertx.core.Vertx","java.lang.String","java.lang.String"],"indices":[2]},
{"cls":"jakarta.security.auth.message.callback.PasswordValidationCallback","name":"PasswordValidationCallback","args":["javax.security.auth.Subject","java.lang.String","char[]"],"indices":[2]},
{"cls":"java.net.PasswordAuthentication","name":"PasswordAuthentication","args":["java.lang.String","char[]"],"indices":[1]},
{"cls":"java.security.KeyStore","name":"getKey","args":["java.lang.String","char[]"],"indices":[1]},
{"cls":"java.security.KeyStore","name":"load","args":["java.io.InputStream","char[]"],"indices":[1]},
Expand Down Expand Up @@ -156,7 +155,9 @@
{"cls":"javax.security.auth.message.callback.PasswordValidationCallback","name":"PasswordValidationCallback","args":["javax.security.auth.Subject","java.lang.String","char[]"],"indices":[2]},
{"cls":"javax.security.auth.message.callback.PasswordValidationCallback","name":"PasswordValidationCallback","args":["javax.security.auth.Subject","java.lang.String","char[]"],"indices":[2]},
{"cls":"javax.servlet.http.HttpServletRequest","name":"login","args":["java.lang.String","java.lang.String"],"indices":[1]},
{"cls":"jakarta.servlet.http.HttpServletRequest","name":"login","args":["java.lang.String","java.lang.String"],"indices":[1]},
{"cls":"javax.servlet.http.HttpServletRequestWrapper","name":"login","args":["java.lang.String","java.lang.String"],"indices":[1]},
{"cls":"jakarta.servlet.http.HttpServletRequestWrapper","name":"login","args":["java.lang.String","java.lang.String"],"indices":[1]},
{"cls":"javax.sql.ConnectionPoolDataSource","name":"getPooledConnection","args":["java.lang.String","java.lang.String"],"indices":[1]},
{"cls":"javax.sql.DataSource","name":"getConnection","args":["java.lang.String","java.lang.String"],"indices":[1]},
{"cls":"javax.sql.RowSet","name":"setPassword","args":["java.lang.String"],"indices":[0]},
Expand Down

0 comments on commit a7fa130

Please sign in to comment.