You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by he...@apache.org on 2020/05/28 20:11:09 UTC

[commons-jexl] 01/04: JEXL-275: edge case of assignment from undefined var in safe mode was/is ok Task #JEXL-275 - Allow safe navigation as option

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

henrib pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jexl.git

commit f62455eba65a067b54aef36434122aa5d67892c0
Author: henrib <he...@apache.org>
AuthorDate: Thu May 28 17:06:45 2020 +0200

    JEXL-275: edge case of assignment from undefined var in safe mode was/is ok
    Task #JEXL-275 - Allow safe navigation as option
---
 .../commons/jexl3/internal/InterpreterBase.java    |  9 +++---
 .../org/apache/commons/jexl3/AnnotationTest.java   |  2 ++
 .../org/apache/commons/jexl3/Issues200Test.java    | 34 +++++++++++++++++++++-
 3 files changed, 40 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/commons/jexl3/internal/InterpreterBase.java b/src/main/java/org/apache/commons/jexl3/internal/InterpreterBase.java
index 35ca21e..cee0905 100644
--- a/src/main/java/org/apache/commons/jexl3/internal/InterpreterBase.java
+++ b/src/main/java/org/apache/commons/jexl3/internal/InterpreterBase.java
@@ -33,6 +33,7 @@ import org.apache.commons.jexl3.introspection.JexlPropertyGet;
 import org.apache.commons.jexl3.introspection.JexlPropertySet;
 import org.apache.commons.jexl3.introspection.JexlUberspect;
 import org.apache.commons.jexl3.parser.ASTArrayAccess;
+import org.apache.commons.jexl3.parser.ASTAssignment;
 import org.apache.commons.jexl3.parser.ASTFunctionNode;
 import org.apache.commons.jexl3.parser.ASTIdentifier;
 import org.apache.commons.jexl3.parser.ASTIdentifierAccess;
@@ -289,10 +290,10 @@ public abstract class InterpreterBase extends ParserVisitor {
         }
         String name = identifier.getName();
         Object value = context.get(name);
-        if (value == null
-            && !(identifier.jjtGetParent() instanceof ASTReference)
-            && !(context.has(name))) {
-                return unsolvableVariable(identifier, name, true); // undefined
+        if (value == null && !context.has(name)
+            && !(identifier.jjtGetParent() instanceof ASTAssignment && isSafe())
+            && !(identifier.jjtGetParent() instanceof ASTReference)) {
+            return unsolvableVariable(identifier, name, true); // undefined
         }
         return value;
     }
diff --git a/src/test/java/org/apache/commons/jexl3/AnnotationTest.java b/src/test/java/org/apache/commons/jexl3/AnnotationTest.java
index 9ac3572..b299179 100644
--- a/src/test/java/org/apache/commons/jexl3/AnnotationTest.java
+++ b/src/test/java/org/apache/commons/jexl3/AnnotationTest.java
@@ -136,6 +136,7 @@ public class AnnotationTest extends JexlTestCase {
 
         r = null;
         // will make an error and throw
+        options.setSafe(false);
         try {
             r = e.execute(jc, true, false);
             Assert.fail("should have thrown");
@@ -151,6 +152,7 @@ public class AnnotationTest extends JexlTestCase {
         } catch (JexlException.Variable xjexl) {
             Assert.fail("should not have thrown");
         }
+        options.setSafe(true);
 
         r = null;
         // will not make an error and will not throw
diff --git a/src/test/java/org/apache/commons/jexl3/Issues200Test.java b/src/test/java/org/apache/commons/jexl3/Issues200Test.java
index 6d66ec9..3045efd 100644
--- a/src/test/java/org/apache/commons/jexl3/Issues200Test.java
+++ b/src/test/java/org/apache/commons/jexl3/Issues200Test.java
@@ -617,7 +617,39 @@ public class Issues200Test extends JexlTestCase {
             Assert.assertEquals("xyz", xvar.getVariable());
         }
     }
-    
+
+    @Test
+    public void test275b() throws Exception {
+        JexlContext ctxt = new MapContext();
+        //ctxt.set("out", System.out);
+        JexlEngine jexl = new JexlBuilder().strict(true).safe(true).create();
+        JexlScript e = jexl.createScript("var xyz = xyz");
+        try {
+            Object o = e.execute(ctxt);
+            Assert.assertNull(o);
+        } catch (JexlException.Variable xvar) {
+            Assert.fail("should not have thrown");
+            // Assert.assertEquals("xyz", xvar.getVariable());
+        }
+    }
+
+    @Test
+    public void test275c() throws Exception {
+        JexlContext ctxt = new MapContext();
+        //ctxt.set("out", System.out);
+        JexlEngine jexl = new JexlBuilder().strict(true).safe(true).silent(true).create();
+        JexlScript e;
+        Object r;
+        e = jexl.createScript("(s, v)->{  var x = y ; 42; }");
+        // wont make an error
+        try {
+            r = e.execute(ctxt, false, true);
+            Assert.assertEquals(42, r);
+        } catch (JexlException.Variable xjexl) {
+            Assert.fail("should not have thrown");
+        }
+    }
+
     @Test
     public void test278() throws Exception {
         String[] srcs = new String[]{