Skip to content

Commit a7fa130

Browse files
SONARJAVA-4383 jakarta support for S5122, S2254, S6437, S1168 (#4476)
1 parent 1e111ce commit a7fa130

10 files changed

Lines changed: 34 additions & 12 deletions

File tree

its/ruling/src/test/java/org/sonar/java/it/AutoScanTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public void javaCheckTestSources() throws Exception {
188188
* No differences would mean that we find the same issues with and without the bytecode and libraries
189189
*/
190190
String differences = Files.readString(pathFor(TARGET_ACTUAL + PROJECT_KEY + "-no-binaries_differences"));
191-
assertThat(differences).isEqualTo("Issues differences: 3294");
191+
assertThat(differences).isEqualTo("Issues differences: 3299");
192192
}
193193

194194
private static Path pathFor(String path) {

its/ruling/src/test/resources/autoscan/autoscan-diff-by-rules.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,7 @@
11601160
{
11611161
"ruleKey": "S2254",
11621162
"hasTruePositives": true,
1163-
"falseNegatives": 0,
1163+
"falseNegatives": 1,
11641164
"falsePositives": 0
11651165
},
11661166
{
@@ -2108,7 +2108,7 @@
21082108
{
21092109
"ruleKey": "S5122",
21102110
"hasTruePositives": true,
2111-
"falseNegatives": 15,
2111+
"falseNegatives": 18,
21122112
"falsePositives": 0
21132113
},
21142114
{
@@ -2846,7 +2846,7 @@
28462846
{
28472847
"ruleKey": "S6437",
28482848
"hasTruePositives": true,
2849-
"falseNegatives": 56,
2849+
"falseNegatives": 57,
28502850
"falsePositives": 0
28512851
},
28522852
{

java-checks-test-sources/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,12 @@
416416
<version>3.1.0</version>
417417
<scope>provided</scope>
418418
</dependency>
419+
<dependency>
420+
<groupId>jakarta.servlet</groupId>
421+
<artifactId>jakarta.servlet-api</artifactId>
422+
<version>6.0.0</version>
423+
<scope>provided</scope>
424+
</dependency>
419425
<dependency>
420426
<groupId>com.google.guava</groupId>
421427
<artifactId>guava</artifactId>

java-checks-test-sources/src/main/java/checks/CORSCheck.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
4646

4747
resp.getWriter().write("response");
4848
}
49+
50+
protected void doGetJakarta(jakarta.servlet.http.HttpServletRequest req, jakarta.servlet.http.HttpServletResponse resp) {
51+
resp.setHeader("Access-Control-Allow-Origin", "*"); // Noncompliant [[sc=10;ec=19]]
52+
resp.setHeader("Access-control-allow-Origin", "*"); // Noncompliant [[sc=10;ec=19]]
53+
resp.addHeader("Access-Control-Allow-Origin", "*"); // Noncompliant [[sc=10;ec=19]]
54+
}
55+
4956
// === Spring MVC Controller annotation ===
5057
@CrossOrigin(origins = "*") // Noncompliant [[sc=4;ec=15]] {{Make sure that enabling CORS is safe here.}}
5158
@RequestMapping("")
@@ -160,19 +167,19 @@ class Local {
160167
public CorsFilter corsFilter4() {
161168
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
162169
CorsConfiguration config = new CorsConfiguration();
163-
config.addAllowedOrigin("*"); // Noncompliant [[secondary=164,165]]
170+
config.addAllowedOrigin("*"); // Noncompliant [[secondary=+1,+2]]
164171
config.applyPermitDefaultValues();
165172
config.applyPermitDefaultValues();
166-
config.addAllowedOrigin("*"); // Noncompliant [[secondary=164,165]]
173+
config.addAllowedOrigin("*"); // Noncompliant [[secondary=-2,-1]]
167174
return new CorsFilter(source);
168175
}
169176
}
170177
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
171178
CorsConfiguration config = new CorsConfiguration();
172-
config.addAllowedOrigin("*"); // Noncompliant [[secondary=173,174]]
179+
config.addAllowedOrigin("*"); // Noncompliant [[secondary=+1,+2]]
173180
config.applyPermitDefaultValues();
174181
config.applyPermitDefaultValues();
175-
config.addAllowedOrigin("*"); // Noncompliant [[secondary=173,174]]
182+
config.addAllowedOrigin("*"); // Noncompliant [[secondary=-2,-1]]
176183
return new CorsFilter(source);
177184
}
178185

java-checks-test-sources/src/main/java/checks/GetRequestedSessionIdCheck.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,7 @@ public class GetRequestedSessionIdCheck extends HttpServlet {
1010
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
1111
String sessionId = request.getRequestedSessionId(); // Noncompliant [[sc=32;ec=53]] {{Remove use of this unsecured "getRequestedSessionId()" method}}
1212
}
13+
protected void doPostJakarta(jakarta.servlet.http.HttpServletRequest request) {
14+
String sessionId = request.getRequestedSessionId(); // Noncompliant
15+
}
1316
}

java-checks-test-sources/src/main/java/checks/ReturnEmptyArrayNotNullCheck.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public int[] bark() {
123123
return null;
124124
}
125125

126+
@jakarta.annotation.Nullable
127+
public int[] jakartaArr() { return null; }
128+
126129
int[] qix(){
127130
takeLambda(a -> {
128131
return null;

java-checks-test-sources/src/main/java/checks/security/HardCodedCredentialsShouldNotBeUsedCheck.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class HardCodedCredentialsShouldNotBeUsedCheck {
3838
private static char[] secretCharArrayField = new char[]{0xC, 0xA, 0xF, 0xE};
3939
private static CharSequence secretCharSequenceField = "Hello, World!".subSequence(0, 12);
4040

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

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

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

7476
store.getKey("", new char[]{0xC, 0xA, 0xF, 0xE}); // Noncompliant

java-checks/src/main/java/org/sonar/java/checks/CORSCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
public class CORSCheck extends IssuableSubscriptionVisitor {
4646

4747
private static final MethodMatchers SET_ADD_HEADER_MATCHER = MethodMatchers.create()
48-
.ofTypes("javax.servlet.http.HttpServletResponse")
48+
.ofTypes("javax.servlet.http.HttpServletResponse", "jakarta.servlet.http.HttpServletResponse")
4949
.names("setHeader", "addHeader")
5050
.withAnyParameters()
5151
.build();

java-checks/src/main/java/org/sonar/java/checks/GetRequestedSessionIdCheck.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class GetRequestedSessionIdCheck extends AbstractMethodDetection {
3131
@Override
3232
protected MethodMatchers getMethodInvocationMatchers() {
3333
return MethodMatchers.create()
34-
.ofTypes("javax.servlet.http.HttpServletRequest")
34+
.ofTypes("javax.servlet.http.HttpServletRequest", "jakarta.servlet.http.HttpServletRequest")
3535
.names("getRequestedSessionId")
3636
.addWithoutParametersMatcher()
3737
.build();

java-checks/src/main/resources/org/sonar/java/checks/security/S6437-methods.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@
119119
{"cls":"io.vertx.ext.auth.oauth2.providers.StripeAuth","name":"create","args":["io.vertx.core.Vertx","java.lang.String","java.lang.String"],"indices":[2]},
120120
{"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]},
121121
{"cls":"io.vertx.ext.auth.oauth2.providers.TwitterAuth","name":"create","args":["io.vertx.core.Vertx","java.lang.String","java.lang.String"],"indices":[2]},
122-
{"cls":"jakarta.security.auth.message.callback.PasswordValidationCallback","name":"PasswordValidationCallback","args":["javax.security.auth.Subject","java.lang.String","char[]"],"indices":[2]},
123122
{"cls":"java.net.PasswordAuthentication","name":"PasswordAuthentication","args":["java.lang.String","char[]"],"indices":[1]},
124123
{"cls":"java.security.KeyStore","name":"getKey","args":["java.lang.String","char[]"],"indices":[1]},
125124
{"cls":"java.security.KeyStore","name":"load","args":["java.io.InputStream","char[]"],"indices":[1]},
@@ -156,7 +155,9 @@
156155
{"cls":"javax.security.auth.message.callback.PasswordValidationCallback","name":"PasswordValidationCallback","args":["javax.security.auth.Subject","java.lang.String","char[]"],"indices":[2]},
157156
{"cls":"javax.security.auth.message.callback.PasswordValidationCallback","name":"PasswordValidationCallback","args":["javax.security.auth.Subject","java.lang.String","char[]"],"indices":[2]},
158157
{"cls":"javax.servlet.http.HttpServletRequest","name":"login","args":["java.lang.String","java.lang.String"],"indices":[1]},
158+
{"cls":"jakarta.servlet.http.HttpServletRequest","name":"login","args":["java.lang.String","java.lang.String"],"indices":[1]},
159159
{"cls":"javax.servlet.http.HttpServletRequestWrapper","name":"login","args":["java.lang.String","java.lang.String"],"indices":[1]},
160+
{"cls":"jakarta.servlet.http.HttpServletRequestWrapper","name":"login","args":["java.lang.String","java.lang.String"],"indices":[1]},
160161
{"cls":"javax.sql.ConnectionPoolDataSource","name":"getPooledConnection","args":["java.lang.String","java.lang.String"],"indices":[1]},
161162
{"cls":"javax.sql.DataSource","name":"getConnection","args":["java.lang.String","java.lang.String"],"indices":[1]},
162163
{"cls":"javax.sql.RowSet","name":"setPassword","args":["java.lang.String"],"indices":[0]},

0 commit comments

Comments
 (0)