安全性如何创建Java沙盒?
我想让我的应用程序运行其他人的代码,也就是插件。但是,我有什么办法可以确保安全,这样他们就不会编写恶意代码了。我如何控制他们能做什么或不能做什么
我偶然发现JVM有一个“内置沙盒”特性——这是什么?这是唯一的方法吗?是否有用于制作沙盒的第三方Java库
我有什么选择?链接到指南和例子是感激的
你可以在下面搜索框中键入要查询的问题!
我想让我的应用程序运行其他人的代码,也就是插件。但是,我有什么办法可以确保安全,这样他们就不会编写恶意代码了。我如何控制他们能做什么或不能做什么
我偶然发现JVM有一个“内置沙盒”特性——这是什么?这是唯一的方法吗?是否有用于制作沙盒的第三方Java库
我有什么选择?链接到指南和例子是感激的
# 1 楼答案
对于AWT/Swing应用程序,您需要使用非标准的
AppContext
类,该类可以随时更改。因此,为了有效,您需要启动另一个进程来运行插件代码,并处理两者之间的通信(有点像Chrome)。插件过程将需要一个SecurityManager
集和一个ClassLoader
来隔离插件代码并对插件类应用适当的ProtectionDomain
# 2 楼答案
下面是使用SecurityManager解决问题的方法:
https://svn.code.sf.net/p/loggifier/code/trunk/de.unkrig.commons.lang/src/de/unkrig/commons/lang/security/Sandbox.java
# 3 楼答案
看看the java-sandbox project,它允许轻松创建非常灵活的沙盒来运行不受信任的代码
# 4 楼答案
你在找security manager。可以通过指定policy来限制应用程序的权限
# 5 楼答案
关于这个问题的讨论启发我启动了自己的沙箱项目
https://github.com/Black-Mantha/sandbox
在这篇文章中,我遇到了一个重要的安全性问题:“如何允许沙箱之外的代码绕过
SecurityManager
?”我将沙盒代码放在它自己的线程组中,并且在该组之外时总是授予权限。如果仍然需要在该组中运行特权代码(例如在回调中),则可以使用ThreadLocal仅为该线程设置标志。类加载器将阻止沙盒访问ThreadLocal。此外,如果这样做,则需要禁止使用终结器,因为它们在ThreadGroup之外的专用线程中运行
# 6 楼答案
定义和注册您自己的安全管理器将允许您限制代码的功能-有关SecurityManager,请参阅oracle文档