|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object 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()
|
Method Summary | |
---|---|
void
|
addExpressionCheckers(SecureASTCustomizer.ExpressionChecker... checkers)
|
void
|
addStatementCheckers(SecureASTCustomizer.StatementChecker... checkers)
|
void
|
call(SourceUnit source, GeneratorContext context, ClassNode classNode)
|
java.util.List
|
getConstantTypesBlackList()
|
java.util.List
|
getConstantTypesWhiteList()
|
java.util.List
|
getExpressionsBlacklist()
|
java.util.List
|
getExpressionsWhitelist()
|
java.util.List
|
getImportsBlacklist()
|
java.util.List
|
getImportsWhitelist()
|
java.util.List
|
getReceiversBlackList()
Sets the list of classes which deny method calls. |
java.util.List
|
getReceiversWhiteList()
Sets the list of classes which may accept method calls. |
java.util.List
|
getStarImportsBlacklist()
|
java.util.List
|
getStarImportsWhitelist()
|
java.util.List
|
getStatementsBlacklist()
|
java.util.List
|
getStatementsWhitelist()
|
java.util.List
|
getStaticImportsBlacklist()
|
java.util.List
|
getStaticImportsWhitelist()
|
java.util.List
|
getStaticStarImportsBlacklist()
|
java.util.List
|
getStaticStarImportsWhitelist()
|
java.util.List
|
getTokensBlacklist()
|
java.util.List
|
getTokensWhitelist()
|
boolean
|
isClosuresAllowed()
|
boolean
|
isIndirectImportCheckEnabled()
|
boolean
|
isMethodDefinitionAllowed()
|
boolean
|
isPackageAllowed()
|
void
|
setClosuresAllowed(boolean closuresAllowed)
|
void
|
setConstantTypesBlackList(java.util.List constantTypesBlackList)
|
void
|
setConstantTypesClassesBlackList(java.util.List constantTypesBlackList)
|
void
|
setConstantTypesClassesWhiteList(java.util.List constantTypesWhiteList)
|
void
|
setConstantTypesWhiteList(java.util.List constantTypesWhiteList)
|
void
|
setExpressionsBlacklist(java.util.List expressionsBlacklist)
|
void
|
setExpressionsWhitelist(java.util.List expressionsWhitelist)
|
void
|
setImportsBlacklist(java.util.List importsBlacklist)
|
void
|
setImportsWhitelist(java.util.List importsWhitelist)
|
void
|
setIndirectImportCheckEnabled(boolean indirectImportCheckEnabled)
Sets the list of tokens which are blacklisted. |
void
|
setMethodDefinitionAllowed(boolean methodDefinitionAllowed)
|
void
|
setPackageAllowed(boolean packageAllowed)
|
void
|
setReceiversBlackList(java.util.List receiversBlackList)
An alternative way of setting receiver classes. |
void
|
setReceiversClassesBlackList(java.util.List receiversBlacklist)
|
void
|
setReceiversClassesWhiteList(java.util.List receiversWhitelist)
|
void
|
setReceiversWhiteList(java.util.List receiversWhiteList)
An alternative way of setting receiver classes. |
void
|
setStarImportsBlacklist(java.util.List starImportsBlacklist)
|
void
|
setStarImportsWhitelist(java.util.List starImportsWhitelist)
Ensures that every star import ends with .* as this is the expected syntax in import checks. |
void
|
setStatementsBlacklist(java.util.List statementsBlacklist)
|
void
|
setStatementsWhitelist(java.util.List statementsWhitelist)
|
void
|
setStaticImportsBlacklist(java.util.List staticImportsBlacklist)
|
void
|
setStaticImportsWhitelist(java.util.List staticImportsWhitelist)
|
void
|
setStaticStarImportsBlacklist(java.util.List staticStarImportsBlacklist)
|
void
|
setStaticStarImportsWhitelist(java.util.List staticStarImportsWhitelist)
|
void
|
setTokensBlacklist(java.util.List tokensBlacklist)
|
void
|
setTokensWhitelist(java.util.List tokensWhitelist)
|
Methods inherited from class CompilationCustomizer | |
---|---|
getPhase |
Constructor Detail |
---|
public SecureASTCustomizer()
Method Detail |
---|
public void addExpressionCheckers(SecureASTCustomizer.ExpressionChecker... checkers)
public void addStatementCheckers(SecureASTCustomizer.StatementChecker... checkers)
assertImportIsAllowed(className); public void call(SourceUnit source, GeneratorContext context, ClassNode classNode)
public java.util.List getConstantTypesBlackList()
public java.util.List getConstantTypesWhiteList()
public java.util.List getExpressionsBlacklist()
public java.util.List getExpressionsWhitelist()
public java.util.List getImportsBlacklist()
public java.util.List getImportsWhitelist()
public java.util.List getReceiversBlackList()
receiversBlackList
- the list of refused classes, as fully qualified names
public java.util.List getReceiversWhiteList()
receiversWhiteList
- the list of accepted classes, as fully qualified names
public java.util.List getStarImportsBlacklist()
public java.util.List getStarImportsWhitelist()
public java.util.List getStatementsBlacklist()
public java.util.List getStatementsWhitelist()
public java.util.List getStaticImportsBlacklist()
public java.util.List getStaticImportsWhitelist()
public java.util.List getStaticStarImportsBlacklist()
public java.util.List getStaticStarImportsWhitelist()
public java.util.List getTokensBlacklist()
public java.util.List getTokensWhitelist()
public boolean isClosuresAllowed()
public boolean isIndirectImportCheckEnabled()
public boolean isMethodDefinitionAllowed()
public boolean isPackageAllowed()
public void setClosuresAllowed(boolean closuresAllowed)
public void setConstantTypesBlackList(java.util.List constantTypesBlackList)
public void setConstantTypesClassesBlackList(java.util.List constantTypesBlackList)
public void setConstantTypesClassesWhiteList(java.util.List constantTypesWhiteList)
public void setConstantTypesWhiteList(java.util.List constantTypesWhiteList)
public void setExpressionsBlacklist(java.util.List expressionsBlacklist)
public void setExpressionsWhitelist(java.util.List expressionsWhitelist)
public void setImportsBlacklist(java.util.List importsBlacklist)
public void setImportsWhitelist(java.util.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(java.util.List receiversBlackList)
receiversBlacklist
- a list of classes.
public void setReceiversClassesBlackList(java.util.List receiversBlacklist)
public void setReceiversClassesWhiteList(java.util.List receiversWhitelist)
public void setReceiversWhiteList(java.util.List receiversWhiteList)
receiversWhitelist
- a list of classes.
public void setStarImportsBlacklist(java.util.List starImportsBlacklist)
public void setStarImportsWhitelist(java.util.List starImportsWhitelist)
public void setStatementsBlacklist(java.util.List statementsBlacklist)
public void setStatementsWhitelist(java.util.List statementsWhitelist)
public void setStaticImportsBlacklist(java.util.List staticImportsBlacklist)
public void setStaticImportsWhitelist(java.util.List staticImportsWhitelist)
public void setStaticStarImportsBlacklist(java.util.List staticStarImportsBlacklist)
public void setStaticStarImportsWhitelist(java.util.List staticStarImportsWhitelist)
public void setTokensBlacklist(java.util.List tokensBlacklist)
public void setTokensWhitelist(java.util.List tokensWhitelist)
Groovy Documentation