From 0d2f0f1ea72b97d9968f31085a754baef167777b Mon Sep 17 00:00:00 2001 From: Yang Jun Date: Sat, 11 Jul 2026 15:29:52 +0800 Subject: [PATCH 01/10] feat: remove memoryLimit option (#910) Co-authored-by: Cursor --- docs/source/tutorials/security-model.md | 30 +++------ src/context/context.ts | 7 +-- src/filters/array.ts | 18 ------ src/filters/base64.ts | 3 - src/filters/crypto.ts | 2 - src/filters/date.ts | 12 ++-- src/filters/html.ts | 4 -- src/filters/misc.ts | 84 +++++++++---------------- src/filters/string.ts | 27 -------- src/liquid-options.ts | 6 -- src/render/expression.ts | 1 - src/util/strftime.ts | 12 ++-- test/e2e/issues.spec.ts | 7 --- test/integration/filters/date.spec.ts | 26 +------- test/integration/liquid/dos.spec.ts | 81 +----------------------- 15 files changed, 51 insertions(+), 269 deletions(-) diff --git a/docs/source/tutorials/security-model.md b/docs/source/tutorials/security-model.md index 5d65a01967..ff35d25714 100644 --- a/docs/source/tutorials/security-model.md +++ b/docs/source/tutorials/security-model.md @@ -2,7 +2,7 @@ title: Security Model --- -LiquidJS provides DoS-oriented limits (`parseLimit`, `renderLimit`, `memoryLimit`) to reduce risk. This page summarizes those limits, [`ownPropertyOnly`][ownPropertyOnly], custom [`Drop`][drop] usage, and the security boundary to assume in production. +LiquidJS provides DoS-oriented limits (`parseLimit`, `renderLimit`) to reduce risk. This page summarizes those limits, [`ownPropertyOnly`][ownPropertyOnly], custom [`Drop`][drop] usage, and the security boundary to assume in production. ## Security boundary @@ -12,11 +12,14 @@ The built-in limits are cooperative safeguards, not strict runtime isolation. - They do **not** sandbox JavaScript execution. - They should be combined with process/container limits and request timeouts for defense in depth. +LiquidJS does **not** enforce memory or CPU budgets inside the engine. Major template engines take the same approach: byte-level heap tracking is unreliable in garbage-collected runtimes (non-deterministic GC, accounting overhead) and does not map cleanly to real process memory. [Jinja2](https://jinja.palletsprojects.com/en/stable/sandbox/) relies on `sys.setrecursionlimit`, `SandboxedEnvironment` for access control, and advises OS/process limits (`ulimit`, cgroups). [Twig](https://twig.symfony.com/doc/3.x/api.html#security-policy) documents a `SecurityPolicy` for tags/filters/methods and explicitly leaves resource limits to PHP (`memory_limit`, execution timeouts). Handlebars and EJS provide no render budgets; Node.js users typically combine [`vm.Script` timeouts](https://nodejs.org/api/vm.html) or [`worker_threads`](https://nodejs.org/api/worker_threads.html) with process isolation for untrusted templates. + +For LiquidJS in production, prefer **external** controls: Node.js `vm` or worker threads (or packages such as [`isolated-vm`](https://www.npmjs.com/package/isolated-vm) when stronger isolation is required), separate processes or containers, OS/container memory and CPU quotas, and request timeouts — not in-engine heap tracking. + ## Limits at a glance - [parseLimit][parseLimit]: limit total template size per `parse()` call. - [renderLimit][renderLimit]: limit total render time per `render()` call. -- [memoryLimit][memoryLimit]: cooperatively limit memory-sensitive allocations counted by LiquidJS. ## Limit details @@ -40,25 +43,7 @@ Render time is checked on a per-template basis (before rendering each template). `renderLimit` is not a hard CPU limiter. It is checked between template renders, so compute-intensive filters/tags/user-defined functions or deeply nested template execution between checks can still cause DoS. -### memoryLimit - -`memoryLimit` only limits operations that LiquidJS explicitly counts. - -- Counted: memory-sensitive LiquidJS operations that call internal memory accounting. -- Not guaranteed counted: arbitrary user object behavior such as custom `toValue()`/`toString()` chains, or other host-side code that allocates outside LiquidJS accounting points. - -In other words, `memoryLimit` limits what LiquidJS counts, not every byte your process may allocate. - -Even with a small number of templates and iterations, memory usage can grow exponentially. In the following example, memory doubles with each iteration: - -```liquid -{% assign array = "1,2,3" | split: "," %} -{% for i in (1..32) %} - {% assign array = array | concat: array %} -{% endfor %} -``` - -As [JavaScript uses GC to manage memory](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_management), `memoryLimit` may not reflect the actual memory footprint. +Memory-heavy templates (for example exponential `concat` in a loop) are not capped by LiquidJS. Mitigate them with process/container memory limits, output size checks after render, or template restrictions — the same pattern Jinja2 and Twig recommend for heap and CPU. ## `ownPropertyOnly` and scope data @@ -74,14 +59,13 @@ If you run an online service, avoid rendering fully user-defined templates whene - Prefer curated templates or a restricted template subset. - If user-defined templates are required, isolate rendering (worker/process/container), enforce OS/container memory and CPU limits, and apply request rate limits. -- Treat `parseLimit`/`renderLimit`/`memoryLimit` as one layer in a broader DoS defense strategy. +- Treat `parseLimit` and `renderLimit` as one layer in a broader DoS defense strategy. For heavy single-template operations, process-level isolation is still recommended (for example with [paralleljs][paralleljs]). [paralleljs]: https://www.npmjs.com/package/paralleljs [parseLimit]: /api/interfaces/LiquidOptions.html#parseLimit [renderLimit]: /api/interfaces/LiquidOptions.html#renderLimit -[memoryLimit]: /api/interfaces/LiquidOptions.html#memoryLimit [ownPropertyOnly]: /api/interfaces/LiquidOptions.html#ownPropertyOnly [renderOwnPropertyOnly]: /api/interfaces/RenderOptions.html#ownPropertyOnly [strictVariables]: /api/interfaces/LiquidOptions.html#strictVariables diff --git a/src/context/context.ts b/src/context/context.ts index 9dcb711ea7..19b36d522b 100644 --- a/src/context/context.ts +++ b/src/context/context.ts @@ -36,16 +36,14 @@ export class Context { */ public strictVariables: boolean; public ownPropertyOnly: boolean; - public memoryLimit: Limiter; public renderLimit: Limiter; - public constructor (env: object = {}, opts: NormalizedFullOptions = defaultOptions, renderOptions: RenderOptions = {}, { memoryLimit, renderLimit }: { [key: string]: Limiter } = {}) { + public constructor (env: object = {}, opts: NormalizedFullOptions = defaultOptions, renderOptions: RenderOptions = {}, { renderLimit }: { renderLimit?: Limiter } = {}) { this.sync = !!renderOptions.sync this.opts = opts this.globals = renderOptions.globals ?? opts.globals this.environments = isObject(env) ? env : Object(env) this.strictVariables = renderOptions.strictVariables ?? this.opts.strictVariables this.ownPropertyOnly = renderOptions.ownPropertyOnly ?? opts.ownPropertyOnly - this.memoryLimit = memoryLimit ?? new Limiter('memory alloc', renderOptions.memoryLimit ?? opts.memoryLimit) this.renderLimit = renderLimit ?? new Limiter('template render', getPerformance().now() + (renderOptions.renderLimit ?? opts.renderLimit)) } public getRegister (key: string, defaultValue: T = undefined as T): T { @@ -109,8 +107,7 @@ export class Context { strictVariables: this.strictVariables, ownPropertyOnly: this.ownPropertyOnly }, { - renderLimit: this.renderLimit, - memoryLimit: this.memoryLimit + renderLimit: this.renderLimit }) } private findScope (key: string | number) { diff --git a/src/filters/array.ts b/src/filters/array.ts index bf16b59511..2a5cb8a0e0 100644 --- a/src/filters/array.ts +++ b/src/filters/array.ts @@ -8,9 +8,6 @@ import { EmptyDrop } from '../drop' export const join = argumentsToValue(function (this: FilterImpl, v: any[], arg: string) { const array = toArray(v) const sep = isNil(arg) ? ' ' : stringify(arg) - let outputSize = sep.length * Math.max(array.length - 1, 0) - for (let i = 0; i < array.length; i++) outputSize += String(array[i]).length - this.context.memoryLimit.use(outputSize) return Array.prototype.join.call(array, sep) }) export const last = argumentsToValue(function (this: FilterImpl, v: any) { @@ -21,14 +18,12 @@ export const first = argumentsToValue(function (this: FilterImpl, v: any) { }) export const reverse = argumentsToValue(function (this: FilterImpl, v: any[]) { const array = toArray(v) - this.context.memoryLimit.use(array.length) return [...array].reverse() }) function * sortBy (this: FilterImpl, arr: T[], property: string | undefined, comparator: (a: unknown, b: unknown) => number): IterableIterator { const values: [T, unknown][] = [] const array = toArray(arr) - this.context.memoryLimit.use(array.length) for (const item of array) { values.push([ item, @@ -51,7 +46,6 @@ export const size = (v: string | any[]) => v?.length || 0 export function * map (this: FilterImpl, arr: Scope[], property: string): IterableIterator { const results = [] const array = toArray(arr) - this.context.memoryLimit.use(array.length) for (const item of array) { results.push(yield this.context._getFromScope(item, stringify(property), false)) } @@ -70,14 +64,12 @@ export function * sum (this: FilterImpl, arr: Scope[], property?: string): Itera export function compact (this: FilterImpl, arr: T[]) { const array = toArray(arr) - this.context.memoryLimit.use(array.length) return Array.prototype.filter.call(array, x => !isNil(toValue(x))) } export function concat (this: FilterImpl, v: T1[], arg: T2[] = []): (T1 | T2)[] { const lhs = toArray(v) const rhs = toArray(arg) - this.context.memoryLimit.use(lhs.length + rhs.length) return Array.prototype.concat.call(lhs, rhs) } @@ -87,7 +79,6 @@ export function push (this: FilterImpl, v: T[], arg: T): T[] { export function unshift (this: FilterImpl, v: T[], arg: T): T[] { const array = toArray(v) - this.context.memoryLimit.use(array.length) const clone = [...array] clone.unshift(arg) return clone @@ -95,7 +86,6 @@ export function unshift (this: FilterImpl, v: T[], arg: T): T[] { export function pop (this: FilterImpl, v: T[]): T[] { const array = toArray(v) - this.context.memoryLimit.use(array.length) const clone = [...array] clone.pop() return clone @@ -103,7 +93,6 @@ export function pop (this: FilterImpl, v: T[]): T[] { export function shift (this: FilterImpl, v: T[]): T[] { const array = toArray(v) - this.context.memoryLimit.use(array.length) const clone = [...array] clone.shift() return clone @@ -114,7 +103,6 @@ export function slice (this: FilterImpl, v: T[] | string, begin: number, leng if (isNil(v)) return [] if (!isArray(v)) v = stringify(v) begin = begin < 0 ? v.length + begin : begin - this.context.memoryLimit.use(length) return isArray(v) ? Array.prototype.slice.call(v, begin, begin + length) : String.prototype.slice.call(v, begin, begin + length) @@ -133,7 +121,6 @@ function expectedMatcher (this: FilterImpl, expected: any): (v: any) => boolean function * filter (this: FilterImpl, include: boolean, arr: T[], property: string, expected: any): IterableIterator { const values: unknown[] = [] arr = toArray(arr) - this.context.memoryLimit.use(arr.length) const token = new Tokenizer(stringify(property)).readScopeValue() for (const item of arr) { values.push(yield evalToken(token, this.context.spawn(item))) @@ -146,7 +133,6 @@ function * filter_exp (this: FilterImpl, include: boolean, arr const filtered: unknown[] = [] const keyTemplate = new Value(stringify(exp), this.liquid) const array = toArray(arr) - this.context.memoryLimit.use(array.length) for (const item of array) { this.context.push({ [itemName]: item }) const value = yield keyTemplate.value(this.context) @@ -176,7 +162,6 @@ export function * group_by (this: FilterImpl, arr: T[], proper const map = new Map() arr = toEnumerable(arr) const token = new Tokenizer(stringify(property)).readScopeValue() - this.context.memoryLimit.use(arr.length) for (const item of arr) { const key = yield evalToken(token, this.context.spawn(item)) if (!map.has(key)) map.set(key, []) @@ -189,7 +174,6 @@ export function * group_by_exp (this: FilterImpl, arr: T[], it const map = new Map() const keyTemplate = new Value(stringify(exp), this.liquid) arr = toEnumerable(arr) - this.context.memoryLimit.use(arr.length) for (const item of arr) { this.context.push({ [itemName]: item }) const key = yield keyTemplate.value(this.context) @@ -253,7 +237,6 @@ export function * find_exp (this: FilterImpl, arr: T[], itemNa export function uniq (this: FilterImpl, arr: T[]): T[] { arr = toArray(arr) - this.context.memoryLimit.use(arr.length) return [...new Set(arr)] } @@ -261,7 +244,6 @@ export function sample (this: FilterImpl, v: T[] | string, count = 1): T | st v = toValue(v) if (isNil(v)) return [] if (!isArray(v)) v = stringify(v) - this.context.memoryLimit.use(v.length) const shuffled = [...v].sort(() => Math.random() - 0.5) if (count === 1) return shuffled[0] return shuffled.slice(0, count) diff --git a/src/filters/base64.ts b/src/filters/base64.ts index 29ef84e52c..5324c74732 100644 --- a/src/filters/base64.ts +++ b/src/filters/base64.ts @@ -10,16 +10,13 @@ import { base64Encode, base64Decode } from './base64-impl' export function base64_encode (this: FilterImpl, value: string | Buffer): string { if (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) { - this.context.memoryLimit.use(value.byteLength) return value.toString('base64') } const str = stringify(value) - this.context.memoryLimit.use(str.length) return base64Encode(str) } export function base64_decode (this: FilterImpl, value: string): string { const str = stringify(value) - this.context.memoryLimit.use(str.length) return base64Decode(str) } diff --git a/src/filters/crypto.ts b/src/filters/crypto.ts index 7210523bde..26caebd267 100644 --- a/src/filters/crypto.ts +++ b/src/filters/crypto.ts @@ -10,13 +10,11 @@ import { sha256 as sha256Impl, hmacSha256 as hmacSha256Impl } from './crypto-imp export function sha256 (this: FilterImpl, value: unknown): string | Promise { const str = stringify(value) - this.context.memoryLimit.use(str.length) return sha256Impl(str) } export function hmac_sha256 (this: FilterImpl, value: unknown, key: unknown): string | Promise { const str = stringify(value) const keyStr = stringify(key) - this.context.memoryLimit.use(str.length + keyStr.length) return hmacSha256Impl(str, keyStr) } diff --git a/src/filters/date.ts b/src/filters/date.ts index 07665c9579..e2567421b1 100644 --- a/src/filters/date.ts +++ b/src/filters/date.ts @@ -3,14 +3,11 @@ import { FilterImpl } from '../template' import { NormalizedFullOptions } from '../liquid-options' export function date (this: FilterImpl, v: string | Date, format?: string, timezoneOffset?: number | string) { - const size = ((v as string)?.length ?? 0) + ((timezoneOffset as string)?.length ?? 0) - this.context.memoryLimit.use(size) const date = parseDate(v, this.context.opts, timezoneOffset) if (!date) return v format = toValue(format) format = isNil(format) ? this.context.opts.dateFormat : stringify(format) - this.context.memoryLimit.use(format.length) - return strftime(date, format, this.context.memoryLimit) + return strftime(date, format) } export function date_to_xmlschema (this: FilterImpl, v: string | Date) { @@ -32,14 +29,13 @@ export function date_to_long_string (this: FilterImpl, v: string | Date, type?: function stringify_date (this: FilterImpl, v: string | Date, month_type: string, type?: string, style?: string) { const date = parseDate(v, this.context.opts) if (!date) return v - const ml = this.context.memoryLimit if (type === 'ordinal') { const d = date.getDate() return style === 'US' - ? strftime(date, `${month_type} ${d}%q, %Y`, ml) - : strftime(date, `${d}%q ${month_type} %Y`, ml) + ? strftime(date, `${month_type} ${d}%q, %Y`) + : strftime(date, `${d}%q ${month_type} %Y`) } - return strftime(date, `%d ${month_type} %Y`, ml) + return strftime(date, `%d ${month_type} %Y`) } function parseDate (v: string | Date, opts: NormalizedFullOptions, timezoneOffset?: number | string): LiquidDate | undefined { diff --git a/src/filters/html.ts b/src/filters/html.ts index b52f1b3da2..01cd67d654 100644 --- a/src/filters/html.ts +++ b/src/filters/html.ts @@ -18,7 +18,6 @@ const unescapeMap: Record = { export function escape (this: FilterImpl, str: string) { str = stringify(str) - this.context.memoryLimit.use(str.length) return str.replace(/&|<|>|"|'/g, m => escapeMap[m]) } @@ -28,7 +27,6 @@ export function xml_escape (this: FilterImpl, str: string) { function unescape (this: FilterImpl, str: string) { str = stringify(str) - this.context.memoryLimit.use(str.length) return str.replace(/&(amp|lt|gt|#34|#39);/g, m => unescapeMap[m]) } @@ -38,7 +36,6 @@ export function escape_once (this: FilterImpl, str: string) { export function newline_to_br (this: FilterImpl, v: string) { const str = stringify(v) - this.context.memoryLimit.use(str.length) return str.replace(/\r?\n/gm, '
\n') } @@ -46,7 +43,6 @@ export function newline_to_br (this: FilterImpl, v: string) { // equivalent is O(n^2) in V8 on unclosed openers. export function strip_html (this: FilterImpl, v: string) { const str = stringify(v) - this.context.memoryLimit.use(str.length) const blocks = new Map([[''], [''], [''], ['<', '>']]) let out = '' let i = 0 diff --git a/src/filters/misc.ts b/src/filters/misc.ts index 4473765508..33f4289534 100644 --- a/src/filters/misc.ts +++ b/src/filters/misc.ts @@ -1,68 +1,42 @@ -import { isFalsy } from '../render/boolean' -import { identify, isArray, isString, toValue } from '../util/underscore' -import { FilterImpl } from '../template' - -function chargeJsonReplacerValue (memoryLimit: { use(count: number): void }, val: unknown) { - if (typeof val === 'string') { - memoryLimit.use(val.length) - } else if (val === null || typeof val === 'number' || typeof val === 'boolean') { - memoryLimit.use(JSON.stringify(val).length) - } else if (Array.isArray(val)) { - memoryLimit.use(val.length + 1) - } else if (typeof val === 'object') { - memoryLimit.use(2) - } -} - +import { isFalsy } from '../render/boolean' +import { identify, isArray, isString, toValue } from '../util/underscore' +import { FilterImpl } from '../template' function defaultFilter (this: FilterImpl, value: T1, defaultValue: T2, ...args: Array<[string, any]>): T1 | T2 { - value = toValue(value) - if (isArray(value) || isString(value)) return value.length ? value : defaultValue - if (value === false && (new Map(args)).get('allow_false')) return false as T1 + value = toValue(value) + if (isArray(value) || isString(value)) return value.length ? value : defaultValue + if (value === false && (new Map(args)).get('allow_false')) return false as T1 return isFalsy(value, this.context) ? defaultValue : value -} - +} function json (this: FilterImpl, value: any, space = 0) { - const memoryLimit = this.context.memoryLimit - return JSON.stringify(value, (_key, val) => { - chargeJsonReplacerValue(memoryLimit, val) - return val - }, space) -} - + return JSON.stringify(value, undefined, space) +} function inspect (this: FilterImpl, value: any, space = 0) { - const memoryLimit = this.context.memoryLimit - const ancestors: object[] = [] + const ancestors: object[] = [] return JSON.stringify(value, function (this: unknown, _key: unknown, value: any) { if (typeof value !== 'object' || value === null) { - chargeJsonReplacerValue(memoryLimit, value) return value - } - // `this` is the object that value is contained in, i.e., its direct parent. - while (ancestors.length > 0 && ancestors[ancestors.length - 1] !== this) ancestors.pop() + } + // `this` is the object that value is contained in, i.e., its direct parent. + while (ancestors.length > 0 && ancestors[ancestors.length - 1] !== this) ancestors.pop() if (ancestors.includes(value)) { - memoryLimit.use('[Circular]'.length) return '[Circular]' - } - ancestors.push(value) - chargeJsonReplacerValue(memoryLimit, value) + } + ancestors.push(value) return value }, space) -} - +} function to_integer (value: any) { return Number(value) -} - -const raw = { - raw: true, - handler: identify -} - -export default { - default: defaultFilter, - raw, - jsonify: json, - to_integer, - json, - inspect -} +} +const raw = { + raw: true, + handler: identify +} +export default { + default: defaultFilter, + raw, + jsonify: json, + to_integer, + json, + inspect +} diff --git a/src/filters/string.ts b/src/filters/string.ts index d0c59708ed..3d19ace6a2 100644 --- a/src/filters/string.ts +++ b/src/filters/string.ts @@ -22,7 +22,6 @@ export function append (this: FilterImpl, v: string, arg: string) { assert(arguments.length === 2, 'append expect 2 arguments') const lhs = stringify(v) const rhs = stringify(arg) - this.context.memoryLimit.use(lhs.length + rhs.length) return lhs + rhs } @@ -30,16 +29,13 @@ export function prepend (this: FilterImpl, v: string, arg: string) { assert(arguments.length === 2, 'prepend expect 2 arguments') const lhs = stringify(v) const rhs = stringify(arg) - this.context.memoryLimit.use(lhs.length + rhs.length) return rhs + lhs } export function lstrip (this: FilterImpl, v: string, chars?: string) { const str = stringify(v) - this.context.memoryLimit.use(str.length) if (chars) { chars = stringify(chars) - this.context.memoryLimit.use(chars.length) for (let i = 0, set = new Set(chars); i < str.length; i++) { if (!set.has(str[i])) return str.slice(i) } @@ -50,34 +46,29 @@ export function lstrip (this: FilterImpl, v: string, chars?: string) { export function downcase (this: FilterImpl, v: string) { const str = stringify(v) - this.context.memoryLimit.use(str.length) return str.toLowerCase() } export function upcase (this: FilterImpl, v: string) { const str = stringify(v) - this.context.memoryLimit.use(str.length) return stringify(str).toUpperCase() } export function remove (this: FilterImpl, v: string, arg: string) { const str = stringify(v) arg = stringify(arg) - this.context.memoryLimit.use(str.length + arg.length) return str.split(arg).join('') } export function remove_first (this: FilterImpl, v: string, l: string) { const str = stringify(v) l = stringify(l) - this.context.memoryLimit.use(str.length + l.length) return str.replace(l, '') } export function remove_last (this: FilterImpl, v: string, l: string) { const str = stringify(v) const pattern = stringify(l) - this.context.memoryLimit.use(str.length + pattern.length) const index = str.lastIndexOf(pattern) if (index === -1) return str return str.substring(0, index) + str.substring(index + pattern.length) @@ -85,10 +76,8 @@ export function remove_last (this: FilterImpl, v: string, l: string) { export function rstrip (this: FilterImpl, str: string, chars?: string) { str = stringify(str) - this.context.memoryLimit.use(str.length) if (chars) { chars = stringify(chars) - this.context.memoryLimit.use(chars.length) for (let i = str.length - 1, set = new Set(chars); i >= 0; i--) { if (!set.has(str[i])) return str.slice(0, i + 1) } @@ -99,7 +88,6 @@ export function rstrip (this: FilterImpl, str: string, chars?: string) { export function split (this: FilterImpl, v: string, arg: string) { const str = stringify(v) - this.context.memoryLimit.use(str.length) const arr = str.split(stringify(arg)) // align to ruby split, which is the behavior of shopify/liquid // see: https://ruby-doc.org/core-2.4.0/String.html#method-i-split @@ -109,10 +97,8 @@ export function split (this: FilterImpl, v: string, arg: string) { export function strip (this: FilterImpl, v: string, chars?: string) { const str = stringify(v) - this.context.memoryLimit.use(str.length) if (chars) { const set = new Set(stringify(chars)) - this.context.memoryLimit.use(set.size) let i = 0 let j = str.length - 1 while (set.has(str[i])) i++ @@ -124,13 +110,11 @@ export function strip (this: FilterImpl, v: string, chars?: string) { export function strip_newlines (this: FilterImpl, v: string) { const str = stringify(v) - this.context.memoryLimit.use(str.length) return str.replace(/\r?\n/gm, '') } export function capitalize (this: FilterImpl, str: string) { str = stringify(str) - this.context.memoryLimit.use(str.length) return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase() } @@ -139,8 +123,6 @@ export function replace (this: FilterImpl, v: string, pattern: string, replaceme pattern = stringify(pattern) replacement = stringify(replacement) const parts = str.split(pattern) - const outputSize = str.length + (parts.length - 1) * (replacement.length - pattern.length) - this.context.memoryLimit.use(outputSize) return parts.join(replacement) } @@ -148,7 +130,6 @@ export function replace_first (this: FilterImpl, v: string, arg1: string, arg2: const str = stringify(v) arg1 = stringify(arg1) arg2 = stringify(arg2) - this.context.memoryLimit.use(str.length + arg1.length + arg2.length) return str.replace(arg1, () => arg2) } @@ -156,7 +137,6 @@ export function replace_last (this: FilterImpl, v: string, arg1: string, arg2: s const str = stringify(v) const pattern = stringify(arg1) const replacement = stringify(arg2) - this.context.memoryLimit.use(str.length + pattern.length + replacement.length) const index = str.lastIndexOf(pattern) if (index === -1) return str return str.substring(0, index) + replacement + str.substring(index + pattern.length) @@ -165,7 +145,6 @@ export function replace_last (this: FilterImpl, v: string, arg1: string, arg2: s export function truncate (this: FilterImpl, v: string, l = 50, o = '...') { const str = stringify(v) o = stringify(o) - this.context.memoryLimit.use(str.length + o.length) if (str.length <= l) return v return str.substring(0, l - o.length) + o } @@ -173,7 +152,6 @@ export function truncate (this: FilterImpl, v: string, l = 50, o = '...') { export function truncatewords (this: FilterImpl, v: string, words = 15, o = '...') { const str = stringify(v) o = stringify(o) - this.context.memoryLimit.use(str.length + o.length) const arr = str.split(/\s+/) if (words <= 0) words = 1 let ret = arr.slice(0, words).join(' ') @@ -183,13 +161,11 @@ export function truncatewords (this: FilterImpl, v: string, words = 15, o = '... export function normalize_whitespace (this: FilterImpl, v: string) { const str = stringify(v) - this.context.memoryLimit.use(str.length) return str.replace(/\s+/g, ' ') } export function number_of_words (this: FilterImpl, input: string, mode?: 'cjk' | 'auto') { const str = stringify(input) - this.context.memoryLimit.use(str.length) input = str.trim() if (!input) return 0 switch (mode) { @@ -209,9 +185,6 @@ export function number_of_words (this: FilterImpl, input: string, mode?: 'cjk' | export function array_to_sentence_string (this: FilterImpl, array: unknown[], connector = 'and') { connector = stringify(connector) - let outputSize = connector.length + array.length * 2 - for (let i = 0; i < array.length; i++) outputSize += stringify(array[i]).length - this.context.memoryLimit.use(outputSize) switch (array.length) { case 0: return '' diff --git a/src/liquid-options.ts b/src/liquid-options.ts index f41ba1ec81..03531b0be5 100644 --- a/src/liquid-options.ts +++ b/src/liquid-options.ts @@ -89,8 +89,6 @@ export interface LiquidOptions { parseLimit?: number; /** For DoS handling, limit total time (in ms) for each `render()` call. */ renderLimit?: number; - /** For DoS handling, limit new objects creation, including array concat/join/strftime, etc. A typical PC can handle 1e9 (1G) memory without issue. */ - memoryLimit?: number; } export interface RenderOptions { @@ -114,8 +112,6 @@ export interface RenderOptions { templateLimit?: number; /** For DoS handling, limit total time (in ms) for each `render()` call. */ renderLimit?: number; - /** For DoS handling, limit new objects creation, including array concat/join/strftime, etc. A typical PC can handle 1e9 (1G) memory without issue.. */ - memoryLimit?: number; } export interface RenderFileOptions extends RenderOptions { @@ -161,7 +157,6 @@ export interface NormalizedFullOptions extends NormalizedOptions { operators: Operators; parseLimit: number; renderLimit: number; - memoryLimit: number; } export const defaultOptions: NormalizedFullOptions = { @@ -194,7 +189,6 @@ export const defaultOptions: NormalizedFullOptions = { lenientIf: false, globals: {}, operators: defaultOperators, - memoryLimit: Infinity, parseLimit: Infinity, renderLimit: Infinity } diff --git a/src/render/expression.ts b/src/render/expression.ts index 62041ae174..a05aca395e 100644 --- a/src/render/expression.ts +++ b/src/render/expression.ts @@ -67,7 +67,6 @@ export function evalQuotedToken (token: QuotedToken) { function * evalRangeToken (token: RangeToken, ctx: Context) { const low: number = yield evalToken(token.lhs, ctx) const high: number = yield evalToken(token.rhs, ctx) - ctx.memoryLimit.use(high - low + 1) return range(+low, +high + 1) } diff --git a/src/util/strftime.ts b/src/util/strftime.ts index 8bf8b09d11..732b50dd14 100644 --- a/src/util/strftime.ts +++ b/src/util/strftime.ts @@ -1,13 +1,11 @@ import { changeCase, padStart, padEnd } from './underscore' import { LiquidDate } from './liquid-date' -import type { Limiter } from './limiter' const rFormat = /%([-_0^#:]+)?(\d+)?([EO])?(.)/ interface FormatOptions { flags: Record; width?: string; modifier?: string; - memoryLimit?: Pick; } // prototype extensions @@ -99,7 +97,6 @@ const formatCodes: Record = { N: (d: LiquidDate, opts: FormatOptions) => { const width = Number(opts.width) || 9 const str = String(d.getMilliseconds()).slice(0, width) - opts.memoryLimit?.use(width - str.length) return padEnd(str, width, '0') }, p: (d: LiquidDate) => (d.getHours() < 12 ? 'AM' : 'PM'), @@ -123,25 +120,25 @@ const formatCodes: Record = { } formatCodes.h = formatCodes.b -export function strftime (d: LiquidDate, formatStr: string, memoryLimit?: Pick) { +export function strftime (d: LiquidDate, formatStr: string) { let output = '' let remaining = formatStr let match while ((match = rFormat.exec(remaining))) { output += remaining.slice(0, match.index) remaining = remaining.slice(match.index + match[0].length) - output += format(d, match, memoryLimit) + output += format(d, match) } return output + remaining } -function format (d: LiquidDate, match: RegExpExecArray, memoryLimit?: Pick) { +function format (d: LiquidDate, match: RegExpExecArray) { const [input, flagStr = '', width, modifier, conversion] = match const convert = formatCodes[conversion] if (!convert) return input const flags: Record = {} for (const flag of flagStr) flags[flag] = true - let ret = String(convert(d, { flags, width, modifier, memoryLimit })) + let ret = String(convert(d, { flags, width, modifier })) let padChar = padSpaceChars.has(conversion) ? ' ' : '0' let padWidth = Number(width) || padWidths[conversion] || 0 if (flags['^']) ret = ret.toUpperCase() @@ -149,6 +146,5 @@ function format (d: LiquidDate, match: RegExpExecArray, memoryLimit?: Pick { - const engine = new Liquid({ - memoryLimit: 1e5 - }) - const tpl = `{% for i in (1..1000000000) %} {{'a'}} {% endfor %}` - expect(() => engine.parseAndRenderSync(tpl)).toThrow('memory alloc limit exceeded, line:1, col:1') - }) it('group_by_exp fails with object as input #785', () => { const site = { tags: { diff --git a/test/integration/filters/date.spec.ts b/test/integration/filters/date.spec.ts index 9928769061..3d3a683d4b 100644 --- a/test/integration/filters/date.spec.ts +++ b/test/integration/filters/date.spec.ts @@ -204,31 +204,11 @@ describe('filters/date', function () { return test('{{ "1990-12-31T23:00:00Z" | date: "%Y-%m-%dT%H:%M:%S" }}', '1991-01-01T04:30:00', undefined, optsWithDateFormat) }) }) - describe('strftime width / memoryLimit', () => { - it('should charge memoryLimit for huge numeric strftime widths', () => { - const liquid = new Liquid({ memoryLimit: 500 }) - expect(() => liquid.parseAndRenderSync('{{ d | date: f }}', { d: 'now', f: '%5000000d' })) - .toThrow('memory alloc limit exceeded') - }) - it('should charge memoryLimit for array format PoC', () => { - const liquid = new Liquid({ memoryLimit: 50, renderLimit: 1e9 }) - expect(() => liquid.parseAndRenderSync('{{ d | date: f }}', { d: 'now', f: ['a'.repeat(2000000)] })) - .toThrow('memory alloc limit exceeded') - }) - it('should charge memoryLimit for object toString format PoC', () => { - const liquid = new Liquid({ memoryLimit: 50, renderLimit: 1e9 }) - const huge = 'a'.repeat(2000000) - const f = { toString: () => huge } - expect(() => liquid.parseAndRenderSync('{{ d | date: f }}', { d: 'now', f })) - .toThrow('memory alloc limit exceeded') - }) - it('should honor numeric strftime pad width when memoryLimit allows', () => { - const liquid = new Liquid({ memoryLimit: 1e7 }) + describe('strftime width', () => { + it('should honor numeric strftime pad width', () => { + const liquid = new Liquid() const out = liquid.parseAndRenderSync('{{ d | date: f }}', { d: 'now', f: '%5000d' }) expect(out.length).toBe(5000) - const tight = new Liquid({ memoryLimit: 100 }) - expect(() => tight.parseAndRenderSync('{{ d | date: f }}', { d: 'now', f: '%5000d' })) - .toThrow('memory alloc limit exceeded') }) }) }) diff --git a/test/integration/liquid/dos.spec.ts b/test/integration/liquid/dos.spec.ts index 65d78adb52..28dbfb8568 100644 --- a/test/integration/liquid/dos.spec.ts +++ b/test/integration/liquid/dos.spec.ts @@ -49,93 +49,16 @@ describe('DoS related', function () { await expect(liquid.parseAndRender('{% render "small" %}')).resolves.toBe('12345') }) it('should enforce renderLimit when for body has no template nodes', () => { - const liquid = new Liquid({ memoryLimit: 1e9, renderLimit: 1 }) + const liquid = new Liquid({ renderLimit: 1 }) expect(() => liquid.parseAndRenderSync('{%- for i in (1..5000000) -%}{%- endfor -%}', {})) .toThrow('template render limit exceeded') }) it('should enforce renderLimit when tablerow body has no template nodes', () => { - const liquid = new Liquid({ memoryLimit: 1e9, renderLimit: 1 }) + const liquid = new Liquid({ renderLimit: 1 }) expect(() => liquid.parseAndRenderSync('{%- tablerow i in (1..1000000) cols:1 -%}{%- endtablerow -%}', {})) .toThrow('template render limit exceeded') }) }) - describe('#memoryLimit', () => { - it('should throw for too many array creation in filters', async () => { - const array = Array(1e3).fill(0) - const liquid = new Liquid({ memoryLimit: 100 }) - await expect(liquid.parseAndRender('{{ array | slice: 0, 3 | join }}', { array })).resolves.toBe('0 0 0') - await expect(liquid.parseAndRender('{{ array | slice: 0, 300 | join }}', { array })).rejects.toThrow('memory alloc limit exceeded, line:1, col:1') - }) - it('should support reset when calling render', async () => { - const array = Array(1e3).fill(0) - const liquid = new Liquid({ memoryLimit: 100 }) - await expect(liquid.parseAndRender('{{ array | slice: 0, 300 | join }}', { array })).rejects.toThrow('memory alloc limit exceeded, line:1, col:1') - await expect(liquid.parseAndRender('{{ array | slice: 0, 300 | join }}', { array }, { memoryLimit: 1e3 })).resolves.toBe(Array(300).fill(0).join(' ')) - }) - it('should throw for too many array iteration in tags', async () => { - const array = ['a'] - const liquid = new Liquid({ memoryLimit: 100 }) - const src = '{% for i in (1..count) %}{% assign array = array | concat: array %}{% endfor %}{{ array | join }}' - await expect(liquid.parseAndRender(src, { array, count: 3 })).resolves.toBe('a a a a a a a a') - await expect(liquid.parseAndRender(src, { array, count: 100 })).rejects.toThrow('memory alloc limit exceeded, line:1, col:26') - }) - it('should charge pop allocation to memoryLimit', async () => { - const array = Array(1e3).fill(0) - const liquid = new Liquid({ memoryLimit: 100 }) - await expect(liquid.parseAndRender('{{ array | pop | size }}', { array })).rejects.toThrow('memory alloc limit exceeded') - }) - it('should charge sample allocation to memoryLimit', async () => { - const array = Array(1e3).fill(0) - const liquid = new Liquid({ memoryLimit: 100 }) - await expect(liquid.parseAndRender('{{ array | sample: 1 | size }}', { array })).rejects.toThrow('memory alloc limit exceeded') - }) - it('should charge join by produced output size, not element count', () => { - const array = ['a'.repeat(100), 'b'.repeat(100)] - const liquid = new Liquid({ memoryLimit: 100 }) - expect(() => liquid.parseAndRenderSync('{{ array | join: "" }}', { array })) - .toThrow('memory alloc limit exceeded') - }) - it('should allow join within memoryLimit', () => { - const array = ['a'.repeat(20), 'b'.repeat(20)] - const liquid = new Liquid({ memoryLimit: 100 }) - expect(liquid.parseAndRenderSync('{{ array | join: "" }}', { array })).toBe('a'.repeat(20) + 'b'.repeat(20)) - }) - it('should prevent concat doubling from bypassing join memoryLimit', () => { - const liquid = new Liquid({ memoryLimit: 1e4 }) - const src = '{%- assign a = s | split: "NOSEP" -%}' + - '{%- assign a = a | concat: a -%}{%- assign a = a | concat: a -%}{%- assign a = a | concat: a -%}' + - '{{ a | join: "" | size }}' - expect(() => liquid.parseAndRenderSync(src, { s: 'a'.repeat(5000) })) - .toThrow('memory alloc limit exceeded') - }) - it('should charge array_to_sentence_string by produced output size', () => { - const array = ['a'.repeat(100), 'b'.repeat(100), 'c'.repeat(100)] - const liquid = new Liquid({ memoryLimit: 100 }) - expect(() => liquid.parseAndRenderSync('{{ array | array_to_sentence_string }}', { array })) - .toThrow('memory alloc limit exceeded') - }) - it('should charge json serialization of concat-doubled arrays', () => { - const liquid = new Liquid({ memoryLimit: 1e4 }) - const src = '{%- assign a = s | split: "NOSEP" -%}' + - '{%- assign a = a | concat: a -%}{%- assign a = a | concat: a -%}{%- assign a = a | concat: a -%}' + - '{{ a | json | size }}' - expect(() => liquid.parseAndRenderSync(src, { s: 'a'.repeat(5000) })) - .toThrow('memory alloc limit exceeded') - }) - it('should charge inspect serialization of concat-doubled arrays', () => { - const liquid = new Liquid({ memoryLimit: 1e4 }) - const src = '{%- assign a = s | split: "NOSEP" -%}' + - '{%- assign a = a | concat: a -%}{%- assign a = a | concat: a -%}{%- assign a = a | concat: a -%}' + - '{{ a | inspect | size }}' - expect(() => liquid.parseAndRenderSync(src, { s: 'a'.repeat(5000) })) - .toThrow('memory alloc limit exceeded') - }) - it('should charge strip_html input length to memoryLimit', () => { - const liquid = new Liquid({ memoryLimit: 100 }) - expect(() => liquid.parseAndRenderSync('{{ s | strip_html }}', { s: 'a'.repeat(200) })) - .toThrow('memory alloc limit exceeded') - }) - }) describe('strip_html ReDoS', () => { // Regression for O(n^2) backtracking on unclosed ` Date: Tue, 14 Jul 2026 21:27:52 +0800 Subject: [PATCH 02/10] feat: add templateLimit, outputLengthLimit, and maxDepth DoS limits Enforce v11 resource guards in render and tags, fix for offset/else behavior, and update tutorials for Tag-class registration. Co-authored-by: Cursor --- .../source/tutorials/register-filters-tags.md | 48 ++---- docs/source/tutorials/render-tag-content.md | 37 +++-- docs/source/tutorials/security-model.md | 28 +++- docs/source/tutorials/sync-and-async.md | 2 + docs/themes/navy/source/js/main.js | 3 +- src/context/context.ts | 24 ++- src/emitters/simple-emitter.ts | 11 +- src/emitters/streamed-emitter.ts | 12 +- src/liquid-options.ts | 22 ++- src/render/render.spec.ts | 3 +- src/render/render.ts | 13 +- src/tags/for.ts | 18 ++- src/tags/include.ts | 33 ++-- src/tags/layout.ts | 33 ++-- src/tags/render.ts | 47 +++--- src/tags/tablerow.ts | 7 +- test/integration/liquid/dos.spec.ts | 149 ++++++++++++++++-- test/integration/tags/for.spec.ts | 12 ++ 18 files changed, 342 insertions(+), 160 deletions(-) diff --git a/docs/source/tutorials/register-filters-tags.md b/docs/source/tutorials/register-filters-tags.md index 2e6bf7d334..e15bdd7ded 100644 --- a/docs/source/tutorials/register-filters-tags.md +++ b/docs/source/tutorials/register-filters-tags.md @@ -6,41 +6,24 @@ title: Register Filters/Tags ```typescript // Usage: {% upper name %} -import { Value, TagToken, Context, Emitter, TopLevelToken } from 'liquidjs' - -engine.registerTag('upper', { - parse: function(tagToken: TagToken, remainTokens: TopLevelToken[]) { - this.value = new Value(tagToken.args, engine) - }, - render: function*(ctx: Context) { - const str = yield this.value.value(ctx); // 'alice' - return str.toUpperCase() // 'ALICE' - } -}); -``` - -* `parse`: Read tokens from `remainTokens` until your end token. -* `render`: Combine scope data with your parsed tokens into HTML string. - -For complex tag implementation, you can also provide a tag class: - -```typescript -// Usage: {% upper name:"alice" %} -import { Hash, Tag, TagToken, Context, Emitter, TopLevelToken, Liquid } from 'liquidjs' +import { Value, Tag, TagToken, Context, TopLevelToken, Liquid } from 'liquidjs' engine.registerTag('upper', class UpperTag extends Tag { - private hash: Hash + private value: Value constructor(tagToken: TagToken, remainTokens: TopLevelToken[], liquid: Liquid) { super(tagToken, remainTokens, liquid) - this.hash = new Hash(tagToken.args) + this.value = new Value(tagToken.args, liquid) } * render(ctx: Context) { - const hash = yield this.hash.render(); - return hash.name.toUpperCase() // 'ALICE' + const str = yield this.value.value(ctx) // 'alice' + return str.toUpperCase() // 'ALICE' } }); ``` +* `constructor`: Parse tag arguments and read tokens from `remainTokens` until your end token. `liquid` is passed as the third argument. +* `render`: Return an HTML string (or `return yield` a value) for simple tags that produce one value; use `emitter.write()` when writing incrementally or delegating via `yield this.liquid.renderer.renderTemplates()`, since nested templates write through the shared emitter. + See existing tag implementations here: See demo example here: https://github.com/harttle/liquidjs/blob/master/demo/typescript/index.ts @@ -64,14 +47,17 @@ See existing filter implementations here: ``` -Firstly, [register][register-tags] a tag named `wrap` and parse the content into `this.tpls`. Here in `parse(tagToken, remainTokens)`: +Firstly, [register][register-tags] a tag named `wrap` and parse the content into `this.tpls`. In the tag `constructor(tagToken, remainTokens, liquid)`: - `tagToken` is current token `{%raw%}{% wrap %}{%endraw%}`, and - `remainTokens` is an array of all tokens following `{%raw%}{% wrap %}{%endraw%}` until the end of this template file. @@ -30,11 +30,14 @@ Firstly, [register][register-tags] a tag named `wrap` and parse the content into Basically, what we need to do is take/`.shift()` enough tags from `remainTokens` until we get an `endwrap` token (the name can be arbitrary, but by convention it should be `endwrap`). And if there's no `endwrap` until the end of the template file, we need to throw a tag-not-closed `Error`. ```javascript -engine.registerTag('wrap', { - parse(tagToken, remainTokens) { +const { Tag } = require('liquidjs') + +engine.registerTag('wrap', class WrapTag extends Tag { + constructor(tagToken, remainTokens, liquid) { + super(tagToken, remainTokens, liquid) this.tpls = [] let closed = false - while(remainTokens.length) { + while (remainTokens.length) { let token = remainTokens.shift() // we got the end tag! stop taking tokens if (token.name === 'endwrap') { @@ -44,11 +47,11 @@ engine.registerTag('wrap', { // parse token into template // parseToken() may consume more than 1 tokens // e.g. {% if %}...{% endif %} - let tpl = this.liquid.parser.parseToken(token, remainTokens) + let tpl = liquid.parser.parseToken(token, remainTokens) this.tpls.push(tpl) } if (!closed) throw new Error(`tag ${tagToken.getText()} not closed`) - }, + } * render(context, emitter) { emitter.write("
") yield this.liquid.renderer.renderTemplates(this.tpls, context, emitter) @@ -57,16 +60,17 @@ engine.registerTag('wrap', { }) ``` -`.renderTemplates()` can be async; we need `yield` to wait for it to complete. For more details on async in LiquidJS, see [Sync and Async][async]. Other parts of the `render()` method are quite straightforward. Here's a JSFiddle version: +`.renderTemplates()` can be async; we need `yield` to wait for it to complete. For more details on async in LiquidJS, see [Sync and Async][async]. Here's a JSFiddle version: ## Using ParseStream -When it comes to complex tags like [for][for] and [if][if], the `parse()` can be very complicated. There's a [ParseStream][ParseStream] utility to organize the `parse()` in event-based style. Following is a re-written `parse()` using `ParseStream` that does exactly the same as the example above. +For more complex tags such as [for][for] and [if][if], constructor parsing can get unwieldy. [ParseStream][ParseStream] offers an event-based API for this. The constructor below is equivalent to the example above: ```javascript -parse(tagToken, remainTokens) { +constructor(tagToken, remainTokens, liquid) { + super(tagToken, remainTokens, liquid) this.tpls = [] - this.liquid.parser.parseStream(remainTokens) + liquid.parser.parseStream(remainTokens) .on('template', tpl => this.tpls.push(tpl)) // note that we cannot use arrow function because we need `this` .on('tag:endwrap', function () { this.stop() }) @@ -103,15 +107,18 @@ As you've noticed, there's an additional `repeat.i` in the context of `repeat`. Each time we enter a new *Context*, we need to push a new *Scope*. And when we finish rendering and exit the *Context*, we pop the *Scope* from the *Context*. As you can see in the following implementation: ```javascript -engine.registerTag('repeat', { - parse(tagToken, remainTokens) { +const { Tag } = require('liquidjs') + +engine.registerTag('repeat', class RepeatTag extends Tag { + constructor(tagToken, remainTokens, liquid) { + super(tagToken, remainTokens, liquid) this.tpls = [] - this.liquid.parser.parseStream(remainTokens) + liquid.parser.parseStream(remainTokens) .on('template', tpl => this.tpls.push(tpl)) .on('tag:endrepeat', function () { this.stop() }) .on('end', () => { throw new Error(`tag ${tagToken.getText()} not closed`) }) .start() - }, + } * render(context, emitter) { const repeat = { i: 1 } context.push({ repeat }) @@ -123,7 +130,7 @@ engine.registerTag('repeat', { }) ``` -The `parse()` is exactly the same as `wrap` tag, we repeat the content simply by calling `.renderTemplates(this.tpls)` twice during `render()`. Here's the JSFiddle: +The constructor is exactly the same as `wrap` tag, we repeat the content simply by calling `.renderTemplates(this.tpls)` twice during `render()`. Here's the JSFiddle: {% note warn Use Push & Pop in Pairs %} `context.push()` and `context.pop()` have to be used in pairs. Failing to `pop()` the *Scope* you pushed will leak the *Scope* to latter templates and may corrupt the *Context* stack. diff --git a/docs/source/tutorials/security-model.md b/docs/source/tutorials/security-model.md index ff35d25714..9555742fb1 100644 --- a/docs/source/tutorials/security-model.md +++ b/docs/source/tutorials/security-model.md @@ -2,7 +2,7 @@ title: Security Model --- -LiquidJS provides DoS-oriented limits (`parseLimit`, `renderLimit`) to reduce risk. This page summarizes those limits, [`ownPropertyOnly`][ownPropertyOnly], custom [`Drop`][drop] usage, and the security boundary to assume in production. +LiquidJS provides DoS-oriented limits (`parseLimit`, `templateLimit`, `outputLengthLimit`, `maxDepth`) to reduce risk. This page summarizes those limits, [`ownPropertyOnly`][ownPropertyOnly], custom [`Drop`][drop] usage, and the security boundary to assume in production. ## Security boundary @@ -19,7 +19,9 @@ For LiquidJS in production, prefer **external** controls: Node.js `vm` or worker ## Limits at a glance - [parseLimit][parseLimit]: limit total template size per `parse()` call. -- [renderLimit][renderLimit]: limit total render time per `render()` call. +- [templateLimit][templateLimit]: limit total tag/HTML/output nodes rendered per `render()` call. +- [outputLengthLimit][outputLengthLimit]: limit total output length per `render()` call. +- [maxDepth][maxDepth]: limit nesting depth of `{% render %}`, `{% include %}`, and `{% layout %}`. ## Limit details @@ -29,9 +31,9 @@ For LiquidJS in production, prefer **external** controls: Node.js `vm` or worker A typical PC handles `1e8` (100M) characters without issues. -### renderLimit +### templateLimit -Restricting template size alone is insufficient because dynamic loops with large counts can occur during rendering. [renderLimit][renderLimit] mitigates this by limiting the time consumed by each `render()` call. +Restricting template size alone is insufficient because dynamic loops with large counts can occur during rendering. [templateLimit][templateLimit] mitigates this by limiting the number of tag, HTML literal, and output nodes rendered in each `render()` call. ```liquid {%- for i in (1..10000000) -%} @@ -39,9 +41,17 @@ Restricting template size alone is insufficient because dynamic loops with large {%- endfor -%} ``` -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. +Each template node (the `for` tag, literal `order: `, output `{{i}}`, and so on) counts toward the limit. In the above example, a limit of `30000000` would be exceeded before the loop finishes. -`renderLimit` is not a hard CPU limiter. It is checked between template renders, so compute-intensive filters/tags/user-defined functions or deeply nested template execution between checks can still cause DoS. +`templateLimit` is checked before each node render, so compute-intensive filters/tags/user-defined functions between checks can still cause DoS. + +### outputLengthLimit + +[outputLengthLimit][outputLengthLimit] caps the cumulative length of output written during a `render()` call, including output from partials rendered via `{% render %}`. + +### maxDepth + +[maxDepth][maxDepth] limits how deeply `{% render %}`, `{% include %}`, and `{% layout %}` can nest. Defaults to `128`. Memory-heavy templates (for example exponential `concat` in a loop) are not capped by LiquidJS. Mitigate them with process/container memory limits, output size checks after render, or template restrictions — the same pattern Jinja2 and Twig recommend for heap and CPU. @@ -59,13 +69,15 @@ If you run an online service, avoid rendering fully user-defined templates whene - Prefer curated templates or a restricted template subset. - If user-defined templates are required, isolate rendering (worker/process/container), enforce OS/container memory and CPU limits, and apply request rate limits. -- Treat `parseLimit` and `renderLimit` as one layer in a broader DoS defense strategy. +- Treat `parseLimit`, `templateLimit`, `outputLengthLimit`, and `maxDepth` as one layer in a broader DoS defense strategy. For heavy single-template operations, process-level isolation is still recommended (for example with [paralleljs][paralleljs]). [paralleljs]: https://www.npmjs.com/package/paralleljs [parseLimit]: /api/interfaces/LiquidOptions.html#parseLimit -[renderLimit]: /api/interfaces/LiquidOptions.html#renderLimit +[templateLimit]: /api/interfaces/LiquidOptions.html#templateLimit +[outputLengthLimit]: /api/interfaces/LiquidOptions.html#outputLengthLimit +[maxDepth]: /api/interfaces/LiquidOptions.html#maxDepth [ownPropertyOnly]: /api/interfaces/LiquidOptions.html#ownPropertyOnly [renderOwnPropertyOnly]: /api/interfaces/RenderOptions.html#ownPropertyOnly [strictVariables]: /api/interfaces/LiquidOptions.html#strictVariables diff --git a/docs/source/tutorials/sync-and-async.md b/docs/source/tutorials/sync-and-async.md index f47c48d384..69acafa308 100644 --- a/docs/source/tutorials/sync-and-async.md +++ b/docs/source/tutorials/sync-and-async.md @@ -50,6 +50,8 @@ All built-in tags are implemented this way and are safe to use in both sync and - do not directly `return `, and - do not call any APIs that return a Promise. +You can write output with `emitter.write()` or `return` / `return yield` an HTML string — both are emitted to output. Returning is handy for simple tags that produce one value (for example `{% cycle %}`); use `emitter.write()` when writing output incrementally or when delegating via `yield renderTemplates()`, since nested templates write through the shared emitter. + ## Call APIs that return a Promise But LiquidJS is Promise-friendly, right? You can still call Promise-based functions and wait for that Promise within tag implementations. Just replace `await` with `yield`. e.g. we're calling `fs.readFile()` which returns a `Promise`: diff --git a/docs/themes/navy/source/js/main.js b/docs/themes/navy/source/js/main.js index 1d4e5eb4cd..e55e35ea07 100644 --- a/docs/themes/navy/source/js/main.js +++ b/docs/themes/navy/source/js/main.js @@ -42,8 +42,7 @@ if (!/\/playground(?:\.html)?$/.test(location.pathname)) return; updateVersion(liquidjs.version); const engine = new liquidjs.Liquid({ - memoryLimit: 1e5, - renderLimit: 1e5 + templateLimit: 1e5 }); const colorScheme = window.matchMedia('(prefers-color-scheme: dark)'); const editor = createEditor('editorEl', 'liquid'); diff --git a/src/context/context.ts b/src/context/context.ts index 19b36d522b..63af8c993e 100644 --- a/src/context/context.ts +++ b/src/context/context.ts @@ -1,9 +1,8 @@ -import { getPerformance } from '../util/performance' import { Drop } from '../drop/drop' import { __assign } from 'tslib' import { NormalizedFullOptions, defaultOptions, RenderOptions } from '../liquid-options' import { createScope, Scope } from './scope' -import { hasOwnProperty, isArray, isNil, isUndefined, isString, isFunction, isNumber, toLiquid, InternalUndefinedVariableError, toValueSync, isObject, Limiter, toValue, readArrayElement } from '../util' +import { hasOwnProperty, isArray, isNil, isUndefined, isString, isFunction, isNumber, toLiquid, InternalUndefinedVariableError, toValueSync, isObject, Limiter, toValue, readArrayElement, assert } from '../util' type PropertyKey = string | number; @@ -36,15 +35,26 @@ export class Context { */ public strictVariables: boolean; public ownPropertyOnly: boolean; - public renderLimit: Limiter; - public constructor (env: object = {}, opts: NormalizedFullOptions = defaultOptions, renderOptions: RenderOptions = {}, { renderLimit }: { renderLimit?: Limiter } = {}) { + public templateLimit: Limiter; + public outputLengthLimit: Limiter; + public depth: number; + public constructor (env: object = {}, opts: NormalizedFullOptions = defaultOptions, renderOptions: RenderOptions = {}, { templateLimit, outputLengthLimit, depth }: { templateLimit?: Limiter, outputLengthLimit?: Limiter, depth?: number } = {}) { this.sync = !!renderOptions.sync this.opts = opts this.globals = renderOptions.globals ?? opts.globals this.environments = isObject(env) ? env : Object(env) this.strictVariables = renderOptions.strictVariables ?? this.opts.strictVariables this.ownPropertyOnly = renderOptions.ownPropertyOnly ?? opts.ownPropertyOnly - this.renderLimit = renderLimit ?? new Limiter('template render', getPerformance().now() + (renderOptions.renderLimit ?? opts.renderLimit)) + this.templateLimit = templateLimit ?? new Limiter('template', renderOptions.templateLimit ?? opts.templateLimit) + this.outputLengthLimit = outputLengthLimit ?? new Limiter('output length', renderOptions.outputLengthLimit ?? opts.outputLengthLimit) + this.depth = depth ?? 0 + } + public increaseDepth () { + assert(this.depth < this.opts.maxDepth, 'template depth limit exceeded') + this.depth++ + } + public decreaseDepth () { + this.depth-- } public getRegister (key: string, defaultValue: T = undefined as T): T { return (this.registers[key] = this.registers[key] || defaultValue) @@ -107,7 +117,9 @@ export class Context { strictVariables: this.strictVariables, ownPropertyOnly: this.ownPropertyOnly }, { - renderLimit: this.renderLimit + templateLimit: this.templateLimit, + outputLengthLimit: this.outputLengthLimit, + depth: this.depth }) } private findScope (key: string | number) { diff --git a/src/emitters/simple-emitter.ts b/src/emitters/simple-emitter.ts index f1d0482068..3de1e368f5 100644 --- a/src/emitters/simple-emitter.ts +++ b/src/emitters/simple-emitter.ts @@ -1,10 +1,17 @@ -import { stringify } from '../util' +import { stringify, Limiter } from '../util' import { Emitter } from './emitter' export class SimpleEmitter implements Emitter { public buffer = ''; + private outputLengthLimit?: Limiter + + constructor (outputLengthLimit?: Limiter) { + this.outputLengthLimit = outputLengthLimit + } public write (html: any) { - this.buffer += stringify(html) + const str = stringify(html) + this.outputLengthLimit?.use(str.length) + this.buffer += str } } diff --git a/src/emitters/streamed-emitter.ts b/src/emitters/streamed-emitter.ts index 6750ea3b4e..a9c93e4c5b 100644 --- a/src/emitters/streamed-emitter.ts +++ b/src/emitters/streamed-emitter.ts @@ -1,12 +1,20 @@ -import { stringify } from '../util' +import { stringify, Limiter } from '../util' import { Emitter } from './emitter' import { PassThrough } from 'stream' export class StreamedEmitter implements Emitter { public buffer = ''; public stream: NodeJS.ReadWriteStream = new PassThrough() + private outputLengthLimit?: Limiter + + constructor (outputLengthLimit?: Limiter) { + this.outputLengthLimit = outputLengthLimit + } + public write (html: any) { - this.stream.write(stringify(html)) + const str = stringify(html) + this.outputLengthLimit?.use(str.length) + this.stream.write(str) } public error (err: Error) { this.stream.emit('error', err) diff --git a/src/liquid-options.ts b/src/liquid-options.ts index 03531b0be5..f0fc1bff68 100644 --- a/src/liquid-options.ts +++ b/src/liquid-options.ts @@ -87,8 +87,12 @@ export interface LiquidOptions { orderedFilterParameters?: boolean; /** For DoS handling, limit total length of templates parsed in one `parse()` call. A typical PC can handle 1e8 (100M) characters without issues. */ parseLimit?: number; - /** For DoS handling, limit total time (in ms) for each `render()` call. */ - renderLimit?: number; + /** For DoS handling, limit total renders of tag/HTML/output in one `render()` call. */ + templateLimit?: number; + /** For DoS handling, limit total output length in one `render()` call. */ + outputLengthLimit?: number; + /** For DoS handling, limit nesting depth of `{% render %}`, `{% include %}`, and `{% layout %}` tags. Defaults to `128`. */ + maxDepth?: number; } export interface RenderOptions { @@ -108,10 +112,10 @@ export interface RenderOptions { * Same as `ownPropertyOnly` on LiquidOptions, but only for current render() call */ ownPropertyOnly?: boolean; - /** For DoS handling, limit total renders of tag/HTML/output in one `render()` call. A typical PC can handle 1e5 renders of typical templates per second. */ + /** For DoS handling, limit total renders of tag/HTML/output in one `render()` call. */ templateLimit?: number; - /** For DoS handling, limit total time (in ms) for each `render()` call. */ - renderLimit?: number; + /** For DoS handling, limit total output length in one `render()` call. */ + outputLengthLimit?: number; } export interface RenderFileOptions extends RenderOptions { @@ -156,7 +160,9 @@ export interface NormalizedFullOptions extends NormalizedOptions { globals: object; operators: Operators; parseLimit: number; - renderLimit: number; + templateLimit: number; + outputLengthLimit: number; + maxDepth: number; } export const defaultOptions: NormalizedFullOptions = { @@ -190,7 +196,9 @@ export const defaultOptions: NormalizedFullOptions = { globals: {}, operators: defaultOperators, parseLimit: Infinity, - renderLimit: Infinity + templateLimit: Infinity, + outputLengthLimit: Infinity, + maxDepth: 128 } export function normalize (options: LiquidOptions): NormalizedFullOptions { diff --git a/src/render/render.spec.ts b/src/render/render.spec.ts index bde3279452..111db9ebfe 100644 --- a/src/render/render.spec.ts +++ b/src/render/render.spec.ts @@ -2,7 +2,6 @@ import { Context } from '../context' import { HTMLToken, TagToken } from '../tokens' import { Render } from './render' import { Tag, HTML } from '../template' -import { SimpleEmitter } from '../emitters' import { toPromise } from '../util' describe('render', function () { @@ -15,7 +14,7 @@ describe('render', function () { it('should render html', async function () { const scope = new Context() const token = { getContent: () => '

' } as HTMLToken - const html = await toPromise(render.renderTemplates([new HTML(token)], scope, new SimpleEmitter())) + const html = await toPromise(render.renderTemplates([new HTML(token)], scope)) return expect(html).toBe('

') }) }) diff --git a/src/render/render.ts b/src/render/render.ts index 15d6ab8655..57aaa0699a 100644 --- a/src/render/render.ts +++ b/src/render/render.ts @@ -1,4 +1,3 @@ -import { getPerformance } from '../util/performance' import { toPromise, RenderError, LiquidErrors, LiquidError } from '../util' import { Context } from '../context' import { Template } from '../template' @@ -6,23 +5,17 @@ import { Emitter, StreamedEmitter, SimpleEmitter } from '../emitters' export class Render { public renderTemplatesToNodeStream (templates: Template[], ctx: Context): NodeJS.ReadableStream { - const emitter = new StreamedEmitter() + const emitter = new StreamedEmitter(ctx.outputLengthLimit) Promise.resolve().then(() => toPromise(this.renderTemplates(templates, ctx, emitter))) .then(() => emitter.end(), err => emitter.error(err)) return emitter.stream } - public * renderTemplates (templates: Template[], ctx: Context, emitter?: Emitter): IterableIterator { - if (!emitter) { - emitter = new SimpleEmitter() - } - ctx.renderLimit.check(getPerformance().now()) + public * renderTemplates (templates: Template[], ctx: Context, emitter: Emitter = new SimpleEmitter(ctx.outputLengthLimit)): IterableIterator { const errors = [] for (const tpl of templates) { - ctx.renderLimit.check(getPerformance().now()) + ctx.templateLimit.use(1) try { - // if tpl.render supports emitter, it'll return empty `html` const html = yield tpl.render(ctx, emitter) - // if not, it'll return an `html`, write to the emitter for it html && emitter.write(html) if (ctx.breakCalled || ctx.continueCalled) break } catch (e) { diff --git a/src/tags/for.ts b/src/tags/for.ts index 1987501160..bb05dcdb38 100644 --- a/src/tags/for.ts +++ b/src/tags/for.ts @@ -41,15 +41,8 @@ export default class extends Tag { stream.start() } - * render (ctx: Context, emitter: Emitter): Generator { + * render (ctx: Context, emitter: Emitter): Generator { const r = this.liquid.renderer - let collection = toEnumerable(yield evalToken(this.collection, ctx)) - - if (!collection.length) { - yield r.renderTemplates(this.elseTemplates, ctx, emitter) - return - } - const continueKey = 'continue-' + this.variable + '-' + this.collection.getText() ctx.push(createScope({ continue: ctx.getRegister(continueKey, {}) })) const hash = (yield this.hash.render(ctx)) as Record @@ -59,6 +52,7 @@ export default class extends Tag { ? Object.keys(hash).filter(x => MODIFIERS.includes(x)) : MODIFIERS.filter(x => hash[x] !== undefined) + let collection = toEnumerable(yield evalToken(this.collection, ctx)) collection = modifiers.reduce((collection, modifier: valueOf) => { if (modifier === 'offset') return offset(collection, hash['offset']) if (modifier === 'limit') return limit(collection, hash['limit']) @@ -66,6 +60,14 @@ export default class extends Tag { }, collection) ctx.setRegister(continueKey, (hash['offset'] || 0) + collection.length) + + if (!collection.length) { + yield r.renderTemplates(this.elseTemplates, ctx, emitter) + return + } + + if (!this.templates.length) return + const scope = createScope({ forloop: new ForloopDrop(collection.length, this.collection.getText(), this.variable) }) ctx.push(scope) for (const item of collection) { diff --git a/src/tags/include.ts b/src/tags/include.ts index a00dda4e26..0a98ee989d 100644 --- a/src/tags/include.ts +++ b/src/tags/include.ts @@ -28,21 +28,26 @@ export default class extends Tag { this.hash = new Hash(tokenizer, liquid.options.jekyllInclude || liquid.options.keyValueSeparator) } * render (ctx: Context, emitter: Emitter): Generator { - const { liquid, hash, withVar } = this - const { renderer } = liquid - const filepath = (yield renderFilePath(this.file, ctx, liquid)) as string - assert(filepath, () => `illegal file path "${filepath}"`) + ctx.increaseDepth() + try { + const { liquid, hash, withVar } = this + const { renderer } = liquid + const filepath = (yield renderFilePath(this.file, ctx, liquid)) as string + assert(filepath, () => `illegal file path "${filepath}"`) - const saved = ctx.saveRegister('blocks', 'blockMode') - ctx.setRegister('blocks', {}) - ctx.setRegister('blockMode', BlockMode.OUTPUT) - const scope = createScope((yield hash.render(ctx)) as Scope) - if (withVar) scope[filepath] = yield evalToken(withVar, ctx) - const templates = (yield liquid._parsePartialFile(filepath, ctx.sync, this.currentFile)) as Template[] - ctx.push(ctx.opts.jekyllInclude ? createScope({ include: scope }) : scope) - yield renderer.renderTemplates(templates, ctx, emitter) - ctx.pop() - ctx.restoreRegister(saved) + const saved = ctx.saveRegister('blocks', 'blockMode') + ctx.setRegister('blocks', {}) + ctx.setRegister('blockMode', BlockMode.OUTPUT) + const scope = createScope((yield hash.render(ctx)) as Scope) + if (withVar) scope[filepath] = yield evalToken(withVar, ctx) + const templates = (yield liquid._parsePartialFile(filepath, ctx.sync, this.currentFile)) as Template[] + ctx.push(ctx.opts.jekyllInclude ? createScope({ include: scope }) : scope) + yield renderer.renderTemplates(templates, ctx, emitter) + ctx.pop() + ctx.restoreRegister(saved) + } finally { + ctx.decreaseDepth() + } } public * children (partials: boolean, sync: boolean): Generator { diff --git a/src/tags/layout.ts b/src/tags/layout.ts index 156ab0ee42..bff09cfae5 100644 --- a/src/tags/layout.ts +++ b/src/tags/layout.ts @@ -26,23 +26,28 @@ export default class extends Tag { yield renderer.renderTemplates(this.templates, ctx, emitter) return } - const filepath = (yield renderFilePath(this.file, ctx, liquid)) as string - assert(filepath, () => `illegal file path "${filepath}"`) - const templates = (yield liquid._parseLayoutFile(filepath, ctx.sync, this.currentFile)) as Template[] + ctx.increaseDepth() + try { + const filepath = (yield renderFilePath(this.file, ctx, liquid)) as string + assert(filepath, () => `illegal file path "${filepath}"`) + const templates = (yield liquid._parseLayoutFile(filepath, ctx.sync, this.currentFile)) as Template[] - // render remaining contents and store rendered results - ctx.setRegister('blockMode', BlockMode.STORE) - const html = yield renderer.renderTemplates(this.templates, ctx) - const blocks = ctx.getRegister('blocks', {} as Record) + // render remaining contents and store rendered results + ctx.setRegister('blockMode', BlockMode.STORE) + const html = yield renderer.renderTemplates(this.templates, ctx) + const blocks = ctx.getRegister('blocks', {} as Record) - // set whole content to anonymous block if anonymous doesn't specified - if (blocks[''] === undefined) blocks[''] = (parent: BlankDrop, emitter: Emitter) => emitter.write(html) - ctx.setRegister('blockMode', BlockMode.OUTPUT) + // set whole content to anonymous block if anonymous doesn't specified + if (blocks[''] === undefined) blocks[''] = (parent: BlankDrop, emitter: Emitter) => emitter.write(html) + ctx.setRegister('blockMode', BlockMode.OUTPUT) - // render the layout file use stored blocks - ctx.push(createScope((yield args.render(ctx)) as Scope)) - yield renderer.renderTemplates(templates, ctx, emitter) - ctx.pop() + // render the layout file use stored blocks + ctx.push(createScope((yield args.render(ctx)) as Scope)) + yield renderer.renderTemplates(templates, ctx, emitter) + ctx.pop() + } finally { + ctx.decreaseDepth() + } } public * children (partials: boolean): Generator { diff --git a/src/tags/render.ts b/src/tags/render.ts index bf86a28cb5..221d15b164 100644 --- a/src/tags/render.ts +++ b/src/tags/render.ts @@ -55,31 +55,36 @@ export default class extends Tag { this.hash = new Hash(tokenizer, liquid.options.keyValueSeparator) } * render (ctx: Context, emitter: Emitter): Generator { - const { liquid, hash } = this - const filepath = (yield renderFilePath(this.file, ctx, liquid)) as string - assert(filepath, () => `illegal file path "${filepath}"`) - - const childCtx = ctx.spawn() - const scope = childCtx.bottom() - __assign(scope, yield hash.render(ctx)) - if (this.with) { - const { value, alias } = this.with - scope[alias || filepath] = yield evalToken(value, ctx) - } + ctx.increaseDepth() + try { + const { liquid, hash } = this + const filepath = (yield renderFilePath(this.file, ctx, liquid)) as string + assert(filepath, () => `illegal file path "${filepath}"`) + + const childCtx = ctx.spawn() + const scope = childCtx.bottom() + __assign(scope, yield hash.render(ctx)) + if (this.with) { + const { value, alias } = this.with + scope[alias || filepath] = yield evalToken(value, ctx) + } - if (this.forBinding) { - const { value, alias } = this.forBinding - const collection = toEnumerable(yield evalToken(value, ctx)) - scope['forloop'] = new ForloopDrop(collection.length, value.getText(), alias as string) - for (const item of collection) { - scope[alias as string] = item + if (this.forBinding) { + const { value, alias } = this.forBinding + const collection = toEnumerable(yield evalToken(value, ctx)) + scope['forloop'] = new ForloopDrop(collection.length, value.getText(), alias as string) + for (const item of collection) { + scope[alias as string] = item + const templates = (yield liquid._parsePartialFile(filepath, childCtx.sync, this.currentFile)) as Template[] + yield liquid.renderer.renderTemplates(templates, childCtx, emitter) + scope['forloop'].next() + } + } else { const templates = (yield liquid._parsePartialFile(filepath, childCtx.sync, this.currentFile)) as Template[] yield liquid.renderer.renderTemplates(templates, childCtx, emitter) - scope['forloop'].next() } - } else { - const templates = (yield liquid._parsePartialFile(filepath, childCtx.sync, this.currentFile)) as Template[] - yield liquid.renderer.renderTemplates(templates, childCtx, emitter) + } finally { + ctx.decreaseDepth() } } diff --git a/src/tags/tablerow.ts b/src/tags/tablerow.ts index 1676fe46df..c63dba179b 100644 --- a/src/tags/tablerow.ts +++ b/src/tags/tablerow.ts @@ -39,12 +39,17 @@ export default class extends Tag { } * render (ctx: Context, emitter: Emitter): Generator { - let collection = toEnumerable(yield evalToken(this.collection, ctx)) const args = (yield this.args.render(ctx)) as Record const offset = args.offset || 0 + + let collection = toEnumerable(yield evalToken(this.collection, ctx)) const limit = (args.limit === undefined) ? collection.length : args.limit collection = collection.slice(offset, offset + limit) + if (!collection.length) return + + if (!this.templates.length) return + const cols = args.cols || collection.length const r = this.liquid.renderer diff --git a/test/integration/liquid/dos.spec.ts b/test/integration/liquid/dos.spec.ts index 28dbfb8568..5a7c95ae4e 100644 --- a/test/integration/liquid/dos.spec.ts +++ b/test/integration/liquid/dos.spec.ts @@ -4,15 +4,18 @@ import { mock, restore } from '../../stub/mockfs' describe('DoS related', function () { describe('#parseLimit', function () { afterEach(restore) + it('should throw when parse limit exceeded', async () => { const noLimit = new Liquid() const limit10 = new Liquid({ parseLimit: 10 }) const limit90 = new Liquid({ parseLimit: 90 }) const template = '{% capture bar %}{{ foo | bar: 3, a[3] }}{% endcapture %}' + await expect(noLimit.parseAndRender(template)).resolves.toBe('') await expect(limit10.parseAndRender(template)).rejects.toThrow('parse length limit exceeded') await expect(limit90.parseAndRender(template)).resolves.toBe('') }) + it('should take included template into account', async () => { mock({ '/small': 'Lorem ipsum', @@ -23,42 +26,152 @@ describe('DoS related', function () { await expect(liquid.parseAndRender('{% include "large" %}')).rejects.toThrow('parse length limit exceeded') }) }) - describe('#renderLimit', () => { + + describe('#templateLimit', () => { it('should throw when rendering too many templates', async () => { const src = '{% for i in (1..1000) %}{{i}},{% endfor %}' const noLimit = new Liquid() - const limitSmall = new Liquid({ renderLimit: 0.01 }) - const limitLarge = new Liquid({ renderLimit: 2e4 }) + const limitSmall = new Liquid({ templateLimit: 100 }) + const limitLarge = new Liquid({ templateLimit: 2001 }) await expect(noLimit.parseAndRender(src)).resolves.toMatch(/^1,2,3,4,5,.*,999,1000,$/) - await expect(limitSmall.parseAndRender(src)).rejects.toThrow('template render limit exceeded') + await expect(limitSmall.parseAndRender(src)).rejects.toThrow('template limit exceeded') await expect(limitLarge.parseAndRender(src)).resolves.toMatch(/^1,2,3,4,5,.*,999,1000,$/) }) + it('should support reset when calling render', async () => { const src = '{% for i in (1..1000) %}{{i}},{% endfor %}' - const liquid = new Liquid({ renderLimit: 0.01 }) - await expect(liquid.parseAndRender(src)).rejects.toThrow('template render limit exceeded') - await expect(liquid.parseAndRender(src, {}, { renderLimit: 1e6 })).resolves.toMatch(/^1,2,3,4,5,.*,999,1000,$/) + const liquid = new Liquid({ templateLimit: 100 }) + await expect(liquid.parseAndRender(src)).rejects.toThrow('template limit exceeded') + await expect(liquid.parseAndRender(src, {}, { templateLimit: 2001 })).resolves.toMatch(/^1,2,3,4,5,.*,999,1000,$/) }) + it('should take partials into account', async () => { mock({ '/small': '{% for i in (1..5) %}{{i}}{% endfor %}', '/large': '{% for i in (1..50000000) %}{{i}}{% endfor %}' }) - const liquid = new Liquid({ root: '/', renderLimit: 1000 }) - await expect(liquid.parseAndRender('{% render "large" %}')).rejects.toThrow('template render limit exceeded') + const liquid = new Liquid({ root: '/', templateLimit: 1000 }) + await expect(liquid.parseAndRender('{% render "large" %}')).rejects.toThrow('template limit exceeded') await expect(liquid.parseAndRender('{% render "small" %}')).resolves.toBe('12345') }) - it('should enforce renderLimit when for body has no template nodes', () => { - const liquid = new Liquid({ renderLimit: 1 }) - expect(() => liquid.parseAndRenderSync('{%- for i in (1..5000000) -%}{%- endfor -%}', {})) - .toThrow('template render limit exceeded') + + }) + + describe('#outputLengthLimit', () => { + it('should throw when output length exceeded', async () => { + const src = '{% for i in (1..1000) %}{{i}},{% endfor %}' + const noLimit = new Liquid() + const limitSmall = new Liquid({ outputLengthLimit: 10 }) + const limitLarge = new Liquid({ outputLengthLimit: 5000 }) + await expect(noLimit.parseAndRender(src)).resolves.toMatch(/^1,2,3,4,5,.*,999,1000,$/) + await expect(limitSmall.parseAndRender(src)).rejects.toThrow('output length limit exceeded') + await expect(limitLarge.parseAndRender(src)).resolves.toMatch(/^1,2,3,4,5,.*,999,1000,$/) + }) + + it('should support reset when calling render', async () => { + const src = '{% for i in (1..1000) %}{{i}},{% endfor %}' + const liquid = new Liquid({ outputLengthLimit: 10 }) + await expect(liquid.parseAndRender(src)).rejects.toThrow('output length limit exceeded') + await expect(liquid.parseAndRender(src, {}, { outputLengthLimit: 5000 })).resolves.toMatch(/^1,2,3,4,5,.*,999,1000,$/) + }) + + it('should take partials into account', async () => { + mock({ + '/small': 'abc', + '/large': '{% for i in (1..1000) %}{{i}}{% endfor %}' + }) + const liquid = new Liquid({ root: '/', outputLengthLimit: 10 }) + await expect(liquid.parseAndRender('{% render "small" %}')).resolves.toBe('abc') + await expect(liquid.parseAndRender('{% render "large" %}')).rejects.toThrow('output length limit exceeded') + }) + + it('should enforce outputLengthLimit in sync render', () => { + const liquid = new Liquid({ outputLengthLimit: 5 }) + expect(() => liquid.parseAndRenderSync('{% for i in (1..100) %}{{i}}{% endfor %}')) + .toThrow('output length limit exceeded') + }) + + it('should enforce outputLengthLimit in stream render', async () => { + const liquid = new Liquid({ outputLengthLimit: 5 }) + const tpl = liquid.parse('{% for i in (1..100) %}{{i}}{% endfor %}') + const stream = liquid.renderToNodeStream(tpl) + await expect(new Promise((resolve, reject) => { + stream.on('error', reject) + stream.on('end', resolve) + })).rejects.toThrow('output length limit exceeded') + }) + }) + + describe('#maxDepth', () => { + function chain (depth: number, tag: string) { + const templates: Record = {} + for (let i = 0; i < depth; i++) { + templates[`t${i}`] = i === depth - 1 ? 'done' : `{% ${tag} "t${i + 1}" %}` + } + return templates + } + + it('should throw when include depth exceeded', async () => { + const liquid = new Liquid({ templates: chain(3, 'include'), maxDepth: 2 }) + await expect(liquid.parseAndRender('{% include "t0" %}')).rejects.toThrow('template depth limit exceeded') + }) + + it('should allow include within maxDepth', async () => { + const liquid = new Liquid({ templates: chain(2, 'include'), maxDepth: 2 }) + await expect(liquid.parseAndRender('{% include "t0" %}')).resolves.toBe('done') }) - it('should enforce renderLimit when tablerow body has no template nodes', () => { - const liquid = new Liquid({ renderLimit: 1 }) - expect(() => liquid.parseAndRenderSync('{%- tablerow i in (1..1000000) cols:1 -%}{%- endtablerow -%}', {})) - .toThrow('template render limit exceeded') + + it('should throw when render depth exceeded', async () => { + const liquid = new Liquid({ templates: chain(3, 'render'), maxDepth: 2 }) + await expect(liquid.parseAndRender('{% render "t0" %}')).rejects.toThrow('template depth limit exceeded') + }) + + it('should allow render within maxDepth', async () => { + const liquid = new Liquid({ templates: chain(2, 'render'), maxDepth: 2 }) + await expect(liquid.parseAndRender('{% render "t0" %}')).resolves.toBe('done') + }) + + it('should throw when layout depth exceeded', async () => { + const liquid = new Liquid({ + templates: { + a: '{% layout "b" %}body-a', + b: '{% layout "c" %}body-b', + c: 'body-c' + }, + maxDepth: 2 + }) + await expect(liquid.parseAndRender('{% layout "a" %}root')).rejects.toThrow('template depth limit exceeded') + }) + + it('should allow layout within maxDepth', async () => { + const liquid = new Liquid({ + templates: { + a: '{% layout "b" %}body-a', + b: 'body-b' + }, + maxDepth: 2 + }) + await expect(liquid.parseAndRender('{% layout "a" %}root')).resolves.toBe('body-b') + }) + + it('should not count layout none toward depth', async () => { + const liquid = new Liquid({ maxDepth: 0 }) + await expect(liquid.parseAndRender('{% layout none %}ok')).resolves.toBe('ok') + }) + + it('should default maxDepth to 128', async () => { + const liquid = new Liquid({ templates: chain(128, 'include') }) + await expect(liquid.parseAndRender('{% include "t0" %}')).resolves.toBe('done') + const overflow = new Liquid({ templates: chain(129, 'include') }) + await expect(overflow.parseAndRender('{% include "t0" %}')).rejects.toThrow('template depth limit exceeded') + }) + + it('should enforce maxDepth in sync render', () => { + const liquid = new Liquid({ templates: chain(3, 'include'), maxDepth: 2 }) + expect(() => liquid.parseAndRenderSync('{% include "t0" %}')).toThrow('template depth limit exceeded') }) }) + describe('strip_html ReDoS', () => { // Regression for O(n^2) backtracking on unclosed ` { const liquid = new Liquid() const payload = ' but no in linear time', () => { const liquid = new Liquid() const payload = '