|
Groovy 2.2.0 | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.groovy.control.CompilationUnit.PrimaryClassNodeOperation
org.codehaus.groovy.control.customizers.CompilationCustomizer
org.codehaus.groovy.control.customizers.SecureASTCustomizer
public class SecureASTCustomizer extends CompilationCustomizer
This customizer allows securing source code by controlling what code constructs are allowed. For example, if you only want to allow arithmetic operations in a groovy shell, you can configure this customizer to restrict package imports, method calls and so on.
Most of the securization options found in this class work with either blacklist or whitelist. This means that, for a single option, you can set a whitelist OR a blacklist, but not both. You can mix whitelist/blacklist strategies for different options. For example, you can have import whitelist and tokens blacklist.
The recommanded way of securing shells is to use whitelists because it is guaranteed that future features of the Groovy language won't be allowed by defaut. Using blacklists, you can limit the features of the languages by opting out, but new language features would require you to update your configuration.
If you set neither a whitelist nor a blacklist, then everything is authorized.
Combinations of import and star imports constraints are authorized as long as you use the same type of list for both. For example, you may use an import whitelist and a star import whitelist together, but you cannot use an import white list with a star import blacklist. static imports are handled separately, meaning that blacklisting an import does not prevent from using a static import.
Eventually, if the features provided here are not sufficient, you may implement custom AST filtering handlers, either implementing the StatementChecker interface or ExpressionChecker interface then register your handlers thanks to the addExpressionCheckers(org.codehaus.groovy.control.customizers.SecureASTCustomizer.ExpressionChecker...) and addStatementCheckers(org.codehaus.groovy.control.customizers.SecureASTCustomizer.StatementChecker...) methods.
Here is an example of usage. We will create a groovy classloader which only supports arithmetic operations and imports the java.lang.Math classes by default.
final ImportCustomizer imports = new ImportCustomizer().addStaticStars('java.lang.Math') // add static import of java.lang.Math final SecureASTCustomizer secure = new SecureASTCustomizer() secure.with { closuresAllowed = false methodDefinitionAllowed = false importsWhitelist = [] staticImportsWhitelist = [] staticStarImportsWhitelist = ['java.lang.Math'] // only java.lang.Math is allowed tokensWhitelist = [ PLUS, MINUS, MULTIPLY, DIVIDE, MOD, POWER, PLUS_PLUS, MINUS_MINUS, COMPARE_EQUAL, COMPARE_NOT_EQUAL, COMPARE_LESS_THAN, COMPARE_LESS_THAN_EQUAL, COMPARE_GREATER_THAN, COMPARE_GREATER_THAN_EQUAL, ].asImmutable() constantTypesClassesWhiteList = [ Integer, Float, Long, Double, BigDecimal, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE ].asImmutable() receiversClassesWhiteList = [ Math, Integer, Float, Double, Long, BigDecimal ].asImmutable() } CompilerConfiguration config = new CompilerConfiguration() config.addCompilationCustomizers(imports, secure) GroovyClassLoader loader = new GroovyClassLoader(this.class.classLoader, config)
Nested Class Summary | |
---|---|
static interface |
SecureASTCustomizer.ExpressionChecker
|
static interface |
SecureASTCustomizer.StatementChecker
|
Constructor Summary | |
SecureASTCustomizer()
|
Methods inherited from class CompilationCustomizer | |
---|---|
getPhase |
Constructor Detail |
---|
public SecureASTCustomizer()
Method Detail |
---|
public void addExpressionCheckers(SecureASTCustomizer.ExpressionChecker... checkers)
public void addStatementCheckers(SecureASTCustomizer.StatementChecker... checkers)
@OverrideassertImportIsAllowed(className); public void call(SourceUnit source, GeneratorContext context, ClassNode classNode)
public List getConstantTypesBlackList()
public List getConstantTypesWhiteList()
public List getExpressionsBlacklist()
public List getExpressionsWhitelist()
public List getImportsBlacklist()
public List getImportsWhitelist()
public List getReceiversBlackList()
public List getReceiversWhiteList()
receiversWhiteList
- the list of accepted classes, as fully qualified names
public List getStarImportsBlacklist()
public List getStarImportsWhitelist()
public List getStatementsBlacklist()
public List getStatementsWhitelist()
public List getStaticImportsBlacklist()
public List getStaticImportsWhitelist()
public List getStaticStarImportsBlacklist()
public List getStaticStarImportsWhitelist()
public List getTokensBlacklist()
public List getTokensWhitelist()
public boolean isClosuresAllowed()
public boolean isIndirectImportCheckEnabled()
public boolean isMethodDefinitionAllowed()
public boolean isPackageAllowed()
public void setClosuresAllowed(boolean closuresAllowed)
public void setConstantTypesBlackList(List constantTypesBlackList)
public void setConstantTypesClassesBlackList(List constantTypesBlackList)
public void setConstantTypesClassesWhiteList(List constantTypesWhiteList)
public void setConstantTypesWhiteList(List constantTypesWhiteList)
public void setExpressionsBlacklist(List expressionsBlacklist)
public void setExpressionsWhitelist(List expressionsWhitelist)
public void setImportsBlacklist(List importsBlacklist)
public void setImportsWhitelist(List importsWhitelist)
public void setIndirectImportCheckEnabled(boolean indirectImportCheckEnabled)
tokensBlacklist
- the tokens. The values of the tokens must be those of Types
public void setMethodDefinitionAllowed(boolean methodDefinitionAllowed)
public void setPackageAllowed(boolean packageAllowed)
public void setReceiversBlackList(List receiversBlackList)
receiversBlacklist
- a list of classes.
public void setReceiversClassesBlackList(List receiversBlacklist)
public void setReceiversClassesWhiteList(List receiversWhitelist)
public void setReceiversWhiteList(List receiversWhiteList)
receiversWhitelist
- a list of classes.
public void setStarImportsBlacklist(List starImportsBlacklist)
public void setStarImportsWhitelist(List starImportsWhitelist)
public void setStatementsBlacklist(List statementsBlacklist)
public void setStatementsWhitelist(List statementsWhitelist)
public void setStaticImportsBlacklist(List staticImportsBlacklist)
public void setStaticImportsWhitelist(List staticImportsWhitelist)
public void setStaticStarImportsBlacklist(List staticStarImportsBlacklist)
public void setStaticStarImportsWhitelist(List staticStarImportsWhitelist)
public void setTokensBlacklist(List tokensBlacklist)
public void setTokensWhitelist(List tokensWhitelist)
Copyright © 2003-2013 The Codehaus. All rights reserved.