public class SecureASTCustomizer extends CompilationCustomizer
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)
Modifier and Type | Class and Description |
---|---|
static interface |
SecureASTCustomizer.ExpressionChecker
This interface allows the user to provide a custom expression checker if the dis/allowed expression lists are not
sufficient
|
static interface |
SecureASTCustomizer.StatementChecker
This interface allows the user to provide a custom statement checker if the dis/allowed statement lists are not
sufficient
|
Constructor and Description |
---|
SecureASTCustomizer() |
getPhase
needSortedInput
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()
getDisallowedImports()
public void setImportsBlacklist(List<String> disallowedImports)
setDisallowedImports(List)
public List<String> getImportsWhitelist()
getAllowedImports()
public void setImportsWhitelist(List<String> allowedImports)
setAllowedImports(List)
public List<String> getStarImportsBlacklist()
getDisallowedStarImports()
public void setStarImportsBlacklist(List<String> disallowedStarImports)
setDisallowedStarImports(List)
public List<String> getStarImportsWhitelist()
getAllowedStarImports()
public void setStarImportsWhitelist(List<String> allowedStarImports)
setAllowedStarImports(List)
public List<String> getStaticImportsBlacklist()
getDisallowedStaticImports()
public void setDisallowedStaticImports(List<String> disallowedStaticImports)
public void setStaticImportsBlacklist(List<String> disallowedStaticImports)
setDisallowedStaticImports(List)
public List<String> getStaticImportsWhitelist()
getAllowedStaticImports()
public void setStaticImportsWhitelist(List<String> allowedStaticImports)
setAllowedStaticImports(List)
public List<String> getStaticStarImportsBlacklist()
getDisallowedStaticStarImports()
public void setDisallowedStaticStarImports(List<String> disallowedStaticStarImports)
public void setStaticStarImportsBlacklist(List<String> disallowedStaticStarImports)
setDisallowedStaticStarImports(List)
public List<String> getStaticStarImportsWhitelist()
getAllowedStaticStarImports()
public void setAllowedStaticStarImports(List<String> allowedStaticStarImports)
public void setStaticStarImportsWhitelist(List<String> allowedStaticStarImports)
setAllowedStaticStarImports(List)
public List<Class<? extends Expression>> getDisallowedExpressions()
public List<Class<? extends Expression>> getExpressionsBlacklist()
getDisallowedExpressions()
public void setDisallowedExpressions(List<Class<? extends Expression>> disallowedExpressions)
public void setExpressionsBlacklist(List<Class<? extends Expression>> disallowedExpressions)
setDisallowedExpressions(List)
public List<Class<? extends Expression>> getAllowedExpressions()
public List<Class<? extends Expression>> getExpressionsWhitelist()
getAllowedExpressions()
public void setAllowedExpressions(List<Class<? extends Expression>> allowedExpressions)
public void setExpressionsWhitelist(List<Class<? extends Expression>> allowedExpressions)
setAllowedExpressions(List)
public List<Class<? extends Statement>> getStatementsBlacklist()
getDisallowedStatements()
public void setDisallowedStatements(List<Class<? extends Statement>> disallowedStatements)
public void setStatementsBlacklist(List<Class<? extends Statement>> disallowedStatements)
setDisallowedStatements(List)
public List<Class<? extends Statement>> getStatementsWhitelist()
getAllowedStatements()
public void setAllowedStatements(List<Class<? extends Statement>> allowedStatements)
public void setStatementsWhitelist(List<Class<? extends Statement>> allowedStatements)
setAllowedStatements(List)
public boolean isIndirectImportCheckEnabled()
public void setIndirectImportCheckEnabled(boolean indirectImportCheckEnabled)
indirectImportCheckEnabled
- set to true to enable indirect checkspublic List<Integer> getTokensBlacklist()
getDisallowedTokens()
public void setDisallowedTokens(List<Integer> disallowedTokens)
disallowedTokens
- the tokens. The values of the tokens must be those of Types
public void setTokensBlacklist(List<Integer> disallowedTokens)
setDisallowedTokens(List)
.public List<Integer> getTokensWhitelist()
getAllowedTokens()
public void setAllowedTokens(List<Integer> allowedTokens)
allowedTokens
- the tokens. The values of the tokens must be those of Types
public void setTokensWhitelist(List<Integer> allowedTokens)
setAllowedTokens(List)
public void addStatementCheckers(SecureASTCustomizer.StatementChecker... checkers)
public void addExpressionCheckers(SecureASTCustomizer.ExpressionChecker... checkers)
public List<String> getConstantTypesBlackList()
getDisallowedConstantTypes()
public void setConstantTypesBlackList(List<String> constantTypesBlackList)
public List<String> getConstantTypesWhiteList()
getAllowedStatements()
public void setConstantTypesWhiteList(List<String> allowedConstantTypes)
setAllowedConstantTypes(List)
public void setAllowedConstantTypesClasses(List<Class> allowedConstantTypes)
allowedConstantTypes
- a list of classes.public void setConstantTypesClassesWhiteList(List<Class> allowedConstantTypes)
setAllowedConstantTypesClasses(List)
public void setDisallowedConstantTypesClasses(List<Class> disallowedConstantTypes)
disallowedConstantTypes
- a list of classes.public void setConstantTypesClassesBlackList(List<Class> disallowedConstantTypes)
setDisallowedConstantTypesClasses(List)
public List<String> getReceiversBlackList()
getDisallowedReceivers()
public void setDisallowedReceivers(List<String> disallowedReceivers)
disallowedReceivers
- the list of refused classes, as fully qualified namespublic void setReceiversBlackList(List<String> disallowedReceivers)
setDisallowedReceivers(List)
public void setDisallowedReceiversClasses(List<Class> disallowedReceivers)
receiver classes
.disallowedReceivers
- a list of classes.public void setReceiversClassesBlackList(List<Class> disallowedReceivers)
setDisallowedReceiversClasses(List)
.public List<String> getReceiversWhiteList()
getAllowedReceivers()
public void setAllowedReceivers(List<String> allowedReceivers)
allowedReceivers
- the list of accepted classes, as fully qualified namespublic void setReceiversWhiteList(List<String> allowedReceivers)
setAllowedReceivers(List)
public void setAllowedReceiversClasses(List<Class> allowedReceivers)
receiver classes
.allowedReceivers
- a list of classes.public void setReceiversClassesWhiteList(List<Class> allowedReceivers)
setAllowedReceiversClasses(List)
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException
call
in class CompilationUnit.PrimaryClassNodeOperation
CompilationFailedException