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
When the template or data context cannot be trusted, enabling DoS prevention options is crucial. LiquidJS provides 3 options for this purpose: `parseLimit`, `renderLimit`, and `memoryLimit`.
6
+
7
+
## TL;DR
8
+
9
+
Setting these options can largely ensure that your LiquidJS instance won't hang for extended periods or consume excessive memory. These limits are based on the available JavaScript APIs, so they are not precise hard limits but thresholds to help prevent your process from failing or hanging.
10
+
11
+
```typescript
12
+
const liquid =newLiquid({
13
+
parseLimit: 1e8, // typical size of your templates in each render
14
+
renderLimit: 1000, // limit each render to be completed in 1s
15
+
memoryLimit: 1e9, // memory available for LiquidJS (1e9 for 1GB)
16
+
})
17
+
```
18
+
19
+
When a `parse()` or `render()` cannot be completed within given resource, it throws.
20
+
21
+
## parseLimit
22
+
23
+
[parseLimit][parseLimit] restricts the size (character length) of templates parsed in each `.parse()` call, including referenced partials and layouts. Since LiquidJS parses template strings in near O(n) time, limiting total template length is usually sufficient.
24
+
25
+
A typical PC handles `1e8` (100M) characters without issues.
26
+
27
+
## renderLimit
28
+
29
+
Restricting template size alone is insufficient because dynamic loops with large counts can occur in render time. [renderLimit][renderLimit] mitigates this by limiting the time consumed by each `render()` call.
30
+
31
+
```liquid
32
+
{%- for i in (1..10000000) -%}
33
+
order: {{i}}
34
+
{%- endfor -%}
35
+
```
36
+
37
+
Render time is checked on a per-template basis (before rendering each template). In the above example, there are 2 templates in the loop: `order: ` and `{{i}}`, render time will be checked 10000000x2 times.
38
+
39
+
For time-consuming tags and filters within a single template, the process can still hang. For fully controlled rendering, consider using a process manager like [paralleljs][paralleljs].
40
+
41
+
## memoryLimit
42
+
43
+
Even with small number of templates and iterations, memory usage can grow exponentially. In the following example, memory doubles with each iteration:
44
+
45
+
```liquid
46
+
{% assign array = "1,2,3" | split: "," %}
47
+
{% for i in (1..32) %}
48
+
{% assign array = array | concat: array %}
49
+
{% endfor %}
50
+
```
51
+
52
+
[memoryLimit][memoryLimit] restricts memory-sensitive filters to prevent excessive memory allocation. As [JavaScript uses GC to manage memory](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_management), `memoryLimit` limits only the total number of objects allocated by memory sensitive filters in LiquidJS thus may not reflect the actual memory footprint.
0 commit comments