Class SecureASTCustomizer
- java.lang.Object
- 
- org.codehaus.groovy.control.customizers.CompilationCustomizer
- 
- org.codehaus.groovy.control.customizers.SecureASTCustomizer
 
 
- 
- All Implemented Interfaces:
- CompilationUnit.IPrimaryClassNodeOperation
 
 public class SecureASTCustomizer extends CompilationCustomizer This customizer allows securing source code by controlling what code constructs are permitted. This is typically done when using Groovy for its scripting or domain specific language (DSL) features. 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 security customization options found in this class work with either allowed or disallowed lists. This means that, for a single option, you can set an allowed list OR a disallowed list, but not both. You can mix allowed/disallowed strategies for different options. For example, you can have an allowed import list and a disallowed tokens list. The recommended way of securing shells is to use allowed lists because it is guaranteed that future features of the Groovy language won't be accidentally allowed unless explicitly added to the allowed list. Using disallowed lists, you can limit the features of the language constructs supported by your shell by opting out, but new language features are then implicitly also available and this may not be desirable. The implication is that you might need to update your configuration with each new release. If neither an allowed list nor a disallowed list is set, then everything is permitted. Combinations of import and star import constraints are authorized as long as you use the same type of list for both. For example, you may use an import allowed list and a star import allowed list together, but you cannot use an import allowed list with a star import disallowed list. Static imports are handled separately, meaning that disallowing an import does not prevent from allowing a static import. Eventually, if the features provided here are not sufficient, you may implement custom AST filtering handlers, either implementing the SecureASTCustomizer.StatementCheckerinterface orSecureASTCustomizer.ExpressionCheckerinterface then register your handlers thanks to theaddExpressionCheckers(org.codehaus.groovy.control.customizers.SecureASTCustomizer.ExpressionChecker...)andaddStatementCheckers(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.Mathclasses 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 allowedImports = [] allowedStaticImports = [] allowedStaticStarImports = ['java.lang.Math'] // only java.lang.Math is allowed allowedTokens = [ 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() allowedConstantTypesClasses = [ Integer, Float, Long, Double, BigDecimal, Integer.TYPE, Long.TYPE, Float.TYPE, Double.TYPE ].asImmutable() allowedReceiversClasses = [ Math, Integer, Float, Double, Long, BigDecimal ].asImmutable() } CompilerConfiguration config = new CompilerConfiguration() config.addCompilationCustomizers(imports, secure) GroovyClassLoader loader = new GroovyClassLoader(this.class.classLoader, config)- Since:
- 1.8.0
 
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description static interfaceSecureASTCustomizer.ExpressionCheckerThis interface allows the user to provide a custom expression checker if the dis/allowed expression lists are not sufficientprotected classSecureASTCustomizer.SecuringCodeVisitorThis visitor directly implements theGroovyCodeVisitorinterface instead of using theCodeVisitorSupportclass to make sure that future features of the language gets managed by this visitor.static interfaceSecureASTCustomizer.StatementCheckerThis interface allows the user to provide a custom statement checker if the dis/allowed statement lists are not sufficient
 - 
Constructor SummaryConstructors Constructor Description SecureASTCustomizer()
 - 
Method Summary- 
Methods inherited from class org.codehaus.groovy.control.customizers.CompilationCustomizergetPhase
 - 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 - 
Methods inherited from interface org.codehaus.groovy.control.CompilationUnit.IPrimaryClassNodeOperationdoPhaseOperation, needSortedInput
 
- 
 
- 
- 
- 
Method Detail- 
isMethodDefinitionAllowedpublic boolean isMethodDefinitionAllowed() 
 - 
setMethodDefinitionAllowedpublic void setMethodDefinitionAllowed(boolean methodDefinitionAllowed) 
 - 
isPackageAllowedpublic boolean isPackageAllowed() 
 - 
isClosuresAllowedpublic boolean isClosuresAllowed() 
 - 
setClosuresAllowedpublic void setClosuresAllowed(boolean closuresAllowed) 
 - 
setPackageAllowedpublic void setPackageAllowed(boolean packageAllowed) 
 - 
getImportsBlacklistpublic List<String> getImportsBlacklist() Legacy alias forgetDisallowedImports()
 - 
setImportsBlacklistpublic void setImportsBlacklist(List<String> disallowedImports) Legacy alias forsetDisallowedImports(List)
 - 
getImportsWhitelistpublic List<String> getImportsWhitelist() Legacy alias forgetAllowedImports()
 - 
setImportsWhitelistpublic void setImportsWhitelist(List<String> allowedImports) Legacy alias forsetAllowedImports(List)
 - 
getStarImportsBlacklistpublic List<String> getStarImportsBlacklist() Legacy alias forgetDisallowedStarImports()
 - 
setStarImportsBlacklistpublic void setStarImportsBlacklist(List<String> disallowedStarImports) Legacy alias forsetDisallowedStarImports(List)
 - 
getStarImportsWhitelistpublic List<String> getStarImportsWhitelist() Legacy alias forgetAllowedStarImports()
 - 
setStarImportsWhitelistpublic void setStarImportsWhitelist(List<String> allowedStarImports) Legacy alias forsetAllowedStarImports(List)
 - 
getStaticImportsBlacklistpublic List<String> getStaticImportsBlacklist() Legacy alias forgetDisallowedStaticImports()
 - 
setDisallowedStaticImportspublic void setDisallowedStaticImports(List<String> disallowedStaticImports) 
 - 
setStaticImportsBlacklistpublic void setStaticImportsBlacklist(List<String> disallowedStaticImports) Legacy alias forsetDisallowedStaticImports(List)
 - 
getStaticImportsWhitelistpublic List<String> getStaticImportsWhitelist() Legacy alias forgetAllowedStaticImports()
 - 
setStaticImportsWhitelistpublic void setStaticImportsWhitelist(List<String> allowedStaticImports) Legacy alias forsetAllowedStaticImports(List)
 - 
getStaticStarImportsBlacklistpublic List<String> getStaticStarImportsBlacklist() Legacy alias forgetDisallowedStaticStarImports()
 - 
setDisallowedStaticStarImportspublic void setDisallowedStaticStarImports(List<String> disallowedStaticStarImports) 
 - 
setStaticStarImportsBlacklistpublic void setStaticStarImportsBlacklist(List<String> disallowedStaticStarImports) Legacy alias forsetDisallowedStaticStarImports(List)
 - 
getStaticStarImportsWhitelistpublic List<String> getStaticStarImportsWhitelist() Legacy alias forgetAllowedStaticStarImports()
 - 
setAllowedStaticStarImportspublic void setAllowedStaticStarImports(List<String> allowedStaticStarImports) 
 - 
setStaticStarImportsWhitelistpublic void setStaticStarImportsWhitelist(List<String> allowedStaticStarImports) Legacy alias forsetAllowedStaticStarImports(List)
 - 
getDisallowedExpressionspublic List<Class<? extends Expression>> getDisallowedExpressions() 
 - 
getExpressionsBlacklistpublic List<Class<? extends Expression>> getExpressionsBlacklist() Legacy alias forgetDisallowedExpressions()
 - 
setDisallowedExpressionspublic void setDisallowedExpressions(List<Class<? extends Expression>> disallowedExpressions) 
 - 
setExpressionsBlacklistpublic void setExpressionsBlacklist(List<Class<? extends Expression>> disallowedExpressions) Legacy alias forsetDisallowedExpressions(List)
 - 
getAllowedExpressionspublic List<Class<? extends Expression>> getAllowedExpressions() 
 - 
getExpressionsWhitelistpublic List<Class<? extends Expression>> getExpressionsWhitelist() Legacy alias forgetAllowedExpressions()
 - 
setAllowedExpressionspublic void setAllowedExpressions(List<Class<? extends Expression>> allowedExpressions) 
 - 
setExpressionsWhitelistpublic void setExpressionsWhitelist(List<Class<? extends Expression>> allowedExpressions) Legacy alias forsetAllowedExpressions(List)
 - 
getStatementsBlacklistpublic List<Class<? extends Statement>> getStatementsBlacklist() Legacy alias forgetDisallowedStatements()
 - 
setDisallowedStatementspublic void setDisallowedStatements(List<Class<? extends Statement>> disallowedStatements) 
 - 
setStatementsBlacklistpublic void setStatementsBlacklist(List<Class<? extends Statement>> disallowedStatements) Legacy alias forsetDisallowedStatements(List)
 - 
getStatementsWhitelistpublic List<Class<? extends Statement>> getStatementsWhitelist() Legacy alias forgetAllowedStatements()
 - 
setAllowedStatementspublic void setAllowedStatements(List<Class<? extends Statement>> allowedStatements) 
 - 
setStatementsWhitelistpublic void setStatementsWhitelist(List<Class<? extends Statement>> allowedStatements) Legacy alias forsetAllowedStatements(List)
 - 
isIndirectImportCheckEnabledpublic boolean isIndirectImportCheckEnabled() 
 - 
setIndirectImportCheckEnabledpublic void setIndirectImportCheckEnabled(boolean indirectImportCheckEnabled) Set this option to true if you want your import rules to be checked against every class node. This means that if someone uses a fully qualified class name, then it will also be checked against the import rules, preventing, for example, instantiation of classes without imports thanks to FQCN.- Parameters:
- indirectImportCheckEnabled- set to true to enable indirect checks
 
 - 
getTokensBlacklistpublic List<Integer> getTokensBlacklist() Legacy alias forgetDisallowedTokens()
 - 
setDisallowedTokenspublic void setDisallowedTokens(List<Integer> disallowedTokens) Sets the list of tokens which are not permitted.- Parameters:
- disallowedTokens- the tokens. The values of the tokens must be those of- Types
 
 - 
setTokensBlacklistpublic void setTokensBlacklist(List<Integer> disallowedTokens) Alias forsetDisallowedTokens(List).
 - 
getTokensWhitelistpublic List<Integer> getTokensWhitelist() Legacy alias forgetAllowedTokens()
 - 
setAllowedTokenspublic void setAllowedTokens(List<Integer> allowedTokens) Sets the list of tokens which are permitted.- Parameters:
- allowedTokens- the tokens. The values of the tokens must be those of- Types
 
 - 
setTokensWhitelistpublic void setTokensWhitelist(List<Integer> allowedTokens) Legacy alias forsetAllowedTokens(List)
 - 
addStatementCheckerspublic void addStatementCheckers(SecureASTCustomizer.StatementChecker... checkers) 
 - 
addExpressionCheckerspublic void addExpressionCheckers(SecureASTCustomizer.ExpressionChecker... checkers) 
 - 
getConstantTypesBlackListpublic List<String> getConstantTypesBlackList() Legacy alias forgetDisallowedConstantTypes()
 - 
setConstantTypesBlackListpublic void setConstantTypesBlackList(List<String> constantTypesBlackList) 
 - 
getConstantTypesWhiteListpublic List<String> getConstantTypesWhiteList() Legacy alias forgetAllowedStatements()
 - 
setConstantTypesWhiteListpublic void setConstantTypesWhiteList(List<String> allowedConstantTypes) Legacy alias forsetAllowedConstantTypes(List)
 - 
setAllowedConstantTypesClassespublic void setAllowedConstantTypesClasses(List<Class> allowedConstantTypes) An alternative way of setting constant types.- Parameters:
- allowedConstantTypes- a list of classes.
 
 - 
setConstantTypesClassesWhiteListpublic void setConstantTypesClassesWhiteList(List<Class> allowedConstantTypes) Legacy alias forsetAllowedConstantTypesClasses(List)
 - 
setDisallowedConstantTypesClassespublic void setDisallowedConstantTypesClasses(List<Class> disallowedConstantTypes) An alternative way of setting constant types.- Parameters:
- disallowedConstantTypes- a list of classes.
 
 - 
setConstantTypesClassesBlackListpublic void setConstantTypesClassesBlackList(List<Class> disallowedConstantTypes) Legacy alias forsetDisallowedConstantTypesClasses(List)
 - 
getReceiversBlackListpublic List<String> getReceiversBlackList() Legacy alias forgetDisallowedReceivers()
 - 
setDisallowedReceiverspublic void setDisallowedReceivers(List<String> disallowedReceivers) Sets the list of classes which deny method calls. Please note that since Groovy is a dynamic language, and this class performs a static type check, it will be relatively simple to bypass any disallowed list unless the disallowed receivers list contains, at a minimum, Object, Script, GroovyShell, and Eval. Additionally, it is necessary to also have MethodPointerExpression in the disallowed expressions list for the disallowed receivers list to function as a security check.- Parameters:
- disallowedReceivers- the list of refused classes, as fully qualified names
 
 - 
setReceiversBlackListpublic void setReceiversBlackList(List<String> disallowedReceivers) Legacy alias forsetDisallowedReceivers(List)
 - 
setDisallowedReceiversClassespublic void setDisallowedReceiversClasses(List<Class> disallowedReceivers) An alternative way of settingreceiver classes.- Parameters:
- disallowedReceivers- a list of classes.
 
 - 
setReceiversClassesBlackListpublic void setReceiversClassesBlackList(List<Class> disallowedReceivers) Legacy alias forsetDisallowedReceiversClasses(List).
 - 
getReceiversWhiteListpublic List<String> getReceiversWhiteList() Legacy alias forgetAllowedReceivers()
 - 
setAllowedReceiverspublic void setAllowedReceivers(List<String> allowedReceivers) Sets the list of classes which may accept method calls.- Parameters:
- allowedReceivers- the list of accepted classes, as fully qualified names
 
 - 
setReceiversWhiteListpublic void setReceiversWhiteList(List<String> allowedReceivers) Legacy alias forsetAllowedReceivers(List)
 - 
setAllowedReceiversClassespublic void setAllowedReceiversClasses(List<Class> allowedReceivers) An alternative way of settingreceiver classes.- Parameters:
- allowedReceivers- a list of classes.
 
 - 
setReceiversClassesWhiteListpublic void setReceiversClassesWhiteList(List<Class> allowedReceivers) Legacy alias forsetAllowedReceiversClasses(List)
 - 
callpublic void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException - Throws:
- CompilationFailedException
 
 - 
createGroovyCodeVisitorprotected GroovyCodeVisitor createGroovyCodeVisitor() 
 - 
checkMethodDefinitionAllowedprotected void checkMethodDefinitionAllowed(ClassNode owner) 
 - 
filterMethodsprotected static List<MethodNode> filterMethods(ClassNode owner) 
 - 
assertStarImportIsAllowedprotected void assertStarImportIsAllowed(String packageName) 
 - 
assertImportIsAllowedprotected void assertImportIsAllowed(String className) 
 
- 
 
-