Class GroovySandbox

java.lang.Object
com.axelor.script.GroovySandbox

public final class GroovySandbox extends Object
Runtime checks of the Groovy script sandbox.

The policy transformations (GroovyCheckTransformation, GroovyCheckPropTransformation) compile calls to these static methods directly into the script. They are static on purpose: a script cannot shadow or replace a class reference, whereas a variable holding a checker could be shadowed by a local variable or resolved on a closure delegate.

Per-evaluation state (the execution timeout) is attached to the current thread with enter() for the duration of an evaluation.

This class is internal to the scripting support. It is public only so that compiled scripts can call it; scripts themselves cannot reference it as it is not allowed by the policy.

  • Method Details

    • enter

      public static GroovySandbox.Scope enter()
      Starts an evaluation on the current thread, with the application script timeout, until the returned scope is closed.
    • enter

      public static GroovySandbox.Scope enter(long timeout)
      Starts an evaluation on the current thread, with the given timeout (in milliseconds; 0 or less for none), until the returned scope is closed. Evaluations can be nested (a script calling a service that evaluates another script): a nested evaluation can only tighten the enclosing deadline, never extend it, and closing the scope restores the enclosing one.
    • timeout

      public static void timeout()
      Checks the execution timeout of the current evaluation, if any. Called by the compiled code on each receiver check, and at the start of each loop iteration (a loop body may contain no call), method and closure body (recursion, closures called by Java: each, times...). A call that cannot be interrupted (a long database query) fails the evaluation at the next check after it returns.
    • check

      public static <T> T check(T obj)
      Validates an object used as a call receiver or property owner against the policy.

      Classes compiled by the sandbox (SandboxCode: scripts, their closures and the classes they declare) are allowed: their code is checked, and their supertypes are allowed.

    • checkProperty

      public static Object checkProperty(Object obj, String name)
      Validates the owner of a property access (obj.name), as check(Object).

      A property that a collection or an array does not have itself is read from each of its elements instead ([a, b].name is [a.name, b.name]), so the elements are validated as owners too, at any depth ([[a]].name is [[a.name]]). They are collected in a single pass, and the property is read from them by the GroovySandbox.PropertyOwners returned in place of the container: a stateful collection could give other elements to a second iteration. The name is null when it is computed at runtime.

      A collection overriding getProperty(String) reads its properties through its override, which is called as in Groovy: a collection declared in a script has its elements checked by its metaclass (see readsThroughOverride(Object)), and the elements of another one are checked here first.

    • collectionMetaClass

      public static groovy.lang.MetaClass collectionMetaClass(Object collection)
      The metaclass of a collection declared in a script, see GroovySandbox.CollectionMetaClass. Called by the getMetaClass() method GroovyClassCheck adds to these classes.
    • methodPointer

      public static Object methodPointer(Object pointer)
      Returns a method pointer (obj.&name, obj::name, Type::new) whose owner was validated by check(Object), wrapped so that its calls are validated too (see SandboxMethodPointer).
    • constructed

      public static <T> T constructed(Object checkedType, T obj)
      Returns the constructed object. Generated as constructed(check(Type), new Type(...)): the first argument checks the class before the second one runs the constructor, so a denied class is never constructed (its constructor could have side effects, e.g. create a file).
    • checkSpread

      public static Object checkSpread(Object obj)
      Validates the receiver of a spread call or property access (recv*.name).

      The call is dispatched on each element of the receiver, not on the receiver itself, so each element is validated as a receiver (as check(Object)). The elements are collected the same way Groovy iterates them for the spread (including iterators, maps, etc.), and that list is returned to be spread instead, so the receiver is iterated only once.

    • checkSpreadProperty

      public static Object checkSpreadProperty(Object obj, String name)
      Validates the receiver of a spread property access (recv*.name), as checkSpread(Object).

      The property is read from each element, so each element is validated as a property owner (checkProperty(Object, String)): an element that is a collection or an array reads the property from its own elements, at any depth ([[a]]*.name reads a.name).