|
|
|
@ -18,66 +18,82 @@ import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; |
|
|
|
|
import org.sonar.plugins.java.api.tree.*; |
|
|
|
|
|
|
|
|
|
import java.util.*; |
|
|
|
|
import java.util.stream.Collectors; |
|
|
|
|
|
|
|
|
|
@Rule(key = "PasswordRegexCheck") |
|
|
|
|
public class PasswordRegexCheck extends IssuableSubscriptionVisitor { |
|
|
|
|
|
|
|
|
|
private static final String MATCHER_METHOD = "matcher"; |
|
|
|
|
private static final String PASSWORD_PARAMETER = "password"; |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public List<Tree.Kind> nodesToVisit() { |
|
|
|
|
// 在这里,我们只考虑方法
|
|
|
|
|
return Collections.singletonList(Tree.Kind.METHOD); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void visitNode(Tree tree) { |
|
|
|
|
if (tree.is(Tree.Kind.METHOD)) { |
|
|
|
|
MethodTree methodTree = (MethodTree) tree; |
|
|
|
|
checkPasswordValidation(methodTree); |
|
|
|
|
MethodTree method = (MethodTree) tree; |
|
|
|
|
// 检查是否存在名为 “password”的参数
|
|
|
|
|
List<VariableTree> passwordParameters = method.parameters().stream() |
|
|
|
|
.filter(param -> "password".equals(param.simpleName().name())) |
|
|
|
|
.collect(Collectors.toList()); |
|
|
|
|
|
|
|
|
|
if (!passwordParameters.isEmpty()) { |
|
|
|
|
// 确认参数 "password" 是否在正确的正则表达式之下被检查
|
|
|
|
|
boolean passwordIsCheckedWithRegex = passwordParameters.stream() |
|
|
|
|
.anyMatch(param -> isPasswordValidatedWithRegex(method, param)); |
|
|
|
|
|
|
|
|
|
if (!passwordIsCheckedWithRegex) { |
|
|
|
|
reportIssue(method.simpleName(), "未对口令进行复杂度验证"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void checkPasswordValidation(MethodTree methodTree) { |
|
|
|
|
boolean hasPasswordValidation = false; |
|
|
|
|
|
|
|
|
|
for (StatementTree statement : methodTree.block().body()) { |
|
|
|
|
if (statement.is(Tree.Kind.VARIABLE)) { |
|
|
|
|
VariableTree variableTree = (VariableTree) statement; |
|
|
|
|
if (variableTree.initializer() != null && variableTree.initializer().is(Tree.Kind.METHOD_INVOCATION)) { |
|
|
|
|
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) variableTree.initializer(); |
|
|
|
|
String methodName = methodInvocationTree.methodSymbol().name(); |
|
|
|
|
if (MATCHER_METHOD.equals(methodName)) { |
|
|
|
|
hasPasswordValidation = hasPasswordValidation || hasPasswordParameter(methodInvocationTree.arguments()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean isPasswordValidatedWithRegex(Tree tree, VariableTree passwordParameter) { |
|
|
|
|
PasswordValidationVisitor visitor = new PasswordValidationVisitor(passwordParameter); |
|
|
|
|
tree.accept(visitor); |
|
|
|
|
return visitor.isPasswordValidated(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private static class PasswordValidationVisitor extends BaseTreeVisitor { |
|
|
|
|
|
|
|
|
|
private final VariableTree passwordParameter; |
|
|
|
|
private boolean passwordValidated = false; |
|
|
|
|
|
|
|
|
|
PasswordValidationVisitor(VariableTree passwordParameter) { |
|
|
|
|
this.passwordParameter = passwordParameter; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void visitMethodInvocation(MethodInvocationTree tree) { |
|
|
|
|
// 改变行为,查找是否存在匹配指定方法调用模式的表达式
|
|
|
|
|
if (isCorrectPasswordValidationMethod(tree)) { |
|
|
|
|
passwordValidated = true; |
|
|
|
|
} |
|
|
|
|
super.visitMethodInvocation(tree); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!hasPasswordValidation) { |
|
|
|
|
//如果没有发现密码验证,报告问题
|
|
|
|
|
System.out.println("未对口令进行复杂度验证"+methodTree.simpleName()); |
|
|
|
|
reportIssue(methodTree.simpleName(), "未对口令进行复杂度验证"); |
|
|
|
|
public boolean isPasswordValidated() { |
|
|
|
|
return passwordValidated; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean hasPasswordParameter(List<ExpressionTree> arguments) { |
|
|
|
|
for (ExpressionTree argument : arguments) { |
|
|
|
|
if (argument.is(Tree.Kind.IDENTIFIER)) { |
|
|
|
|
IdentifierTree identifier = (IdentifierTree) argument; |
|
|
|
|
if (PASSWORD_PARAMETER.equalsIgnoreCase(identifier.name())) { |
|
|
|
|
//检查标识符是否有密码验证
|
|
|
|
|
return hasPasswordValidationInMethod(identifier.name()); |
|
|
|
|
private boolean isCorrectPasswordValidationMethod(MethodInvocationTree mit) { |
|
|
|
|
if ("matches".equals(mit.methodSymbol().name()) |
|
|
|
|
&& mit.methodSelect().is(Tree.Kind.MEMBER_SELECT) |
|
|
|
|
&& ((MemberSelectExpressionTree) mit.methodSelect()).identifier().name().equals("matcher") |
|
|
|
|
&& ((MemberSelectExpressionTree) mit.methodSelect()).expression().is(Tree.Kind.METHOD_INVOCATION)) { |
|
|
|
|
|
|
|
|
|
MethodInvocationTree innerMit = (MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression(); |
|
|
|
|
if ("compile".equals(innerMit.methodSymbol().name()) |
|
|
|
|
&& innerMit.methodSelect().is(Tree.Kind.MEMBER_SELECT) |
|
|
|
|
&& "Pattern".equals( ((MemberSelectExpressionTree)innerMit.methodSelect()).expression().toString())) { |
|
|
|
|
// 需要确定参数中包含名为 'password' 的参数
|
|
|
|
|
List<VariableTree> arguments = mit.methodSymbol().declaration().parameters().stream().filter(param -> param.symbol().name().equals("password")).collect(Collectors.toList()); |
|
|
|
|
return (!arguments.isEmpty()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean hasPasswordValidationInMethod(String paramName) { |
|
|
|
|
//参数名是'password',已经存在
|
|
|
|
|
return PASSWORD_PARAMETER.equalsIgnoreCase(paramName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|