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:
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.
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.
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 would 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.
UNTRUSTED policy intended for fully untrusted applications. This assumes that a
potentially 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. JIT spraying or speculative execution attacks.
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.
Context.Builder#sandbox(SandboxPolicy) , Engine.Builder#sandbox(SandboxPolicy)
| Modifier and Type | Field 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. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | isStricterOrEqual(SandboxPolicy other)
Tests whether this |
| public boolean | |
| public static SandboxPolicy | |
| public static SandboxPolicy[] |
| CONSTRAINED | back 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
HostAccess must not allow
public access,
access inheritance,
all class implementations,
all interface implementations and
mutable target type mappings.
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);
}
| |
| ISOLATED | back 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
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);
}
| |
| TRUSTED | back 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.
| |
| UNTRUSTED | back 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
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);
}
| |
| SandboxPolicy | back to summary |
|---|---|
| private SandboxPolicy() | |
| isStricterOrEqual | back to summary |
|---|---|
| public boolean isStricterOrEqual(SandboxPolicy other) Tests whether this
| |
| isStricterThan | back to summary |
|---|---|
| public boolean isStricterThan(SandboxPolicy other) Tests whether this
| |
| valueOf | back to summary |
|---|---|
| public static SandboxPolicy valueOf(String name) | |
| values | back to summary |
|---|---|
| public static SandboxPolicy[] values() | |