You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by sh...@apache.org on 2020/07/24 08:52:30 UTC

[unomi] 01/01: Improve scripting security: - OGNL is now disabled by default as it wasn't really used much (possible to reactive through a system setting) - A new condition sanitizer has been added in the ContextServlet to filter out any MVEL scripts (again not used much and can be reactivate using a system property) - A new ExpressionFilter has been added that will use configurable (system property) regular expressions to filter out possible malicious expressions - OGNL sandboxing has been improved - MV [...]

This is an automated email from the ASF dual-hosted git repository.

shuber pushed a commit to branch improve-scripting-security
in repository https://gitbox.apache.org/repos/asf/unomi.git

commit 28895c512affa214c4cf9cb4148cb7f3ca7f9548
Author: Serge Huber <sh...@apache.org>
AuthorDate: Fri Jul 24 10:52:21 2020 +0200

    Improve scripting security:
    - OGNL is now disabled by default as it wasn't really used much (possible to reactive through a system setting)
    - A new condition sanitizer has been added in the ContextServlet to filter out any MVEL scripts (again not used much and can be reactivate using a system property)
    - A new ExpressionFilter has been added that will use configurable (system property) regular expressions to filter out possible malicious expressions
    - OGNL sandboxing has been improved
    - MVEL default imports have been modified to prevent using system-level classes
---
 common/pom.xml                                     | 41 ++++++++++
 .../org/apache/unomi/common/ExpressionFilter.java  | 94 ++++++++++++++++++++++
 .../apache/unomi/common/MvelScriptExecutor.java    | 71 ++++++++++++++++
 .../org/apache/unomi/common/ScriptExecutor.java    | 28 +++++++
 .../unomi/common/SecureFilteringClassLoader.java   | 20 ++++-
 .../unomi/common/MvelScriptExecutorTest.java       | 65 +++++++--------
 .../test/java/org/apache/unomi/itests/BaseIT.java  | 37 +++++++--
 .../org/apache/unomi/itests/ContextServletIT.java  | 57 +++++++++++--
 .../test/resources/security/mvel-payload-1.json    | 20 +++++
 .../test/resources/security/ognl-payload-1.json    | 28 +++++++
 .../main/resources/etc/custom.system.properties    |  7 +-
 persistence-elasticsearch/core/pom.xml             |  4 -
 .../conditions/ConditionContextHelper.java         | 29 ++-----
 .../conditions/PropertyConditionEvaluator.java     | 90 ++++++++++++++++++++-
 .../conditions/PropertyConditionEvaluatorTest.java |  9 ++-
 services/pom.xml                                   |  5 --
 .../services/actions/ActionExecutorDispatcher.java | 33 +++-----
 .../java/org/apache/unomi/web/ContextServlet.java  | 84 ++++++++++++++++++-
 18 files changed, 608 insertions(+), 114 deletions(-)

diff --git a/common/pom.xml b/common/pom.xml
index 5321dd7..d1b4e0a 100644
--- a/common/pom.xml
+++ b/common/pom.xml
@@ -54,6 +54,47 @@
             <version>${version.karaf}</version>
             <scope>provided</scope>
         </dependency>
+
+        <dependency>
+            <groupId>org.mvel</groupId>
+            <artifactId>mvel2</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.unomi</groupId>
+            <artifactId>unomi-api</artifactId>
+            <version>2.0.0-SNAPSHOT</version>
+            <scope>test</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>1.6.6</version>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
+                        <Import-Package>
+                            org.apache.unomi.api,
+                            org.apache.unomi.api.conditions,
+                            org.apache.unomi.api.goals,
+                            *
+                        </Import-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
 </project>
diff --git a/common/src/main/java/org/apache/unomi/common/ExpressionFilter.java b/common/src/main/java/org/apache/unomi/common/ExpressionFilter.java
new file mode 100644
index 0000000..245197f
--- /dev/null
+++ b/common/src/main/java/org/apache/unomi/common/ExpressionFilter.java
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.unomi.common;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.regex.Pattern;
+
+public class ExpressionFilter {
+
+    private static final Logger logger = LoggerFactory.getLogger(ExpressionFilter.class.getName());
+
+    Set<Pattern> allowedExpressionPatterns = null;
+    Set<Pattern> forbiddenExpressionPatterns = null;
+
+    private static Set<Pattern> defaultAllowedExpressionPatterns = null;
+    private static Set<Pattern> defaultForbiddenExpressionPatterns = null;
+
+    static {
+        String systemAllowedPatterns = System.getProperty("org.apache.unomi.scripting.filter.allow", "all");
+        if (systemAllowedPatterns != null) {
+            if ("all".equals(systemAllowedPatterns.trim())) {
+                defaultAllowedExpressionPatterns = null;
+            } else {
+                if (systemAllowedPatterns.trim().length() > 0) {
+                    String[] systemAllowedPatternParts = systemAllowedPatterns.split(",");
+                    defaultAllowedExpressionPatterns = new HashSet<>();
+                    for (String systemAllowedPatternPart : systemAllowedPatternParts) {
+                        defaultAllowedExpressionPatterns.add(Pattern.compile(systemAllowedPatternPart));
+                    }
+                } else {
+                    defaultAllowedExpressionPatterns = null;
+                }
+            }
+        }
+
+        String systemForbiddenPatterns = System.getProperty("org.apache.unomi.scripting.filter.forbid", ".*Runtime.*,.*ProcessBuilder.*,.*exec.*,.*invoke.*,.*getClass.*,.*Class.*,.*ClassLoader.*,.*System.*,.*Method.*,.*method.*,.*Compiler.*,.*Thread.*,.*FileWriter.*,.*forName.*,.*Socket.*,.*DriverManager.*");
+        if (systemForbiddenPatterns != null) {
+            if (systemForbiddenPatterns.trim().length() > 0) {
+                String[] systemForbiddenPatternParts = systemForbiddenPatterns.split(",");
+                defaultForbiddenExpressionPatterns = new HashSet<>();
+                for (String systemForbiddenPatternPart : systemForbiddenPatternParts) {
+                    defaultForbiddenExpressionPatterns.add(Pattern.compile(systemForbiddenPatternPart));
+                }
+            } else {
+                defaultForbiddenExpressionPatterns = null;
+            }
+        }
+    }
+
+    public ExpressionFilter() {
+        allowedExpressionPatterns = defaultAllowedExpressionPatterns;
+        forbiddenExpressionPatterns = defaultForbiddenExpressionPatterns;
+    }
+
+    public String filter(String expression) {
+        if (forbiddenExpressionPatterns != null && expressionMatches(expression, forbiddenExpressionPatterns)) {
+            logger.warn("Expression {} is forbidden by expression filter", expression);
+            return null;
+        }
+        if (allowedExpressionPatterns != null && !expressionMatches(expression, allowedExpressionPatterns)) {
+            logger.warn("Expression {} is not allowed by expression filter", expression);
+            return null;
+        }
+        return expression;
+    }
+
+    private boolean expressionMatches(String expression, Set<Pattern> patterns) {
+        for (Pattern pattern : patterns) {
+            if (pattern.matcher(expression).matches()) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+}
diff --git a/common/src/main/java/org/apache/unomi/common/MvelScriptExecutor.java b/common/src/main/java/org/apache/unomi/common/MvelScriptExecutor.java
new file mode 100644
index 0000000..b205e50
--- /dev/null
+++ b/common/src/main/java/org/apache/unomi/common/MvelScriptExecutor.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.unomi.common;
+
+import org.mvel2.MVEL;
+import org.mvel2.ParserConfiguration;
+import org.mvel2.ParserContext;
+
+import java.io.Serializable;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class MvelScriptExecutor implements ScriptExecutor {
+
+    private Map<String, Serializable> mvelExpressions = new ConcurrentHashMap<>();
+    private SecureFilteringClassLoader secureFilteringClassLoader = new SecureFilteringClassLoader(getClass().getClassLoader());
+    private ExpressionFilter expressionFilter = new ExpressionFilter();
+
+    @Override
+    public Object execute(String script, Map<String, Object> context) {
+
+        final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
+        try {
+            if (!mvelExpressions.containsKey(script)) {
+
+                if (expressionFilter.filter(script) == null) {
+                    mvelExpressions.put(script, null);
+                } else {
+                    Thread.currentThread().setContextClassLoader(secureFilteringClassLoader);
+                    ParserConfiguration parserConfiguration = new ParserConfiguration();
+                    parserConfiguration.setClassLoader(secureFilteringClassLoader);
+                    ParserContext parserContext = new ParserContext(parserConfiguration);
+
+                    // override hardcoded Class Literals that are inserted by default in MVEL and that may be a security risk
+                    parserContext.addImport("Runtime", String.class);
+                    parserContext.addImport("System", String.class);
+                    parserContext.addImport("ProcessBuilder", String.class);
+                    parserContext.addImport("Class", String.class);
+                    parserContext.addImport("ClassLoader", String.class);
+                    parserContext.addImport("Thread", String.class);
+                    parserContext.addImport("Compiler", String.class);
+                    parserContext.addImport("ThreadLocal", String.class);
+                    parserContext.addImport("SecurityManager", String.class);
+
+                    mvelExpressions.put(script, MVEL.compileExpression(script, parserContext));
+                }
+            }
+            if (mvelExpressions.containsKey(script) && mvelExpressions.get(script) != null) {
+                return MVEL.executeExpression(mvelExpressions.get(script), context);
+            } else {
+                return script;
+            }
+        } finally {
+            Thread.currentThread().setContextClassLoader(tccl);
+        }
+    }
+}
diff --git a/common/src/main/java/org/apache/unomi/common/ScriptExecutor.java b/common/src/main/java/org/apache/unomi/common/ScriptExecutor.java
new file mode 100644
index 0000000..c32cdaa
--- /dev/null
+++ b/common/src/main/java/org/apache/unomi/common/ScriptExecutor.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.unomi.common;
+
+import java.util.Map;
+
+/**
+ * An interface to execute scripts with different implementations.
+ */
+public interface ScriptExecutor {
+
+    Object execute(String script, Map<String,Object> context);
+
+}
diff --git a/common/src/main/java/org/apache/unomi/common/SecureFilteringClassLoader.java b/common/src/main/java/org/apache/unomi/common/SecureFilteringClassLoader.java
index 61b91bf..e28675b 100644
--- a/common/src/main/java/org/apache/unomi/common/SecureFilteringClassLoader.java
+++ b/common/src/main/java/org/apache/unomi/common/SecureFilteringClassLoader.java
@@ -21,7 +21,7 @@ import java.util.HashSet;
 import java.util.Set;
 
 /**
- * A class loader that uses a whitelist and a black list of classes that it will allow to resolve. This is useful for providing proper
+ * A class loader that uses a allow list and a deny list of classes that it will allow to resolve. This is useful for providing proper
  * sandboxing to scripting engine such as MVEL, OGNL or Groovy.
  */
 public class SecureFilteringClassLoader extends ClassLoader {
@@ -34,7 +34,7 @@ public class SecureFilteringClassLoader extends ClassLoader {
 
     static {
         String systemAllowedClasses = System.getProperty("org.apache.unomi.scripting.allow",
-                "org.apache.unomi.api.Event,org.apache.unomi.api.Profile,org.apache.unomi.api.Session,org.apache.unomi.api.Item,org.apache.unomi.api.CustomItem,ognl.*,java.lang.Object,java.util.Map,java.lang.Integer,org.mvel2.*");
+                "org.apache.unomi.api.Event,org.apache.unomi.api.Profile,org.apache.unomi.api.Session,org.apache.unomi.api.Item,org.apache.unomi.api.CustomItem,ognl.*,java.lang.Object,java.util.Map,java.util.HashMap,java.lang.Integer,org.mvel2.*");
         if (systemAllowedClasses != null) {
             if ("all".equals(systemAllowedClasses.trim())) {
                 defaultAllowedClasses = null;
@@ -98,6 +98,22 @@ public class SecureFilteringClassLoader extends ClassLoader {
         return delegate.loadClass(name);
     }
 
+    @Override
+    protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
+        if (forbiddenClasses != null && classNameMatches(forbiddenClasses, name)) {
+            throw new ClassNotFoundException("Access to class " + name + " not allowed");
+        }
+        if (allowedClasses != null && !classNameMatches(allowedClasses, name)) {
+            throw new ClassNotFoundException("Access to class " + name + " not allowed");
+        }
+        return super.loadClass(name, resolve);
+    }
+
+    @Override
+    protected Class<?> findClass(String name) throws ClassNotFoundException {
+        return super.findClass(name);
+    }
+
     private boolean classNameMatches(Set<String> classesToTest, String className) {
         for (String classToTest : classesToTest) {
             if (classToTest.endsWith("*")) {
diff --git a/services/src/test/java/org/apache/unomi/services/actions/ActionExecutorDispatcherTest.java b/common/src/test/java/org/apache/unomi/common/MvelScriptExecutorTest.java
similarity index 57%
rename from services/src/test/java/org/apache/unomi/services/actions/ActionExecutorDispatcherTest.java
rename to common/src/test/java/org/apache/unomi/common/MvelScriptExecutorTest.java
index 813c067..137b4a9 100644
--- a/services/src/test/java/org/apache/unomi/services/actions/ActionExecutorDispatcherTest.java
+++ b/common/src/test/java/org/apache/unomi/common/MvelScriptExecutorTest.java
@@ -14,23 +14,22 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.unomi.services.actions;
+package org.apache.unomi.common;
 
 import org.apache.unomi.api.CustomItem;
 import org.apache.unomi.api.Event;
-import org.apache.unomi.common.SecureFilteringClassLoader;
 import org.junit.Test;
-import org.mvel2.CompileException;
-import org.mvel2.MVEL;
-import org.mvel2.ParserConfiguration;
-import org.mvel2.ParserContext;
 
-import java.io.*;
-import java.util.*;
+import java.io.File;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.junit.Assert.assertFalse;
 
-public class ActionExecutorDispatcherTest {
+public class MvelScriptExecutorTest {
+
+    ScriptExecutor scriptExecutor = new MvelScriptExecutor();
 
     public static final String MOCK_ITEM_ID = "mockItemId";
     public static final String DIGITALL_SCOPE = "digitall";
@@ -50,44 +49,41 @@ public class ActionExecutorDispatcherTest {
         }
         Object result = null;
         try {
-            result = executeMVEL("java.io.PrintWriter writer = new java.io.PrintWriter(new java.io.BufferedWriter(new java.io.FileWriter(\"" + vulnFile.getCanonicalPath() + "\", true)));\nwriter.println(\"test\");\nwriter.close();", ctx);
-        } catch (CompileException ce) {
+            result = scriptExecutor.execute("java.io.PrintWriter writer = new java.io.PrintWriter(new java.io.BufferedWriter(new java.io.FileWriter(\"" + vulnFile.getCanonicalPath() + "\", true)));\nwriter.println(\"test\");\nwriter.close();", ctx);
+        } catch (Throwable t) {
             // this is expected since access to these classes should not be allowed
+            System.out.println("Expected error : " + t.getMessage());
         }
         System.out.println("result=" + result);
         try {
-            result = executeMVEL("import java.util.*;\nimport java.io.*;\nPrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(\"" + vulnFile.getCanonicalPath() + "\", true)));\nwriter.println(\"test\");\nwriter.close();", ctx);
-        } catch (CompileException ce) {
+            result = scriptExecutor.execute("import java.util.*;\nimport java.io.*;\nPrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(\"" + vulnFile.getCanonicalPath() + "\", true)));\nwriter.println(\"test\");\nwriter.close();", ctx);
+        } catch (Throwable t) {
             // this is expected since access to these classes should not be allowed
+            System.out.println("Expected error : " + t.getMessage());
         }
         System.out.println("result=" + result);
         try {
-            result = executeMVEL("import java.util.*;\nimport java.io.*;\nnew Scanner(new File(\"" + vulnFile.getCanonicalPath() + "\")).useDelimiter(\"\\\\Z\").next();", ctx);
-        } catch (CompileException ce) {
+            result = scriptExecutor.execute("import java.util.*;\nimport java.io.*;\nnew Scanner(new File(\"" + vulnFile.getCanonicalPath() + "\")).useDelimiter(\"\\\\Z\").next();", ctx);
+        } catch (Throwable t) {
             // this is expected since access to these classes should not be allowed
+            System.out.println("Expected error : " + t.getMessage());
+        }
+        System.out.println("result=" + result);
+        try {
+            result = scriptExecutor.execute("Runtime r = Runtime.getRuntime(); r.exec(\"touch "+vulnFile.getCanonicalPath()+"\");", ctx);
+        } catch (Throwable t) {
+            // this is expected since access to these classes should not be allowed
+            System.out.println("Expected error : " + t.getMessage());
         }
         System.out.println("result=" + result);
-        assertFalse("Vulnerability successfully executed ! File created at " + vulnFile.getCanonicalPath(), vulnFile.exists());
-    }
-
-    private Object executeMVEL(String expression, Object ctx) {
-        final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
         try {
-            ParserConfiguration parserConfiguration = new ParserConfiguration();
-            ClassLoader secureFilteringClassLoader = new SecureFilteringClassLoader(getClass().getClassLoader());
-            Thread.currentThread().setContextClassLoader(secureFilteringClassLoader);
-            parserConfiguration.setClassLoader(secureFilteringClassLoader);
-            ParserContext parserContext = new ParserContext(parserConfiguration);
-            Serializable compiledExpression = MVEL.compileExpression(expression, parserContext);
-            try {
-                return MVEL.executeExpression(compiledExpression, ctx, new HashMap());
-            } catch (CompileException ce) {
-                // this is expected
-            }
-            return null;
-        } finally {
-            Thread.currentThread().setContextClassLoader(tccl);
+            result = scriptExecutor.execute("Runtime r = Runtime.getClass().forName(\"java.lang.Runtime\").getDeclaredMethod(\"getRuntime\", null ).invoke(null, null); r.exec(\"touch "+vulnFile.getCanonicalPath()+"\");", ctx);
+        } catch (Throwable t) {
+            // this is expected since access to these classes should not be allowed
+            System.out.println("Expected error : " + t.getMessage());
         }
+        System.out.println("result=" + result);
+        assertFalse("Vulnerability successfully executed ! File created at " + vulnFile.getCanonicalPath(), vulnFile.exists());
     }
 
     private static Event generateMockEvent() {
@@ -102,5 +98,4 @@ public class ActionExecutorDispatcherTest {
         targetItem.getProperties().put("pageInfo", pageInfoMap);
         return mockEvent;
     }
-
 }
diff --git a/itests/src/test/java/org/apache/unomi/itests/BaseIT.java b/itests/src/test/java/org/apache/unomi/itests/BaseIT.java
index f6c35e4..bc2df79 100644
--- a/itests/src/test/java/org/apache/unomi/itests/BaseIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/BaseIT.java
@@ -17,9 +17,12 @@
 
 package org.apache.unomi.itests;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.commons.io.IOUtils;
 import org.apache.unomi.api.Item;
 import org.apache.unomi.api.conditions.Condition;
 import org.apache.unomi.api.services.DefinitionsService;
+import org.apache.unomi.persistence.spi.CustomObjectMapper;
 import org.apache.unomi.persistence.spi.PersistenceService;
 import org.junit.Assert;
 import org.ops4j.pax.exam.Configuration;
@@ -30,23 +33,21 @@ import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
 import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
 import org.ops4j.pax.exam.options.extra.VMOption;
 import org.ops4j.pax.exam.util.Filter;
+import org.osgi.framework.BundleContext;
 
 import javax.inject.Inject;
 import java.io.File;
+import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.function.Predicate;
 import java.util.function.Supplier;
 
 import static org.ops4j.pax.exam.CoreOptions.maven;
 import static org.ops4j.pax.exam.CoreOptions.systemProperty;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.debugConfiguration;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
-import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.replaceConfigurationFile;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.*;
 
 /**
  * Base class for integration tests.
@@ -68,6 +69,9 @@ public abstract class BaseIT {
     @Filter(timeout = 600000)
     protected DefinitionsService definitionsService;
 
+    @Inject
+    protected BundleContext bundleContext;
+
     protected void removeItems(final Class<? extends Item> ...classes) throws InterruptedException {
         Condition condition = new Condition(definitionsService.getConditionType("matchAllCondition"));
         for (Class<? extends Item> aClass : classes) {
@@ -206,4 +210,25 @@ public abstract class BaseIT {
         }
         return value;
     }
+
+    protected String bundleResourceAsString(final String resourcePath) throws IOException {
+        final java.net.URL url = bundleContext.getBundle().getResource(resourcePath);
+        if (url != null) {
+            return IOUtils.toString(url);
+        } else {
+            return null;
+        }
+    }
+
+    protected String getValidatedBundleJSON(final String resourcePath, Map<String,String> parameters) throws IOException {
+        String jsonString = bundleResourceAsString(resourcePath);
+        if (parameters != null && parameters.size() > 0) {
+            for (Map.Entry<String,String> parameterEntry : parameters.entrySet()) {
+                jsonString = jsonString.replace("###" + parameterEntry.getKey() + "###", parameterEntry.getValue());
+            }
+        }
+        ObjectMapper objectMapper = CustomObjectMapper.getObjectMapper();
+        return objectMapper.writeValueAsString(objectMapper.readTree(jsonString));
+    }
+
 }
diff --git a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
index d4cfd63..812f36c 100644
--- a/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
+++ b/itests/src/test/java/org/apache/unomi/itests/ContextServletIT.java
@@ -21,12 +21,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.entity.ContentType;
 import org.apache.http.entity.StringEntity;
-import org.apache.unomi.api.ContextRequest;
-import org.apache.unomi.api.ContextResponse;
-import org.apache.unomi.api.Event;
-import org.apache.unomi.api.Metadata;
-import org.apache.unomi.api.Profile;
-import org.apache.unomi.api.Session;
+import org.apache.unomi.api.*;
 import org.apache.unomi.api.conditions.Condition;
 import org.apache.unomi.api.segments.Segment;
 import org.apache.unomi.api.services.DefinitionsService;
@@ -44,6 +39,7 @@ import org.ops4j.pax.exam.spi.reactors.PerSuite;
 import org.ops4j.pax.exam.util.Filter;
 
 import javax.inject.Inject;
+import java.io.File;
 import java.io.IOException;
 import java.net.URI;
 import java.time.LocalDateTime;
@@ -51,10 +47,11 @@ import java.time.ZoneId;
 import java.time.ZoneOffset;
 import java.util.Arrays;
 import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
 
 import static org.hamcrest.core.IsCollectionContaining.hasItem;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertThat;
+import static org.junit.Assert.*;
 
 
 /**
@@ -346,4 +343,48 @@ public class ContextServletIT extends BaseIT {
 		Profile profile =  this.profileService.load(profileId);
 		assertEquals(profileId, profile.getItemId());
 	}
+
+	@Test
+	public void testOGNLVulnerability() throws IOException, InterruptedException {
+
+		File vulnFile = new File("target/vuln-file.txt");
+		if (vulnFile.exists()) {
+			vulnFile.delete();
+		}
+		String vulnFileCanonicalPath = vulnFile.getCanonicalPath();
+		vulnFileCanonicalPath = vulnFileCanonicalPath.replace("\\", "\\\\"); // this is required for Windows support
+
+		Map<String,String> parameters = new HashMap<>();
+		parameters.put("VULN_FILE_PATH", vulnFileCanonicalPath);
+		HttpPost request = new HttpPost(URL + CONTEXT_URL);
+		request.setEntity(new StringEntity(getValidatedBundleJSON("security/ognl-payload-1.json", parameters), ContentType.create("application/json")));
+		TestUtils.executeContextJSONRequest(request);
+		refreshPersistence();
+		Thread.sleep(2000); //Making sure event is updated in DB
+
+		assertFalse("Vulnerability successfully executed ! File created at " + vulnFileCanonicalPath, vulnFile.exists());
+
+	}
+
+	@Test
+	public void testMVELVulnerability() throws IOException, InterruptedException {
+
+		File vulnFile = new File("target/vuln-file.txt");
+		if (vulnFile.exists()) {
+			vulnFile.delete();
+		}
+		String vulnFileCanonicalPath = vulnFile.getCanonicalPath();
+		vulnFileCanonicalPath = vulnFileCanonicalPath.replace("\\", "\\\\"); // this is required for Windows support
+
+		Map<String,String> parameters = new HashMap<>();
+		parameters.put("VULN_FILE_PATH", vulnFileCanonicalPath);
+		HttpPost request = new HttpPost(URL + CONTEXT_URL);
+		request.setEntity(new StringEntity(getValidatedBundleJSON("security/mvel-payload-1.json", parameters), ContentType.create("application/json")));
+		TestUtils.executeContextJSONRequest(request);
+		refreshPersistence();
+		Thread.sleep(2000); //Making sure event is updated in DB
+
+		assertFalse("Vulnerability successfully executed ! File created at " + vulnFileCanonicalPath, vulnFile.exists());
+
+	}
 }
diff --git a/itests/src/test/resources/security/mvel-payload-1.json b/itests/src/test/resources/security/mvel-payload-1.json
new file mode 100644
index 0000000..da2c16d
--- /dev/null
+++ b/itests/src/test/resources/security/mvel-payload-1.json
@@ -0,0 +1,20 @@
+{
+  "filters": [
+    {
+      "id": "filter1",
+      "filters": [
+        {
+          "condition": {
+            "parameterValues": {
+              "propertyName": "prop",
+              "comparisonOperator": "equals",
+              "propertyValue": "script::Runtime r = Runtime.getRuntime(); r.exec(\"touch ###VULN_FILE_PATH###\");"
+            },
+            "type": "profilePropertyCondition"
+          }
+        }
+      ]
+    }
+  ],
+  "sessionId": "demo-session-id"
+}
\ No newline at end of file
diff --git a/itests/src/test/resources/security/ognl-payload-1.json b/itests/src/test/resources/security/ognl-payload-1.json
new file mode 100644
index 0000000..526a914
--- /dev/null
+++ b/itests/src/test/resources/security/ognl-payload-1.json
@@ -0,0 +1,28 @@
+{
+  "personalizations":[
+    {
+      "id":"gender-test",
+      "strategy":"matching-first",
+      "strategyOptions":{
+        "fallback":"var2"
+      },
+      "contents":[
+        {
+          "filters":[
+            {
+              "condition":{
+                "parameterValues":{
+                  "propertyName":"(#runtimeclass = #this.getClass().forName(\"java.lang.Runtime\")).(#getruntimemethod = #runtimeclass.getDeclaredMethods().{^ #this.name.equals(\"getRuntime\")}[0]).(#rtobj = #getruntimemethod.invoke(null,null)).(#execmethod = #runtimeclass.getDeclaredMethods().{? #this.name.equals(\"exec\")}.{? #this.getParameters()[0].getType().getName().equals(\"java.lang.String\")}.{? #this.getParameters().length < 2}[0]).(#execmethod.invoke(#rtobj,\" touch ###VULN_FI [...]
+                  "comparisonOperator":"equals",
+                  "propertyValue":"male"
+                },
+                "type":"profilePropertyCondition"
+              }
+            }
+          ]
+        }
+      ]
+    }
+  ],
+  "sessionId":"demo-session-id"
+}
diff --git a/package/src/main/resources/etc/custom.system.properties b/package/src/main/resources/etc/custom.system.properties
index 87b7464..e5b44b2 100644
--- a/package/src/main/resources/etc/custom.system.properties
+++ b/package/src/main/resources/etc/custom.system.properties
@@ -31,8 +31,13 @@ org.apache.unomi.hazelcast.network.port=${env:UNOMI_HAZELCAST_NETWORK_PORT:-5701
 ## Security settings                                                                                                 ##
 #######################################################################################################################
 org.apache.unomi.security.root.password=${env:UNOMI_ROOT_PASSWORD:-karaf}
-org.apache.unomi.scripting.allow=${env:UNOMI_ALLOW_SCRIPTING_CLASSES:-org.apache.unomi.api.Event,org.apache.unomi.api.Profile,org.apache.unomi.api.Session,org.apache.unomi.api.Item,org.apache.unomi.api.CustomItem,ognl.*,java.lang.Object,java.util.Map,java.lang.Integer,org.mvel2.*}
+org.apache.unomi.scripting.allow=${env:UNOMI_ALLOW_SCRIPTING_CLASSES:-org.apache.unomi.api.Event,org.apache.unomi.api.Profile,org.apache.unomi.api.Session,org.apache.unomi.api.Item,org.apache.unomi.api.CustomItem,ognl.*,java.lang.Object,java.util.Map,java.util.HashMap,java.lang.Integer,org.mvel2.*}
 org.apache.unomi.scripting.forbid=${env:UNOMI_FORBID_SCRIPTING_CLASSES:-}
+org.apache.unomi.scripting.filter.allow=${env:UNOMI_SCRIPTING_FILTER_ALLOW:-all}
+org.apache.unomi.scripting.filter.forbid=${env:UNOMI_SCRIPTING_FILTER_FORBID:-.*Runtime.*,.*ProcessBuilder.*,.*exec.*,.*invoke.*,.*getClass.*,.*Class.*,.*ClassLoader.*,.*System.*,.*Method.*,.*method.*,.*Compiler.*,.*Thread.*,.*FileWriter.*,.*forName.*,.*Socket.*,.*DriverManager.*}
+org.apache.unomi.security.properties.useOGNLScripting=${env:UNOMI_SCRIPTING_USE_OGNL:-false}
+org.apache.unomi.security.personalization.sanitizeConditions=${env:UNOMI_SECURITY_SANITIZEPERSONALIZATIONCONDITIONS:-true}
+
 #######################################################################################################################
 ## HTTP Settings                                                                                                     ##
 #######################################################################################################################
diff --git a/persistence-elasticsearch/core/pom.xml b/persistence-elasticsearch/core/pom.xml
index c903640..1e2ae6b 100644
--- a/persistence-elasticsearch/core/pom.xml
+++ b/persistence-elasticsearch/core/pom.xml
@@ -189,10 +189,6 @@
             <groupId>commons-collections</groupId>
             <artifactId>commons-collections</artifactId>
         </dependency>
-        <dependency>
-            <groupId>org.mvel</groupId>
-            <artifactId>mvel2</artifactId>
-        </dependency>
 
         <dependency>
             <groupId>org.apache.unomi</groupId>
diff --git a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/conditions/ConditionContextHelper.java b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/conditions/ConditionContextHelper.java
index 8355015..d74d03a 100644
--- a/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/conditions/ConditionContextHelper.java
+++ b/persistence-elasticsearch/core/src/main/java/org/apache/unomi/persistence/elasticsearch/conditions/ConditionContextHelper.java
@@ -24,24 +24,23 @@ import org.apache.logging.log4j.core.util.IOUtils;
 import org.apache.lucene.analysis.charfilter.MappingCharFilterFactory;
 import org.apache.lucene.analysis.util.ClasspathResourceLoader;
 import org.apache.unomi.api.conditions.Condition;
-import org.apache.unomi.common.SecureFilteringClassLoader;
-import org.mvel2.MVEL;
-import org.mvel2.ParserConfiguration;
-import org.mvel2.ParserContext;
+import org.apache.unomi.common.MvelScriptExecutor;
+import org.apache.unomi.common.ScriptExecutor;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.IOException;
 import java.io.Reader;
-import java.io.Serializable;
 import java.io.StringReader;
-import java.util.*;
-import java.util.concurrent.ConcurrentHashMap;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 public class ConditionContextHelper {
     private static final Logger logger = LoggerFactory.getLogger(ConditionContextHelper.class);
 
-    private static Map<String,Serializable> mvelExpressions = new ConcurrentHashMap<>();
+    private static final ScriptExecutor scriptExecutor = new MvelScriptExecutor();
 
     private static MappingCharFilterFactory mappingCharFilterFactory;
     static {
@@ -105,19 +104,7 @@ public class ConditionContextHelper {
     }
 
     private static Object executeScript(Map<String, Object> context, String script) {
-        final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-        try {
-            if (!mvelExpressions.containsKey(script)) {
-                ClassLoader secureFilteringClassLoader = new SecureFilteringClassLoader(ConditionContextHelper.class.getClassLoader());
-                Thread.currentThread().setContextClassLoader(secureFilteringClassLoader);
-                ParserConfiguration parserConfiguration = new ParserConfiguration();
-                parserConfiguration.setClassLoader(secureFilteringClassLoader);
-                mvelExpressions.put(script, MVEL.compileExpression(script, new ParserContext(parserConfiguration)));
-            }
-            return MVEL.executeExpression(mvelExpressions.get(script), context);
-        } finally {
-            Thread.currentThread().setContextClassLoader(tccl);
-        }
+        return scriptExecutor.execute(script, context);
     }
 
     private static boolean hasContextualParameter(Object value) {
diff --git a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluator.java b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluator.java
index d7dbf01..18926f5 100644
--- a/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluator.java
+++ b/plugins/baseplugin/src/main/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluator.java
@@ -23,6 +23,7 @@ import org.apache.commons.lang3.ObjectUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.unomi.api.*;
 import org.apache.unomi.api.conditions.Condition;
+import org.apache.unomi.common.ExpressionFilter;
 import org.apache.unomi.common.SecureFilteringClassLoader;
 import org.apache.unomi.persistence.elasticsearch.conditions.ConditionContextHelper;
 import org.apache.unomi.persistence.elasticsearch.conditions.ConditionEvaluator;
@@ -53,6 +54,10 @@ public class PropertyConditionEvaluator implements ConditionEvaluator {
 
     private Map<String, Map<String, ExpressionAccessor>> expressionCache = new HashMap<>(64);
     private boolean usePropertyConditionOptimizations = true;
+    private static ClassLoader secureFilteringClassLoader = new SecureFilteringClassLoader(PropertyConditionEvaluator.class.getClassLoader());
+    private static ExpressionFilter expressionFilter = new ExpressionFilter();
+
+    private boolean useOGNLScripting = Boolean.parseBoolean(System.getProperty("org.apache.unomi.security.properties.useOGNLScripting", "false"));
 
     public void setUsePropertyConditionOptimizations(boolean usePropertyConditionOptimizations) {
         this.usePropertyConditionOptimizations = usePropertyConditionOptimizations;
@@ -280,7 +285,12 @@ public class PropertyConditionEvaluator implements ConditionEvaluator {
                 return result;
             }
         }
-        return getOGNLPropertyValue(item, expression);
+        if (useOGNLScripting) {
+            return getOGNLPropertyValue(item, expression);
+        } else {
+            logger.warn("Expression {} will not be evaluated because OGNL scripting is deactivated", expression);
+            return null;
+        }
     }
 
     protected Object getHardcodedPropertyValue(Item item, String expression) {
@@ -307,8 +317,41 @@ public class PropertyConditionEvaluator implements ConditionEvaluator {
             if ("scope".equals(expression)) {
                 return event.getScope();
             }
+            if ("eventType".equals(expression)) {
+                return event.getEventType();
+            }
+            if ("profile".equals(expression)) {
+                return event.getProfile();
+            }
+            if ("profileId".equals(expression)) {
+                return event.getProfileId();
+            }
+            if ("session".equals(expression)) {
+                return event.getSession();
+            }
+            if ("sessionId".equals(expression)) {
+                return event.getSessionId();
+            }
+            if ("source".equals(expression)) {
+                return event.getSource();
+            }
+            if ("target".equals(expression)) {
+                return event.getTarget();
+            }
+            if ("timeStamp".equals(expression)) {
+                return event.getTimeStamp();
+            }
+            if ("itemId".equals(expression)) {
+                return event.getItemId();
+            }
+            if ("itemType".equals(expression)) {
+                return event.getItemType();
+            }
         } else if (item instanceof Session) {
             Session session = (Session) item;
+            if ("scope".equals(expression)) {
+                return session.getScope();
+            }
             if ("timeStamp".equals(expression)) {
                 return session.getTimeStamp();
             }
@@ -318,12 +361,27 @@ public class PropertyConditionEvaluator implements ConditionEvaluator {
             if ("size".equals(expression)) {
                 return session.getSize();
             }
+            if ("lastEventDate".equals(expression)) {
+                return session.getLastEventDate();
+            }
             if (expression.startsWith("properties.")) {
                 return getNestedPropertyValue(expression.substring("properties.".length()), session.getProperties());
             }
             if (expression.startsWith("systemProperties.")) {
                 return getNestedPropertyValue(expression.substring("systemProperties.".length()), session.getSystemProperties());
             }
+            if ("itemId".equals(expression)) {
+                return session.getItemId();
+            }
+            if ("itemType".equals(expression)) {
+                return session.getItemType();
+            }
+            if ("profile".equals(expression)) {
+                return session.getProfile();
+            }
+            if ("profileId".equals(expression)) {
+                return session.getProfileId();
+            }
         } else if (item instanceof Profile) {
             Profile profile = (Profile) item;
             if ("segments".equals(expression)) {
@@ -341,17 +399,38 @@ public class PropertyConditionEvaluator implements ConditionEvaluator {
             if (expression.startsWith("systemProperties.")) {
                 return getNestedPropertyValue(expression.substring("systemProperties.".length()), profile.getSystemProperties());
             }
+            if ("itemId".equals(expression)) {
+                return profile.getItemId();
+            }
+            if ("itemType".equals(expression)) {
+                return profile.getItemType();
+            }
+            if ("mergedWith".equals(expression)) {
+                return profile.getMergedWith();
+            }
         } else if (item instanceof CustomItem) {
             CustomItem customItem = (CustomItem) item;
             if (expression.startsWith("properties.")) {
                 return getNestedPropertyValue(expression.substring("properties.".length()), customItem.getProperties());
             }
+            if ("itemId".equals(expression)) {
+                return customItem.getItemId();
+            }
+            if ("itemType".equals(expression)) {
+                return customItem.getItemType();
+            }
+            if ("scope".equals(expression)) {
+                return customItem.getScope();
+            }
         }
         return NOT_OPTIMIZED_MARKER;
     }
 
     protected Object getOGNLPropertyValue(Item item, String expression) throws Exception {
-        ClassLoader secureFilteringClassLoader = new SecureFilteringClassLoader(PropertyConditionEvaluator.class.getClassLoader());
+        if (expressionFilter.filter(expression) == null) {
+            logger.warn("Expression {} is not allowed !", expression);
+            return null;
+        }
         OgnlContext ognlContext = getOgnlContext(secureFilteringClassLoader);
         ExpressionAccessor accessor = getPropertyAccessor(item, expression, ognlContext, secureFilteringClassLoader);
         return accessor != null ? accessor.get(ognlContext, item) : null;
@@ -399,8 +478,11 @@ public class PropertyConditionEvaluator implements ConditionEvaluator {
                     @Override
                     public boolean isAccessible(Map context, Object target, Member member, String propertyName) {
                         int modifiers = member.getModifiers();
-                        boolean result = Modifier.isPublic(modifiers);
-                        return result;
+                        if (target instanceof Item) {
+                            return Modifier.isPublic(modifiers);
+                        }
+                        logger.warn("Target {} and member {} for property {} are not allowed by OGNL security filter", target, member, propertyName);
+                        return false;
                     }
                 }, new ClassLoaderClassResolver(classLoader),
                 null);
diff --git a/plugins/baseplugin/src/test/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluatorTest.java b/plugins/baseplugin/src/test/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluatorTest.java
index 05ac126..f098ced 100644
--- a/plugins/baseplugin/src/test/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluatorTest.java
+++ b/plugins/baseplugin/src/test/java/org/apache/unomi/plugins/baseplugin/conditions/PropertyConditionEvaluatorTest.java
@@ -62,7 +62,7 @@ public class PropertyConditionEvaluatorTest {
         assertNull("Unexisting property should be null", propertyConditionEvaluator.getHardcodedPropertyValue(mockProfile, "properties.email"));
 
         // here let's make sure our reporting of non optimized expressions works.
-        assertEquals("Should have received the non-optimized marker string", NOT_OPTIMIZED_MARKER, propertyConditionEvaluator.getHardcodedPropertyValue(mockSession, "lastEventDate"));
+        assertEquals("Should have received the non-optimized marker string", NOT_OPTIMIZED_MARKER, propertyConditionEvaluator.getHardcodedPropertyValue(mockSession, "profile.itemId"));
 
     }
 
@@ -139,6 +139,13 @@ public class PropertyConditionEvaluatorTest {
         }
         vulnFile = new File("target/vuln-file.txt");
         assertFalse("Vulnerability successfully executed ! File created at " + vulnFileCanonicalPath, vulnFile.exists());
+        try {
+            propertyConditionEvaluator.getOGNLPropertyValue(mockEvent, "(#runtimeclass = #this.getClass().forName(\"java.lang.Runtime\")).(#getruntimemethod = #runtimeclass.getDeclaredMethods().{^ #this.name.equals(\"getRuntime\")}[0]).(#rtobj = #getruntimemethod.invoke(null,null)).(#execmethod = #runtimeclass.getDeclaredMethods().{? #this.name.equals(\"exec\")}.{? #this.getParameters()[0].getType().getName().equals(\"java.lang.String\")}.{? #this.getParameters().length < 2}[0]).(#execme [...]
+        } catch (RuntimeException | MethodFailedException re) {
+            // we ignore these exceptions as they are expected.
+        }
+        vulnFile = new File("target/vuln-file.txt");
+        assertFalse("Vulnerability successfully executed ! File created at " + vulnFileCanonicalPath, vulnFile.exists());
     }
 
     private void runHardcodedTest(int workerCount, ExecutorService executorService) throws InterruptedException {
diff --git a/services/pom.xml b/services/pom.xml
index 2400db7..a9baac5 100644
--- a/services/pom.xml
+++ b/services/pom.xml
@@ -75,11 +75,6 @@
         </dependency>
 
         <dependency>
-            <groupId>org.mvel</groupId>
-            <artifactId>mvel2</artifactId>
-        </dependency>
-
-        <dependency>
             <groupId>com.github.fge</groupId>
             <artifactId>json-patch</artifactId>
             <version>1.9</version>
diff --git a/services/src/main/java/org/apache/unomi/services/actions/ActionExecutorDispatcher.java b/services/src/main/java/org/apache/unomi/services/actions/ActionExecutorDispatcher.java
index e148a94..014bcc8 100644
--- a/services/src/main/java/org/apache/unomi/services/actions/ActionExecutorDispatcher.java
+++ b/services/src/main/java/org/apache/unomi/services/actions/ActionExecutorDispatcher.java
@@ -24,18 +24,15 @@ import org.apache.unomi.api.actions.Action;
 import org.apache.unomi.api.actions.ActionDispatcher;
 import org.apache.unomi.api.actions.ActionExecutor;
 import org.apache.unomi.api.services.EventService;
-import org.apache.unomi.common.SecureFilteringClassLoader;
+import org.apache.unomi.common.MvelScriptExecutor;
+import org.apache.unomi.common.ScriptExecutor;
 import org.apache.unomi.metrics.MetricAdapter;
 import org.apache.unomi.metrics.MetricsService;
-import org.mvel2.MVEL;
-import org.mvel2.ParserConfiguration;
-import org.mvel2.ParserContext;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.io.Serializable;
 import java.lang.reflect.InvocationTargetException;
 import java.util.HashMap;
 import java.util.Map;
@@ -44,12 +41,12 @@ import java.util.concurrent.ConcurrentHashMap;
 public class ActionExecutorDispatcher {
     private static final Logger logger = LoggerFactory.getLogger(ActionExecutorDispatcher.class.getName());
     private static final String VALUE_NAME_SEPARATOR = "::";
-    private final Map<String, Serializable> mvelExpressions = new ConcurrentHashMap<>();
     private final Map<String, ValueExtractor> valueExtractors = new HashMap<>(11);
     private Map<String, ActionExecutor> executors = new ConcurrentHashMap<>();
     private MetricsService metricsService;
     private Map<String, ActionDispatcher> actionDispatchers = new ConcurrentHashMap<>();
     private BundleContext bundleContext;
+    private ScriptExecutor scriptExecutor = new MvelScriptExecutor();
 
     public void setMetricsService(MetricsService metricsService) {
         this.metricsService = metricsService;
@@ -226,24 +223,12 @@ public class ActionExecutorDispatcher {
         }
     }
 
-    protected Object executeScript(String valueAsString, Event event) {
-        final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
-        try {
-            ClassLoader secureFilteringClassLoader = new SecureFilteringClassLoader(getClass().getClassLoader());
-            Thread.currentThread().setContextClassLoader(secureFilteringClassLoader);
-            if (!mvelExpressions.containsKey(valueAsString)) {
-                ParserConfiguration parserConfiguration = new ParserConfiguration();
-                parserConfiguration.setClassLoader(secureFilteringClassLoader);
-                mvelExpressions.put(valueAsString, MVEL.compileExpression(valueAsString, new ParserContext(parserConfiguration)));
-            }
-            Map<String, Object> ctx = new HashMap<>();
-            ctx.put("event", event);
-            ctx.put("session", event.getSession());
-            ctx.put("profile", event.getProfile());
-            return MVEL.executeExpression(mvelExpressions.get(valueAsString), ctx);
-        } finally {
-            Thread.currentThread().setContextClassLoader(tccl);
-        }
+    protected Object executeScript(String script, Event event) {
+        Map<String, Object> context = new HashMap<>();
+        context.put("event", event);
+        context.put("session", event.getSession());
+        context.put("profile", event.getProfile());
+        return scriptExecutor.execute(script, context);
     }
 
 }
diff --git a/wab/src/main/java/org/apache/unomi/web/ContextServlet.java b/wab/src/main/java/org/apache/unomi/web/ContextServlet.java
index 1557f5a..79efddb 100644
--- a/wab/src/main/java/org/apache/unomi/web/ContextServlet.java
+++ b/wab/src/main/java/org/apache/unomi/web/ContextServlet.java
@@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.unomi.api.*;
+import org.apache.unomi.api.conditions.Condition;
 import org.apache.unomi.api.services.*;
 import org.apache.unomi.persistence.spi.CustomObjectMapper;
 import org.slf4j.Logger;
@@ -35,7 +36,6 @@ import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
 import java.io.IOException;
-import java.io.InputStream;
 import java.io.Writer;
 import java.util.*;
 
@@ -59,6 +59,8 @@ public class ContextServlet extends HttpServlet {
     private PersonalizationService personalizationService;
     private ConfigSharingService configSharingService;
 
+    private boolean sanitizeConditions = Boolean.parseBoolean(System.getProperty("org.apache.unomi.security.personalization.sanitizeConditions", "true"));
+
     @Override
     public void init(ServletConfig config) throws ServletException {
         super.init(config);
@@ -363,7 +365,7 @@ public class ContextServlet extends HttpServlet {
         List<PersonalizationService.PersonalizedContent> filterNodes = contextRequest.getFilters();
         if (filterNodes != null) {
             data.setFilteringResults(new HashMap<>());
-            for (PersonalizationService.PersonalizedContent personalizedContent : filterNodes) {
+            for (PersonalizationService.PersonalizedContent personalizedContent : sanitizePersonalizedContentObjects(filterNodes)) {
                 data.getFilteringResults().put(personalizedContent.getId(), personalizationService.filter(profile,
                         session, personalizedContent));
             }
@@ -372,7 +374,7 @@ public class ContextServlet extends HttpServlet {
         List<PersonalizationService.PersonalizationRequest> personalizations = contextRequest.getPersonalizations();
         if (personalizations != null) {
             data.setPersonalizations(new HashMap<>());
-            for (PersonalizationService.PersonalizationRequest personalization : personalizations) {
+            for (PersonalizationService.PersonalizationRequest personalization : sanitizePersonalizations(personalizations)) {
                 data.getPersonalizations().put(personalization.getId(), personalizationService.personalizeList(profile,
                         session, personalization));
             }
@@ -470,4 +472,80 @@ public class ContextServlet extends HttpServlet {
     public void setConfigSharingService(ConfigSharingService configSharingService) {
         this.configSharingService = configSharingService;
     }
+
+    private List<PersonalizationService.PersonalizedContent> sanitizePersonalizedContentObjects(List<PersonalizationService.PersonalizedContent> personalizedContentObjects) {
+        if (!sanitizeConditions) {
+            return personalizedContentObjects;
+        }
+        List<PersonalizationService.PersonalizedContent> result = new ArrayList<>();
+        for (PersonalizationService.PersonalizedContent personalizedContentObject : personalizedContentObjects) {
+            boolean foundInvalidCondition = false;
+            if (personalizedContentObject.getFilters() != null) {
+                for (PersonalizationService.Filter filter : personalizedContentObject.getFilters()) {
+                    if (sanitizeCondition(filter.getCondition()) == null) {
+                        foundInvalidCondition = true;
+                        break;
+                    }
+                }
+            }
+            if (!foundInvalidCondition) {
+                result.add(personalizedContentObject);
+            }
+        }
+
+        return result;
+    }
+
+    private List<PersonalizationService.PersonalizationRequest> sanitizePersonalizations(List<PersonalizationService.PersonalizationRequest> personalizations) {
+        if (!sanitizeConditions) {
+            return personalizations;
+        }
+        List<PersonalizationService.PersonalizationRequest> result = new ArrayList<>();
+        for (PersonalizationService.PersonalizationRequest personalizationRequest : personalizations) {
+            List<PersonalizationService.PersonalizedContent> personalizedContents = sanitizePersonalizedContentObjects(personalizationRequest.getContents());
+            if (personalizedContents != null && personalizedContents.size() > 0) {
+                result.add(personalizationRequest);
+            }
+        }
+        return result;
+    }
+
+    private Condition sanitizeCondition(Condition condition) {
+        Map<String,Object> newParameterValues = new LinkedHashMap<>();
+        for (Map.Entry<String,Object> parameterEntry : condition.getParameterValues().entrySet()) {
+            Object sanitizedValue = sanitizeValue(parameterEntry.getValue());
+            if (sanitizedValue != null) {
+                newParameterValues.put(parameterEntry.getKey(), parameterEntry.getValue());
+            } else {
+                newParameterValues.put(parameterEntry.getKey(), "");
+            }
+        }
+        Condition newCondition = new Condition();
+        if (condition.getConditionType() != null) {
+            newCondition.setConditionType(condition.getConditionType());
+        }
+        newCondition.setConditionTypeId(condition.getConditionTypeId());
+        newCondition.setParameterValues(newParameterValues);
+        return newCondition;
+    }
+
+    private Object sanitizeValue(Object value) {
+        if (value instanceof String && value.toString().startsWith("script::")) {
+            return null;
+        } else if (value instanceof List) {
+            List values = (List) value;
+            List newValues = new ArrayList();
+            for (Object listObject : values) {
+                Object newObject = sanitizeValue(listObject);
+                if (newObject != null) {
+                    newValues.add(newObject);
+                }
+            }
+        } else if (value instanceof Condition) {
+            return sanitizeCondition((Condition) value);
+        } else {
+            return value;
+        }
+        return value;
+    }
 }