public interface NashornSandbox
Created on 2015-08-06
| Modifier and Type | Method and Description |
|---|---|
void |
allow(Class<?> clazz)
Add a new class to the list of allowed classes.
|
void |
allowExitFunctions(boolean v)
Allow Nashorn quit and exit functions.
|
void |
allowGlobalsObjects(boolean v)
Allow Nashorn globals object $ARG, $ENV, $EXEC, $OPTIONS, $OUT, $ERR and $EXIT.
|
void |
allowLoadFunctions(boolean v)
Allow Nashorn load and loadWithNewGlobal functions.
|
void |
allowNoBraces(boolean v)
Force, to check if all blocks are enclosed with curly braces "{}".
|
void |
allowPrintFunctions(boolean v)
Allow Nashorn print and echo functions.
|
void |
allowReadFunctions(boolean v)
Allow Nashorn readLine and readFully functions.
|
Bindings |
createBindings()
Create new bindings used to replace the state of the current script engine
|
void |
disallow(Class<?> clazz)
Remove a class from the list of allowed classes.
|
void |
disallowAllClasses()
Remove all classes from the list of allowed classes.
|
Object |
eval(String js)
Evaluates the JavaScript string.
|
Object |
eval(String js,
Bindings bindings)
Evaluates the JavaScript string.
|
Object |
eval(String js,
ScriptContext scriptContext)
Evaluates the JavaScript string for a given script context
|
Object |
eval(String js,
ScriptContext scriptContext,
Bindings bindings)
Evaluates the JavaScript string for a given script context
|
Object |
get(String variableName)
Obtains the value of the specified JavaScript variable.
|
ExecutorService |
getExecutor()
Gets the current executor service.
|
Invocable |
getSandboxedInvocable()
Returns an
Invocable instance, so that method invocations are also sandboxed. |
void |
inject(String variableName,
Object object)
Will add a global variable available to all scripts executed with this sandbox.
|
boolean |
isAllowed(Class<?> clazz)
Check if a class is in the list of allowed classes.
|
void |
setExecutor(ExecutorService executor)
Specifies the executor service which is used to run scripts when a CPU time
limit is specified.
|
void |
setMaxCPUTime(long limit)
Sets the maximum CPU time in milliseconds allowed for script execution.
|
void |
setMaxMemory(long limit)
Sets the maximum memory in Bytes which JS executor thread can allocate.
|
void |
setMaxPreparedStatements(int max)
The size of prepared statements LRU cache.
|
void |
setScriptCache(SecuredJsCache cache)
Overwrites the cache for pre-processed javascript.
|
void |
setWriter(Writer writer)
Sets the writer, when want to have output from writer function called in
JS script
|
void allow(Class<?> clazz)
void disallow(Class<?> clazz)
boolean isAllowed(Class<?> clazz)
void disallowAllClasses()
void inject(String variableName, Object object)
variableName - the name of the variableobject - the value, can be nullvoid setMaxCPUTime(long limit)
Note, ExecutorService should be also set when time is set greater
than 0.
limit - time limit in millisecondssetExecutor(ExecutorService)void setMaxMemory(long limit)
Note, thread memory usage is only approximation.
Note, ExecutorService should be also set when memory limit is set
greater than 0. Nashorn takes some memory at start, be generous and give
at least 1MB.
Current implementation of this limit works only on Sun/Oracle JVM.
limit - limit in bytesThreadMXBean.getThreadAllocatedBytes(long)void setWriter(Writer writer)
writer - the writer, eg. StringWritervoid setExecutor(ExecutorService executor)
executor - the executor servicesetMaxCPUTime(long)ExecutorService getExecutor()
Object eval(String js) throws ScriptCPUAbuseException, ScriptException
js - the JavaScript script to be evaluatedScriptCPUAbuseException - when execution time exceeded (when greater
than 0 is setScriptException - when script syntax error occurssetMaxCPUTime(long)Object eval(String js, Bindings bindings) throws ScriptCPUAbuseException, ScriptException
js - the JavaScript script to be evaluatedbindings - the Bindings to use for evaluationScriptCPUAbuseException - when execution time exceeded (when greater
than 0 is setScriptException - when script syntax error occurssetMaxCPUTime(long)Object eval(String js, ScriptContext scriptContext) throws ScriptCPUAbuseException, ScriptException
js - the JavaScript script to be evaluatedscriptContext - the ScriptContext exposing sets of attributes in different scopes.ScriptCPUAbuseException - when execution time exceeded (when greater
than 0 is setScriptException - when script syntax error occurssetMaxCPUTime(long)Object eval(String js, ScriptContext scriptContext, Bindings bindings) throws ScriptCPUAbuseException, ScriptException
js - the JavaScript script to be evaluatedbindings - the Bindings to use for evaluationscriptContext - the ScriptContext exposing sets of attributes in different scopes.ScriptCPUAbuseException - when execution time exceeded (when greater
than 0 is setScriptException - when script syntax error occurssetMaxCPUTime(long)void allowPrintFunctions(boolean v)
Only before first eval(String) call cause effect.
void allowReadFunctions(boolean v)
Only before first eval(String) call cause effect.
void allowLoadFunctions(boolean v)
Only before first eval(String) call cause effect.
void allowExitFunctions(boolean v)
Only before first eval(String) call cause effect.
void allowGlobalsObjects(boolean v)
Only before first eval(String) call cause effect.
void allowNoBraces(boolean v)
Explanation: all loops (for, do-while, while, and if-else, and functions should use braces, because poison_pill() function will be inserted after each open brace "{", to ensure interruption checking. Otherwise simple code like:
while(true) while(true) {
// do nothing
}
or even:
while(true)
cause unbreakable loop, which force this sandbox to use Thread.stop()
which make JVM unstable.
Properly written code (even in bad intention) like:
while(true) { while(true) {
// do nothing
}}
will be changed into:
while(true) {poison_pill();
while(true) {poison_pill();
// do nothing
}
}
which finish nicely when interrupted.
For legacy code, this check can be turned off, but with no guarantee, the JS thread will gracefully finish when interrupted.
v - true when sandbox should check if all required braces
are placed into JS code, false when no check should be
performedvoid setMaxPreparedStatements(int max)
Each statements when setMaxCPUTime(long) is set is prepared to
quit itself when time exceeded. To execute only once this procedure per
statement set this value.
When setMaxCPUTime(long) is set 0, this value is ignored.
max - the maximum number of statements in the LRU cacheBindings createBindings()
This can be typically used to override ECMAScript "global" properties
Invocable getSandboxedInvocable()
Invocable instance, so that method invocations are also sandboxed.void setScriptCache(SecuredJsCache cache)
eval(String)
and its overloads.cache - the new cache to useCopyright © 2019. All rights reserved.