Class SecureASTCustomizer
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.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
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 Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceThis interface allows the user to provide a custom expression checker if the dis/allowed expression lists are not sufficientstatic interfaceThis interface allows the user to provide a custom statement checker if the dis/allowed statement lists are not sufficient -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidaddStatementCheckers(SecureASTCustomizer.StatementChecker... checkers) voidcall(SourceUnit source, GeneratorContext context, ClassNode classNode) List<Class<? extends Expression>> Legacy alias forgetDisallowedConstantTypes()Legacy alias forgetAllowedStatements()List<Class<? extends Expression>> List<Class<? extends Expression>> Legacy alias forgetDisallowedExpressions()List<Class<? extends Expression>> Legacy alias forgetAllowedExpressions()Legacy alias forgetDisallowedImports()Legacy alias forgetAllowedImports()Legacy alias forgetDisallowedReceivers()Legacy alias forgetAllowedReceivers()Legacy alias forgetDisallowedStarImports()Legacy alias forgetAllowedStarImports()Legacy alias forgetDisallowedStatements()Legacy alias forgetAllowedStatements()Legacy alias forgetDisallowedStaticImports()Legacy alias forgetAllowedStaticImports()Legacy alias forgetDisallowedStaticStarImports()Legacy alias forgetAllowedStaticStarImports()Legacy alias forgetDisallowedTokens()Legacy alias forgetAllowedTokens()booleanbooleanbooleanbooleanvoidsetAllowedConstantTypes(List<String> allowedConstantTypes) voidsetAllowedConstantTypesClasses(List<Class> allowedConstantTypes) An alternative way of setting constant types.voidsetAllowedExpressions(List<Class<? extends Expression>> allowedExpressions) voidsetAllowedImports(List<String> allowedImports) voidsetAllowedReceivers(List<String> allowedReceivers) Sets the list of classes which may accept method calls.voidsetAllowedReceiversClasses(List<Class> allowedReceivers) An alternative way of settingreceiver classes.voidsetAllowedStarImports(List<String> allowedStarImports) voidsetAllowedStatements(List<Class<? extends Statement>> allowedStatements) voidsetAllowedStaticImports(List<String> allowedStaticImports) voidsetAllowedStaticStarImports(List<String> allowedStaticStarImports) voidsetAllowedTokens(List<Integer> allowedTokens) Sets the list of tokens which are permitted.voidsetClosuresAllowed(boolean closuresAllowed) voidsetConstantTypesBlackList(List<String> constantTypesBlackList) voidsetConstantTypesClassesBlackList(List<Class> disallowedConstantTypes) Legacy alias forsetDisallowedConstantTypesClasses(List)voidsetConstantTypesClassesWhiteList(List<Class> allowedConstantTypes) Legacy alias forsetAllowedConstantTypesClasses(List)voidsetConstantTypesWhiteList(List<String> allowedConstantTypes) Legacy alias forsetAllowedConstantTypes(List)voidsetDisallowedConstantTypesClasses(List<Class> disallowedConstantTypes) An alternative way of setting constant types.voidsetDisallowedExpressions(List<Class<? extends Expression>> disallowedExpressions) voidsetDisallowedImports(List<String> disallowedImports) voidsetDisallowedReceivers(List<String> disallowedReceivers) Sets the list of classes which deny method calls.voidsetDisallowedReceiversClasses(List<Class> disallowedReceivers) An alternative way of settingreceiver classes.voidsetDisallowedStarImports(List<String> disallowedStarImports) voidsetDisallowedStatements(List<Class<? extends Statement>> disallowedStatements) voidsetDisallowedStaticImports(List<String> disallowedStaticImports) voidsetDisallowedStaticStarImports(List<String> disallowedStaticStarImports) voidsetDisallowedTokens(List<Integer> disallowedTokens) Sets the list of tokens which are not permitted.voidsetExpressionsBlacklist(List<Class<? extends Expression>> disallowedExpressions) Legacy alias forsetDisallowedExpressions(List)voidsetExpressionsWhitelist(List<Class<? extends Expression>> allowedExpressions) Legacy alias forsetAllowedExpressions(List)voidsetImportsBlacklist(List<String> disallowedImports) Legacy alias forsetDisallowedImports(List)voidsetImportsWhitelist(List<String> allowedImports) Legacy alias forsetAllowedImports(List)voidsetIndirectImportCheckEnabled(boolean indirectImportCheckEnabled) Set this option to true if you want your import rules to be checked against every class node.voidsetMethodDefinitionAllowed(boolean methodDefinitionAllowed) voidsetPackageAllowed(boolean packageAllowed) voidsetReceiversBlackList(List<String> disallowedReceivers) Legacy alias forsetDisallowedReceivers(List)voidsetReceiversClassesBlackList(List<Class> disallowedReceivers) Legacy alias forsetDisallowedReceiversClasses(List).voidsetReceiversClassesWhiteList(List<Class> allowedReceivers) Legacy alias forsetAllowedReceiversClasses(List)voidsetReceiversWhiteList(List<String> allowedReceivers) Legacy alias forsetAllowedReceivers(List)voidsetStarImportsBlacklist(List<String> disallowedStarImports) Legacy alias forsetDisallowedStarImports(List)voidsetStarImportsWhitelist(List<String> allowedStarImports) Legacy alias forsetAllowedStarImports(List)voidsetStatementsBlacklist(List<Class<? extends Statement>> disallowedStatements) Legacy alias forsetDisallowedStatements(List)voidsetStatementsWhitelist(List<Class<? extends Statement>> allowedStatements) Legacy alias forsetAllowedStatements(List)voidsetStaticImportsBlacklist(List<String> disallowedStaticImports) Legacy alias forsetDisallowedStaticImports(List)voidsetStaticImportsWhitelist(List<String> allowedStaticImports) Legacy alias forsetAllowedStaticImports(List)voidsetStaticStarImportsBlacklist(List<String> disallowedStaticStarImports) Legacy alias forsetDisallowedStaticStarImports(List)voidsetStaticStarImportsWhitelist(List<String> allowedStaticStarImports) Legacy alias forsetAllowedStaticStarImports(List)voidsetTokensBlacklist(List<Integer> disallowedTokens) Alias forsetDisallowedTokens(List).voidsetTokensWhitelist(List<Integer> allowedTokens) Legacy alias forsetAllowedTokens(List)Methods inherited from class CompilationCustomizer
getPhaseMethods inherited from class CompilationUnit.PrimaryClassNodeOperation
needSortedInput
-
Constructor Details
-
SecureASTCustomizer
public SecureASTCustomizer()
-
-
Method Details
-
isMethodDefinitionAllowed
public boolean isMethodDefinitionAllowed() -
setMethodDefinitionAllowed
public void setMethodDefinitionAllowed(boolean methodDefinitionAllowed) -
isPackageAllowed
public boolean isPackageAllowed() -
isClosuresAllowed
public boolean isClosuresAllowed() -
setClosuresAllowed
public void setClosuresAllowed(boolean closuresAllowed) -
setPackageAllowed
public void setPackageAllowed(boolean packageAllowed) -
getDisallowedImports
-
getImportsBlacklist
Legacy alias forgetDisallowedImports() -
setDisallowedImports
-
setImportsBlacklist
Legacy alias forsetDisallowedImports(List) -
getAllowedImports
-
getImportsWhitelist
Legacy alias forgetAllowedImports() -
setAllowedImports
-
setImportsWhitelist
Legacy alias forsetAllowedImports(List) -
getDisallowedStarImports
-
getStarImportsBlacklist
Legacy alias forgetDisallowedStarImports() -
setDisallowedStarImports
-
setStarImportsBlacklist
Legacy alias forsetDisallowedStarImports(List) -
getAllowedStarImports
-
getStarImportsWhitelist
Legacy alias forgetAllowedStarImports() -
setAllowedStarImports
-
setStarImportsWhitelist
Legacy alias forsetAllowedStarImports(List) -
getDisallowedStaticImports
-
getStaticImportsBlacklist
Legacy alias forgetDisallowedStaticImports() -
setDisallowedStaticImports
-
setStaticImportsBlacklist
Legacy alias forsetDisallowedStaticImports(List) -
getAllowedStaticImports
-
getStaticImportsWhitelist
Legacy alias forgetAllowedStaticImports() -
setAllowedStaticImports
-
setStaticImportsWhitelist
Legacy alias forsetAllowedStaticImports(List) -
getDisallowedStaticStarImports
-
getStaticStarImportsBlacklist
Legacy alias forgetDisallowedStaticStarImports() -
setDisallowedStaticStarImports
-
setStaticStarImportsBlacklist
Legacy alias forsetDisallowedStaticStarImports(List) -
getAllowedStaticStarImports
-
getStaticStarImportsWhitelist
Legacy alias forgetAllowedStaticStarImports() -
setAllowedStaticStarImports
-
setStaticStarImportsWhitelist
Legacy alias forsetAllowedStaticStarImports(List) -
getDisallowedExpressions
-
getExpressionsBlacklist
Legacy alias forgetDisallowedExpressions() -
setDisallowedExpressions
-
setExpressionsBlacklist
Legacy alias forsetDisallowedExpressions(List) -
getAllowedExpressions
-
getExpressionsWhitelist
Legacy alias forgetAllowedExpressions() -
setAllowedExpressions
-
setExpressionsWhitelist
Legacy alias forsetAllowedExpressions(List) -
getDisallowedStatements
-
getStatementsBlacklist
Legacy alias forgetDisallowedStatements() -
setDisallowedStatements
-
setStatementsBlacklist
Legacy alias forsetDisallowedStatements(List) -
getAllowedStatements
-
getStatementsWhitelist
Legacy alias forgetAllowedStatements() -
setAllowedStatements
-
setStatementsWhitelist
Legacy alias forsetAllowedStatements(List) -
isIndirectImportCheckEnabled
public boolean isIndirectImportCheckEnabled() -
setIndirectImportCheckEnabled
public 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
-
getDisallowedTokens
-
getTokensBlacklist
Legacy alias forgetDisallowedTokens() -
setDisallowedTokens
-
setTokensBlacklist
Alias forsetDisallowedTokens(List). -
getAllowedTokens
-
getTokensWhitelist
Legacy alias forgetAllowedTokens() -
setAllowedTokens
-
setTokensWhitelist
Legacy alias forsetAllowedTokens(List) -
addStatementCheckers
-
addExpressionCheckers
-
getDisallowedConstantTypes
-
getConstantTypesBlackList
Legacy alias forgetDisallowedConstantTypes() -
setConstantTypesBlackList
-
getAllowedConstantTypes
-
getConstantTypesWhiteList
Legacy alias forgetAllowedStatements() -
setAllowedConstantTypes
-
setConstantTypesWhiteList
Legacy alias forsetAllowedConstantTypes(List) -
setAllowedConstantTypesClasses
-
setConstantTypesClassesWhiteList
Legacy alias forsetAllowedConstantTypesClasses(List) -
setDisallowedConstantTypesClasses
-
setConstantTypesClassesBlackList
Legacy alias forsetDisallowedConstantTypesClasses(List) -
getDisallowedReceivers
-
getReceiversBlackList
Legacy alias forgetDisallowedReceivers() -
setDisallowedReceivers
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
-
setReceiversBlackList
Legacy alias forsetDisallowedReceivers(List) -
setDisallowedReceiversClasses
An alternative way of settingreceiver classes.- Parameters:
disallowedReceivers- a list of classes.
-
setReceiversClassesBlackList
Legacy alias forsetDisallowedReceiversClasses(List). -
getAllowedReceivers
-
getReceiversWhiteList
Legacy alias forgetAllowedReceivers() -
setAllowedReceivers
-
setReceiversWhiteList
Legacy alias forsetAllowedReceivers(List) -
setAllowedReceiversClasses
An alternative way of settingreceiver classes.- Parameters:
allowedReceivers- a list of classes.
-
setReceiversClassesWhiteList
Legacy alias forsetAllowedReceiversClasses(List) -
call
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException - Specified by:
callin classCompilationUnit.PrimaryClassNodeOperation- Throws:
CompilationFailedException
-