Skip to content

Commit 1e111ce

Browse files
author
Angelo Buono
authored
SONARJAVA-4612 Supported jakarta package in SpringComponentWithNonAutowiredMembersCheck, AbstractRegexCheck (#4475)
1 parent 912ab49 commit 1e111ce

17 files changed

Lines changed: 402 additions & 25 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: 3254");
191+
assertThat(differences).isEqualTo("Issues differences: 3294");
192192
}
193193

194194
private static Path pathFor(String path) {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@
302302
{
303303
"ruleKey": "S1161",
304304
"hasTruePositives": true,
305-
"falseNegatives": 6,
305+
"falseNegatives": 7,
306306
"falsePositives": 0
307307
},
308308
{
@@ -338,7 +338,7 @@
338338
{
339339
"ruleKey": "S1172",
340340
"hasTruePositives": true,
341-
"falseNegatives": 11,
341+
"falseNegatives": 13,
342342
"falsePositives": 0
343343
},
344344
{
@@ -698,7 +698,7 @@
698698
{
699699
"ruleKey": "S1874",
700700
"hasTruePositives": true,
701-
"falseNegatives": 89,
701+
"falseNegatives": 102,
702702
"falsePositives": 0
703703
},
704704
{
@@ -974,7 +974,7 @@
974974
{
975975
"ruleKey": "S2160",
976976
"hasTruePositives": true,
977-
"falseNegatives": 0,
977+
"falseNegatives": 1,
978978
"falsePositives": 0
979979
},
980980
{
@@ -1322,7 +1322,7 @@
13221322
{
13231323
"ruleKey": "S2637",
13241324
"hasTruePositives": true,
1325-
"falseNegatives": 19,
1325+
"falseNegatives": 21,
13261326
"falsePositives": 0
13271327
},
13281328
{
@@ -1610,7 +1610,7 @@
16101610
{
16111611
"ruleKey": "S3330",
16121612
"hasTruePositives": true,
1613-
"falseNegatives": 30,
1613+
"falseNegatives": 51,
16141614
"falsePositives": 0
16151615
},
16161616
{

java-checks-test-sources/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,18 @@
386386
<type>jar</type>
387387
<scope>provided</scope>
388388
</dependency>
389+
<dependency>
390+
<groupId>jakarta.ws.rs</groupId>
391+
<artifactId>jakarta.ws.rs-api</artifactId>
392+
<version>3.1.0</version>
393+
<scope>provided</scope>
394+
</dependency>
395+
<dependency>
396+
<groupId>jakarta.servlet</groupId>
397+
<artifactId>jakarta.servlet-api</artifactId>
398+
<version>6.0.0</version>
399+
<scope>provided</scope>
400+
</dependency>
389401
<dependency>
390402
<groupId>javax.inject</groupId>
391403
<artifactId>javax.inject</artifactId>

java-checks-test-sources/src/main/files/non-compiling/checks/security/CookieHttpOnlyCheck.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,43 @@ void baw() {
8383
Unknown.unkown(() -> { Class<String> v = unknown(); });
8484
}
8585
}
86+
87+
class JakartaCookieHttpOnlyCheck {
88+
89+
jakarta.servlet.http.Cookie field4;
90+
jakarta.servlet.http.Cookie field6;
91+
92+
void servletCookie(boolean param, jakarta.servlet.http.Cookie c0) {
93+
field6.setHttpOnly(false); // Noncompliant
94+
95+
jakarta.servlet.http.Cookie c7 = new UnknownCookie("name", "value"); // Noncompliant
96+
Object c8 = new jakarta.servlet.http.Cookie("name", "value"); // Noncompliant
97+
98+
jakarta.servlet.http.Cookie c13;
99+
c13 = new UnknownCookie("name", "value"); // Noncompliant
100+
101+
field4 = new jakarta.servlet.http.Cookie("name, value"); // FN
102+
}
103+
104+
jakarta.servlet.http.Cookie getC0() {
105+
return new UnknownCookie("name", "value"); // FN
106+
}
107+
108+
void compliant(jakarta.ws.rs.core.Cookie c) {
109+
c.isHttpOnly();
110+
}
111+
}
112+
113+
class JakartaCookieHttpOnlyCheckCookie extends jakarta.servlet.http.Cookie {
114+
public jakarta.servlet.http.Cookie c;
115+
public void setHttpOnly(boolean isHttpOnly) { }
116+
void foo() {
117+
setHttpOnly(false); // Noncompliant
118+
}
119+
void bar(boolean x) {
120+
setHttpOnly(x);
121+
}
122+
void baz() {
123+
setHttpOnly(true);
124+
}
125+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package annotations.nullability.no_default;
2+
3+
import jakarta.annotation.Nonnull;
4+
import jakarta.annotation.Nullable;
5+
6+
public class JakartaNullabilityAnnotation {
7+
@Nullable
8+
Object id9002_type_WEAK_NULLABLE_level_VARIABLE;
9+
@Nonnull
10+
Object id9003_type_NON_NULL_level_VARIABLE;
11+
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,33 @@ public void autowiredMethod3(List<Object> list) { // Compliant - List interface
9090
list.size();
9191
}
9292

93+
@jakarta.annotation.Resource
94+
public void jakartaResourceAnnotatedMethod3(List<Object> list) { // Noncompliant {{Use 'java.util.Collection' here; it is a more general type than 'List'.}}
95+
for (Object o : list) {
96+
o.toString();
97+
}
98+
}
99+
100+
@jakarta.inject.Inject
101+
public void jakartaInjectAnnotatedMethod1(List<Object> list) { // Noncompliant {{Use 'java.util.Collection' here; it is a more general type than 'List'.}}
102+
for (Object o : list) {
103+
o.toString();
104+
}
105+
}
106+
@jakarta.annotation.Resource
107+
public void jakartaRAnnotatedMethod4(Collection<Object> list) { // Compliant - since Spring annotated methods cannot take 'Iterable' as argument
108+
for (Object o : list) {
109+
o.toString();
110+
}
111+
}
112+
113+
@jakarta.inject.Inject
114+
public void jakartaInjectAnnotatedMethod2(Collection<Object> list) { // Compliant - since Spring annotated methods cannot take 'Iterable' as argument
115+
for (Object o : list) {
116+
o.toString();
117+
}
118+
}
119+
93120
public static void staticMethod(List<Object> list) { // Noncompliant {{Use 'java.util.Collection' here; it is a more general type than 'List'.}}
94121
list.size();
95122
}

java-checks-test-sources/src/main/java/checks/regex/RedosCheck.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ public class RedosCheck {
88
@Email(regexp = "(.*-)*@.*") // Noncompliant [[sc=4;ec=9]] {{Make sure the regex used here, which is vulnerable to polynomial runtime due to backtracking, cannot lead to denial of service.}}
99
String email;
1010

11+
@jakarta.validation.constraints.Email(regexp = "(.*-)*@.*") // Noncompliant [[sc=4;ec=40]] {{Make sure the regex used here, which is vulnerable to polynomial runtime due to backtracking, cannot lead to denial of service.}}
12+
String email2;
13+
1114
void realWorldExamples(String str) {
1215
String cloudflareAttack = "(?:(?:\"|'|\\]|\\}|\\\\|\\d|(?:nan|infinity|true|false|null|undefined|symbol|math)|\\`|\\-|\\+)+[)]*;?((?:\\s|-|~|!|\\{\\}|\\|\\||\\+)*.*(?:.*=.*)))";
1316
String stackOverflowAttack = "^[\\s\\u200c]+|[\\s\\u200c]+$";

java-checks-test-sources/src/main/java/checks/regex/RedosCheckJava8.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ public class RedosCheckJava8 {
77
@Email(regexp = "(.*-)*@.*") // Noncompliant [[sc=4;ec=9]] {{Make sure the regex used here, which is vulnerable to exponential runtime due to backtracking, cannot lead to denial of service.}}
88
String email;
99

10+
@jakarta.validation.constraints.Email(regexp = "(.*-)*@.*") // Noncompliant [[sc=4;ec=40]] {{Make sure the regex used here, which is vulnerable to exponential runtime due to backtracking, cannot lead to denial of service.}}
11+
String email2;
12+
1013
void alwaysExponential(String str) {
1114
str.matches("(.*,)*?"); // Noncompliant [[sc=9;ec=16]] {{Make sure the regex used here, which is vulnerable to exponential runtime due to backtracking, cannot lead to denial of service.}}
1215
str.matches("(.?,)*?"); // Noncompliant {{Make sure the regex used here, which is vulnerable to exponential runtime due to backtracking, cannot lead to denial of service.}}

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

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,175 @@ void baz() {
256256

257257
class CookieHttpOnlyCheckCookieB {
258258
CookieHttpOnlyCheckCookieA a;
259-
public void setHttpOnly(boolean isHttpOnly) { }
259+
260+
public void setHttpOnly(boolean isHttpOnly) {
261+
}
262+
260263
void foo() {
261264
setHttpOnly(false);
262265
}
263-
void bar() { return; }
266+
267+
void bar() {
268+
return;
269+
}
270+
264271
CookieHttpOnlyCheckCookieA getA() {
265272
return new CookieHttpOnlyCheckCookieA(); // Noncompliant
266273
}
274+
267275
void baw() {
268276
int i;
269277
i = 1;
270278
a.c = new Cookie("1", "2"); // FN
271279
}
272280
}
281+
282+
class JakartaCookieHttpOnlyCheckCookieACookieHttpOnlyCheck {
283+
284+
private static final boolean FALSE_CONSTANT = false;
285+
286+
jakarta.servlet.http.Cookie field1 = new jakarta.servlet.http.Cookie("name", "value"); // FN
287+
jakarta.ws.rs.core.Cookie field3 = new jakarta.ws.rs.core.Cookie("name", "value"); // FN
288+
jakarta.servlet.http.Cookie field6;
289+
void servletCookie(boolean param, jakarta.servlet.http.Cookie c0) {
290+
c0.setHttpOnly(false); // Noncompliant [[sc=19;ec=26]] {{Make sure creating this cookie without the "HttpOnly" flag is safe.}}
291+
field6.setHttpOnly(false); // Noncompliant
292+
293+
jakarta.servlet.http.Cookie c1 = new jakarta.servlet.http.Cookie("name", "value");
294+
if (param) {
295+
c1.setHttpOnly(false); // Noncompliant
296+
} else {
297+
c1.setHttpOnly(true);
298+
}
299+
300+
jakarta.servlet.http.Cookie c2 = new jakarta.servlet.http.Cookie("name", "value"); // Noncompliant [[sc=42;ec=69]]
301+
302+
c1.setHttpOnly(false); // Noncompliant
303+
304+
c1.setHttpOnly(FALSE_CONSTANT); // Noncompliant
305+
306+
boolean b = false;
307+
c1.setHttpOnly(b); // Noncompliant
308+
309+
c1.setHttpOnly(param);
310+
311+
jakarta.servlet.http.Cookie c3;
312+
c3 = new jakarta.servlet.http.Cookie("name", "value");
313+
c3.setHttpOnly(false); // Noncompliant
314+
315+
c3.setHttpOnly(true);
316+
317+
boolean bValue = true;
318+
c3.setHttpOnly(!bValue); // FN
319+
}
320+
321+
jakarta.servlet.http.Cookie getC1() {
322+
return new jakarta.servlet.http.Cookie("name", "value"); // Noncompliant [[sc=16;ec=43]]
323+
}
324+
325+
jakarta.servlet.http.Cookie returnHttpCookie(jakarta.servlet.http.HttpServletResponse response) {
326+
jakarta.servlet.http.Cookie cookie = new jakarta.servlet.http.Cookie("name", "value"); // Noncompliant
327+
response.addCookie(new jakarta.servlet.http.Cookie("name", "value")); // Noncompliant
328+
return new jakarta.servlet.http.Cookie("name", "value"); // Noncompliant
329+
}
330+
331+
void jakartaRsCookie() {
332+
jakarta.ws.rs.core.Cookie c1 = new jakarta.ws.rs.core.Cookie("name", "value"); // Noncompliant
333+
jakarta.ws.rs.core.Cookie c2 = new jakarta.ws.rs.core.Cookie("name", "value", "path", "domain"); // Noncompliant
334+
}
335+
336+
void jakartaRsNewCookie(jakarta.ws.rs.core.Cookie cookie) {
337+
jakarta.ws.rs.core.NewCookie c1 = new jakarta.ws.rs.core.NewCookie("name", "value", "path", "domain", "comment", 1, true); // Noncompliant
338+
jakarta.ws.rs.core.NewCookie c2 = new jakarta.ws.rs.core.NewCookie(cookie, "comment", 2, true); // Noncompliant
339+
jakarta.ws.rs.core.NewCookie c3 = new jakarta.ws.rs.core.NewCookie(cookie); // Noncompliant
340+
jakarta.ws.rs.core.NewCookie c4 = new jakarta.ws.rs.core.NewCookie(cookie, "c", 1, true); // Noncompliant
341+
342+
jakarta.ws.rs.core.NewCookie c5 = new jakarta.ws.rs.core.NewCookie(cookie, "c", 1, new Date(), false, true); // last param is HttpOnly
343+
jakarta.ws.rs.core.NewCookie c6 = new jakarta.ws.rs.core.NewCookie("1", "2", "3", "4", 5, "6", 7, new Date(), false, true);
344+
jakarta.ws.rs.core.NewCookie c7 = new jakarta.ws.rs.core.NewCookie("1", "2", "3", "4", "5", 6, false, true);
345+
}
346+
347+
jakarta.ws.rs.core.NewCookie getC3() {
348+
return new jakarta.ws.rs.core.NewCookie("name", "value", "path", "domain", "comment", 1, true); // Noncompliant
349+
}
350+
351+
// SONARJAVA-2772
352+
jakarta.servlet.http.Cookie xsfrToken() {
353+
String cookieName = "XSRF-TOKEN";
354+
355+
jakarta.servlet.http.Cookie xsfrToken = new jakarta.servlet.http.Cookie("XSRF-TOKEN", "value"); // OK, used to implement XSRF
356+
xsfrToken.setHttpOnly(false);
357+
358+
jakarta.servlet.http.Cookie xsfrToken2 = new jakarta.servlet.http.Cookie("XSRF-TOKEN", "value");
359+
xsfrToken2.setHttpOnly(true);
360+
361+
jakarta.servlet.http.Cookie xsfrToken3 = new jakarta.servlet.http.Cookie("XSRF-TOKEN", "value");
362+
363+
jakarta.ws.rs.core.Cookie xsfrToken6 = new jakarta.ws.rs.core.Cookie("XSRF-TOKEN", "value");
364+
365+
jakarta.ws.rs.core.Cookie xsfrToken7 = new jakarta.ws.rs.core.Cookie("XSRF-TOKEN", "value", "path", "domain");
366+
367+
play.mvc.Http.CookieBuilder xsfrToken10;
368+
xsfrToken10 = play.mvc.Http.Cookie.builder("XSRF-TOKEN", "2");
369+
xsfrToken10.withHttpOnly(false);
370+
371+
play.mvc.Http.CookieBuilder xsfrToken11 = play.mvc.Http.Cookie.builder("XSRF-TOKEN", "2");
372+
xsfrToken11.withHttpOnly(false);
373+
374+
jakarta.servlet.http.Cookie xsfrToken12 = new jakarta.servlet.http.Cookie("CSRFToken", "value");
375+
xsfrToken12.setHttpOnly(false);
376+
377+
jakarta.servlet.http.Cookie xsfrToken13 = new jakarta.servlet.http.Cookie("Csrf-token", "value");
378+
xsfrToken13.setHttpOnly(false);
379+
380+
return new jakarta.servlet.http.Cookie("XSRF-TOKEN", "value");
381+
}
382+
}
383+
384+
class JakartaCookieHttpOnlyCheckCookieA extends jakarta.servlet.http.Cookie {
385+
public jakarta.servlet.http.Cookie c;
386+
387+
public JakartaCookieHttpOnlyCheckCookieA() {
388+
super("name", "value");
389+
}
390+
391+
public void setHttpOnly(boolean isHttpOnly) {
392+
}
393+
394+
void foo() {
395+
setHttpOnly(false); // Noncompliant
396+
}
397+
398+
void bar(boolean x) {
399+
setHttpOnly(x);
400+
}
401+
402+
void baz() {
403+
setHttpOnly(true);
404+
}
405+
}
406+
407+
class JakartaCookieHttpOnlyCheckCookieB {
408+
JakartaCookieHttpOnlyCheckCookieA a;
409+
410+
public void setHttpOnly(boolean isHttpOnly) {
411+
}
412+
413+
void foo() {
414+
setHttpOnly(false);
415+
}
416+
417+
void bar() {
418+
return;
419+
}
420+
421+
JakartaCookieHttpOnlyCheckCookieA getC() {
422+
return new JakartaCookieHttpOnlyCheckCookieA(); // Noncompliant
423+
}
424+
425+
void baw() {
426+
int i;
427+
i = 1;
428+
a.c = new jakarta.servlet.http.Cookie("1", "2"); // FN
429+
}
430+
}

0 commit comments

Comments
 (0)