8281217: Source file launch with security manager enabled fails
Reviewed-by: sundar
This commit is contained in:
parent
35076af13a
commit
99b8ed9dbf
src/jdk.compiler/share/classes/com/sun/tools/javac
test/langtools/tools/javac/launcher
@ -64,8 +64,6 @@ import java.util.Map;
|
||||
import java.util.MissingResourceException;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.ResourceBundle;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.NestingKind;
|
||||
@ -129,7 +127,9 @@ public class Main {
|
||||
*/
|
||||
public static void main(String... args) throws Throwable {
|
||||
try {
|
||||
new Main(System.err).run(VM.getRuntimeArguments(), args);
|
||||
new Main(System.err)
|
||||
.checkSecurityManager()
|
||||
.run(VM.getRuntimeArguments(), args);
|
||||
} catch (Fault f) {
|
||||
System.err.println(f.getMessage());
|
||||
System.exit(1);
|
||||
@ -162,6 +162,19 @@ public class Main {
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a security manager is present and throws an exception if so.
|
||||
* @return this object
|
||||
* @throws Fault if a security manager is present
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
private Main checkSecurityManager() throws Fault {
|
||||
if (System.getSecurityManager() != null) {
|
||||
throw new Fault(Errors.SecurityManager);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles a source file, and executes the main method it contains.
|
||||
*
|
||||
|
@ -84,6 +84,9 @@ launcher.error=\
|
||||
launcher.err.no.args=\
|
||||
no path for source file
|
||||
|
||||
launcher.err.security.manager=\
|
||||
cannot use source-code launcher with a security manager enabled
|
||||
|
||||
# 0: string
|
||||
launcher.err.invalid.filename=\
|
||||
invalid path for source file: {0}
|
||||
|
@ -213,48 +213,22 @@ public class SourceLauncherTest extends TestRunner {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPermissions(Path base) throws IOException {
|
||||
// does not work on exploded image, because the default policy file assumes jrt:; skip the test
|
||||
if (Files.exists(Path.of(System.getProperty("java.home")).resolve("modules"))) {
|
||||
out.println("JDK using exploded modules; test skipped");
|
||||
return;
|
||||
}
|
||||
|
||||
Path policyFile = base.resolve("test.policy");
|
||||
Path sourceFile = base.resolve("TestPermissions.java");
|
||||
|
||||
tb.writeFile(policyFile,
|
||||
"grant codeBase \"jrt:/jdk.compiler\" {\n" +
|
||||
" permission java.security.AllPermission;\n" +
|
||||
"};\n" +
|
||||
"grant codeBase \"" + sourceFile.toUri().toURL() + "\" {\n" +
|
||||
" permission java.util.PropertyPermission \"user.dir\", \"read\";\n" +
|
||||
"};\n");
|
||||
|
||||
public void testSecurityManager(Path base) throws IOException {
|
||||
Path sourceFile = base.resolve("HelloWorld.java");
|
||||
tb.writeJavaFiles(base,
|
||||
"import java.net.URL;\n" +
|
||||
"class TestPermissions {\n" +
|
||||
" public static void main(String... args) {\n" +
|
||||
" System.out.println(\"user.dir=\" + System.getProperty(\"user.dir\"));\n" +
|
||||
" try {\n" +
|
||||
" System.setProperty(\"user.dir\", \"\");\n" +
|
||||
" System.out.println(\"no exception\");\n" +
|
||||
" System.exit(1);\n" +
|
||||
" } catch (SecurityException e) {\n" +
|
||||
" System.out.println(\"exception: \" + e);\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
"class HelloWorld {\n" +
|
||||
" public static void main(String... args) {\n" +
|
||||
" System.out.println(\"Hello World!\");\n" +
|
||||
" }\n" +
|
||||
"}");
|
||||
|
||||
String log = new JavaTask(tb)
|
||||
.vmOptions("-Djava.security.manager", "-Djava.security.policy=" + policyFile)
|
||||
.vmOptions("-Djava.security.manager=default")
|
||||
.className(sourceFile.toString())
|
||||
.run(Task.Expect.SUCCESS)
|
||||
.getOutput(Task.OutputKind.STDOUT);
|
||||
checkEqual("stdout", log.trim(),
|
||||
"user.dir=" + System.getProperty("user.dir") + "\n" +
|
||||
"exception: java.security.AccessControlException: " +
|
||||
"access denied (\"java.util.PropertyPermission\" \"user.dir\" \"write\")");
|
||||
.run(Task.Expect.FAIL)
|
||||
.getOutput(Task.OutputKind.STDERR);
|
||||
checkContains("stderr", log,
|
||||
"error: cannot use source-code launcher with a security manager enabled");
|
||||
}
|
||||
|
||||
public void testSystemProperty(Path base) throws IOException {
|
||||
@ -710,6 +684,14 @@ public class SourceLauncherTest extends TestRunner {
|
||||
}
|
||||
}
|
||||
|
||||
void checkContains(String name, String found, String expect) {
|
||||
expect = expect.replace("\n", tb.lineSeparator);
|
||||
out.println(name + ": " + found);
|
||||
if (!found.contains(expect)) {
|
||||
error("Expected output not found: " + expect);
|
||||
}
|
||||
}
|
||||
|
||||
void checkEqual(String name, List<String> found, List<String> expect) {
|
||||
out.println(name + ": " + found);
|
||||
tb.checkEqual(expect, found);
|
||||
|
Loading…
x
Reference in New Issue
Block a user