Documentation
¶
Index ¶
- Constants
- Variables
- func CachedFingerprint(src string) (string, bool)
- func Check(src string) error
- func Compile(src string) (*goja.Program, error)
- func Fingerprint(src string) string
- func JoinParts(prefix string, inputs map[string]any) string
- func LenientJsonParse(vm *goja.Runtime) error
- func NewRuntime() *goja.Runtime
- func Normalize(value any, maxBytes int) (any, error)
- func SetFingerprintMode(mode string) error
- func Truthy(value any) (bool, error)
- type Assignment
- type Blocklist
- type Error
- type Result
- type Sandbox
Constants ¶
const ( // FingerprintText compares the text without // comments: fast, but another indentation or // spacing is another script FingerprintText = "text" // FingerprintTree compares the syntax tree goja parses: copies that differ in any formatting // match, at the cost of a parse per script FingerprintTree = "tree" )
The ways to compute a fingerprint (SCRIPT_FINGERPRINT).
const MaxDepth = 5000
MaxDepth is the deepest syntax tree a script may have. goja's compiler recurses over the tree without a limit, and its parser limits nesting, not a chain like a+a+...+a: a few million terms overflow the stack of the goroutine, which ends the process, also when a model is only validated. Compiling a chain takes time quadratic in its length besides.
Variables ¶
var ErrBlocked = errors.New("the script is blocked")
ErrBlocked is the error of a script on the blocklist.
var ErrTimeout = errors.New("script execution timeout")
var ErrTooDeep = fmt.Errorf("the script is nested more than %v levels deep", MaxDepth)
ErrTooDeep refuses a script whose syntax tree is deeper than MaxDepth.
var GojaPatches = goja.EnginePatches()
GojaPatches are the patches the engine applies to goja (third_party/goja-patches). Referring to them keeps the engine from building with goja unpatched, e.g. without the replace in go.mod.
var MaxNativeDepth int
MaxNativeDepth bounds the recursion of goja's own Go code over values a script builds (goja.SetMaxNativeDepth); zero is goja's default. Past it, the run ends with an error instead of a stack overflow that ends the process. Tests that need such a crash raise it.
Functions ¶
func CachedFingerprint ¶ added in v0.31.0
CachedFingerprint returns the fingerprint of src if it is known already, without computing it.
func Compile ¶ added in v0.31.0
Compile compiles a script as goja.Compile, but without following a sourceMappingURL comment: goja would read the file it names before the script runs, without a limit, which makes file:///dev/zero end the process when its memory runs out, and before the crash brake knows. A script deeper than MaxDepth is refused before goja's compiler recurses over it.
The result is cached: a program is bound to no runtime and runs in several at once, so that a script is parsed and compiled once, not at every run. With tree fingerprints, the walk over the parsed script that measures the depth computes the fingerprint as well, which the blocklist then finds cached.
func Fingerprint ¶ added in v0.31.0
Fingerprint identifies a script for the blocklist (EXPR-03), in the mode SetFingerprintMode chose: by its text without // comments (textFingerprint) or by its syntax tree (treeFingerprint). It is cached by a 64-bit hash of the source, not by the source, which a module script makes kilobytes long.
func JoinParts ¶ added in v0.31.0
JoinParts joins the string inputs whose name starts with prefix, in the order of their names, as the pre- and postscripts of module tasks run (TASK-28).
func LenientJsonParse ¶ added in v0.7.1
LenientJsonParse makes JSON.parse return a value that is no string as it is. Camunda handed some variables over as JSON text that the engine keeps as objects, e.g. the value of a device event, and models parse them; parsing an object would read "[object Object]".
func NewRuntime ¶ added in v0.31.0
NewRuntime returns a goja runtime that compiles, also for eval and new Function, as Compile, and bounds its native recursion by MaxNativeDepth.
func Normalize ¶
Normalize converts a Go value into its JSON form and enforces maxBytes if positive.
func SetFingerprintMode ¶ added in v0.31.0
SetFingerprintMode chooses how Fingerprint compares scripts, FingerprintText or FingerprintTree; empty is FingerprintText. It is set once at the start, before any script is fingerprinted: entries of the blocklist computed in the other mode match only through their source.
Types ¶
type Assignment ¶
type Blocklist ¶ added in v0.31.0
type Blocklist struct {
// contains filtered or unexported fields
}
Blocklist holds the fingerprints of blocked scripts with the reason (EXPR-03). It is safe for concurrent use; a nil Blocklist blocks nothing, and an empty one computes no fingerprint.
func NewBlocklist ¶ added in v0.31.0
func NewBlocklist() *Blocklist
func (*Blocklist) Blocked ¶ added in v0.31.0
Blocked returns the reason a script is blocked for, if it is.
type Result ¶
type Result struct {
// JSON compatible value: nil, bool, float64, string, []any or map[string]any (EXPR-05)
Value any
Logs []string
Assignments []Assignment
}
type Sandbox ¶
type Sandbox struct {
Timeout time.Duration
MaxResultBytes int
MaxLogEntries int
MaxCallStack int
// scripts that ended the engine; nil blocks nothing (EXPR-03)
Blocklist *Blocklist
}
Sandbox evaluates JavaScript, the only expression and script language of the engine (EXPR-01). Scripts see the variables as globals plus log(...) and setVariable(name, value) (EXPR-04). They have no access to engine internals, the file system or the network, because goja provides none of these by default and nothing else is registered (EXPR-03).
goja has no memory limit. The timeout and the result size limit bound what a script can do, but a script can allocate memory until the timeout interrupts it.
func NewSandbox ¶
func NewSandbox() *Sandbox