Skip to content

Commit 2758fee

Browse files
SONARJAVA-5134 S2245 considers RandomStringUtils.secure|secureStrong as safe (#4952)
1 parent 8e787b5 commit 2758fee

7 files changed

Lines changed: 29 additions & 12 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public void javaCheckTestSources() throws Exception {
195195
SoftAssertions softly = new SoftAssertions();
196196
softly.assertThat(newDiffs).containsExactlyInAnyOrderElementsOf(knownDiffs.values());
197197
softly.assertThat(newTotal).isEqualTo(knownTotal);
198-
softly.assertThat(rulesCausingFPs).hasSize(11);
198+
softly.assertThat(rulesCausingFPs).hasSize(10);
199199
softly.assertThat(rulesNotReporting).hasSize(10);
200200

201201
/**
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S1874",
33
"hasTruePositives": true,
4-
"falseNegatives": 246,
5-
"falsePositives": 1
4+
"falseNegatives": 257,
5+
"falsePositives": 0
66
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S2245",
33
"hasTruePositives": true,
4-
"falseNegatives": 24,
4+
"falseNegatives": 25,
55
"falsePositives": 0
6-
}
6+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"ruleKey": "S2440",
33
"hasTruePositives": true,
4-
"falseNegatives": 4,
4+
"falseNegatives": 2,
55
"falsePositives": 0
6-
}
6+
}

java-checks-test-sources/default/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@
502502
<dependency>
503503
<groupId>org.apache.commons</groupId>
504504
<artifactId>commons-lang3</artifactId>
505-
<version>3.12.0</version>
505+
<version>3.17.0</version>
506506
<type>jar</type>
507507
<scope>provided</scope>
508508
</dependency>

java-checks-test-sources/default/src/main/java/checks/PseudoRandomCheckSample.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ void fun() {
7474
// ^^^^^^
7575
}
7676

77+
static String randomStringUtilsInstances(int value) {
78+
return switch (value) {
79+
case 0 -> org.apache.commons.lang3.RandomStringUtils.secureStrong().next(42); // Compliant
80+
case 42 -> org.apache.commons.lang3.RandomStringUtils.secure().next(42); // Compliant
81+
default -> org.apache.commons.lang3.RandomStringUtils.insecure().next(42); // Noncompliant
82+
// ^^^^^^^^
83+
};
84+
}
85+
7786
int nextInt() {
7887
return 42;
7988
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,29 @@ public class PseudoRandomCheck extends IssuableSubscriptionVisitor {
3434

3535
private static final String MESSAGE = "Make sure that using this pseudorandom number generator is safe here.";
3636

37+
private static final String LANG3_RANDOM_STRING_UTILS = "org.apache.commons.lang3.RandomStringUtils";
38+
3739
private static final MethodMatchers STATIC_RANDOM_METHODS = MethodMatchers.or(
3840
MethodMatchers.create().ofTypes("java.lang.Math").names("random").addWithoutParametersMatcher().build(),
3941
MethodMatchers.create()
4042
.ofSubTypes("java.util.concurrent.ThreadLocalRandom",
4143
"org.apache.commons.lang.math.RandomUtils",
4244
"org.apache.commons.lang3.RandomUtils",
4345
"org.apache.commons.lang.RandomStringUtils",
44-
"org.apache.commons.lang3.RandomStringUtils")
46+
LANG3_RANDOM_STRING_UTILS)
4547
.anyName()
4648
.withAnyParameters()
4749
.build()
4850
);
4951

52+
private static final MethodMatchers RANDOM_STRING_UTILS_SECURE_INSTANCES = MethodMatchers.create()
53+
.ofSubTypes(LANG3_RANDOM_STRING_UTILS)
54+
.names("secure", "secureStrong")
55+
.withAnyParameters()
56+
.build();
57+
5058
private static final MethodMatchers RANDOM_STRING_UTILS_RANDOM_WITH_RANDOM_SOURCE = MethodMatchers.create()
51-
.ofSubTypes("org.apache.commons.lang.RandomStringUtils", "org.apache.commons.lang3.RandomStringUtils")
59+
.ofSubTypes("org.apache.commons.lang.RandomStringUtils", LANG3_RANDOM_STRING_UTILS)
5260
.names("random")
5361
.addParametersMatcher("int", "int", "int", "boolean", "boolean", "char[]", "java.util.Random")
5462
.build();
@@ -65,8 +73,7 @@ public List<Tree.Kind> nodesToVisit() {
6573

6674
@Override
6775
public void visitNode(Tree tree) {
68-
if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
69-
MethodInvocationTree mit = (MethodInvocationTree) tree;
76+
if (tree instanceof MethodInvocationTree mit) {
7077
IdentifierTree reportLocation = ExpressionUtils.methodName(mit);
7178

7279
if (isStaticCallToInsecureRandomMethod(mit)) {
@@ -83,6 +90,7 @@ public void visitNode(Tree tree) {
8390
private static boolean isStaticCallToInsecureRandomMethod(MethodInvocationTree mit) {
8491
return STATIC_RANDOM_METHODS.matches(mit)
8592
&& !RANDOM_STRING_UTILS_RANDOM_WITH_RANDOM_SOURCE.matches(mit)
93+
&& !RANDOM_STRING_UTILS_SECURE_INSTANCES.matches(mit)
8694
&& mit.methodSymbol().isStatic();
8795
}
8896

0 commit comments

Comments
 (0)