public enum SandboxPolicy extends Enum<SandboxPolicy>
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:
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.
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.
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 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.
SandboxPolicy.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.
SandboxPolicy.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 SandboxPolicy.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)
Enum.EnumDesc<E extends Enum<E>>
Enum Constant and Description |
---|
CONSTRAINED
Policy intended for trusted, but potentially buggy applications.
|
ISOLATED
Policy intended for trusted applications, but which might have security vulnerabilities and
optionally that can be mitigated using this policy.
|
TRUSTED
Policy intended for fully trusted applications.
|
UNTRUSTED
Policy intended for untrusted applications.
|
Modifier and Type | Method and Description |
---|---|
boolean |
isStricterOrEqual(SandboxPolicy other)
Tests whether this
SandboxPolicy is stricter or equal to other . |
boolean |
isStricterThan(SandboxPolicy other)
Tests whether this
SandboxPolicy is stricter than other . |
static SandboxPolicy |
valueOf(String name)
Returns the enum constant of this type with the specified name.
|
static SandboxPolicy[] |
values()
Returns an array containing the constants of this enum type, in
the order they are declared.
|
public static final SandboxPolicy TRUSTED
public static final SandboxPolicy CONSTRAINED
The CONSTRAINED
sandbox policy enforces the following context restriction:
permitted languages
must be explicitly
set.in
is not specified, the
InputStream.nullInputStream()
is used. Otherwise, it must be redirected elsewhere
than to System.in
.
out
and
Context.Builder.err(OutputStream)
err} streams must be redirected.all access
must not be enabled.native access
must not be
enabled.Context.Builder.allowHostClassLoading(boolean)
host class loading} must not
be enabled.external process execution
must
not be enabled.environment access
must be EnvironmentAccess.NONE
.host System.exit
must not be
used.access to the host file system
must be
disabled. IO can be disabled
or it can use a
custom file
system
.default filesystem
or a filesytem wrapping the
default file system.CONSTRAINED
can be used.CONSTRAINED
can be used.HostAccess
is not specified, the HostAccess.CONSTRAINED
is used.HostAccess
must not allow
public access
,
access inheritance
,
all class implementations
,
all interface implementations
and
mutable target type mappings
.
execution listeners
must not be attached.message transport
must not
be set.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); }
public static final SandboxPolicy ISOLATED
In addition to the SandboxPolicy.CONSTRAINED
restrictions, the ISOLATED
sandbox policy
adds the following constraints:
engine.SpawnIsolate
option is preset to true
if it has not been
explicitly set.engine.MaxIsolateMemory
option must be set.sandbox.MaxCPUTime
limits option must be set. Use sandbox.TraceLimits
to estimate an application's optimal sandbox parameters.HostAccess
is not specified, the HostAccess.ISOLATED
is used.HostAccess
must meet all the constraints of the
SandboxPolicy.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); }
public static final SandboxPolicy UNTRUSTED
In addition to the SandboxPolicy.ISOLATED
constraints, the UNTRUSTED
sandbox policy adds
the following requirements:
HostAccess
is not specified, the HostAccess.UNTRUSTED
is used.
Otherwise, the specified HostAccess
must meet all the constraints of the
SandboxPolicy.ISOLATED
sandbox policy.engine.UntrustedCodeMitigation
option is preset to software
if it has
not been explicitly set.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); }
public static SandboxPolicy[] values()
public static SandboxPolicy valueOf(String name)
name
- the name of the enum constant to be returned.IllegalArgumentException
- if this enum type has no constant with the specified nameNullPointerException
- if the argument is nullpublic boolean isStricterThan(SandboxPolicy other)
SandboxPolicy
is stricter than other
.public boolean isStricterOrEqual(SandboxPolicy other)
SandboxPolicy
is stricter or equal to other
.