我在沙箱Groovy上工作,我想阻止.@
运算符。我使用的是SecureASTCustomizer
,我写了一个自定义SecureASTCustomizer.ExpressionChecker
,我删除了授权。
我的问题是:我找不到检测@
运算符的方法。
答案 0 :(得分:5)
你可以这样做:
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.customizers.SecureASTCustomizer
import org.codehaus.groovy.control.customizers.SecureASTCustomizer.ExpressionChecker as EC
import org.codehaus.groovy.ast.expr.AttributeExpression
def config = new CompilerConfiguration()
def secure = new SecureASTCustomizer()
secure.addExpressionCheckers ({ expr ->
!(expr instanceof AttributeExpression)
} as SecureASTCustomizer.ExpressionChecker)
config.addCompilationCustomizers(secure)
def shell = new GroovyShell(config)
shell.evaluate '''
class A { int val }
def a = new A(val:123)
a.@val
'''
答案 1 :(得分:0)
groovy-sandbox库支持拦截此内容。
(SecureASTCustomizer
实际上根本不安全。请勿尝试将其用于沙箱。)