保护不受信任的Java代码

3
我有一个服务器应用程序,可以接收客户端上传的Java代码,然后根据其中的方法调用进行处理。
我最初的想法是编译并运行它,但当我深思熟虑时...安全问题。如何限制特定代码执行的调用,例如:它可以执行基本的Java循环等操作,访问重要类(如Math)和访问我的服务器中的某些方法,但不能对服务器造成任何伤害或有害操作。

8
创建一个Java沙盒,请查看这里 https://dev59.com/bnI-5IYBdhLWcg3wta1q,这里 https://dev59.com/CXE85IYBdhLWcg3wQxLG 和这里 https://dev59.com/f3RB5IYBdhLWcg3wz6ad。 - vzamanillo
2
我建议重新思考这个问题。 - Tom Hawtin - tackline
我认为这个问题需要更多的明确说明,包括它期望哪些输入(是否可以使用任何恶意代码?),需要保护哪些系统资源,以及需要(安全地)允许被允许的代码访问哪些系统访问权限。你提到了 Math,但是你服务器上的那些“其他方法”是什么? - starball
1个回答

0

在运行上传代码时,您需要在运行时定义一个策略。参考 Oracle 的文档 默认策略实现和策略文件语法

Specifying an Additional Policy File at Runtime

It is also possible to specify an additional or a different policy file when invoking execution of an application. This can be done via the "-Djava.security.policy" command line argument, which sets the value of the java.security.policy property. For example, if you use

java -Djava.security.manager -Djava.security.policy=someURL SomeApp

where someURL is a URL specifying the location of a policy file, then the specified policy file will be loaded in addition to all the policy files that are specified in the security properties file.

您可以在该策略中使用权限来限制代码的执行,例如:

permission java.io.FilePermission "/home/user/*", "read";

希望这能帮到你!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接