From 44608456eb34afb7bd03b369869775ab28d94349 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Pluta?= Date: Tue, 9 Apr 2019 14:48:10 +0200 Subject: [PATCH] 10: Create ExternalStoragePlugin --- .../com/bartek/esa/core/di/PluginModule.java | 7 +++ .../core/plugin/ExternalStoragePlugin.java | 44 +++++++++++++++++++ src/main/resources/description.properties | 6 ++- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/main/java/com/bartek/esa/core/plugin/ExternalStoragePlugin.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 86d313c..b1e574c 100644 --- a/src/main/java/com/bartek/esa/core/di/PluginModule.java +++ b/src/main/java/com/bartek/esa/core/di/PluginModule.java @@ -1,6 +1,7 @@ package com.bartek.esa.core.di; import com.bartek.esa.core.archetype.Plugin; +import com.bartek.esa.core.helper.ParentNodeFinder; import com.bartek.esa.core.helper.StaticScopeHelper; import com.bartek.esa.core.java.JavaSyntaxRegexProvider; import com.bartek.esa.core.plugin.*; @@ -82,4 +83,10 @@ public class PluginModule { public Plugin strictModePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper, StaticScopeHelper staticScopeHelper) { return new StrictModePlugin(globMatcher, xmlHelper, staticScopeHelper); } + + @Provides + @IntoSet + public Plugin externalStoragePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper, ParentNodeFinder parentNodeFinder) { + return new ExternalStoragePlugin(globMatcher, xmlHelper, parentNodeFinder); + } } diff --git a/src/main/java/com/bartek/esa/core/plugin/ExternalStoragePlugin.java b/src/main/java/com/bartek/esa/core/plugin/ExternalStoragePlugin.java new file mode 100644 index 0000000..2241c37 --- /dev/null +++ b/src/main/java/com/bartek/esa/core/plugin/ExternalStoragePlugin.java @@ -0,0 +1,44 @@ +package com.bartek.esa.core.plugin; + +import com.bartek.esa.core.archetype.JavaPlugin; +import com.bartek.esa.core.helper.ParentNodeFinder; +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.body.MethodDeclaration; +import com.github.javaparser.ast.expr.MethodCallExpr; + +import javax.inject.Inject; + +public class ExternalStoragePlugin extends JavaPlugin { + private final ParentNodeFinder parentNodeFinder; + + @Inject + public ExternalStoragePlugin(GlobMatcher globMatcher, XmlHelper xmlHelper, ParentNodeFinder parentNodeFinder) { + super(globMatcher, xmlHelper); + this.parentNodeFinder = parentNodeFinder; + } + + @Override + public void run(CompilationUnit compilationUnit) { + compilationUnit.findAll(MethodCallExpr.class).stream() + .filter(expr -> expr.getName().getIdentifier().matches("getExternalStorageDirectory|getExternalStoragePublicDirectory")) + .forEach(this::findCheckingStorageStateForAccessingExternalStorage); + } + + private void findCheckingStorageStateForAccessingExternalStorage(MethodCallExpr accessingToExternalStorage) { + parentNodeFinder.findParentNode(accessingToExternalStorage, MethodDeclaration.class).ifPresent(methodDeclaration -> + findCheckingStorageStateInMethodDeclaration(accessingToExternalStorage, methodDeclaration) + ); + } + + private void findCheckingStorageStateInMethodDeclaration(MethodCallExpr accessingToExternalStorage, MethodDeclaration methodDeclaration) { + boolean isStateBeingChecked = methodDeclaration.findAll(MethodCallExpr.class).stream() + .anyMatch(e -> e.getName().getIdentifier().equals("getExternalStorageState")); + + if (!isStateBeingChecked) { + addIssue(Severity.WARNING, getLineNumberFromExpression(accessingToExternalStorage), accessingToExternalStorage.toString()); + } + } +} diff --git a/src/main/resources/description.properties b/src/main/resources/description.properties index 8424a59..5d53f9a 100644 --- a/src/main/resources/description.properties +++ b/src/main/resources/description.properties @@ -78,4 +78,8 @@ com.bartek.esa.core.plugin.CipherInstancePlugin=Not fully-qualified algorithm na 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 + Strict mode was found in the file. Remember to delete it before publishing. + +com.bartek.esa.core.plugin.ExternalStoragePlugin=External storage state is not checked.\n\ + There is attempt to access to external storage without checking its state.\n\ + External storage state should be checked through 'Environment.getExternalStorageState()' method. \ No newline at end of file