Package: org.codehaus.groovy.control.customizers

## [Java] Class SecureASTCustomizer

- org.codehaus.groovy.control.customizers.SecureASTCustomizer

-     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 StatementChecker interface or 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 classes
    Modifiers | Name | Description
    --- | --- | ---
    `interface` | `SecureASTCustomizer.ExpressionChecker` | This interface allows the user to provide a custom expression checker if the dis/allowed expression lists are not sufficient
    `interface` | `SecureASTCustomizer.StatementChecker` | This interface allows the user to provide a custom statement checker if the dis/allowed statement lists are not sufficient

  - ### Constructor Summary

    Constructors
    Constructor and description
    `SecureASTCustomizer()`

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `addExpressionCheckers(SecureASTCustomizer.ExpressionChecker checkers)`
    `` | `public void` | `addStatementCheckers(SecureASTCustomizer.StatementChecker checkers)`
    `` | `public void` | `call(SourceUnit source, GeneratorContext context, ClassNode classNode)`
    `` | `public List<String>` | `getAllowedConstantTypes()`
    `` | `public List<Class<? extends Expression>>` | `getAllowedExpressions()`
    `` | `public List<String>` | `getAllowedImports()`
    `` | `public List<String>` | `getAllowedReceivers()`
    `` | `public List<String>` | `getAllowedStarImports()`
    `` | `public List<Class<? extends Statement>>` | `getAllowedStatements()`
    `` | `public List<String>` | `getAllowedStaticImports()`
    `` | `public List<String>` | `getAllowedStaticStarImports()`
    `` | `public List<Integer>` | `getAllowedTokens()`
    `` | `public List<String>` | `getConstantTypesBlackList()` Legacy alias for getDisallowedConstantTypes()
    `` | `public List<String>` | `getConstantTypesWhiteList()` Legacy alias for getAllowedStatements()
    `` | `public List<String>` | `getDisallowedConstantTypes()`
    `` | `public List<Class<? extends Expression>>` | `getDisallowedExpressions()`
    `` | `public List<String>` | `getDisallowedImports()`
    `` | `public List<String>` | `getDisallowedReceivers()`
    `` | `public List<String>` | `getDisallowedStarImports()`
    `` | `public List<Class<? extends Statement>>` | `getDisallowedStatements()`
    `` | `public List<String>` | `getDisallowedStaticImports()`
    `` | `public List<String>` | `getDisallowedStaticStarImports()`
    `` | `public List<Integer>` | `getDisallowedTokens()`
    `` | `public List<Class<? extends Expression>>` | `getExpressionsBlacklist()` Legacy alias for getDisallowedExpressions()
    `` | `public List<Class<? extends Expression>>` | `getExpressionsWhitelist()` Legacy alias for getAllowedExpressions()
    `` | `public List<String>` | `getImportsBlacklist()` Legacy alias for getDisallowedImports()
    `` | `public List<String>` | `getImportsWhitelist()` Legacy alias for getAllowedImports()
    `` | `public List<String>` | `getReceiversBlackList()` Legacy alias for getDisallowedReceivers()
    `` | `public List<String>` | `getReceiversWhiteList()` Legacy alias for getAllowedReceivers()
    `` | `public List<String>` | `getStarImportsBlacklist()` Legacy alias for getDisallowedStarImports()
    `` | `public List<String>` | `getStarImportsWhitelist()` Legacy alias for getAllowedStarImports()
    `` | `public List<Class<? extends Statement>>` | `getStatementsBlacklist()` Legacy alias for getDisallowedStatements()
    `` | `public List<Class<? extends Statement>>` | `getStatementsWhitelist()` Legacy alias for getAllowedStatements()
    `` | `public List<String>` | `getStaticImportsBlacklist()` Legacy alias for getDisallowedStaticImports()
    `` | `public List<String>` | `getStaticImportsWhitelist()` Legacy alias for getAllowedStaticImports()
    `` | `public List<String>` | `getStaticStarImportsBlacklist()` Legacy alias for getDisallowedStaticStarImports()
    `` | `public List<String>` | `getStaticStarImportsWhitelist()` Legacy alias for getAllowedStaticStarImports()
    `` | `public List<Integer>` | `getTokensBlacklist()` Legacy alias for getDisallowedTokens()
    `` | `public List<Integer>` | `getTokensWhitelist()` Legacy alias for getAllowedTokens()
    `` | `public boolean` | `isClosuresAllowed()`
    `` | `public boolean` | `isIndirectImportCheckEnabled()`
    `` | `public boolean` | `isMethodDefinitionAllowed()`
    `` | `public boolean` | `isPackageAllowed()`
    `` | `public void` | `setAllowedConstantTypes(List<String> allowedConstantTypes)`
    `` | `public void` | `setAllowedConstantTypesClasses(List<Class> allowedConstantTypes)` An alternative way of setting constant types.
    `` | `public void` | `setAllowedExpressions(List<Class<? extends Expression>> allowedExpressions)`
    `` | `public void` | `setAllowedImports(List<String> allowedImports)`
    `` | `public void` | `setAllowedReceivers(List<String> allowedReceivers)` Sets the list of classes which may accept method calls.
    `` | `public void` | `setAllowedReceiversClasses(List<Class> allowedReceivers)` An alternative way of setting receiver classes.
    `` | `public void` | `setAllowedStarImports(List<String> allowedStarImports)`
    `` | `public void` | `setAllowedStatements(List<Class<? extends Statement>> allowedStatements)`
    `` | `public void` | `setAllowedStaticImports(List<String> allowedStaticImports)`
    `` | `public void` | `setAllowedStaticStarImports(List<String> allowedStaticStarImports)`
    `` | `public void` | `setAllowedTokens(List<Integer> allowedTokens)` Sets the list of tokens which are permitted.
    `` | `public void` | `setClosuresAllowed(boolean closuresAllowed)`
    `` | `public void` | `setConstantTypesBlackList(List<String> constantTypesBlackList)`
    `` | `public void` | `setConstantTypesClassesBlackList(List<Class> disallowedConstantTypes)` Legacy alias for setDisallowedConstantTypesClasses(List)
    `` | `public void` | `setConstantTypesClassesWhiteList(List<Class> allowedConstantTypes)` Legacy alias for setAllowedConstantTypesClasses(List)
    `` | `public void` | `setConstantTypesWhiteList(List<String> allowedConstantTypes)` Legacy alias for setAllowedConstantTypes(List)
    `` | `public void` | `setDisallowedConstantTypesClasses(List<Class> disallowedConstantTypes)` An alternative way of setting constant types.
    `` | `public void` | `setDisallowedExpressions(List<Class<? extends Expression>> disallowedExpressions)`
    `` | `public void` | `setDisallowedImports(List<String> disallowedImports)`
    `` | `public void` | `setDisallowedReceivers(List<String> disallowedReceivers)` Sets the list of classes which deny method calls.
    `` | `public void` | `setDisallowedReceiversClasses(List<Class> disallowedReceivers)` An alternative way of setting receiver classes.
    `` | `public void` | `setDisallowedStarImports(List<String> disallowedStarImports)`
    `` | `public void` | `setDisallowedStatements(List<Class<? extends Statement>> disallowedStatements)`
    `` | `public void` | `setDisallowedStaticImports(List<String> disallowedStaticImports)`
    `` | `public void` | `setDisallowedStaticStarImports(List<String> disallowedStaticStarImports)`
    `` | `public void` | `setDisallowedTokens(List<Integer> disallowedTokens)` Sets the list of tokens which are not permitted.
    `` | `public void` | `setExpressionsBlacklist(List<Class<? extends Expression>> disallowedExpressions)` Legacy alias for setDisallowedExpressions(List)
    `` | `public void` | `setExpressionsWhitelist(List<Class<? extends Expression>> allowedExpressions)` Legacy alias for setAllowedExpressions(List)
    `` | `public void` | `setImportsBlacklist(List<String> disallowedImports)` Legacy alias for setDisallowedImports(List)
    `` | `public void` | `setImportsWhitelist(List<String> allowedImports)` Legacy alias for setAllowedImports(List)
    `` | `public void` | `setIndirectImportCheckEnabled(boolean indirectImportCheckEnabled)` Set this option to true if you want your import rules to be checked against every class node.
    `` | `public void` | `setMethodDefinitionAllowed(boolean methodDefinitionAllowed)`
    `` | `public void` | `setPackageAllowed(boolean packageAllowed)`
    `` | `public void` | `setReceiversBlackList(List<String> disallowedReceivers)` Legacy alias for setDisallowedReceivers(List)
    `` | `public void` | `setReceiversClassesBlackList(List<Class> disallowedReceivers)` Legacy alias for setDisallowedReceiversClasses(List).
    `` | `public void` | `setReceiversClassesWhiteList(List<Class> allowedReceivers)` Legacy alias for setAllowedReceiversClasses(List)
    `` | `public void` | `setReceiversWhiteList(List<String> allowedReceivers)` Legacy alias for setAllowedReceivers(List)
    `` | `public void` | `setStarImportsBlacklist(List<String> disallowedStarImports)` Legacy alias for setDisallowedStarImports(List)
    `` | `public void` | `setStarImportsWhitelist(List<String> allowedStarImports)` Legacy alias for setAllowedStarImports(List)
    `` | `public void` | `setStatementsBlacklist(List<Class<? extends Statement>> disallowedStatements)` Legacy alias for setDisallowedStatements(List)
    `` | `public void` | `setStatementsWhitelist(List<Class<? extends Statement>> allowedStatements)` Legacy alias for setAllowedStatements(List)
    `` | `public void` | `setStaticImportsBlacklist(List<String> disallowedStaticImports)` Legacy alias for setDisallowedStaticImports(List)
    `` | `public void` | `setStaticImportsWhitelist(List<String> allowedStaticImports)` Legacy alias for setAllowedStaticImports(List)
    `` | `public void` | `setStaticStarImportsBlacklist(List<String> disallowedStaticStarImports)` Legacy alias for setDisallowedStaticStarImports(List)
    `` | `public void` | `setStaticStarImportsWhitelist(List<String> allowedStaticStarImports)` Legacy alias for setAllowedStaticStarImports(List)
    `` | `public void` | `setTokensBlacklist(List<Integer> disallowedTokens)` Legacy alias for setDisallowedTokens(List).
    `` | `public void` | `setTokensWhitelist(List<Integer> allowedTokens)` Legacy alias for setAllowedTokens(List)

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class CompilationCustomizer` | `getPhase`

- - ### Constructor Detail

    - #### public SecureASTCustomizer()

  - ### Method Detail

    - #### public void addExpressionCheckers(SecureASTCustomizer.ExpressionChecker checkers)

    - #### public void addStatementCheckers(SecureASTCustomizer.StatementChecker checkers)

    - #### @Override
      public void call(SourceUnit source, GeneratorContext context, ClassNode classNode)

    - #### public List<String> getAllowedConstantTypes()

    - #### public List<Class<? extends Expression>> getAllowedExpressions()

    - #### public List<String> getAllowedImports()

    - #### public List<String> getAllowedReceivers()

    - #### public List<String> getAllowedStarImports()

    - #### public List<Class<? extends Statement>> getAllowedStatements()

    - #### public List<String> getAllowedStaticImports()

    - #### public List<String> getAllowedStaticStarImports()

    - #### public List<Integer> getAllowedTokens()

    - #### @Deprecated
      public List<String> getConstantTypesBlackList()

      Legacy alias for getDisallowedConstantTypes()

    - #### @Deprecated
      public List<String> getConstantTypesWhiteList()

      Legacy alias for getAllowedStatements()

    - #### public List<String> getDisallowedConstantTypes()

    - #### public List<Class<? extends Expression>> getDisallowedExpressions()

    - #### public List<String> getDisallowedImports()

    - #### public List<String> getDisallowedReceivers()

    - #### public List<String> getDisallowedStarImports()

    - #### public List<Class<? extends Statement>> getDisallowedStatements()

    - #### public List<String> getDisallowedStaticImports()

    - #### public List<String> getDisallowedStaticStarImports()

    - #### public List<Integer> getDisallowedTokens()

    - #### @Deprecated
      public List<Class<? extends Expression>> getExpressionsBlacklist()

      Legacy alias for getDisallowedExpressions()

    - #### @Deprecated
      public List<Class<? extends Expression>> getExpressionsWhitelist()

      Legacy alias for getAllowedExpressions()

    - #### @Deprecated
      public List<String> getImportsBlacklist()

      Legacy alias for getDisallowedImports()

    - #### @Deprecated
      public List<String> getImportsWhitelist()

      Legacy alias for getAllowedImports()

    - #### @Deprecated
      public List<String> getReceiversBlackList()

      Legacy alias for getDisallowedReceivers()

    - #### @Deprecated
      public List<String> getReceiversWhiteList()

      Legacy alias for getAllowedReceivers()

    - #### @Deprecated
      public List<String> getStarImportsBlacklist()

      Legacy alias for getDisallowedStarImports()

    - #### @Deprecated
      public List<String> getStarImportsWhitelist()

      Legacy alias for getAllowedStarImports()

    - #### @Deprecated
      public List<Class<? extends Statement>> getStatementsBlacklist()

      Legacy alias for getDisallowedStatements()

    - #### @Deprecated
      public List<Class<? extends Statement>> getStatementsWhitelist()

      Legacy alias for getAllowedStatements()

    - #### @Deprecated
      public List<String> getStaticImportsBlacklist()

      Legacy alias for getDisallowedStaticImports()

    - #### @Deprecated
      public List<String> getStaticImportsWhitelist()

      Legacy alias for getAllowedStaticImports()

    - #### @Deprecated
      public List<String> getStaticStarImportsBlacklist()

      Legacy alias for getDisallowedStaticStarImports()

    - #### @Deprecated
      public List<String> getStaticStarImportsWhitelist()

      Legacy alias for getAllowedStaticStarImports()

    - #### @Deprecated
      public List<Integer> getTokensBlacklist()

      Legacy alias for getDisallowedTokens()

    - #### @Deprecated
      public List<Integer> getTokensWhitelist()

      Legacy alias for getAllowedTokens()

    - #### public boolean isClosuresAllowed()

    - #### public boolean isIndirectImportCheckEnabled()

    - #### public boolean isMethodDefinitionAllowed()

    - #### public boolean isPackageAllowed()

    - #### public void setAllowedConstantTypes(List<String> allowedConstantTypes)

    - #### public void setAllowedConstantTypesClasses(List<Class> allowedConstantTypes)

      An alternative way of setting constant types.

      Parameters:
        `allowedConstantTypes` - a list of classes.

    - #### public void setAllowedExpressions(List<Class<? extends Expression>> allowedExpressions)

    - #### public void setAllowedImports(List<String> allowedImports)

    - #### public 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

    - #### public void setAllowedReceiversClasses(List<Class> allowedReceivers)

      An alternative way of setting receiver classes.

      Parameters:
        `allowedReceivers` - a list of classes.

    - #### public void setAllowedStarImports(List<String> allowedStarImports)

    - #### public void setAllowedStatements(List<Class<? extends Statement>> allowedStatements)

    - #### public void setAllowedStaticImports(List<String> allowedStaticImports)

    - #### public void setAllowedStaticStarImports(List<String> allowedStaticStarImports)

    - #### public 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

    - #### public void setClosuresAllowed(boolean closuresAllowed)

    - #### public void setConstantTypesBlackList(List<String> constantTypesBlackList)

    - #### @Deprecated
      public void setConstantTypesClassesBlackList(List<Class> disallowedConstantTypes)

      Legacy alias for setDisallowedConstantTypesClasses(List)

    - #### @Deprecated
      public void setConstantTypesClassesWhiteList(List<Class> allowedConstantTypes)

      Legacy alias for setAllowedConstantTypesClasses(List)

    - #### @Deprecated
      public void setConstantTypesWhiteList(List<String> allowedConstantTypes)

      Legacy alias for setAllowedConstantTypes(List)

    - #### public void setDisallowedConstantTypesClasses(List<Class> disallowedConstantTypes)

      An alternative way of setting constant types.

      Parameters:
        `disallowedConstantTypes` - a list of classes.

    - #### public void setDisallowedExpressions(List<Class<? extends Expression>> disallowedExpressions)

    - #### public void setDisallowedImports(List<String> disallowedImports)

    - #### public 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

    - #### public void setDisallowedReceiversClasses(List<Class> disallowedReceivers)

      An alternative way of setting receiver classes.

      Parameters:
        `disallowedReceivers` - a list of classes.

    - #### public void setDisallowedStarImports(List<String> disallowedStarImports)

    - #### public void setDisallowedStatements(List<Class<? extends Statement>> disallowedStatements)

    - #### public void setDisallowedStaticImports(List<String> disallowedStaticImports)

    - #### public void setDisallowedStaticStarImports(List<String> disallowedStaticStarImports)

    - #### public 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

    - #### @Deprecated
      public void setExpressionsBlacklist(List<Class<? extends Expression>> disallowedExpressions)

      Legacy alias for setDisallowedExpressions(List)

    - #### @Deprecated
      public void setExpressionsWhitelist(List<Class<? extends Expression>> allowedExpressions)

      Legacy alias for setAllowedExpressions(List)

    - #### @Deprecated
      public void setImportsBlacklist(List<String> disallowedImports)

      Legacy alias for setDisallowedImports(List)

    - #### @Deprecated
      public void setImportsWhitelist(List<String> allowedImports)

      Legacy alias for setAllowedImports(List)

    - #### 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

    - #### public void setMethodDefinitionAllowed(boolean methodDefinitionAllowed)

    - #### public void setPackageAllowed(boolean packageAllowed)

    - #### @Deprecated
      public void setReceiversBlackList(List<String> disallowedReceivers)

      Legacy alias for setDisallowedReceivers(List)

    - #### @Deprecated
      public void setReceiversClassesBlackList(List<Class> disallowedReceivers)

      Legacy alias for setDisallowedReceiversClasses(List).

    - #### @Deprecated
      public void setReceiversClassesWhiteList(List<Class> allowedReceivers)

      Legacy alias for setAllowedReceiversClasses(List)

    - #### @Deprecated
      public void setReceiversWhiteList(List<String> allowedReceivers)

      Legacy alias for setAllowedReceivers(List)

    - #### @Deprecated
      public void setStarImportsBlacklist(List<String> disallowedStarImports)

      Legacy alias for setDisallowedStarImports(List)

    - #### @Deprecated
      public void setStarImportsWhitelist(List<String> allowedStarImports)

      Legacy alias for setAllowedStarImports(List)

    - #### @Deprecated
      public void setStatementsBlacklist(List<Class<? extends Statement>> disallowedStatements)

      Legacy alias for setDisallowedStatements(List)

    - #### @Deprecated
      public void setStatementsWhitelist(List<Class<? extends Statement>> allowedStatements)

      Legacy alias for setAllowedStatements(List)

    - #### @Deprecated
      public void setStaticImportsBlacklist(List<String> disallowedStaticImports)

      Legacy alias for setDisallowedStaticImports(List)

    - #### @Deprecated
      public void setStaticImportsWhitelist(List<String> allowedStaticImports)

      Legacy alias for setAllowedStaticImports(List)

    - #### @Deprecated
      public void setStaticStarImportsBlacklist(List<String> disallowedStaticStarImports)

      Legacy alias for setDisallowedStaticStarImports(List)

    - #### @Deprecated
      public void setStaticStarImportsWhitelist(List<String> allowedStaticStarImports)

      Legacy alias for setAllowedStaticStarImports(List)

    - #### @Deprecated
      public void setTokensBlacklist(List<Integer> disallowedTokens)

      Legacy alias for setDisallowedTokens(List).

    - #### @Deprecated
      public void setTokensWhitelist(List<Integer> allowedTokens)

      Legacy alias for setAllowedTokens(List)