From 378b2fa967adaccd878740f62538b7ee201a388f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Sat, 6 Apr 2019 11:32:10 +0200 Subject: [PATCH] 10: Create StrictModePlugin --- .../com/bartek/esa/core/di/PluginModule.java | 6 ++ .../esa/core/plugin/StrictModePlugin.java | 63 +++++++++++++++++++ src/main/resources/description.properties | 5 +- 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bartek/esa/core/plugin/StrictModePlugin.java diff --git a/src/main/java/com/bartek/esa/core/di/PluginModule.java b/src/main/java/com/bartek/esa/core/di/PluginModule.java index 9c2e8ba..174f2bc 100644 --- a/src/main/java/com/bartek/esa/core/di/PluginModule.java +++ b/src/main/java/com/bartek/esa/core/di/PluginModule.java @@ -75,4 +75,10 @@ public class PluginModule { public Plugin cipherInstancePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) { return new CipherInstancePlugin(globMatcher, xmlHelper); } + + @Provides + @IntoSet + public Plugin strictModePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) { + return new StrictModePlugin(globMatcher, xmlHelper); + } } diff --git a/src/main/java/com/bartek/esa/core/plugin/StrictModePlugin.java b/src/main/java/com/bartek/esa/core/plugin/StrictModePlugin.java new file mode 100644 index 0000000..e31aac0 --- /dev/null +++ b/src/main/java/com/bartek/esa/core/plugin/StrictModePlugin.java @@ -0,0 +1,63 @@ +package com.bartek.esa.core.plugin; + +import com.bartek.esa.core.archetype.JavaPlugin; +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 javax.inject.Inject; +import java.util.Optional; + +public class StrictModePlugin extends JavaPlugin { + + @Inject + public StrictModePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper) { + super(globMatcher, xmlHelper); + } + + @Override + public void run(CompilationUnit compilationUnit) { + compilationUnit.findAll(MethodCallExpr.class).stream() + .filter(expr -> expr.getName().getIdentifier().equals("setThreadPolicy")) + .filter(expr -> isStrictModeScope(expr, compilationUnit)) + .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; + } +} diff --git a/src/main/resources/description.properties b/src/main/resources/description.properties index 87a867b..8424a59 100644 --- a/src/main/resources/description.properties +++ b/src/main/resources/description.properties @@ -75,4 +75,7 @@ com.bartek.esa.core.plugin.CipherInstancePlugin=Not fully-qualified algorithm na Passing a shortcut instead of fully-qualified algorithm name in Cipher.getInstance() method is not portable across providers\n\ and can impact the system low secure than intended to be.\n\ Fully-qualified name matches the pattern: algorithm/mode/pattern\n\ - For example: AES/CBC/PKCS5Padding \ No newline at end of file + For example: AES/CBC/PKCS5Padding + +com.bartek.esa.core.plugin.StrictModePlugin=Strict mode is turned on.\n\ + Strict mode was found in the file. Remember to delete it before publishing. \ No newline at end of file