1
0
mirror of https://github.com/mitb-archive/filebot synced 2024-11-10 19:35:15 -05:00
filebot/source/net/filebot/format/SecureCompiledScript.java

93 lines
3.2 KiB
Java
Raw Normal View History

2014-04-19 02:30:29 -04:00
package net.filebot.format;
2012-03-24 22:50:28 -04:00
import java.io.File;
import java.io.FilePermission;
2013-10-17 03:11:32 -04:00
import java.lang.management.ManagementPermission;
2012-03-24 22:50:28 -04:00
import java.net.SocketPermission;
import java.security.AccessControlContext;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.security.ProtectionDomain;
import java.util.PropertyPermission;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
2014-04-19 02:30:29 -04:00
import net.filebot.util.ExceptionUtilities;
2012-03-24 22:50:28 -04:00
public class SecureCompiledScript extends CompiledScript {
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
public static PermissionCollection getDefaultSandboxPermissions() {
Permissions permissions = new Permissions();
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
permissions.add(new RuntimePermission("createClassLoader"));
2013-12-03 10:24:57 -05:00
permissions.add(new RuntimePermission("accessDeclaredMembers"));
permissions.add(new RuntimePermission("accessClassInPackage.*"));
2013-04-04 17:41:47 -04:00
permissions.add(new RuntimePermission("modifyThread"));
2012-03-24 22:50:28 -04:00
permissions.add(new FilePermission("<<ALL FILES>>", "read"));
permissions.add(new SocketPermission("*", "connect"));
permissions.add(new PropertyPermission("*", "read"));
permissions.add(new RuntimePermission("getenv.*"));
permissions.add(new RuntimePermission("getFileSystemAttributes"));
2013-10-17 03:11:32 -04:00
permissions.add(new ManagementPermission("monitor"));
2012-03-24 22:50:28 -04:00
// write permissions for temp and cache folders
try {
permissions.add(new FilePermission(new File(System.getProperty("java.io.tmpdir")).getAbsolutePath() + File.separator + "-", "write, delete"));
permissions.add(new FilePermission(new File(System.getProperty("ehcache.disk.store.dir")).getAbsolutePath() + File.separator + "-", "write, delete"));
} catch (Exception e) {
// ignore
}
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
return permissions;
}
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
private final CompiledScript compiledScript;
private final AccessControlContext sandbox;
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
public SecureCompiledScript(CompiledScript compiledScript) {
this(compiledScript, new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, getDefaultSandboxPermissions()) }));
}
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
public SecureCompiledScript(CompiledScript compiledScript, AccessControlContext sandbox) {
this.compiledScript = compiledScript;
this.sandbox = sandbox;
}
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
@Override
public Object eval(final ScriptContext context) throws ScriptException {
try {
return AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
@Override
public Object run() throws ScriptException {
return compiledScript.eval(context);
}
}, sandbox);
} catch (PrivilegedActionException e) {
AccessControlException accessException = ExceptionUtilities.findCause(e, AccessControlException.class);
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
// try to unwrap AccessControlException
if (accessException != null)
throw new ExpressionException(accessException);
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
// forward ScriptException
// e.getException() should be an instance of ScriptException,
// as only "checked" exceptions will be "wrapped" in a PrivilegedActionException
throw (ScriptException) e.getException();
}
}
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
@Override
public ScriptEngine getEngine() {
return compiledScript.getEngine();
}
2013-10-17 03:11:32 -04:00
2012-03-24 22:50:28 -04:00
}