Scripting Security Policy

Overview

The Scripting Security Policy provides control over Java class accessibility from scripts. This prevents unauthorized access to sensitive operations while allowing necessary business logic execution.

The policy is applied to all supported scripting engines: Groovy, JavaScript, and Expression Language. It is also applied to Groovy template engines.

Policy Configuration

You can configure the policy using either annotation-based whitelisting and/or via programmatic configuration.

For best practice, you should prefer applying the rules on service interfaces, not implementations.

You can apply rules on top-level classes and interfaces only. Nested/inner classes are checked against rules applied to their top-level classes.

Annotation-based Whitelisting

Use @com.axelor.script.ScriptAllowed annotation to allow script access.

@ScriptAllowed
public interface SaleOrderService {

  void validate(Order order);

  // Other methods
}
public class SaleOrderService implements SaleOrderService {

  @Override
  public void validate(Order order) {
    // Business code
  }

  // Other implemented methods
}

Programmatic Configuration

Create a class that implements com.axelor.script.ScriptPolicyConfigurator interface. The application will scan for all classes implementing this interface and will call them in module resolution order to configure the scripting policy.

public class CustomPolicy implements ScriptPolicyConfigurator {

  @Override
  public void configure(List<String> allowPackages,
                        List<Class<?>> allowClasses,
                        List<String> denyPackages,
                        List<Class<?>> denyClasses) {
    allowPackages.add("com.axelor.sale.service.*");
    allowClasses.add(PaymentService.class);
    denyClasses.add(PaymentValidationService.class);
  }
}

The package name pattern can be like:

  • java.time - match all classes in this package

  • java.time.* - match all classes in this package and its immediate sub-packages

Package rules match by package name, while class rules match by inheritance. Denied classes/packages take precedence over allowed classes/packages.

Core Security Rules

These rules are added after any custom configuration.

Allowed

  • Java Core Packages

    • java.lang

    • java.util

    • java.util.regex

    • java.time.*

    • java.text

    • java.math

  • Java Core Interfaces

    • java.util.Map

    • java.util.Map.Entry

    • java.util.Iterator

    • java.lang.Iterable

    • java.lang.CharSequence

  • Java Persistence

    • jakarta.persistence.Query

  • Hibernate

    • org.hibernate.proxy.HibernateProxy

    • org.hibernate.collection.spi.PersistentCollection

    • org.hibernate.collection.spi.LazyInitializable

  • Axelor Common

    • com.axelor.common.StringUtils

    • com.axelor.common.ObjectUtils

    • com.axelor.common.HtmlUtils

    • com.axelor.common.Inflections

    • com.axelor.common.Inflector

  • Axelor DB

    • com.axelor.db.Model

    • com.axelor.db.Query

    • com.axelor.db.Repository

    • com.axelor.db.ValueEnum

    • com.axelor.db.EntityHelper

  • Axelor i18n

    • com.axelor.i18n.I18n

    • com.axelor.i18n.L10n

  • Axelor RPC

    • com.axelor.rpc.Context

    • com.axelor.rpc.JsonContext

    • com.axelor.rpc.ActionRequest

    • com.axelor.rpc.ActionResponse

Denied (cannot be overridden)

  • java.lang.Class

  • java.lang.ClassLoader

  • java.lang.Process

  • java.lang.ProcessBuilder

  • java.lang.ProcessHandle

  • java.lang.Runtime

  • java.lang.System

  • java.lang.Thread

  • java.lang.ThreadGroup

  • java.util.Properties

  • java.util.ResourceBundle

  • java.util.Timer

  • java.util.Formatter

In addition, the following are denied whatever the receiver, including allowed classes such as String or List:

  • Starting operating system processes, including through Groovy shortcuts such as "cmd".execute() or ["cmd", "arg"].execute()

  • Nested script evaluation, such as Groovy’s evaluate(…​), which would run code outside the policy

  • Groovy categories and mixins (use(…​), mixin(…​)), which would expose methods of any class on allowed objects

Static imports (import static …​) are checked against the same rules as other class accesses: importing from a class that is not allowed fails when the script is compiled.

Groovy scripts

Groovy scripts and templates follow additional rules:

  • Annotations are not allowed, except @Override and @Deprecated. In a script with star imports (import x.y.*), write them fully qualified: @java.lang.Override.

  • Package declarations are not allowed.

  • Classes declared in a script can only extend and implement allowed types, and cannot define finalize().

  • Reflection is limited to the public members of allowed classes.

  • Method pointers and references (obj.&name, Type::name, Type::new) are checked like the calls they make, and subscripts with a name (obj['name']) like property accesses.

  • The following methods of allowed classes are denied, as they read or change state shared by the whole application:

    • java.util.Locale.setDefault

    • java.util.TimeZone.setDefault

    • java.time.zone.ZoneRulesProvider.registerProvider and refresh

    • java.util.ServiceLoader.load and loadInstalled

    • java.lang.Integer.getInteger, java.lang.Long.getLong and java.lang.Boolean.getBoolean, which read system properties

    • com.axelor.common.Inflections methods changing the inflection rules (ignore, irregular, singular, plural)

__bean__ Helper

The __bean__(Class<T>) is a script safe alternative to com.axelor.inject.Beans.get(Class<T>). It ensures that the requested bean class complies with the scripting policy. If the class is not allowed by the policy, IllegalArgumentException is thrown.

Example:

def service = __bean__(com.example.MyService)
def result = service.myMethod()

Application Settings

com.axelor.app.AppSettings is forbidden to scripts, as it allows unrestricted access to all application settings, which can include sensitive data.

You need to write your own script-allowed helper to selectively allow access to application properties.

Example Java helper
package com.axelor.app.script;

import com.axelor.app.AppSettings;
import com.axelor.app.AvailableAppSettings;
import com.axelor.script.ScriptAllowed;

@ScriptAllowed
public class ScriptAppSettings {
  private final AppSettings settings = AppSettings.get();

  public String getApplicationMode() {
    return settings.get(AvailableAppSettings.APPLICATION_MODE, "dev");
  }
}
Example usage in script
def mode = __bean__(com.axelor.app.script.ScriptAppSettings).getApplicationMode()

Execution Timeouts

Prevent runaway scripts (infinite loops, unbounded recursion…​) with script execution timeouts.

Application-wide script timeout configuration:

# Groovy/JavaScript scripts execution timeout (in milliseconds), 0 to disable it
# Defaults to 300000 ms (5 minutes)
application.script.timeout = 300000

Programmatic timeout configuration per script:

// Set 500 ms timeout (locally overrides application-wide script timeout configuration)
ScriptHelper helper = new GroovyScriptHelper(context).withTimeout(500);
Object result = helper.eval("while (true) { /* infinite loop */ }");

A script evaluated while another script of the same language runs on the same thread (for example, by a service called from a script) cannot extend the enclosing timeout: the earliest deadline applies.

The way timeout check is performed depends on the script type:

  • Groovy: the script is stopped once the timeout is reached, at its next check (each call, loop iteration, method or closure). A script blocked inside a Java call, such as a long database query, cannot be interrupted while it waits; it is stopped as soon as the call returns, and the evaluation fails with a timeout even if the script would have finished.

  • JavaScript: the script is interrupted once the timeout is reached, at its next safepoint (such as a loop iteration or a function call). A script blocked inside a Java call, such as a long database query, cannot be interrupted while it waits; it is stopped as soon as the call returns, and the evaluation fails with a timeout even if the script would have finished.

  • Expression Language: not checked, as loops are not supported.