沙盒Java / Groovy / Freemarker代码 - 防止执行特定的方法 [英] Sandboxing Java / Groovy / Freemarker Code - Preventing execution of specific methods
问题描述
我正在开发一个系统,允许开发者上传自定义的groovy脚本和freemarker模板。
我可以提供一定程度的安全性,默认的Java安全基础设施 - 即阻止代码访问文件系统或网络,但是我需要限制对特定方法的访问。
我的计划是修改Groovy和Freemarker的运行时间来读取将白名单或黑名单某些方法的注释,但这会迫使我维护他们代码的分叉版本,这是不可取的。
全部我基本上需要能够做的是在从Groovy或Freemarker调用时阻止执行特定的方法。我已经考虑过一个可以看到调用堆栈的黑客攻击,但是这会造成巨大的速度(而且非常混乱)。
有没有人有任何其他的想法来实现这个?
您可以通过继承GroovyClassLoader并在AST访问器中强制约束来实现。这篇文章解释了如何做到这一点: http:// hamletdarcy .blogspot.com / 2009/01 / groovy-compile-time-meta-magic.html此外,引用的代码位于samples文件夹中Groovy 1.6安装程序。
I'm developing a system that allows developers to upload custom groovy scripts and freemarker templates.
I can provide a certain level of security at a very high level with the default Java security infrastructure - i.e. prevent code from accessing the filesystem or network, however I have a need to restrict access to specific methods.
My plan was to modify the Groovy and Freemarker runtimes to read Annotations that would either whitelist or blacklist certain methods, however this would force me to maintain a forked version of their code, which is not desirable.
All I essentially need to be able to do is prevent the execution of specific methods when called from Groovy or Freemarker. I've considered a hack that would look at the call stack, but this would be a massive speed hit (and it quite messy).
Does anyone have any other ideas for implementing this?
You can do it by subclassing the GroovyClassLoader and enforcing your constraints within an AST Visitor. THis post explains how to do it: http://hamletdarcy.blogspot.com/2009/01/groovy-compile-time-meta-magic.html
Also, the code referenced there is in the samples folder of Groovy 1.6 installer.
这篇关于沙盒Java / Groovy / Freemarker代码 - 防止执行特定的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!