Top Description Fields Constructors Methods
org.graalvm.polyglot

public final Enum SandboxPolicy

extends Enum<SandboxPolicy>
Class Inheritance
Imports
org.graalvm.polyglot.io.FileSystem, .IOAccess, .MessageTransport, java.io.InputStream, .OutputStream

The sandbox policy presets and validates configurations of a context or engine to be suitable as a code sandbox. The policy is set by passing it to the engine or context builder method.

There are four policies to choose from that become strictly more strict:

It is unsupported to run untrusted code with any other policy than UNTRUSTED.

Compatibility Notice: The behavior of sandbox policies is subject to incompatible changes for new GraalVM major releases. New presets and validations may be added in new GraalVM releases that may let configurations valid in older versions fail for newer versions. Therefore, adopting a new GraalVM version with a set sandbox policy might require changes for the embedder. This applies to all policies other than TRUSTED. Changes to the policy are announced in the SDK release changelog.

For further information on Polyglot Sandboxing, please refer to the security guide.

Since
23.0
See Also
Context.Builder#sandbox(SandboxPolicy), Engine.Builder#sandbox(SandboxPolicy)

Field Summary

Modifier and TypeField and Description
public static final SandboxPolicy
CONSTRAINED

Policy intended for trusted, but potentially buggy applications.

public static final SandboxPolicy
ISOLATED

Policy intended for trusted applications, but which might have security vulnerabilities and optionally that can be mitigated using this policy.

public static final SandboxPolicy
TRUSTED

Policy intended for fully trusted applications.

public static final SandboxPolicy
UNTRUSTED

Policy intended for untrusted applications.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public boolean
isStricterOrEqual(SandboxPolicy other)

Tests whether this SandboxPolicy is stricter or equal to other.

public boolean
isStricterThan(SandboxPolicy other)

Tests whether this SandboxPolicy is stricter than other.

public static SandboxPolicy
public static SandboxPolicy[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

CONSTRAINEDback to summary
public static final SandboxPolicy CONSTRAINED

Policy intended for trusted, but potentially buggy applications. In this mode, any access to host resources is required to be as restrictive as possible. In this mode, the guest and host application share a heap and execute on the same underlying virtual machine.

The CONSTRAINED sandbox policy enforces the following context restriction:

Constrained Context building example:

ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayOutputStream errorOutput = new ByteArrayOutputStream();
try (Context context = Context.newBuilder("js") //
                .sandbox(SandboxPolicy.CONSTRAINED) //
                .out(output) //
                .err(errorOutput) //
                .build()) {
    context.eval(source);
}

Since
23.0
ISOLATEDback to summary
public static final SandboxPolicy ISOLATED

Policy intended for trusted applications, but which might have security vulnerabilities and optionally that can be mitigated using this policy. For example, a script that processes untrusted input. Security vulnerabilities may allow an attacker to compromise the guest application by providing malicious input. In this mode, guest and host application execute on separate virtual machine instances.

In addition to the CONSTRAINED restrictions, the ISOLATED sandbox policy adds the following constraints:

  • The engine.SpawnIsolate option is preset to true if it has not been explicitly set.
  • The engine.MaxIsolateMemory option must be set.
  • The sandbox.MaxCPUTime limits option must be set. Use sandbox.TraceLimits to estimate an application's optimal sandbox parameters.
  • If HostAccess is not specified, the HostAccess#ISOLATED is used.
  • Otherwise, the specified HostAccess must meet all the constraints of the CONSTRAINED sandbox policy and must in addition use scoped references.

Isolated Context building example:

ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayOutputStream errorOutput = new ByteArrayOutputStream();
try (Context context = Context.newBuilder("js") //
                .sandbox(SandboxPolicy.ISOLATED)  //
                .out(output)  //
                .err(errorOutput)  //
                .option("engine.MaxIsolateMemory", "1GB")  //
                .option("sandbox.MaxCPUTime", "10s") //
                .build()) {
    context.eval(source);
}

Since
23.0
TRUSTEDback to summary
public static final SandboxPolicy TRUSTED

Policy intended for fully trusted applications. In this mode, access to any resource of the host might be accessible to the guest application. This is the default mode, there are no restrictions to the context or engine configuration.

Since
23.0
UNTRUSTEDback to summary
public static final SandboxPolicy UNTRUSTED

Policy intended for untrusted applications. This assumes that a malicious actor is supplying the guest code itself that is being run. A strong adversarial scenario is the execution of client-side Javascript in the browser that is supplied by an untrusted website. In this mode, the sandbox employs additional hardening mechanisms at the compiler and runtime level to mitigate e.g. speculative execution attacks.

In addition to the ISOLATED constraints, the UNTRUSTED sandbox policy adds the following requirements:

  • If HostAccess is not specified, the HostAccess#UNTRUSTED is used. Otherwise, the specified HostAccess must meet all the constraints of the ISOLATED sandbox policy.
  • The engine.UntrustedCodeMitigation option is preset to software if it has not been explicitly set.
  • The sandbox.MaxCPUTime, sandbox.MaxHeapMemory, sandbox.MaxASTDepth, sandbox.MaxStackFrames, sandbox.MaxThreads, sandbox.MaxOutputStreamSize, sandbox.MaxErrorStreamSize limits options must be set. Use sandbox.TraceLimits to estimate an application's optimal sandbox parameters.

Untrusted Context building example:

ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayOutputStream errorOutput = new ByteArrayOutputStream();
try (Context context = Context.newBuilder("js") //
                .sandbox(SandboxPolicy.UNTRUSTED) //
                .out(output) //
                .err(errorOutput) //
                .option("engine.MaxIsolateMemory", "1GB") //
                .option("sandbox.MaxHeapMemory", "800MB") //
                .option("sandbox.MaxCPUTime", "10s") //
                .option("sandbox.MaxASTDepth", "100") //
                .option("sandbox.MaxStackFrames", "10") //
                .option("sandbox.MaxThreads", "1") //
                .option("sandbox.MaxOutputStreamSize", "1MB") //
                .option("sandbox.MaxErrorStreamSize", "1MB") //
                .build()) {
    context.eval(source);
}

Since
23.0

Constructor Detail

SandboxPolicyback to summary
private SandboxPolicy()

Method Detail

isStricterOrEqualback to summary
public boolean isStricterOrEqual(SandboxPolicy other)

Tests whether this SandboxPolicy is stricter or equal to other.

Since
23.0
isStricterThanback to summary
public boolean isStricterThan(SandboxPolicy other)

Tests whether this SandboxPolicy is stricter than other.

Since
23.0
valueOfback to summary
public static SandboxPolicy valueOf(String name)
valuesback to summary
public static SandboxPolicy[] values()