我在沙盒 Groovy 上工作,我想阻止 .@
运算符。我正在使用 SecureASTCustomizer
并编写了一个自定义 SecureASTCustomizer.ExpressionChecker
,其中删除了授权。
我的问题是:我找不到检测 @
运算符的方法。
最佳答案
你可以这样做:
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
'''
关于groovy - 如何阻止.@运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17975149/