Class NashornSandboxImpl
- All Implemented Interfaces:
NashornSandbox
Created on 2015-08-07
- Version:
- $Id$
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected booleanprotected booleanprotected booleanprotected booleanprotected booleanprotected booleanprotected Bindingsprotected AtomicBooleanprotected JsEvaluatorprotected ExecutorServiceprotected Invocable(package private) static final org.slf4j.Loggerprotected longMaximum CPU time in milliseconds.protected longMaximum memory of executor thread used.protected intThe size of the LRU cache of prepared statements.protected final SandboxClassFilterprotected JsSanitizerprotected final ScriptEngineprotected SecuredJsCache -
Constructor Summary
ConstructorsConstructorDescriptionNashornSandboxImpl(String... params) NashornSandboxImpl(ScriptEngine engine, String... params) -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd a new class to the list of allowed classes.voidallowExitFunctions(boolean v) Allow Nashorn quit and exit functions.voidallowGlobalsObjects(boolean v) Allow Nashorn globals object $ARG, $ENV, $EXEC, $OPTIONS, $OUT, $ERR and $EXIT.voidallowLoadFunctions(boolean v) Allow Nashorn load and loadWithNewGlobal functions.voidallowNoBraces(boolean v) Force, to check if all blocks are enclosed with curly braces "{}".voidallowPrintFunctions(boolean v) Allow Nashorn print and echo functions.voidallowReadFunctions(boolean v) Allow Nashorn readLine and readFully functions.private voidprivate voidCompile the JavaScript stringCreate new bindings used to replace the state of the current script enginecreateNashornScriptEngineFactory(String... params) private SandboxClassFiltervoidRemove a class from the list of allowed classes.voidRemove all classes from the list of allowed classes.private booleanEvaluates 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 ObjectObtains the value of the specified JavaScript variable.private JsEvaluatorGets the current executor service.private InvocableReturns anInvocableinstance, so that method invocations are also sandboxed.protected JsSanitizervoidWill add a global variable available to all scripts executed with this sandbox.booleanCheck if a class is in the list of allowed classes.private voidprotected voidprotected voidsanitizeBindings(Bindings bindings) protected BindingssecureBindings(Bindings bindings) voidsetExecutor(ExecutorService executor) Specifies the executor service which is used to run scripts when a CPU time limit is specified.voidsetMaxCPUTime(long limit) Sets the maximum CPU time in milliseconds allowed for script execution.voidsetMaxMemory(long limit) Sets the maximum memory in Bytes which JS executor thread can allocate.voidsetMaxPreparedStatements(int max) The size of prepared statements LRU cache.voidsetScriptCache(SecuredJsCache cache) Overwrites the cache for pre-processed javascript.voidSets 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:NashornSandboxEvaluates the JavaScript string.- Specified by:
evalin 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:NashornSandboxEvaluates the JavaScript string.- Specified by:
evalin 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:NashornSandboxEvaluates the JavaScript string for a given script context- Specified by:
evalin 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:NashornSandboxEvaluates the JavaScript string for a given script context- Specified by:
evalin 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:NashornSandboxSets the maximum CPU time in milliseconds allowed for script execution.Note,
ExecutorServiceshould be also set when time is set greater than 0.- Specified by:
setMaxCPUTimein interfaceNashornSandbox- Parameters:
limit- time limit in milliseconds- See Also:
-
setMaxMemory
public void setMaxMemory(long limit) Description copied from interface:NashornSandboxSets the maximum memory in Bytes which JS executor thread can allocate.
Note, thread memory usage is only approximation.
Note,
ExecutorServiceshould 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:
setMaxMemoryin interfaceNashornSandbox- Parameters:
limit- limit in bytes- See Also:
-
getSanitizer
-
allow
Description copied from interface:NashornSandboxAdd a new class to the list of allowed classes.- Specified by:
allowin interfaceNashornSandbox
-
disallow
Description copied from interface:NashornSandboxRemove a class from the list of allowed classes.- Specified by:
disallowin interfaceNashornSandbox
-
isAllowed
Description copied from interface:NashornSandboxCheck if a class is in the list of allowed classes.- Specified by:
isAllowedin interfaceNashornSandbox
-
disallowAllClasses
public void disallowAllClasses()Description copied from interface:NashornSandboxRemove all classes from the list of allowed classes.- Specified by:
disallowAllClassesin interfaceNashornSandbox
-
inject
Description copied from interface:NashornSandboxWill add a global variable available to all scripts executed with this sandbox.- Specified by:
injectin interfaceNashornSandbox- Parameters:
variableName- the name of the variableobject- the value, can benull
-
setExecutor
Description copied from interface:NashornSandboxSpecifies the executor service which is used to run scripts when a CPU time limit is specified.- Specified by:
setExecutorin interfaceNashornSandbox- Parameters:
executor- the executor service- See Also:
-
getExecutor
Description copied from interface:NashornSandboxGets the current executor service.- Specified by:
getExecutorin interfaceNashornSandbox- Returns:
- current executor service
-
get
Description copied from interface:NashornSandboxObtains the value of the specified JavaScript variable.- Specified by:
getin interfaceNashornSandbox
-
allowPrintFunctions
public void allowPrintFunctions(boolean v) Description copied from interface:NashornSandboxAllow Nashorn print and echo functions.Only before first
NashornSandbox.eval(String)call cause effect.- Specified by:
allowPrintFunctionsin interfaceNashornSandbox
-
allowReadFunctions
public void allowReadFunctions(boolean v) Description copied from interface:NashornSandboxAllow Nashorn readLine and readFully functions.Only before first
NashornSandbox.eval(String)call cause effect.- Specified by:
allowReadFunctionsin interfaceNashornSandbox
-
allowLoadFunctions
public void allowLoadFunctions(boolean v) Description copied from interface:NashornSandboxAllow Nashorn load and loadWithNewGlobal functions.Only before first
NashornSandbox.eval(String)call cause effect.- Specified by:
allowLoadFunctionsin interfaceNashornSandbox
-
allowExitFunctions
public void allowExitFunctions(boolean v) Description copied from interface:NashornSandboxAllow Nashorn quit and exit functions.Only before first
NashornSandbox.eval(String)call cause effect.- Specified by:
allowExitFunctionsin interfaceNashornSandbox
-
allowGlobalsObjects
public void allowGlobalsObjects(boolean v) Description copied from interface:NashornSandboxAllow Nashorn globals object $ARG, $ENV, $EXEC, $OPTIONS, $OUT, $ERR and $EXIT.Only before first
NashornSandbox.eval(String)call cause effect.- Specified by:
allowGlobalsObjectsin interfaceNashornSandbox
-
allowNoBraces
public void allowNoBraces(boolean v) Description copied from interface:NashornSandboxForce, 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
setMaxCPUTimeto 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:
allowNoBracesin interfaceNashornSandbox- Parameters:
v-truewhen sandbox should check if all required braces are placed into JS code,falsewhen no check should be performed
-
setWriter
Description copied from interface:NashornSandboxSets the writer, when want to have output from writer function called in JS script- Specified by:
setWriterin interfaceNashornSandbox- Parameters:
writer- the writer, eg.StringWriter
-
setMaxPreparedStatements
public void setMaxPreparedStatements(int max) Description copied from interface:NashornSandboxThe 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:
setMaxPreparedStatementsin interfaceNashornSandbox- Parameters:
max- the maximum number of statements in the LRU cache
-
createBindings
Description copied from interface:NashornSandboxCreate new bindings used to replace the state of the current script engineThis can be typically used to override ECMAScript "global" properties
- Specified by:
createBindingsin interfaceNashornSandbox- Returns:
-
getSandboxedInvocable
Description copied from interface:NashornSandboxReturns anInvocableinstance, so that method invocations are also sandboxed.- Specified by:
getSandboxedInvocablein interfaceNashornSandbox- Returns:
-
getLazySandboxedInvocable
-
setScriptCache
Description copied from interface:NashornSandboxOverwrites the cache for pre-processed javascript. Must be called before the first invocation ofNashornSandbox.eval(String)and its overloads.- Specified by:
setScriptCachein interfaceNashornSandbox- Parameters:
cache- the new cache to use
-
compile
Description copied from interface:NashornSandboxCompile the JavaScript string- Specified by:
compilein interfaceNashornSandbox- Parameters:
js- the JavaScript script to be compiled- Returns:
- a CompiledScript object
- Throws:
ScriptException
-
eval
Description copied from interface:NashornSandboxRun a pre-compiled JavaScript- Specified by:
evalin interfaceNashornSandbox- Throws:
ScriptCPUAbuseExceptionScriptException
-
eval
public Object eval(CompiledScript compiledScript, Bindings bindings) throws ScriptCPUAbuseException, ScriptException - Specified by:
evalin interfaceNashornSandbox- Throws:
ScriptCPUAbuseExceptionScriptException
-
eval
public Object eval(CompiledScript compiledScript, ScriptContext scriptContext) throws ScriptCPUAbuseException, ScriptException - Specified by:
evalin interfaceNashornSandbox- Throws:
ScriptCPUAbuseExceptionScriptException
-
eval
public Object eval(CompiledScript compiledScript, ScriptContext scriptContext, Bindings bindings) throws ScriptCPUAbuseException, ScriptException - Specified by:
evalin interfaceNashornSandbox- Throws:
ScriptCPUAbuseExceptionScriptException
-