Class NashornSandboxImpl
- All Implemented Interfaces:
NashornSandbox
Created on 2015-08-07
- Version:
- $Id$
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
protected boolean
protected boolean
protected boolean
protected boolean
protected boolean
protected Bindings
protected AtomicBoolean
protected JsEvaluator
protected ExecutorService
protected Invocable
(package private) static final org.slf4j.Logger
protected long
Maximum CPU time in milliseconds.protected long
Maximum memory of executor thread used.protected int
The size of the LRU cache of prepared statements.protected final SandboxClassFilter
protected JsSanitizer
protected final ScriptEngine
protected SecuredJsCache
-
Constructor Summary
ConstructorsConstructorDescriptionNashornSandboxImpl
(String... params) NashornSandboxImpl
(ScriptEngine engine, String... params) -
Method Summary
Modifier and TypeMethodDescriptionvoid
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.private void
private void
Compile the JavaScript stringCreate new bindings used to replace the state of the current script enginecreateNashornScriptEngineFactory
(String... params) private SandboxClassFilter
void
Remove a class from the list of allowed classes.void
Remove all classes from the list of allowed classes.private boolean
Evaluates the JavaScript string.Evaluates the JavaScript string.eval
(String js, ScriptContext scriptContext) Evaluates the JavaScript string for a given script contexteval
(String js, ScriptContext scriptContext, Bindings bindings) Evaluates the JavaScript string for a given script contexteval
(CompiledScript compiledScript) Run a pre-compiled JavaScripteval
(CompiledScript compiledScript, Bindings bindings) eval
(CompiledScript compiledScript, ScriptContext scriptContext) eval
(CompiledScript compiledScript, ScriptContext scriptContext, Bindings bindings) protected Object
Obtains the value of the specified JavaScript variable.private JsEvaluator
Gets the current executor service.private Invocable
Returns anInvocable
instance, so that method invocations are also sandboxed.protected JsSanitizer
void
Will add a global variable available to all scripts executed with this sandbox.boolean
Check if a class is in the list of allowed classes.private void
protected void
protected void
sanitizeBindings
(Bindings bindings) protected Bindings
secureBindings
(Bindings bindings) 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
Sets the writer, when want to have output from writer function called in JS script
-
Field Details
-
LOG
static final org.slf4j.Logger LOG -
sandboxClassFilter
-
scriptEngine
-
maxCPUTime
protected long maxCPUTimeMaximum CPU time in milliseconds. -
maxMemory
protected long maxMemoryMaximum memory of executor thread used. -
executor
-
allowPrintFunctions
protected boolean allowPrintFunctions -
allowReadFunctions
protected boolean allowReadFunctions -
allowLoadFunctions
protected boolean allowLoadFunctions -
allowExitFunctions
protected boolean allowExitFunctions -
allowGlobalsObjects
protected boolean allowGlobalsObjects -
allowNoBraces
protected boolean allowNoBraces -
evaluator
-
sanitizer
-
engineAsserted
-
lazyInvocable
-
maxPreparedStatements
protected int maxPreparedStatementsThe size of the LRU cache of prepared statements. -
suppliedCache
-
cached
-
-
Constructor Details
-
NashornSandboxImpl
public NashornSandboxImpl() -
NashornSandboxImpl
-
NashornSandboxImpl
-
-
Method Details
-
createSandboxClassFilter
-
createNashornScriptEngineFactory
-
assertScriptEngine
private void assertScriptEngine() -
engineBindingUnchanged
private boolean engineBindingUnchanged() -
produceSecureBindings
private void produceSecureBindings() -
resetEngineBindings
protected void resetEngineBindings() -
sanitizeBindings
-
eval
Description copied from interface:NashornSandbox
Evaluates the JavaScript string.- Specified by:
eval
in interfaceNashornSandbox
- Parameters:
js
- the JavaScript script to be evaluated- Throws:
ScriptCPUAbuseException
- when execution time exceeded (when greater than 0 is setScriptException
- when script syntax error occurs- See Also:
-
eval
Description copied from interface:NashornSandbox
Evaluates the JavaScript string.- Specified by:
eval
in interfaceNashornSandbox
- Parameters:
js
- the JavaScript script to be evaluatedbindings
- the Bindings to use for evaluation- Throws:
ScriptCPUAbuseException
- when execution time exceeded (when greater than 0 is setScriptException
- when script syntax error occurs- See Also:
-
eval
public Object eval(String js, ScriptContext scriptContext) throws ScriptCPUAbuseException, ScriptException Description copied from interface:NashornSandbox
Evaluates the JavaScript string for a given script context- Specified by:
eval
in interfaceNashornSandbox
- Parameters:
js
- the JavaScript script to be evaluatedscriptContext
- the ScriptContext exposing sets of attributes in different scopes.- Throws:
ScriptCPUAbuseException
- when execution time exceeded (when greater than 0 is setScriptException
- when script syntax error occurs- See Also:
-
eval
public Object eval(String js, ScriptContext scriptContext, Bindings bindings) throws ScriptCPUAbuseException, ScriptException Description copied from interface:NashornSandbox
Evaluates the JavaScript string for a given script context- Specified by:
eval
in interfaceNashornSandbox
- Parameters:
js
- the JavaScript script to be evaluatedscriptContext
- the ScriptContext exposing sets of attributes in different scopes.bindings
- the Bindings to use for evaluation- Throws:
ScriptCPUAbuseException
- when execution time exceeded (when greater than 0 is setScriptException
- when script syntax error occurs- See Also:
-
secureBindings
-
executeSandboxedOperation
protected Object executeSandboxedOperation(ScriptEngineOperation op) throws ScriptCPUAbuseException, ScriptException -
getEvaluator
-
checkExecutorPresence
private void checkExecutorPresence() -
setMaxCPUTime
public void setMaxCPUTime(long limit) Description copied from interface:NashornSandbox
Sets the maximum CPU time in milliseconds allowed for script execution.Note,
ExecutorService
should be also set when time is set greater than 0.- Specified by:
setMaxCPUTime
in interfaceNashornSandbox
- Parameters:
limit
- time limit in milliseconds- See Also:
-
setMaxMemory
public void setMaxMemory(long limit) Description copied from interface:NashornSandbox
Sets the maximum memory in Bytes which JS executor thread can allocate.
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. If bindings are used, Nashorn allocates additional memory for the bindings which might be a multiple of the memory theoretically required by the data types used. For details, see issue 86.Current implementation of this limit works only on Sun/Oracle JVM.
- Specified by:
setMaxMemory
in interfaceNashornSandbox
- Parameters:
limit
- limit in bytes- See Also:
-
getSanitizer
-
allow
Description copied from interface:NashornSandbox
Add a new class to the list of allowed classes.- Specified by:
allow
in interfaceNashornSandbox
-
disallow
Description copied from interface:NashornSandbox
Remove a class from the list of allowed classes.- Specified by:
disallow
in interfaceNashornSandbox
-
isAllowed
Description copied from interface:NashornSandbox
Check if a class is in the list of allowed classes.- Specified by:
isAllowed
in interfaceNashornSandbox
-
disallowAllClasses
public void disallowAllClasses()Description copied from interface:NashornSandbox
Remove all classes from the list of allowed classes.- Specified by:
disallowAllClasses
in interfaceNashornSandbox
-
inject
Description copied from interface:NashornSandbox
Will add a global variable available to all scripts executed with this sandbox.- Specified by:
inject
in interfaceNashornSandbox
- Parameters:
variableName
- the name of the variableobject
- the value, can benull
-
setExecutor
Description copied from interface:NashornSandbox
Specifies the executor service which is used to run scripts when a CPU time limit is specified.- Specified by:
setExecutor
in interfaceNashornSandbox
- Parameters:
executor
- the executor service- See Also:
-
getExecutor
Description copied from interface:NashornSandbox
Gets the current executor service.- Specified by:
getExecutor
in interfaceNashornSandbox
- Returns:
- current executor service
-
get
Description copied from interface:NashornSandbox
Obtains the value of the specified JavaScript variable.- Specified by:
get
in interfaceNashornSandbox
-
allowPrintFunctions
public void allowPrintFunctions(boolean v) Description copied from interface:NashornSandbox
Allow Nashorn print and echo functions.Only before first
NashornSandbox.eval(String)
call cause effect.- Specified by:
allowPrintFunctions
in interfaceNashornSandbox
-
allowReadFunctions
public void allowReadFunctions(boolean v) Description copied from interface:NashornSandbox
Allow Nashorn readLine and readFully functions.Only before first
NashornSandbox.eval(String)
call cause effect.- Specified by:
allowReadFunctions
in interfaceNashornSandbox
-
allowLoadFunctions
public void allowLoadFunctions(boolean v) Description copied from interface:NashornSandbox
Allow Nashorn load and loadWithNewGlobal functions.Only before first
NashornSandbox.eval(String)
call cause effect.- Specified by:
allowLoadFunctions
in interfaceNashornSandbox
-
allowExitFunctions
public void allowExitFunctions(boolean v) Description copied from interface:NashornSandbox
Allow Nashorn quit and exit functions.Only before first
NashornSandbox.eval(String)
call cause effect.- Specified by:
allowExitFunctions
in interfaceNashornSandbox
-
allowGlobalsObjects
public void allowGlobalsObjects(boolean v) Description copied from interface:NashornSandbox
Allow Nashorn globals object $ARG, $ENV, $EXEC, $OPTIONS, $OUT, $ERR and $EXIT.Only before first
NashornSandbox.eval(String)
call cause effect.- Specified by:
allowGlobalsObjects
in interfaceNashornSandbox
-
allowNoBraces
public void allowNoBraces(boolean v) Description copied from interface:NashornSandbox
Force, to check if all blocks are enclosed with curly braces "{}".Warning This option is useful to identify potential abuse but is also prone to identify false positives. Please use with caution. Alternatively you can use
setMaxCPUTime
to prevent abusive script execution.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 useThread.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.
- Specified by:
allowNoBraces
in interfaceNashornSandbox
- Parameters:
v
-true
when sandbox should check if all required braces are placed into JS code,false
when no check should be performed
-
setWriter
Description copied from interface:NashornSandbox
Sets the writer, when want to have output from writer function called in JS script- Specified by:
setWriter
in interfaceNashornSandbox
- Parameters:
writer
- the writer, eg.StringWriter
-
setMaxPreparedStatements
public void setMaxPreparedStatements(int max) Description copied from interface:NashornSandbox
The size of prepared statements LRU cache. Default 0 (disabled).Each statements when
NashornSandbox.setMaxCPUTime(long)
is set is prepared to quit itself when time exceeded. To execute only once this procedure per statement set this value.When
NashornSandbox.setMaxCPUTime(long)
is set 0, this value is ignored.- Specified by:
setMaxPreparedStatements
in interfaceNashornSandbox
- Parameters:
max
- the maximum number of statements in the LRU cache
-
createBindings
Description copied from interface:NashornSandbox
Create new bindings used to replace the state of the current script engineThis can be typically used to override ECMAScript "global" properties
- Specified by:
createBindings
in interfaceNashornSandbox
- Returns:
-
getSandboxedInvocable
Description copied from interface:NashornSandbox
Returns anInvocable
instance, so that method invocations are also sandboxed.- Specified by:
getSandboxedInvocable
in interfaceNashornSandbox
- Returns:
-
getLazySandboxedInvocable
-
setScriptCache
Description copied from interface:NashornSandbox
Overwrites the cache for pre-processed javascript. Must be called before the first invocation ofNashornSandbox.eval(String)
and its overloads.- Specified by:
setScriptCache
in interfaceNashornSandbox
- Parameters:
cache
- the new cache to use
-
compile
Description copied from interface:NashornSandbox
Compile the JavaScript string- Specified by:
compile
in interfaceNashornSandbox
- Parameters:
js
- the JavaScript script to be compiled- Returns:
- a CompiledScript object
- Throws:
ScriptException
-
eval
Description copied from interface:NashornSandbox
Run a pre-compiled JavaScript- Specified by:
eval
in interfaceNashornSandbox
- Throws:
ScriptCPUAbuseException
ScriptException
-
eval
public Object eval(CompiledScript compiledScript, Bindings bindings) throws ScriptCPUAbuseException, ScriptException - Specified by:
eval
in interfaceNashornSandbox
- Throws:
ScriptCPUAbuseException
ScriptException
-
eval
public Object eval(CompiledScript compiledScript, ScriptContext scriptContext) throws ScriptCPUAbuseException, ScriptException - Specified by:
eval
in interfaceNashornSandbox
- Throws:
ScriptCPUAbuseException
ScriptException
-
eval
public Object eval(CompiledScript compiledScript, ScriptContext scriptContext, Bindings bindings) throws ScriptCPUAbuseException, ScriptException - Specified by:
eval
in interfaceNashornSandbox
- Throws:
ScriptCPUAbuseException
ScriptException
-