bpmn-server / SandboxScriptHandler
Class: SandboxScriptHandler#
Runs a model's scripts and expressions in a sandbox: QuickJS (a JavaScript engine compiled to WebAssembly), with no access to Node.js, the file system, the network or the server's objects, a memory limit and a time limit. For servers that run models written by people they do not trust (a public demo):
scriptHandler: () => new SandboxScriptHandler()
A script gets copies of what the server's scripts get (data, item, input, output, instance; this is
item) as plain JSON values. What it changes in data, input, output and the item's assignment fields
(assignee, candidateUsers, ...) is copied back. appServices and appUtils are there, but calling one of their
methods runs nothing: it returns undefined and the run's decisions say so. There is no appDelegate, no
require, no process, no timers; $py scripts are refused.
With callServices, calling a method of appServices or appUtils runs it, in the server, with JSON copies of
its arguments, and the script gets a JSON copy of what it returns. Whoever writes a model can then call every one
of those methods with any arguments, so they must be safe to be called so. Only methods are reached, and only
through plain objects: not a service's other objects (its appDelegate), nor what every object inherits.
Hierarchy#
↳ SandboxScriptHandler
Table of contents#
Constructors#
Properties#
Methods#
- evaluateExpression
- executeScript
- standardFor
- run
- standardValues
- namesRead
- executionOf
- appVarsOf
- contextValues
- evaluateCondition
- evaluateFeel
- evaluateInputExpression
- compile
- scriptName
- getJSvars
- runPython
Constructors#
constructor#
• new SandboxScriptHandler(options?): SandboxScriptHandler
Parameters#
| Name | Type |
|---|---|
options |
Object |
options.timeLimit? |
number |
options.memoryLimit? |
number |
options.callServices? |
boolean |
options.context? |
ScriptContext |
Returns#
Overrides#
Defined in#
packages/bpmn-server/src/engine/SandboxScriptHandler.ts:33
Properties#
timeLimit#
• timeLimit: number = 200
Milliseconds a script or expression may run.
Defined in#
packages/bpmn-server/src/engine/SandboxScriptHandler.ts:27
memoryLimit#
• memoryLimit: number
Bytes a script may use.
Defined in#
packages/bpmn-server/src/engine/SandboxScriptHandler.ts:29
callServices#
• callServices: boolean = false
Calling a method of appServices or appUtils runs it (else it runs nothing, and the run's decisions say so).
Defined in#
packages/bpmn-server/src/engine/SandboxScriptHandler.ts:31
context#
• context: ScriptContext = {}
Variables scripts and expressions see besides their own, by name. The application sets it; models can not.
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:22
Methods#
evaluateExpression#
▸ evaluateExpression(scope, expression, label?): Promise\<any>
execute JavaScript expression , no need for $
Parameters#
| Name | Type | Default value |
|---|---|---|
scope |
Token | Item |
undefined |
expression |
any |
undefined |
label |
string |
'expression' |
Returns#
Promise\<any>
Overrides#
ScriptHandler.evaluateExpression
Defined in#
packages/bpmn-server/src/engine/SandboxScriptHandler.ts:38
executeScript#
▸ executeScript(scope, script, label?): Promise\<any>
label: which script it is (an event's, a script task's): its name for debuggers and errors
Parameters#
| Name | Type | Default value |
|---|---|---|
scope |
Execution | Item |
undefined |
script |
any |
undefined |
label |
string |
'script' |
Returns#
Promise\<any>
Overrides#
Defined in#
packages/bpmn-server/src/engine/SandboxScriptHandler.ts:44
standardFor#
▸ standardFor(scope, body): Record\<string, any>
The standard names (ScriptHandler.standardValues) the script reads, as plain values.
Parameters#
| Name | Type |
|---|---|
scope |
any |
body |
string |
Returns#
Record\<string, any>
Defined in#
packages/bpmn-server/src/engine/SandboxScriptHandler.ts:51
run#
▸ run(scope, body, label): Promise\<any>
Runs a function body against copies of the scope's values; copies back what it changed; returns its value.
Parameters#
| Name | Type |
|---|---|
scope |
any |
body |
string |
label |
string |
Returns#
Promise\<any>
Defined in#
packages/bpmn-server/src/engine/SandboxScriptHandler.ts:58
standardValues#
▸ standardValues(scope, materialized?): Record\<string, any>
The names every expression sees besides the scope's own (docs/design/expressions.md): user (the operation's,
by name), and the add-ons' names for the item's place (engine/AddOns.ts: the policies' add-ons' actors). An
application's script context of the same name is used instead. materialized: plain values, for the sandbox.
Parameters#
| Name | Type | Default value |
|---|---|---|
scope |
any |
undefined |
materialized |
boolean |
false |
Returns#
Record\<string, any>
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:36
namesRead#
▸ namesRead(body): Set\<string>
The names a script reads without declaring them (Expressions.freeNames), remembered by its text.
Parameters#
| Name | Type |
|---|---|
body |
string |
Returns#
Set\<string>
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:51
executionOf#
▸ executionOf(scope): any
The execution a scope (an item, a token, an execution) is of.
Parameters#
| Name | Type |
|---|---|
scope |
any |
Returns#
any
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:62
appVarsOf#
▸ appVarsOf(scope, text): Promise\<Record\<string, any>>
The application's variables a text reads (appVars.evaluatingSource). None read: none asked.
Parameters#
| Name | Type |
|---|---|
scope |
any |
text |
string |
Returns#
Promise\<Record\<string, any>>
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:73
contextValues#
▸ contextValues(scope): Record\<string, any>
The context's values for a scope.
Parameters#
| Name | Type |
|---|---|
scope |
any |
Returns#
Record\<string, any>
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:94
evaluateCondition#
▸ evaluateCondition(scope, expression, language?, label?): Promise\<any>
A sequence flow's condition, in the language it says: FEEL when it says so, any other is JavaScript. A ${...} around it is a wrapper, whatever the language.
Parameters#
| Name | Type | Default value |
|---|---|---|
scope |
Token | Item |
undefined |
expression |
any |
undefined |
language? |
string |
undefined |
label |
string |
'condition' |
Returns#
Promise\<any>
Inherited from#
ScriptHandler.evaluateCondition
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:105
evaluateFeel#
▸ evaluateFeel(scope, expression): Promise\<unknown>
A FEEL expression, with the names every expression sees (docs/design/expressions.md): the data's fields by name,
data, this and item, instance, input, output, actors, user, appServices. A call to an async
service, as the whole expression, is awaited.
Parameters#
| Name | Type |
|---|---|
scope |
any |
expression |
any |
Returns#
Promise\<unknown>
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:116
evaluateInputExpression#
▸ evaluateInputExpression(item, exp, dateFormat?): Promise\<any>
- these expression are strings with $
- scenarios:
- String =>as is
- \(javaScript =>evaulated (\)js, $(js) or ${js})
- a ${js} b =>the text, each ${js} evaluated in its place
- a , b , c =>array
- dateString =>convert to date
- Examples:
- ['T',
user1], ['T',$(appServices.test1(100))], ['T',$(appServices.getSupervisorUser('user1'))], ['T',abc,xyz,user group], ['TD',2022-10-11], - appDelegate.scopeEval -->evaluateExpression
- appDelegate.scopeJS -->executeScript *
Parameters#
| Name | Type | Default value |
|---|---|---|
item |
any |
undefined |
exp |
any |
undefined |
dateFormat |
boolean |
false |
Returns#
Promise\<any>
Inherited from#
ScriptHandler.evaluateInputExpression
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:155
compile#
▸ compile(scope, body, label, names?): Function
A model's script or expression as a function of its scope, named after where it is in the model
(bpmn:///
Parameters#
| Name | Type | Default value |
|---|---|---|
scope |
any |
undefined |
body |
string |
undefined |
label |
string |
undefined |
names |
string[] |
[] |
Returns#
Function
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:268
scriptName#
▸ scriptName(scope, label): string
bpmn:///
Parameters#
| Name | Type |
|---|---|
scope |
any |
label |
string |
Returns#
string
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:286
getJSvars#
▸ getJSvars(scope): "\n var data=this.data;\n var instance=this.execution.instance;\n var input=this.input;\n var output=this.output;\n var appDelegate=this.execution.appDelegate;\n var appServices=this.execution.servicesProvider;\n var appUtils=appDelegate.appUtils;\n var item=this; // for backward support only\n " | "\n var appDelegate=this.appDelegate;\n var instance=this.instance;\n var data=this.instance.data; // a process's own script: the instance's data, at the top\n var appServices=this.servicesProvider;\n var appUtils=appDelegate.appUtils;\n " | "\n var item=this;\n var data=this.data;\n var instance=this.token.execution.instance;\n var input=this.input;\n var output=this.output;\n var appDelegate=this.token.execution.appDelegate;\n var appServices=this.token.execution.servicesProvider;\n var appUtils=appDelegate.appUtils;\n "
Parameters#
| Name | Type |
|---|---|
scope |
any |
Returns#
"\n var data=this.data;\n var instance=this.execution.instance;\n var input=this.input;\n var output=this.output;\n var appDelegate=this.execution.appDelegate;\n var appServices=this.execution.servicesProvider;\n var appUtils=appDelegate.appUtils;\n var item=this; // for backward support only\n " | "\n var appDelegate=this.appDelegate;\n var instance=this.instance;\n var data=this.instance.data; // a process's own script: the instance's data, at the top\n var appServices=this.servicesProvider;\n var appUtils=appDelegate.appUtils;\n " | "\n var item=this;\n var data=this.data;\n var instance=this.token.execution.instance;\n var input=this.input;\n var output=this.output;\n var appDelegate=this.token.execution.appDelegate;\n var appServices=this.token.execution.servicesProvider;\n var appUtils=appDelegate.appUtils;\n "
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:291
runPython#
▸ runPython(item, code, input?): Promise\<any>
Parameters#
| Name | Type |
|---|---|
item |
any |
code |
string |
input |
any |
Returns#
Promise\<any>
Inherited from#
Defined in#
packages/bpmn-server/src/engine/ScriptHandler.ts:333