changeset 2041:7c09d30a05f1 jdk8u141-b01

8171539: Better script accessibility for JavaScript Reviewed-by: jlaskey, sundar
author hannesw
date Thu, 19 Jan 2017 13:43:43 +0100
parents bd33c72a0b19
children a67daf8d1982
files src/jdk/nashorn/internal/objects/Global.java test/src/jdk/nashorn/internal/runtime/test/ClassFilterTest.java
diffstat 2 files changed, 85 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk/nashorn/internal/objects/Global.java	Thu Mar 16 10:25:02 2017 -0700
+++ b/src/jdk/nashorn/internal/objects/Global.java	Thu Jan 19 13:43:43 2017 +0100
@@ -89,9 +89,9 @@
 @ScriptClass("Global")
 public final class Global extends Scope {
     // This special value is used to flag a lazily initialized global property.
-    // This also serves as placeholder value used in place of a location property
-    // (__FILE__, __DIR__, __LINE__)
     private static final Object LAZY_SENTINEL = new Object();
+    // This serves as placeholder value used in place of a location property (__FILE__, __DIR__, __LINE__)
+    private static final Object LOCATION_PLACEHOLDER = new Object();
 
     private final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class);
     private final InvokeByName VALUE_OF  = new InvokeByName("valueOf",  ScriptObject.class);
@@ -771,7 +771,7 @@
     public volatile Object org;
 
     /**
-     * Getter for the Nashorn extension: Java access - global.javaImporter.
+     * Getter for the Nashorn extension: Java access - global.JavaImporter.
      *
      * @param self self reference
      * @return the value of the JavaImporter property
@@ -786,7 +786,7 @@
     }
 
     /**
-     * Setter for the Nashorn extension: Java access - global.javaImporter.
+     * Setter for the Nashorn extension: Java access - global.JavaImporter.
      *
      * @param self self reference
      * @param value value of the JavaImporter property
@@ -830,15 +830,15 @@
 
     /** Nashorn extension: current script's file name */
     @Property(name = "__FILE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
-    public static final Object __FILE__ = LAZY_SENTINEL;
+    public static final Object __FILE__ = LOCATION_PLACEHOLDER;
 
     /** Nashorn extension: current script's directory */
     @Property(name = "__DIR__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
-    public static final Object __DIR__ = LAZY_SENTINEL;
+    public static final Object __DIR__ = LOCATION_PLACEHOLDER;
 
     /** Nashorn extension: current source line number being executed */
     @Property(name = "__LINE__", attributes = Attribute.NON_ENUMERABLE_CONSTANT)
-    public static final Object __LINE__ = LAZY_SENTINEL;
+    public static final Object __LINE__ = LOCATION_PLACEHOLDER;
 
     private volatile NativeDate DEFAULT_DATE;
 
@@ -1860,6 +1860,9 @@
     }
 
     private synchronized ScriptFunction getBuiltinJavaImporter() {
+        if (getContext().getEnv()._no_java) {
+            throw new IllegalStateException();
+        }
         if (this.builtinJavaImporter == null) {
             this.builtinJavaImporter = initConstructor("JavaImporter", ScriptFunction.class);
         }
@@ -1867,6 +1870,9 @@
     }
 
     private synchronized ScriptObject getBuiltinJavaApi() {
+        if (getContext().getEnv()._no_java) {
+            throw new IllegalStateException();
+        }
         if (this.builtinJavaApi == null) {
             this.builtinJavaApi = initConstructor("Java", ScriptObject.class);
         }
@@ -2028,7 +2034,7 @@
      * @return true if the value is a placeholder, false otherwise.
      */
     public static boolean isLocationPropertyPlaceholder(final Object placeholder) {
-        return placeholder == LAZY_SENTINEL;
+        return placeholder == LOCATION_PLACEHOLDER;
     }
 
     /**
@@ -2313,6 +2319,17 @@
             this.javaApi = LAZY_SENTINEL;
             this.javaImporter = LAZY_SENTINEL;
             initJavaAccess();
+        } else {
+            // delete nasgen-created global properties related to java access
+            this.delete("Java", false);
+            this.delete("JavaImporter", false);
+            this.delete("Packages", false);
+            this.delete("com", false);
+            this.delete("edu", false);
+            this.delete("java", false);
+            this.delete("javafx", false);
+            this.delete("javax", false);
+            this.delete("org", false);
         }
 
         if (! env._no_typed_arrays) {
--- a/test/src/jdk/nashorn/internal/runtime/test/ClassFilterTest.java	Thu Mar 16 10:25:02 2017 -0700
+++ b/test/src/jdk/nashorn/internal/runtime/test/ClassFilterTest.java	Thu Jan 19 13:43:43 2017 +0100
@@ -25,6 +25,7 @@
 
 package jdk.nashorn.internal.runtime.test;
 
+import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.fail;
 import java.io.File;
 import javax.script.ScriptEngine;
@@ -77,6 +78,65 @@
         } catch (final ScriptException e) {
             //emtpy
         }
+        try {
+            engine.eval("Java");
+            fail("TypeError should have been thrown");
+        } catch (final ScriptException e) {
+            //emtpy
+        }
+        try {
+            engine.eval("JavaImporter");
+            fail("TypeError should have been thrown");
+        } catch (final ScriptException e) {
+            //emtpy
+        }
+        try {
+            engine.eval("Packages");
+            fail("TypeError should have been thrown");
+        } catch (final ScriptException e) {
+            //emtpy
+        }
+        try {
+            engine.eval("com");
+            fail("TypeError should have been thrown");
+        } catch (final ScriptException e) {
+            //emtpy
+        }
+        try {
+            engine.eval("edu");
+            fail("TypeError should have been thrown");
+        } catch (final ScriptException e) {
+            //emtpy
+        }
+        try {
+            engine.eval("java");
+            fail("TypeError should have been thrown");
+        } catch (final ScriptException e) {
+            //emtpy
+        }
+        try {
+            engine.eval("javafx");
+            fail("TypeError should have been thrown");
+        } catch (final ScriptException e) {
+            //emtpy
+        }
+        try {
+            engine.eval("javax");
+            fail("TypeError should have been thrown");
+        } catch (final ScriptException e) {
+            //emtpy
+        }
+        try {
+            engine.eval("org");
+            fail("TypeError should have been thrown");
+        } catch (final ScriptException e) {
+            //emtpy
+        }
+        try {
+            assertEquals(engine.eval("Java = this[\"__LINE__\"]; Java === this[\"__LINE__\"]"), Boolean.TRUE);
+        } catch (final ScriptException e) {
+            fail("Unexpected exception", e);
+        }
     }
 
     @Test