aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bin/nashornsecure29
-rw-r--r--bin/nashornsecure.bat27
-rw-r--r--src/jdk/nashorn/api/scripting/NashornScriptEngine.java41
-rw-r--r--src/jdk/nashorn/api/scripting/resources/init.js24
-rw-r--r--src/jdk/nashorn/internal/objects/NativeJSAdapter.java6
-rw-r--r--test/script/sandbox/engine.js34
-rw-r--r--test/script/sandbox/engine.js.EXPECTED1
-rw-r--r--test/script/sandbox/jsadapter.js34
8 files changed, 178 insertions, 18 deletions
diff --git a/bin/nashornsecure b/bin/nashornsecure
new file mode 100644
index 00000000..caec3873
--- /dev/null
+++ b/bin/nashornsecure
@@ -0,0 +1,29 @@
+#!/bin/bash
+#
+# Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+#
+# This code is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License version 2 only, as
+# published by the Free Software Foundation. Oracle designates this
+# particular file as subject to the "Classpath" exception as provided
+# by Oracle in the LICENSE file that accompanied this code.
+#
+# This code is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+# version 2 for more details (a copy is included in the LICENSE file that
+# accompanied this code).
+#
+# You should have received a copy of the GNU General Public License version
+# 2 along with this work; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+# or visit www.oracle.com if you need additional information or have any
+# questions.
+#
+
+[ -z "$JAVA_HOME" ] && echo "Please set JAVA_HOME" && exit 1;
+
+$JAVA_HOME/bin/jrunscript -J-Djava.security.manager -J-Xms2G -J-Xmx2G -J-XX:-TieredCompilation -J-server -J-esa -J-ea -J-Djava.ext.dirs=$JAVA_HOME/jre/lib/ext:`dirname $0`/../dist -J-XX:+HeapDumpOnOutOfMemoryError -J-Djava.lang.invoke.MethodHandle.DEBUG_NAMES=false -J-Dnashorn.debug=true -l nashorn $*
diff --git a/bin/nashornsecure.bat b/bin/nashornsecure.bat
new file mode 100644
index 00000000..d35eeb48
--- /dev/null
+++ b/bin/nashornsecure.bat
@@ -0,0 +1,27 @@
+rem
+rem Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+rem DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+rem
+rem This code is free software; you can redistribute it and/or modify it
+rem under the terms of the GNU General Public License version 2 only, as
+rem published by the Free Software Foundation. Oracle designates this
+rem particular file as subject to the "Classpath" exception as provided
+rem by Oracle in the LICENSE file that accompanied this code.
+rem
+rem This code is distributed in the hope that it will be useful, but WITHOUT
+rem ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+rem FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+rem version 2 for more details (a copy is included in the LICENSE file that
+rem accompanied this code).
+rem
+rem You should have received a copy of the GNU General Public License version
+rem 2 along with this work; if not, write to the Free Software Foundation,
+rem Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+rem
+rem Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+rem or visit www.oracle.com if you need additional information or have any
+rem questions.
+rem
+@echo off
+
+jrunscript -J-Djava.security.manager -J-Xms2G -J-Xmx2G -J-XX:-TieredCompilation -J-server -J-esa -J-ea -J-Djava.ext.dirs=%~dp0\..\dist -J-XX:+HeapDumpOnOutOfMemoryError -J-Dnashorn.debug=true -J-Djava.lang.invoke.MethodHandle.DEBUG_NAMES=false -l nashorn
diff --git a/src/jdk/nashorn/api/scripting/NashornScriptEngine.java b/src/jdk/nashorn/api/scripting/NashornScriptEngine.java
index 27ccb9e4..37a7166d 100644
--- a/src/jdk/nashorn/api/scripting/NashornScriptEngine.java
+++ b/src/jdk/nashorn/api/scripting/NashornScriptEngine.java
@@ -91,7 +91,7 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C
// throw ParseException on first error from script
final ErrorManager errors = new Context.ThrowErrorManager();
- // create new Nashorn Context and get global object
+ // create new Nashorn Context
this.nashornContext = AccessController.doPrivileged(new PrivilegedAction<Context>() {
@Override
public Context run() {
@@ -107,7 +107,19 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C
});
// create new global object
- this.global = nashornContext.createGlobal();
+ this.global = AccessController.doPrivileged(new PrivilegedAction<ScriptObject>() {
+ @Override
+ public ScriptObject run() {
+ try {
+ return nashornContext.createGlobal();
+ } catch (final RuntimeException e) {
+ if (Context.DEBUG) {
+ e.printStackTrace();
+ }
+ throw e;
+ }
+ }
+ });
// current ScriptContext exposed as "context"
global.addOwnProperty("context", Property.NOT_ENUMERABLE, context);
@@ -121,14 +133,8 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C
// evaluate engine initial script
try {
- AccessController.doPrivileged(new PrivilegedExceptionAction<Void>() {
- @Override
- public Void run() throws ScriptException {
- evalEngineScript();
- return null;
- }
- });
- } catch (final PrivilegedActionException e) {
+ evalEngineScript();
+ } catch (final ScriptException e) {
if (Context.DEBUG) {
e.printStackTrace();
}
@@ -330,15 +336,20 @@ public final class NashornScriptEngine extends AbstractScriptEngine implements C
evalSupportScript("resources/engine.js");
}
- private void evalSupportScript(String script) throws ScriptException {
- final URL url = NashornScriptEngine.class.getResource(script);
+ private void evalSupportScript(final String script) throws ScriptException {
try {
- final InputStream is = url.openStream();
- put(ScriptEngine.FILENAME, url);
+ final InputStream is = AccessController.doPrivileged(
+ new PrivilegedExceptionAction<InputStream>() {
+ public InputStream run() throws Exception {
+ final URL url = NashornScriptEngine.class.getResource(script);
+ return url.openStream();
+ }
+ });
+ put(ScriptEngine.FILENAME, "<engine>:" + script);
try (final InputStreamReader isr = new InputStreamReader(is)) {
eval(isr);
}
- } catch (final IOException e) {
+ } catch (final PrivilegedActionException | IOException e) {
throw new ScriptException(e);
} finally {
put(ScriptEngine.FILENAME, null);
diff --git a/src/jdk/nashorn/api/scripting/resources/init.js b/src/jdk/nashorn/api/scripting/resources/init.js
index 9a4935f4..26a81e9c 100644
--- a/src/jdk/nashorn/api/scripting/resources/init.js
+++ b/src/jdk/nashorn/api/scripting/resources/init.js
@@ -187,11 +187,31 @@ function jlist(list) {
}
/**
- * This is java.lang.System properties wrapped by jmap.
+ * This is java.lang.System properties wrapped by JSAdapter.
* For eg. to access java.class.path property, you can use
* the syntax sysProps["java.class.path"]
*/
-var sysProps = jmap(java.lang.System.getProperties());
+var sysProps = new JSAdapter({
+ __get__ : function (name) {
+ return java.lang.System.getProperty(name);
+ },
+ __has__ : function (name) {
+ return java.lang.System.getProperty(name) != null;
+ },
+ __getIds__ : function() {
+ return java.lang.System.getProperties().keySet().toArray();
+ },
+ __delete__ : function(name) {
+ java.lang.System.clearProperty(name);
+ return true;
+ },
+ __put__ : function (name, value) {
+ java.lang.System.setProperty(name, value);
+ },
+ toString: function() {
+ return "<system properties>";
+ }
+});
// stdout, stderr & stdin
var out = java.lang.System.out;
diff --git a/src/jdk/nashorn/internal/objects/NativeJSAdapter.java b/src/jdk/nashorn/internal/objects/NativeJSAdapter.java
index edc9ca4c..bb3931f0 100644
--- a/src/jdk/nashorn/internal/objects/NativeJSAdapter.java
+++ b/src/jdk/nashorn/internal/objects/NativeJSAdapter.java
@@ -734,6 +734,10 @@ public final class NativeJSAdapter extends ScriptObject {
}
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) {
- return MH.findStatic(MethodHandles.lookup(), NativeJSAdapter.class, name, MH.type(rtype, types));
+ try {
+ return MethodHandles.lookup().findStatic(NativeJSAdapter.class, name, MH.type(rtype, types));
+ } catch (final NoSuchMethodException | IllegalAccessException e) {
+ throw new AssertionError(e);
+ }
}
}
diff --git a/test/script/sandbox/engine.js b/test/script/sandbox/engine.js
new file mode 100644
index 00000000..e1d87c78
--- /dev/null
+++ b/test/script/sandbox/engine.js
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * Test that sandbox code can create script engine.
+ *
+ * @test
+ * @run
+ * @security
+ */
+
+var mgr = new javax.script.ScriptEngineManager();
+var engine = mgr.getEngineByName("nashorn");
+print(engine.eval("'hello' + ' world'"));
diff --git a/test/script/sandbox/engine.js.EXPECTED b/test/script/sandbox/engine.js.EXPECTED
new file mode 100644
index 00000000..3b18e512
--- /dev/null
+++ b/test/script/sandbox/engine.js.EXPECTED
@@ -0,0 +1 @@
+hello world
diff --git a/test/script/sandbox/jsadapter.js b/test/script/sandbox/jsadapter.js
new file mode 100644
index 00000000..32bedc8c
--- /dev/null
+++ b/test/script/sandbox/jsadapter.js
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * Test that sandbox code can access jsadapter
+ *
+ * @test
+ * @run
+ * @security
+ */
+
+var mgr = new javax.script.ScriptEngineManager();
+var engine = mgr.getEngineByName("nashorn");
+engine.eval("var v = new JSAdapter() {};");