diff --git a/framework/security/src/main/java/org/apache/ofbiz/security/SecuredUpload.java b/framework/security/src/main/java/org/apache/ofbiz/security/SecuredUpload.java index e3fb163488..4dc79d7b48 100644 --- a/framework/security/src/main/java/org/apache/ofbiz/security/SecuredUpload.java +++ b/framework/security/src/main/java/org/apache/ofbiz/security/SecuredUpload.java @@ -34,6 +34,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; @@ -615,47 +616,53 @@ public class SecuredUpload { return false; } String content = new String(bytesFromFile); - return isValidText(content); + ArrayList allowed = new ArrayList<>(); + return isValidText(content, allowed); } - public static boolean isValidText(String content) throws IOException { - return !(content.toLowerCase().contains("freemarker") // Should be OK, should not be used in Freemarker templates, not part of the syntax. - // Else "template.utility.Execute" is a good replacement but not as much catching, who - // knows... - || content.toLowerCase().contains("import=\"java") - || content.toLowerCase().contains("runtime.getruntime().exec(") - || content.toLowerCase().contains("<%@ page") - || content.toLowerCase().contains("") - || content.toLowerCase().contains(" allowed) throws IOException { + // "freemarker" should be OK, should not be used in Freemarker templates, not part of the syntax. + // Else "template.utility.Execute" is a good replacement but not as much catching, who knows... + return isValid(content, "freemarker", allowed) + && isValid(content, "freemarker", allowed) + && isValid(content, "import=\"java", allowed) + && isValid(content, "runtime.getruntime().exec(", allowed) + && isValid(content, "<%@ page", allowed) + && isValid(content, "", allowed) + && isValid(content, " allowed) { + return !content.toLowerCase().contains(string) || allowed.contains(string); + } + private static void deleteBadFile(String fileToCheck) { Debug.logError("File :" + fileToCheck + ", can't be uploaded for security reason", MODULE); File badFile = new File(fileToCheck); diff --git a/framework/webtools/groovyScripts/entity/ProgramExport.groovy b/framework/webtools/groovyScripts/entity/ProgramExport.groovy index 41d29e6e1f..12b82939f2 100644 --- a/framework/webtools/groovyScripts/entity/ProgramExport.groovy +++ b/framework/webtools/groovyScripts/entity/ProgramExport.groovy @@ -20,6 +20,7 @@ import org.apache.ofbiz.entity.Delegator import org.apache.ofbiz.entity.GenericValue import org.apache.ofbiz.entity.model.ModelEntity import org.apache.ofbiz.base.util.* + import org.w3c.dom.Document import org.codehaus.groovy.control.customizers.ImportCustomizer @@ -31,8 +32,7 @@ String groovyProgram = null recordValues = [] errMsgList = [] -if (UtilValidate.isEmpty(parameters.groovyProgram)) { - +if (!parameters.groovyProgram) { groovyProgram = ''' // Use the List variable recordValues to fill it with GenericValue maps. // full groovy syntaxt is available @@ -73,10 +73,7 @@ def shell = new GroovyShell(loader, binding, configuration) if (UtilValidate.isNotEmpty(groovyProgram)) { try { - // TODO more can be added... - if (groovyProgram.contains("new File") - || groovyProgram.contains(".jsp") - || groovyProgram.contains("<%=")) { + if (!org.apache.ofbiz.security.SecuredUpload.isValidText(groovyProgram,["import"])) { request.setAttribute("_ERROR_MESSAGE_", "Not executed for security reason") return }