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 StatementChecker interface or ExpressionChecker interface then register your
handlers thanks to the addExpressionCheckers(ExpressionChecker.
and addStatementCheckers(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)
Note
SecureASTCustomizer allows you to lock down the grammar of scripts but by itself isn't intended
to be the complete solution of all security issues when running scripts on the JVM. You might also want to
consider setting the groovy.grape.enable System property to false, augmenting use of the customizer
with additional techniques, and following standard security principles for JVM applications.
For more information, please read:
| Modifier and Type | Class and Description |
|---|---|
| public static interface | SecureASTCustomizer.ExpressionChecker
This interface allows the user to provide a custom expression checker if the dis/allowed expression lists are not sufficient |
| protected class | SecureASTCustomizer.SecuringCodeVisitor
This visitor directly implements the |
| public static interface | SecureASTCustomizer.StatementChecker
This interface allows the user to provide a custom statement checker if the dis/allowed statement lists are not sufficient |
| Modifier and Type | Field and Description |
|---|---|
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private List | |
| private final List | |
| private boolean | |
| private boolean | |
| private boolean | |
| private boolean | |
| private final List |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public void | |
| public void | |
| protected void | |
| protected void | |
| protected void | |
| public void | call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode)
Implements org. |
| protected void | |
| protected GroovyCodeVisitor | |
| protected static List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| public List | |
| private String | |
| public boolean | |
| public boolean | |
| public boolean | |
| public boolean | |
| private static List | |
| public void | |
| public void | setAllowedConstantTypesClasses(final List<Class<T>>
a list of classes. allowedConstantTypes)An alternative way of setting constant types. |
| public void | |
| public void | |
| public void | setAllowedReceivers(final List<String>
the list of accepted classes, as fully qualified names allowedReceivers)Sets the list of classes which may accept method calls. |
| public void | setAllowedReceiversClasses(final List<Class<T>>
a list of classes. allowedReceivers)An alternative way of setting |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | setAllowedTokens(final List<Integer>
the tokens. The values of the tokens must be those of allowedTokens)org.Sets the list of tokens which are permitted. |
| public void | |
| public void | |
| public void | setConstantTypesClassesBlackList(final List<Class<T>> disallowedConstantTypes)
Legacy alias for |
| public void | setConstantTypesClassesWhiteList(final List<Class<T>> allowedConstantTypes)
Legacy alias for |
| public void | setConstantTypesWhiteList(final List<String> allowedConstantTypes)
Legacy alias for |
| public void | setDisallowedConstantTypesClasses(final List<Class<T>>
a list of classes. disallowedConstantTypes)An alternative way of setting constant types. |
| public void | |
| public void | |
| public void | setDisallowedReceivers(final List<String>
the list of refused classes, as fully qualified names disallowedReceivers)Sets the list of classes which deny method calls. |
| public void | setDisallowedReceiversClasses(final List<Class<T>>
a list of classes. disallowedReceivers)An alternative way of setting |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | setDisallowedTokens(final List<Integer>
the tokens. The values of the tokens must be those of disallowedTokens)org.Sets the list of tokens which are not permitted. |
| public void | setExpressionsBlacklist(final List<Class<? extends Expression>> disallowedExpressions)
Legacy alias for |
| public void | setExpressionsWhitelist(final List<Class<? extends Expression>> allowedExpressions)
Legacy alias for |
| public void | setImportsBlacklist(final List<String> disallowedImports)
Legacy alias for |
| public void | |
| public void | setIndirectImportCheckEnabled(final boolean
set to true to enable indirect checks indirectImportCheckEnabled)Set this option to true if you want your import rules to be checked against every class node. |
| public void | |
| public void | |
| public void | setReceiversBlackList(final List<String> disallowedReceivers)
Legacy alias for |
| public void | setReceiversClassesBlackList(final List<Class<T>> disallowedReceivers)
Legacy alias for |
| public void | setReceiversClassesWhiteList(final List<Class<T>> allowedReceivers)
Legacy alias for |
| public void | setReceiversWhiteList(final List<String> allowedReceivers)
Legacy alias for |
| public void | setStarImportsBlacklist(final List<String> disallowedStarImports)
Legacy alias for |
| public void | setStarImportsWhitelist(final List<String> allowedStarImports)
Legacy alias for |
| public void | setStatementsBlacklist(final List<Class<? extends Statement>> disallowedStatements)
Legacy alias for |
| public void | setStatementsWhitelist(final List<Class<? extends Statement>> allowedStatements)
Legacy alias for |
| public void | setStaticImportsBlacklist(final List<String> disallowedStaticImports)
Legacy alias for |
| public void | setStaticImportsWhitelist(final List<String> allowedStaticImports)
Legacy alias for |
| public void | setStaticStarImportsBlacklist(final List<String> disallowedStaticStarImports)
Legacy alias for |
| public void | setStaticStarImportsWhitelist(final List<String> allowedStaticStarImports)
Legacy alias for |
| public void | |
| public void |