You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/embedding-extending.md
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,12 @@ sidebar_label: Embedding and Extending JSONata
6
6
7
7
## API
8
8
9
-
### jsonata(str)
9
+
### jsonata(str[, options])
10
10
11
11
Parse a string `str` as a JSONata expression and return a compiled JSONata expression object.
12
12
13
+
`options`, if present, is used to control certain aspects of the evaluator, and can be used to protect the server from expressions that take longer to execute than expected. See [Configuring Guardrails](guardrails) for more details.
This page contains information relating to the JavaScript [reference implementation](https://github.com/jsonata-js/jsonata) of JSONata, and not the JSONata expression language itself.
10
+
11
+
JSONata is a Turing-complete expression language, and as such, it is possible to write unbounded, or infinite loops. This can be a potential problem if an application using JSONata is exposing the ability for client users to input expressions that are evaluated on the server. A user could accidently or maliciously provide an expression that, if evaluated unchecked, could cause a denial of service situation.
12
+
13
+
This JSONata library provides a set of configurable 'guardrails' that limit the compute and memory resources that a single expression can consume. If this library is being used in a hosted environment to allow end users to provide their own expressions, then it would be prudent to set constraints. The following sections describe each of the guardrails and how to configure them. It does not provide recommended values or defaults.
14
+
15
+
### Stack overflow
16
+
17
+
In common with other functional languages, JSONata supports looping by writing [recursive functions](https://en.wikipedia.org/wiki/Functional_programming#Recursion). The JSONata evaluator processes an expression using a set of mutually recursive functions (eval-apply cycle). When a function is invoked (by itself or by another function), the call stack in the host JavaScript runtime will grow. If this stack grows too deep, evaluator could exhaust the memory of the host process causing it to crash.
18
+
19
+
The JSONata evaluator can be configured with a maximum stack[^stack] limit to prevent an expression from doing this by specifying the `stack` option. Error `D1011` will be thrown if the expression grows the stack beyond the specified limit.
As an example, the [Ackermann function](https://en.wikipedia.org/wiki/Ackermann_function) could be implemented in JSONata using:
37
+
38
+
```
39
+
(
40
+
$ack := function($m, $n) {
41
+
$m = 0 ? $n + 1 :
42
+
$n = 0 ? $ack($m - 1, 1) :
43
+
$ack($m - 1, $ack($m, $n - 1))
44
+
};
45
+
46
+
$ack(3, 4)
47
+
)
48
+
```
49
+
50
+
Invoked as `$ack(3, 4)` would quickly evaluate to `125`. However, `$ack(4, 3)`, although theoretically computable, will readily hit the configured stack guardrail before causing any problems to the host server.
51
+
52
+
[^stack]: The term 'stack' is a slight misnomer here; it actually limits the number of times round the eval-apply cycle, which is related to the JavaScript stack depth.
53
+
54
+
### Excessive execution time
55
+
56
+
It's possible (and desirable) to write [tail recursive](programming#tail-call-optimization-tail-recursion) functions that don't grow the stack at all. For these types of functions, a [stack guardrail](#stack-overflow) would not be sufficient to protect against unbounded loops.
57
+
58
+
The JSONata evaluator can be configured with a maximum time limit to protect against runaway expressions by specifying the `timeout` option. Error `D1012` will be thrown if the expression runs for longer than the specified timeout (in milliseconds).
59
+
60
+
It's good practice to specify both `stack` and `timeout`.
As an example, an infinite loop could be written in JSONata:
78
+
79
+
```
80
+
(
81
+
$inf := function() {
82
+
$inf()
83
+
};
84
+
85
+
$inf()
86
+
)
87
+
```
88
+
89
+
This is tail recursive, and would run forever without the timeout guardrail.
90
+
91
+
### Excessive sequence length
92
+
93
+
It's possible to write expressions that result in excessively long result sequences. This could ultimately lead to memory exhaustion in the host server. The `sequence` option can be set to specify the maximum sequence length that can be created by an expression, including any intermediate sequences created by sub-expressions. Error `D2015` will be thrown if, during the evaluation of an expression, the evaluator attempts to generate a sequence exceeding this upper limit.
94
+
95
+
96
+
```javascript
97
+
constjsonata=require('jsonata');
98
+
99
+
constdata= {JSON: data};
100
+
constoptions= {
101
+
sequence:1e6// maximum of one million items in a sequence
As an example, the following JSONata expression attempts to generate a sequence of 100 million numbers. The guardrail configured above would prevent this.
111
+
112
+
```
113
+
[1..10000].([1..10000])
114
+
```
115
+
116
+
### Rogue regular expressions
117
+
118
+
A number of functions use [regular expressions](regex) to process strings. Alongside the power and flexibility that regexes provide, there are situations whereby badly crafted or malicious expressions could cause the processing engine take an [excessive amount of time](https://en.wikipedia.org/wiki/ReDoS) (exponential to the input string length). Since the regex processing is not implemented in the core JSONata (eval-apply) evaluator, the `timeout` guardrail cannot protect against this.
119
+
120
+
It is possible to specify which regex processor is invoked by the JSONata evaluator. This is configured using the `RegexEngine` option. When this is not set, the evaluator will use the default JavaScript [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) class.
121
+
122
+
The [packaged version of JSONata](https://www.npmjs.com/package/jsonata) has no runtime dependencies on other packages, but it is possible to use the `RegexEngine` option to invoke a third-party ReDoS library whenever a regular expression is encountered in a JSONata expression.
123
+
124
+
The following code shows how this is done using the [redos-detector](https://github.com/tjenkinson/redos-detector) module:
125
+
126
+
```javascript
127
+
constjsonata=require('jsonata');
128
+
constredos=require('redos-detector');
129
+
130
+
// Simple wrapper that invokes redos-detector before delegating
Other similar libraries are available. This is not an endorsement of any particular one. The developer should choose one according to their requirements.
0 commit comments