public class NashornSandboxImpl extends Object implements NashornSandbox
Created on 2015-08-07
| Modifier and Type | Field and Description |
|---|---|
protected boolean |
allowExitFunctions |
protected boolean |
allowGlobalsObjects |
protected boolean |
allowLoadFunctions |
protected boolean |
allowNoBraces |
protected boolean |
allowPrintFunctions |
protected boolean |
allowReadFunctions |
protected boolean |
engineAsserted |
protected delight.nashornsandbox.internal.JsEvaluator |
evaluator |
protected ExecutorService |
executor |
protected Invocable |
lazyInvocable |
protected long |
maxCPUTime
Maximum CPU time in milliseconds.
|
protected long |
maxMemory
Maximum memory of executor thread used.
|
protected int |
maxPreparedStatements
The size of the LRU cache of prepared statemensts.
|
protected delight.nashornsandbox.internal.SandboxClassFilter |
sandboxClassFilter |
protected delight.nashornsandbox.internal.JsSanitizer |
sanitizer |
protected ScriptEngine |
scriptEngine |
| Constructor and Description |
|---|
NashornSandboxImpl() |
NashornSandboxImpl(String... params) |
| 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,
ScriptContext scriptContext)
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 |
setWriter(Writer writer)
Sets the writer, whem want to have output from writer function called in
JS script
|
protected final delight.nashornsandbox.internal.SandboxClassFilter sandboxClassFilter
protected final ScriptEngine scriptEngine
protected long maxCPUTime
protected long maxMemory
protected ExecutorService executor
protected boolean allowPrintFunctions
protected boolean allowReadFunctions
protected boolean allowLoadFunctions
protected boolean allowExitFunctions
protected boolean allowGlobalsObjects
protected boolean allowNoBraces
protected delight.nashornsandbox.internal.JsEvaluator evaluator
protected delight.nashornsandbox.internal.JsSanitizer sanitizer
protected boolean engineAsserted
protected Invocable lazyInvocable
protected int maxPreparedStatements
public NashornSandboxImpl()
public NashornSandboxImpl(String... params)
public Object eval(String js) throws ScriptCPUAbuseException, ScriptException
NashornSandboxeval in interface NashornSandboxjs - the JavaScript script to be evaluatedScriptCPUAbuseException - when execution time exceeded (when greater
than 0 is setScriptException - when script syntax error occursNashornSandbox.setMaxCPUTime(long)public Object eval(String js, ScriptContext scriptContext) throws ScriptCPUAbuseException, ScriptException
NashornSandboxeval in interface NashornSandboxjs - 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 occursNashornSandbox.setMaxCPUTime(long)public void setMaxCPUTime(long limit)
NashornSandbox
Note, ExecutorService should be also set when time is set greater
than 0.
setMaxCPUTime in interface NashornSandboxlimit - time limit in milisecondsNashornSandbox.setExecutor(ExecutorService)public void setMaxMemory(long limit)
NashornSandboxNote, 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.
setMaxMemory in interface NashornSandboxlimit - limit in bytesThreadMXBean.getThreadAllocatedBytes(long)public void allow(Class<?> clazz)
NashornSandboxallow in interface NashornSandboxpublic void disallow(Class<?> clazz)
NashornSandboxdisallow in interface NashornSandboxpublic boolean isAllowed(Class<?> clazz)
NashornSandboxisAllowed in interface NashornSandboxpublic void disallowAllClasses()
NashornSandboxdisallowAllClasses in interface NashornSandboxpublic void inject(String variableName, Object object)
NashornSandboxinject in interface NashornSandboxvariableName - the name of the variableobject - the value, can be nullpublic void setExecutor(ExecutorService executor)
NashornSandboxsetExecutor in interface NashornSandboxexecutor - the executor serviceNashornSandbox.setMaxCPUTime(long)public ExecutorService getExecutor()
NashornSandboxgetExecutor in interface NashornSandboxpublic Object get(String variableName)
NashornSandboxget in interface NashornSandboxpublic void allowPrintFunctions(boolean v)
NashornSandbox
Only before first NashornSandbox.eval(String) call cause effect.
allowPrintFunctions in interface NashornSandboxpublic void allowReadFunctions(boolean v)
NashornSandbox
Only before first NashornSandbox.eval(String) call cause effect.
allowReadFunctions in interface NashornSandboxpublic void allowLoadFunctions(boolean v)
NashornSandbox
Only before first NashornSandbox.eval(String) call cause effect.
allowLoadFunctions in interface NashornSandboxpublic void allowExitFunctions(boolean v)
NashornSandbox
Only before first NashornSandbox.eval(String) call cause efect.
allowExitFunctions in interface NashornSandboxpublic void allowGlobalsObjects(boolean v)
NashornSandbox
Only before first NashornSandbox.eval(String) call cause effect.
allowGlobalsObjects in interface NashornSandboxpublic void allowNoBraces(boolean v)
NashornSandboxExplanation: 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 writen 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.
allowNoBraces in interface NashornSandboxv - true when sandbox should check if all required braces
are placed into JS code, false when no check should be
performedpublic void setWriter(Writer writer)
NashornSandboxsetWriter in interface NashornSandboxwriter - the writer, eg. public void setMaxPreparedStatements(int max)
NashornSandbox
Each statements when NashornSandbox.setMaxCPUTime(long) is set is prepared to
quit itself when time exceeded. To execute only once this procedure per
statment set this value.
When NashornSandbox.setMaxCPUTime(long) is set 0, this value is ignored.
setMaxPreparedStatements in interface NashornSandboxmax - the maximum number of statements in the LRU cachepublic Bindings createBindings()
NashornSandboxThis can be typically used to override ECMAScript "global" properties
createBindings in interface NashornSandboxpublic Invocable getSandboxedInvocable()
NashornSandboxInvocable instance, so that method invocations are also sandboxed.getSandboxedInvocable in interface NashornSandboxCopyright © 2018. All rights reserved.