Skip to content

Commit 6d79358

Browse files
committed
SONARJAVA-1212 Added getInstance other signatures
1 parent 5becb99 commit 6d79358

2 files changed

Lines changed: 26 additions & 14 deletions

File tree

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

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.sonar.check.Rule;
2828
import org.sonar.java.checks.methods.AbstractMethodDetection;
2929
import org.sonar.java.checks.methods.MethodMatcher;
30+
import org.sonar.java.checks.methods.TypeCriteria;
3031
import org.sonar.java.model.LiteralUtils;
3132
import org.sonar.plugins.java.api.tree.ExpressionTree;
3233
import org.sonar.plugins.java.api.tree.IdentifierTree;
@@ -49,8 +50,9 @@
4950
@SqaleConstantRemediation("30min")
5051
public class DeprecatedHashAlgorithmCheck extends AbstractMethodDetection {
5152

53+
private static final String JAVA_LANG_STRING = "java.lang.String";
5254
private static final String MD5 = "MD5";
53-
private static final String SHA1 = "SHA-1";
55+
private static final String SHA1 = "SHA1";
5456

5557
private static final Map<String, String> ALGORITHM_BY_METHOD_NAME = ImmutableMap.<String, String>builder()
5658
.put("getMd5Digest", MD5)
@@ -70,11 +72,16 @@ protected List<MethodMatcher> getMethodInvocationMatchers() {
7072
.add(MethodMatcher.create()
7173
.typeDefinition("java.security.MessageDigest")
7274
.name("getInstance")
73-
.addParameter("java.lang.String"))
75+
.addParameter(JAVA_LANG_STRING))
76+
.add(MethodMatcher.create()
77+
.typeDefinition("java.security.MessageDigest")
78+
.name("getInstance")
79+
.addParameter(JAVA_LANG_STRING)
80+
.addParameter(TypeCriteria.anyType()))
7481
.add(MethodMatcher.create()
7582
.typeDefinition("org.apache.commons.codec.digest.DigestUtils")
7683
.name("getDigest")
77-
.addParameter("java.lang.String"));
84+
.addParameter(JAVA_LANG_STRING));
7885
for (String methodName : ALGORITHM_BY_METHOD_NAME.keySet()) {
7986
builder.add(MethodMatcher.create()
8087
.typeDefinition("org.apache.commons.codec.digest.DigestUtils")
@@ -94,30 +101,31 @@ protected void onMethodInvocationFound(MethodInvocationTree mit) {
94101
String methodName = methodName(mit);
95102
String algorithm = ALGORITHM_BY_METHOD_NAME.get(methodName);
96103
if (algorithm == null) {
97-
List<ExpressionTree> arguments = mit.arguments();
98-
algorithm = algorithm(arguments.get(0));
104+
algorithm = algorithm(mit.arguments().get(0));
99105
}
100-
if (MD5.equals(algorithm) || SHA1.equals(algorithm)) {
101-
addIssue(mit, "Use a stronger encryption algorithm than " + algorithm + ".");
106+
boolean isMd5 = MD5.equalsIgnoreCase(algorithm);
107+
boolean isSha1 = SHA1.equalsIgnoreCase(algorithm);
108+
if (isMd5 || isSha1) {
109+
String msgAlgo = isSha1 ? "SHA-1" : algorithm;
110+
addIssue(mit, "Use a stronger encryption algorithm than " + msgAlgo + ".");
102111
}
103112
}
104113

105114
private static String methodName(MethodInvocationTree mit) {
106115
String name = null;
107116
ExpressionTree methodSelect = mit.methodSelect();
108117
if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
109-
MemberSelectExpressionTree memberSelectExpressionTree = (MemberSelectExpressionTree) methodSelect;
110-
name = memberSelectExpressionTree.identifier().name();
118+
name = ((MemberSelectExpressionTree) methodSelect).identifier().name();
111119
} else if (methodSelect.is(Tree.Kind.IDENTIFIER)) {
112-
IdentifierTree identifier = (IdentifierTree) methodSelect;
113-
name = identifier.name();
120+
name = ((IdentifierTree) methodSelect).name();
114121
}
115122
return name;
116123
}
117124

118125
private static String algorithm(ExpressionTree invocationArgument) {
119126
if (invocationArgument.is(Tree.Kind.STRING_LITERAL)) {
120-
return LiteralUtils.trimQuotes(((LiteralTree) invocationArgument).value());
127+
String algo = LiteralUtils.trimQuotes(((LiteralTree) invocationArgument).value());
128+
return algo.replaceAll("-", "");
121129
}
122130
return null;
123131
}

java-checks/src/test/files/checks/DeprecatedHashAlgorithmCheck.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import java.security.MessageDigest;
2+
import java.security.Provider;
23
import org.apache.commons.codec.digest.DigestUtils;
34
import static org.apache.commons.codec.digest.DigestUtils.md5Hex;
45

56
class A {
6-
void myMethod(String algorithm) {
7+
void myMethod(String algorithm, Provider provider) {
78
MessageDigest md = null;
89
md = MessageDigest.getInstance("MD5"); // Noncompliant {{Use a stronger encryption algorithm than MD5.}}
910
md = MessageDigest.getInstance("SHA-1"); // Noncompliant {{Use a stronger encryption algorithm than SHA-1.}}
@@ -29,6 +30,9 @@ void myMethod(String algorithm) {
2930
com.google.common.hash.Hashing.md5(); // Noncompliant
3031
com.google.common.hash.Hashing.sha1(); // Noncompliant
3132
com.google.common.hash.Hashing.sha256();
33+
md = MessageDigest.getInstance("MD5", provider); // Noncompliant
34+
md = MessageDigest.getInstance("SHA1", "provider"); // Noncompliant
35+
md = MessageDigest.getInstance("sha-1", "provider"); // Noncompliant
3236
}
3337

34-
}
38+
}

0 commit comments

Comments
 (0)