Class GroovySandbox
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceEnds the evaluation started byenter()when closed. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tcheck(T obj) Validates an object used as a call receiver or property owner against the policy.static ObjectcheckProperty(Object obj, String name) Validates the owner of a property access (obj.name), ascheck(Object).static ObjectcheckSpread(Object obj) Validates the receiver of a spread call or property access (recv*.name).static ObjectcheckSpreadProperty(Object obj, String name) Validates the receiver of a spread property access (recv*.name), ascheckSpread(Object).static groovy.lang.MetaClasscollectionMetaClass(Object collection) The metaclass of a collection declared in a script, seeGroovySandbox.CollectionMetaClass.static <T> Tconstructed(Object checkedType, T obj) Returns the constructed object.static GroovySandbox.Scopeenter()Starts an evaluation on the current thread, with the application script timeout, until the returned scope is closed.static GroovySandbox.Scopeenter(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.static ObjectmethodPointer(Object pointer) Returns a method pointer (obj.&name,obj::name,Type::new) whose owner was validated bycheck(Object), wrapped so that its calls are validated too (seeSandboxMethodPointer).static voidtimeout()Checks the execution timeout of the current evaluation, if any.
-
Method Details
-
enter
Starts an evaluation on the current thread, with the application script timeout, until the returned scope is closed. -
enter
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
Validates the owner of a property access (obj.name), ascheck(Object).A property that a collection or an array does not have itself is read from each of its elements instead (
[a, b].nameis[a.name, b.name]), so the elements are validated as owners too, at any depth ([[a]].nameis[[a.name]]). They are collected in a single pass, and the property is read from them by theGroovySandbox.PropertyOwnersreturned in place of the container: a stateful collection could give other elements to a second iteration. The name isnullwhen 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 (seereadsThroughOverride(Object)), and the elements of another one are checked here first. -
collectionMetaClass
The metaclass of a collection declared in a script, seeGroovySandbox.CollectionMetaClass. Called by thegetMetaClass()methodGroovyClassCheckadds to these classes. -
methodPointer
Returns a method pointer (obj.&name,obj::name,Type::new) whose owner was validated bycheck(Object), wrapped so that its calls are validated too (seeSandboxMethodPointer). -
constructed
Returns the constructed object. Generated asconstructed(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
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
Validates the receiver of a spread property access (recv*.name), ascheckSpread(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]]*.namereadsa.name).
-