8007715: Make sure that not all tests run with AllPermission

Reviewed-by: lagergren, attila
This commit is contained in:
Athijegannathan Sundararajan 2013-02-07 17:17:29 +05:30
parent 57ef7ce075
commit b247ddfcc3
20 changed files with 294 additions and 104 deletions

View File

@ -198,25 +198,40 @@
debug="${javac.debug}"
encoding="${javac.encoding}"
includeantruntime="false"/>
<!-- tests that check nashorn internals and internal API -->
<jar jarfile="${nashorn.internal.tests.jar}">
<fileset dir="${build.test.classes.dir}" excludes="**/api/scripting/*"/>
</jar>
<!-- tests that check nashorn script engine (jsr-223) API -->
<jar jarfile="${nashorn.api.tests.jar}">
<fileset dir="${build.test.classes.dir}" includes="**/api/scripting/*"/>
</jar>
</target>
<target name="generate-policy-file">
<!-- Generating nashorn.policy file -->
<echo message="grant codeBase &quot;file:/${basedir}/dist/nashorn.jar&quot; {" file="${build.dir}/nashorn.policy"/>
<!-- nashorn internal tests jar requires AllPermission -->
<echo message="grant codeBase &quot;file:/${basedir}/${nashorn.internal.tests.jar}&quot; {" file="${build.dir}/nashorn.policy"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="permission java.security.AllPermission;" file="${build.dir}/nashorn.policy" append="true"/>
<echo message=" permission java.security.AllPermission;" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="};" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="grant codeBase &quot;file:/${basedir}/build/test/classes&quot; {" file="${build.dir}/nashorn.policy" append="true"/>
<!-- TestNG framework jar needs AllPermission -->
<echo message="grant codeBase &quot;file:/${basedir}/${file.reference.testng.jar}&quot; {" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message=" permission java.security.AllPermission;" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="};" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="grant codeBase &quot;file:/${basedir}/${file.reference.testng.jar}&quot; {" file="${build.dir}/nashorn.policy" append="true"/>
<!-- AllPermission to test/script/trusted tests -->
<echo message="grant codeBase &quot;file:/${basedir}/test/script/trusted/*&quot; {" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message=" permission java.security.AllPermission;" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
@ -225,14 +240,11 @@
<echo message="grant codeBase &quot;file:/${basedir}/test/script/basic/*&quot; {" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message=" permission java.security.AllPermission;" file="${build.dir}/nashorn.policy" append="true"/>
<!-- test/script/basic .js scripts load other script tests -->
<echo message=" permission java.io.FilePermission &quot;${basedir}/test/script/-&quot;, &quot;read&quot;;" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="};" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="grant codeBase &quot;file:/${basedir}/test/perf/*&quot; {" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message=" permission java.security.AllPermission;" file="${build.dir}/nashorn.policy" append="true"/>
<!-- test/script/basic .js scripts can read nashorn.test.* properties -->
<echo message=" permission java.util.PropertyPermission &quot;nashorn.test.*&quot;, &quot;read&quot;;" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="};" file="${build.dir}/nashorn.policy" append="true"/>
<echo message="" file="${build.dir}/nashorn.policy" append="true"/>

View File

@ -53,6 +53,10 @@ parallel.test.runner=jdk.nashorn.internal.test.framework.ParallelTestRunner
# test classes directory
build.test.classes.dir=${build.dir}/test/classes
# nashorn test jar - internal tests jar and api tests jar
nashorn.internal.tests.jar=${build.dir}/nashorn-internal-tests.jar
nashorn.api.tests.jar=${build.dir}/nashorn-api-tests.jar
# test results directory
build.test.results.dir=${build.dir}/test/reports
@ -116,12 +120,13 @@ test.script.dir=test/script
test.basic.dir=test/script/basic
test.error.dir=test/script/error
test.sandbox.dir=test/script/sandbox
test.trusted.dir=test/script/trusted
test.external.dir=test/script/external
test262.dir=${test.external.dir}/test262
test262.suite.dir=${test262.dir}/test/suite
test-sys-prop.test.dir=${test.dir}
test-sys-prop.test.js.roots=${test.basic.dir} ${test.error.dir} ${test.sandbox.dir}
test-sys-prop.test.js.roots=${test.basic.dir} ${test.error.dir} ${test.sandbox.dir} ${test.trusted.dir}
test-sys-prop.test262.suite.dir=${test262.suite.dir}
test-sys-prop.es5conform.testcases.dir=${test.external.dir}/ES5Conform/TestCases
test-sys-prop.test.basic.dir=${test.basic.dir}
@ -205,7 +210,9 @@ test262-test-sys-prop.test.js.framework=\
run.test.classpath=\
${file.reference.testng.jar}:\
${build.test.classes.dir}
${nashorn.internal.tests.jar}:\
${nashorn.api.tests.jar}
src.dir=src
test.src.dir=test/src

View File

@ -607,24 +607,38 @@ public final class Context {
if (src instanceof String) {
srcName = (String)src;
final File file = new File((String)src);
if (file.isFile()) {
url = file.toURI().toURL();
} else if (srcName.indexOf(':') != -1) {
if (srcName.indexOf(':') != -1) {
try {
url = new URL((String)src);
} catch (final MalformedURLException e) {
// fallback URL - nashorn:foo.js - check under jdk/nashorn/internal/runtime/resources
String str = (String)src;
final String str = (String)src;
if (str.startsWith("nashorn:")) {
str = "resources/" + str.substring("nashorn:".length());
url = Context.class.getResource(str);
if (url == null) {
final String resource = "resources/" + str.substring("nashorn:".length());
// NOTE: even sandbox scripts should be able to load scripts in nashorn: scheme
// These scripts are always available and are loaded from nashorn.jar's resources.
final Source code = AccessController.doPrivileged(
new PrivilegedAction<Source>() {
@Override
public Source run() {
try {
final URL resURL = Context.class.getResource(resource);
return (resURL != null)? new Source(str, resURL) : null;
} catch (final IOException exp) {
return null;
}
}
});
if (code == null) {
throw e;
}
return evaluateSource(code, scope, scope);
} else {
throw e;
}
}
} else if (file.isFile()) {
url = file.toURI().toURL();
}
src = url;
}

View File

@ -456,6 +456,8 @@ public final class JavaAdapterFactory {
private static ClassLoader createClassLoader(final ClassLoader parentLoader, final String className,
final byte[] classBytes, final String privilegedActionClassName) {
return new AdapterLoader(parentLoader) {
private final ProtectionDomain myProtectionDomain = getClass().getProtectionDomain();
@Override
protected Class<?> findClass(final String name) throws ClassNotFoundException {
if(name.equals(className)) {
@ -463,7 +465,7 @@ public final class JavaAdapterFactory {
return defineClass(name, bytes, 0, bytes.length, GENERATED_PROTECTION_DOMAIN);
} else if(name.equals(privilegedActionClassName)) {
final byte[] bytes = generatePrivilegedActionClassBytes(privilegedActionClassName.replace('.', '/'));
return defineClass(name, bytes, 0, bytes.length, getClass().getProtectionDomain());
return defineClass(name, bytes, 0, bytes.length, myProtectionDomain);
} else {
throw new ClassNotFoundException(name);
}

View File

@ -0,0 +1,26 @@
basic:
"basic" language and library tests. These need run only with File read
permission to read files under "test/script" or subdirs and property read
permission to read properties named "nashorn.test.*"
error:
scripts that should result in compile-time error. The expected files check
for the error message format etc.
currently-failing:
Tests that fail currently - but should pass eventually.
These are excluded for now.
sandbox:
Tests to check that sandbox scripts cannot access security sensitive resources.
Scripts under this directory run with no special permissions other than
what is given to all "sandbox" scripts.
trusted:
These tests run under AllPermission. Put only those scripts that really need
AllPermission - say for eg. creating class loader, full reflective access.

View File

@ -25,16 +25,16 @@
* NASHORN-758 : nashorn shell command line options improvements
*
* @test
* @option -Dfoo=bar
* @option -Dhello=world
* @option -Dnashorn.test.foo=bar
* @option -Dnashorn.test.hello=world
* @run
*/
if (java.lang.System.getProperty("foo") != "bar") {
fail("System property 'foo' != 'bar'");
if (java.lang.System.getProperty("nashorn.test.foo") != "bar") {
fail("System property 'nashorn.test.foo' != 'bar'");
}
if (java.lang.System.getProperty("hello") != "world") {
fail("System property 'hello' != 'world'");
if (java.lang.System.getProperty("nashorn.test.hello") != "world") {
fail("System property 'nashorn.test.hello' != 'world'");
}

View File

@ -31,7 +31,7 @@
try {
new java.io.FileInputStream("non_existent_file");
} catch (e) {
print(e instanceof java.io.FileNotFoundException);
print(e instanceof java.io.FileNotFoundException || e instanceof java.lang.SecurityException);
}
try {

View File

@ -29,7 +29,7 @@
*/
var File = java.io.File;
print(new File(".").isDirectory());
print(! new File(".").toString().isEmpty());
var obj = {
foo : function (x) {

View File

@ -0,0 +1,63 @@
/*
* 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.
*/
/**
* Check that user defined interface can be implemented.
*
* @test
* @run
* @security
*/
var Window = Java.type("jdk.nashorn.api.scripting.Window");
var WindowEventHandler = Java.type("jdk.nashorn.api.scripting.WindowEventHandler");
var w = new Window();
var loadedFuncReached = false;
// try function to SAM converter
w.onload = function() {
loadedFuncReached = true;
return true;
}
w.onload.loaded();
if (! loadedFuncReached) {
fail("Interface method impl. not called");
}
// reset
loadedFuncReached = false;
// try direct interface implementation
w.onload = new WindowEventHandler() {
loaded: function() {
loadedFuncReached = true;
return true;
}
};
w.onload.loaded();
if (! loadedFuncReached) {
fail("Interface method impl. not called");
}

View File

@ -0,0 +1,50 @@
/*
* 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.
*/
/**
* Check that nashorn mozilla compatibility script can be loaded in sandbox.
*
* @test
* @run
* @security
*/
load("nashorn:mozilla_compat.js");
var obj = {};
if (obj.__proto__ !== Object.prototype) {
fail("__proto__ does not work as expected");
}
var array = [];
if (array.__proto__ !== Array.prototype) {
fail("__proto__ does not work as expected");
}
if (typeof JavaAdapter != 'function') {
fail("JavaAdapter constructor is missing in compatibility script");
}
if (typeof importPackage != 'function') {
fail("importPackage function is missing in compatibility script");
}

View File

@ -0,0 +1,4 @@
This directory contains tests that need AllPermission to run.
Scripts that need to create classloaders, need to reflectively access
declared members of other classes etc. should go here.

View File

@ -972,46 +972,6 @@ public class ScriptEngineTest {
}
}
private static class MyClassLoader extends ClassLoader {
// to check if script engine uses the specified class loader
private final boolean[] reached = new boolean[1];
@Override
protected Class findClass(final String name) throws ClassNotFoundException {
// flag that it reached here
reached[0] = true;
return super.findClass(name);
}
public boolean reached() {
return reached[0];
}
};
@Test
public void factoryClassLoaderTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final MyClassLoader loader = new MyClassLoader();
// set the classloader as app class loader
final ScriptEngine e = nfac.getScriptEngine(loader);
try {
e.eval("Packages.foo");
// check that the class loader was attempted
assertTrue(loader.reached(), "did not reach class loader!");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
@Test
public void factoryOptionsTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
@ -1033,38 +993,4 @@ public class ScriptEngineTest {
fail("Cannot find nashorn factory!");
}
@Test
public void factoryClassLoaderAndOptionsTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "-strict" };
final MyClassLoader loader = new MyClassLoader();
// set the classloader as app class loader
final ScriptEngine e = nfac.getScriptEngine(options, loader);
try {
e.eval("Packages.foo");
// check that the class loader was attempted
assertTrue(loader.reached(), "did not reach class loader!");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
try {
// strict mode - delete of a var should throw SyntaxError
e.eval("var d = 2; delete d;");
} catch (final ScriptException se) {
// check that the error message contains "SyntaxError"
assertTrue(se.getMessage().contains("SyntaxError"));
}
return;
}
}
fail("Cannot find nashorn factory!");
}
}

View File

@ -27,8 +27,15 @@ package jdk.nashorn.internal.runtime;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.util.Map;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import jdk.nashorn.api.scripting.ScriptEngineTest;
import jdk.nashorn.internal.runtime.options.Options;
import org.testng.annotations.Test;
@ -107,4 +114,83 @@ public class ContextTest {
final ScriptFunction func = cx.compileScript(source, global, cx._strict);
return func != null ? ScriptRuntime.apply(func, global) : null;
}
// Tests for trusted client usage of nashorn script engine factory extension API
private static class MyClassLoader extends ClassLoader {
// to check if script engine uses the specified class loader
private final boolean[] reached = new boolean[1];
@Override
protected Class findClass(final String name) throws ClassNotFoundException {
// flag that it reached here
reached[0] = true;
return super.findClass(name);
}
public boolean reached() {
return reached[0];
}
};
// These are for "private" extension API of NashornScriptEngineFactory that
// accepts a ClassLoader and/or command line options.
@Test
public void factoryClassLoaderTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final MyClassLoader loader = new MyClassLoader();
// set the classloader as app class loader
final ScriptEngine e = nfac.getScriptEngine(loader);
try {
e.eval("Packages.foo");
// check that the class loader was attempted
assertTrue(loader.reached(), "did not reach class loader!");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
return;
}
}
fail("Cannot find nashorn factory!");
}
@Test
public void factoryClassLoaderAndOptionsTest() {
final ScriptEngineManager sm = new ScriptEngineManager();
for (ScriptEngineFactory fac : sm.getEngineFactories()) {
if (fac instanceof NashornScriptEngineFactory) {
final NashornScriptEngineFactory nfac = (NashornScriptEngineFactory)fac;
final String[] options = new String[] { "-strict" };
final MyClassLoader loader = new MyClassLoader();
// set the classloader as app class loader
final ScriptEngine e = nfac.getScriptEngine(options, loader);
try {
e.eval("Packages.foo");
// check that the class loader was attempted
assertTrue(loader.reached(), "did not reach class loader!");
} catch (final ScriptException se) {
se.printStackTrace();
fail(se.getMessage());
}
try {
// strict mode - delete of a var should throw SyntaxError
e.eval("var d = 2; delete d;");
} catch (final ScriptException se) {
// check that the error message contains "SyntaxError"
assertTrue(se.getMessage().contains("SyntaxError"));
}
return;
}
}
fail("Cannot find nashorn factory!");
}
}