Skip to content

Commit 92a9f13

Browse files
SONARJAVA-4895: Find secure IV byte array factories before detecting S3329 violations
1 parent ac186b8 commit 92a9f13

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

java-checks/src/main/java/org/sonar/java/checks/security/CipherBlockChainingCheck.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
*/
1717
package org.sonar.java.checks.security;
1818

19+
import java.util.ArrayList;
1920
import java.util.HashSet;
21+
import java.util.List;
2022
import java.util.Objects;
2123
import java.util.Set;
2224
import java.util.stream.Stream;
@@ -72,6 +74,38 @@ public class CipherBlockChainingCheck extends AbstractMethodDetection {
7274
.withAnyParameters()
7375
.build();
7476

77+
private @Nullable Tree outermostClass = null;
78+
79+
@Override
80+
public List<Tree.Kind> nodesToVisit() {
81+
var baseNodesToVisit = super.nodesToVisit();
82+
var nodesToVisit = new ArrayList<Tree.Kind>(baseNodesToVisit.size() + 1);
83+
nodesToVisit.addAll(baseNodesToVisit);
84+
nodesToVisit.add(Tree.Kind.CLASS);
85+
86+
return nodesToVisit;
87+
}
88+
89+
@Override
90+
public void visitNode(Tree tree) {
91+
if (outermostClass == null && tree.is(Tree.Kind.CLASS)) {
92+
// We only need run SecureByteArrayFactoryFinder once on the outermost class to find all secure IV byte array factory methods.
93+
// If we apply the finder again to nested classes then we explore the same sub-trees multiple times.
94+
outermostClass = tree;
95+
tree.accept(secureByteArrayFactoryFinder);
96+
}
97+
98+
super.visitNode(tree);
99+
}
100+
101+
@Override
102+
public void leaveNode(Tree tree) {
103+
if (tree == outermostClass) {
104+
secureByteArrayFactoryFinder.clear();
105+
}
106+
super.leaveNode(tree);
107+
}
108+
75109
@Override
76110
protected MethodMatchers getMethodInvocationMatchers() {
77111
return MethodMatchers.create().ofTypes("javax.crypto.spec.IvParameterSpec").constructor()

0 commit comments

Comments
 (0)