|
|||||||||
PREV CLASS NEXT CLASS | 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
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 theSecureASTCustomizer.StatementChecker
interface or SecureASTCustomizer.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
This interface allows the user to plugin custom expression checkers if expression blacklist or whitelist are not sufficient |
static interface |
SecureASTCustomizer.StatementChecker
This interface allows the user to plugin custom statement checkers if statement blacklist or whitelist are not sufficient |
Constructor Summary | |
---|---|
SecureASTCustomizer()
|
Methods inherited from class org.codehaus.groovy.control.customizers.CompilationCustomizer |
---|
getPhase |
Methods inherited from class org.codehaus.groovy.control.CompilationUnit.PrimaryClassNodeOperation |
---|
needSortedInput |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public SecureASTCustomizer()
Method Detail |
---|
public boolean isMethodDefinitionAllowed()
public void setMethodDefinitionAllowed(boolean methodDefinitionAllowed)
public boolean isPackageAllowed()
public boolean isClosuresAllowed()
public void setClosuresAllowed(boolean closuresAllowed)
public void setPackageAllowed(boolean packageAllowed)
public List<String> getImportsBlacklist()
public void setImportsBlacklist(List<String> importsBlacklist)
public List<String> getImportsWhitelist()
public void setImportsWhitelist(List<String> importsWhitelist)
public List<String> getStarImportsBlacklist()
public void setStarImportsBlacklist(List<String> starImportsBlacklist)
public List<String> getStarImportsWhitelist()
public void setStarImportsWhitelist(List<String> starImportsWhitelist)
public List<String> getStaticImportsBlacklist()
public void setStaticImportsBlacklist(List<String> staticImportsBlacklist)
public List<String> getStaticImportsWhitelist()
public void setStaticImportsWhitelist(List<String> staticImportsWhitelist)
public List<String> getStaticStarImportsBlacklist()
public void setStaticStarImportsBlacklist(List<String> staticStarImportsBlacklist)
public List<String> getStaticStarImportsWhitelist()
public void setStaticStarImportsWhitelist(List<String> staticStarImportsWhitelist)
public List<Class<? extends Expression>> getExpressionsBlacklist()
public void setExpressionsBlacklist(List<Class<? extends Expression>> expressionsBlacklist)
public List<Class<? extends Expression>> getExpressionsWhitelist()
public void setExpressionsWhitelist(List<Class<? extends Expression>> expressionsWhitelist)
public List<Class<? extends Statement>> getStatementsBlacklist()
public void setStatementsBlacklist(List<Class<? extends Statement>> statementsBlacklist)
public List<Class<? extends Statement>> getStatementsWhitelist()
public void setStatementsWhitelist(List<Class<? extends Statement>> statementsWhitelist)
public List<Integer> getTokensBlacklist()
public boolean isIndirectImportCheckEnabled()
public void setIndirectImportCheckEnabled(boolean indirectImportCheckEnabled)
indirectImportCheckEnabled
- set to true to enable indirect checkspublic void setTokensBlacklist(List<Integer> tokensBlacklist)
tokensBlacklist
- the tokens. The values of the tokens must be those of Types
public List<Integer> getTokensWhitelist()
public void setTokensWhitelist(List<Integer> tokensWhitelist)
tokensWhitelist
- the tokens. The values of the tokens must be those of Types
public void addStatementCheckers(SecureASTCustomizer.StatementChecker... checkers)
public void addExpressionCheckers(SecureASTCustomizer.ExpressionChecker... checkers)
public List<String> getConstantTypesBlackList()
public void setConstantTypesBlackList(List<String> constantTypesBlackList)
public List<String> getConstantTypesWhiteList()
public void setConstantTypesWhiteList(List<String> constantTypesWhiteList)
public void setConstantTypesClassesWhiteList(List<Class> constantTypesWhiteList)
constantTypesWhiteList
- a list of classes.public void setConstantTypesClassesBlackList(List<Class> constantTypesBlackList)
constantTypesBlackList
- a list of classes.public List<String> getReceiversBlackList()
public void setReceiversBlackList(List<String> receiversBlackList)
receiversBlackList
- the list of refused classes, as fully qualified namespublic void setReceiversClassesBlackList(List<Class> receiversBlacklist)
receiver classes
.
receiversBlacklist
- a list of classes.public List<String> getReceiversWhiteList()
public void setReceiversWhiteList(List<String> receiversWhiteList)
receiversWhiteList
- the list of accepted classes, as fully qualified namespublic void setReceiversClassesWhiteList(List<Class> receiversWhitelist)
receiver classes
.
receiversWhitelist
- a list of classes.public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException
call
in class CompilationUnit.PrimaryClassNodeOperation
CompilationFailedException
|
Copyright © 2003-2012 The Codehaus. All rights reserved. | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |