10: Create StaticScopeHelper
This commit is contained in:
@@ -2,6 +2,7 @@ package com.bartek.esa.core.di;
|
||||
|
||||
import com.bartek.esa.core.desc.provider.DescriptionProvider;
|
||||
import com.bartek.esa.core.executor.PluginExecutor;
|
||||
import com.bartek.esa.core.helper.StaticScopeHelper;
|
||||
import com.bartek.esa.core.java.JavaSyntaxRegexProvider;
|
||||
import com.bartek.esa.core.xml.XmlHelper;
|
||||
import dagger.Module;
|
||||
@@ -29,4 +30,9 @@ public class CoreModule {
|
||||
public XmlHelper xmlHelper() {
|
||||
return new XmlHelper();
|
||||
}
|
||||
|
||||
@Provides
|
||||
public StaticScopeHelper staticScopeHelper() {
|
||||
return new StaticScopeHelper();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.bartek.esa.core.di;
|
||||
|
||||
import com.bartek.esa.core.archetype.Plugin;
|
||||
import com.bartek.esa.core.helper.StaticScopeHelper;
|
||||
import com.bartek.esa.core.java.JavaSyntaxRegexProvider;
|
||||
import com.bartek.esa.core.plugin.*;
|
||||
import com.bartek.esa.core.xml.XmlHelper;
|
||||
@@ -24,8 +25,8 @@ public class PluginModule {
|
||||
|
||||
@Provides
|
||||
@IntoSet
|
||||
public Plugin loggingPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
|
||||
return new LoggingPlugin(globMatcher, xmlHelper);
|
||||
public Plugin loggingPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper, StaticScopeHelper staticScopeHelper) {
|
||||
return new LoggingPlugin(globMatcher, xmlHelper, staticScopeHelper);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@@ -72,13 +73,13 @@ public class PluginModule {
|
||||
|
||||
@Provides
|
||||
@IntoSet
|
||||
public Plugin cipherInstancePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
|
||||
return new CipherInstancePlugin(globMatcher, xmlHelper);
|
||||
public Plugin cipherInstancePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper, StaticScopeHelper staticScopeHelper) {
|
||||
return new CipherInstancePlugin(globMatcher, xmlHelper, staticScopeHelper);
|
||||
}
|
||||
|
||||
@Provides
|
||||
@IntoSet
|
||||
public Plugin strictModePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
|
||||
return new StrictModePlugin(globMatcher, xmlHelper);
|
||||
public Plugin strictModePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper, StaticScopeHelper staticScopeHelper) {
|
||||
return new StrictModePlugin(globMatcher, xmlHelper, staticScopeHelper);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.bartek.esa.core.helper;
|
||||
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.ImportDeclaration;
|
||||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.expr.*;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import static java.lang.String.format;
|
||||
|
||||
public class StaticScopeHelper {
|
||||
|
||||
@Inject
|
||||
public StaticScopeHelper() {
|
||||
|
||||
}
|
||||
|
||||
public Predicate<MethodCallExpr> isFromScope(CompilationUnit compilationUnit, String methodName, String scope, String importScope) {
|
||||
return expr -> {
|
||||
boolean isFromScope = expr.getScope()
|
||||
.filter(Expression::isNameExpr)
|
||||
.map(Expression::asNameExpr)
|
||||
.map(NameExpr::getName)
|
||||
.map(SimpleName::getIdentifier)
|
||||
.map(s -> s.equals(scope))
|
||||
.orElse(false);
|
||||
|
||||
if(!isFromScope) {
|
||||
isFromScope = compilationUnit.findAll(ImportDeclaration.class).stream()
|
||||
.filter(ImportDeclaration::isStatic)
|
||||
.filter(e -> e.getName().getIdentifier().matches(methodName))
|
||||
.map(ImportDeclaration::getName)
|
||||
.map(Name::getQualifier)
|
||||
.flatMap(Optional::stream)
|
||||
.map(Node::toString)
|
||||
.anyMatch(q -> q.equals(format("%s.%s", importScope, scope)));
|
||||
}
|
||||
|
||||
if(!isFromScope) {
|
||||
isFromScope = compilationUnit.findAll(ImportDeclaration.class).stream()
|
||||
.filter(ImportDeclaration::isStatic)
|
||||
.filter(ImportDeclaration::isAsterisk)
|
||||
.map(ImportDeclaration::getName)
|
||||
.map(Name::getQualifier)
|
||||
.flatMap(Optional::stream)
|
||||
.map(Node::toString)
|
||||
.anyMatch(q -> q.equals(importScope));
|
||||
}
|
||||
|
||||
return isFromScope;
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,70 +1,36 @@
|
||||
package com.bartek.esa.core.plugin;
|
||||
|
||||
import com.bartek.esa.core.archetype.JavaPlugin;
|
||||
import com.bartek.esa.core.helper.StaticScopeHelper;
|
||||
import com.bartek.esa.core.model.enumeration.Severity;
|
||||
import com.bartek.esa.core.xml.XmlHelper;
|
||||
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.ImportDeclaration;
|
||||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.expr.*;
|
||||
import com.github.javaparser.ast.expr.MethodCallExpr;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Optional;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class CipherInstancePlugin extends JavaPlugin {
|
||||
private static final Pattern ALGORITHM_QUALIFIER = Pattern.compile("^\"\\w+/\\w+/\\w+\"$");
|
||||
private final StaticScopeHelper staticScopeHelper;
|
||||
|
||||
@Inject
|
||||
public CipherInstancePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
|
||||
public CipherInstancePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper, StaticScopeHelper staticScopeHelper) {
|
||||
super(globMatcher, xmlHelper);
|
||||
this.staticScopeHelper = staticScopeHelper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(CompilationUnit compilationUnit) {
|
||||
compilationUnit.findAll(MethodCallExpr.class).stream()
|
||||
.filter(expr -> expr.getName().getIdentifier().equals("getInstance"))
|
||||
.filter(expr -> isCipherMethod(expr, compilationUnit))
|
||||
.filter(staticScopeHelper.isFromScope(compilationUnit, "getInstance", "Cipher", "javax.crypto"))
|
||||
.filter(expr -> expr.getArguments().isNonEmpty())
|
||||
.filter(expr -> !isFullCipherQualifier(expr.getArguments().get(0).toString()))
|
||||
.forEach(expr -> addIssue(Severity.ERROR, getLineNumberFromExpression(expr), expr.toString()));
|
||||
}
|
||||
|
||||
private boolean isCipherMethod(MethodCallExpr expr, CompilationUnit compilationUnit) {
|
||||
boolean isCipherMethod = expr.getScope()
|
||||
.filter(Expression::isNameExpr)
|
||||
.map(Expression::asNameExpr)
|
||||
.map(NameExpr::getName)
|
||||
.map(SimpleName::getIdentifier)
|
||||
.filter(name -> name.equals("Cipher"))
|
||||
.isPresent();
|
||||
|
||||
if(!isCipherMethod) {
|
||||
isCipherMethod = compilationUnit.findAll(ImportDeclaration.class).stream()
|
||||
.filter(ImportDeclaration::isStatic)
|
||||
.filter(e -> e.getName().getIdentifier().equals("getInstance"))
|
||||
.map(ImportDeclaration::getName)
|
||||
.map(Name::getQualifier)
|
||||
.flatMap(Optional::stream)
|
||||
.map(Node::toString)
|
||||
.anyMatch(q -> q.equals("javax.crypto.Cipher"));
|
||||
}
|
||||
|
||||
if(!isCipherMethod) {
|
||||
isCipherMethod = compilationUnit.findAll(ImportDeclaration.class).stream()
|
||||
.filter(ImportDeclaration::isStatic)
|
||||
.filter(ImportDeclaration::isAsterisk)
|
||||
.map(ImportDeclaration::getName)
|
||||
.map(Name::getQualifier)
|
||||
.flatMap(Optional::stream)
|
||||
.map(Node::toString)
|
||||
.anyMatch(q -> q.equals("javax.crypto"));
|
||||
}
|
||||
|
||||
return isCipherMethod;
|
||||
}
|
||||
|
||||
private boolean isFullCipherQualifier(String qualifier) {
|
||||
return ALGORITHM_QUALIFIER.matcher(qualifier).matches();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.bartek.esa.core.plugin;
|
||||
|
||||
import com.bartek.esa.core.archetype.JavaPlugin;
|
||||
import com.bartek.esa.core.helper.StaticScopeHelper;
|
||||
import com.bartek.esa.core.model.enumeration.Severity;
|
||||
import com.bartek.esa.core.xml.XmlHelper;
|
||||
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||
@@ -10,16 +11,19 @@ import com.github.javaparser.ast.expr.MethodCallExpr;
|
||||
import javax.inject.Inject;
|
||||
|
||||
public class LoggingPlugin extends JavaPlugin {
|
||||
private final StaticScopeHelper staticScopeHelper;
|
||||
|
||||
@Inject
|
||||
public LoggingPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
|
||||
public LoggingPlugin(GlobMatcher globMatcher, XmlHelper xmlHelper, StaticScopeHelper staticScopeHelper) {
|
||||
super(globMatcher, xmlHelper);
|
||||
this.staticScopeHelper = staticScopeHelper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(CompilationUnit compilationUnit) {
|
||||
compilationUnit.findAll(MethodCallExpr.class).stream()
|
||||
.filter(expr -> expr.getName().getIdentifier().matches("v|d|i|w|e|wtf"))
|
||||
.filter(staticScopeHelper.isFromScope(compilationUnit, "v|d|i|w|e|wtf", "Log", "android.util"))
|
||||
.forEach(expr -> addIssue(Severity.INFO, getLineNumberFromExpression(expr), expr.toString()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,63 +1,29 @@
|
||||
package com.bartek.esa.core.plugin;
|
||||
|
||||
import com.bartek.esa.core.archetype.JavaPlugin;
|
||||
import com.bartek.esa.core.helper.StaticScopeHelper;
|
||||
import com.bartek.esa.core.model.enumeration.Severity;
|
||||
import com.bartek.esa.core.xml.XmlHelper;
|
||||
import com.bartek.esa.file.matcher.GlobMatcher;
|
||||
import com.github.javaparser.ast.CompilationUnit;
|
||||
import com.github.javaparser.ast.ImportDeclaration;
|
||||
import com.github.javaparser.ast.Node;
|
||||
import com.github.javaparser.ast.expr.*;
|
||||
import com.github.javaparser.ast.expr.MethodCallExpr;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.util.Optional;
|
||||
|
||||
public class StrictModePlugin extends JavaPlugin {
|
||||
private final StaticScopeHelper staticScopeHelper;
|
||||
|
||||
@Inject
|
||||
public StrictModePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) {
|
||||
public StrictModePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper, StaticScopeHelper staticScopeHelper) {
|
||||
super(globMatcher, xmlHelper);
|
||||
this.staticScopeHelper = staticScopeHelper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(CompilationUnit compilationUnit) {
|
||||
compilationUnit.findAll(MethodCallExpr.class).stream()
|
||||
.filter(expr -> expr.getName().getIdentifier().equals("setThreadPolicy"))
|
||||
.filter(expr -> isStrictModeScope(expr, compilationUnit))
|
||||
.filter(staticScopeHelper.isFromScope(compilationUnit, "setThreadPolicy", "StrictMode", "android.os"))
|
||||
.forEach(expr -> addIssue(Severity.INFO, getLineNumberFromExpression(expr), expr.toString()));
|
||||
}
|
||||
|
||||
private boolean isStrictModeScope(MethodCallExpr expr, CompilationUnit compilationUnit) {
|
||||
boolean isStrictModeScope = expr.getScope()
|
||||
.filter(Expression::isNameExpr)
|
||||
.map(Expression::asNameExpr)
|
||||
.map(NameExpr::getName)
|
||||
.map(SimpleName::getIdentifier)
|
||||
.map(s -> s.equals("StrictMode"))
|
||||
.orElse(false);
|
||||
|
||||
if(!isStrictModeScope) {
|
||||
isStrictModeScope = compilationUnit.findAll(ImportDeclaration.class).stream()
|
||||
.filter(ImportDeclaration::isStatic)
|
||||
.filter(e -> e.getName().getIdentifier().equals("setThreadPolicy"))
|
||||
.map(ImportDeclaration::getName)
|
||||
.map(Name::getQualifier)
|
||||
.flatMap(Optional::stream)
|
||||
.map(Node::toString)
|
||||
.anyMatch(q -> q.equals("android.os.StrictMode"));
|
||||
}
|
||||
|
||||
if(!isStrictModeScope) {
|
||||
isStrictModeScope = compilationUnit.findAll(ImportDeclaration.class).stream()
|
||||
.filter(ImportDeclaration::isStatic)
|
||||
.filter(ImportDeclaration::isAsterisk)
|
||||
.map(ImportDeclaration::getName)
|
||||
.map(Name::getQualifier)
|
||||
.flatMap(Optional::stream)
|
||||
.map(Node::toString)
|
||||
.anyMatch(q -> q.equals("android.os"));
|
||||
}
|
||||
|
||||
return isStrictModeScope;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user