Summary
A resource exhaustion vulnerability in LiquidJS's replace_first filter allows attackers to amplify output size far beyond the configured memoryLimit by abusing JavaScript's String.prototype.replace() back references.
By crafting templates that generate exponential string growth while appearing to stay within memory budgets, an attacker can trigger out-of-memory conditions and cause denial-of-service in applications using LiquidJS for template rendering.
Affected context
This issue affects applications that:
- Use LiquidJS as a template engine in Node.js or browser-based environments
- Enable or rely on the
replace_firstfilter within user-controlled or partially user-controlled templates - Enforce rendering limits using
memoryLimitas a safety mechanism
The vulnerability becomes relevant when templates can be influenced by untrusted input (e.g., user-generated pages, theme customizations, or CMS-driven content) and are rendered on shared or constrained infrastructure where memory exhaustion can impact service availability.
Exploit preconditions
- Attack surface: Template rendering layer where LiquidJS processes templates.
- Required access: Ability to provide or influence LiquidJS templates or filter arguments (e.g., via user-editable content, themes, or template variables).
- Trigger point: Execution of the
replace_firstfilter on an attacker-controlled string that causesString.prototype.replace()to emit output much larger than the input by using$&back references or similar constructs. - Why exploitation is possible: The
memoryLimitaccounting logic only considers the input string length, not the size of the expanded output produced byreplace_first. This mismatch allows an attacker to stay within the configuredmemoryLimitwhile causing exponential output growth and consuming excessive memory.
Why this happens
The underlying issue is incorrect resource accounting combined with an API that can expand output non-linearly relative to its input.
LiquidJS's replace_first filter internally uses String.prototype.replace(), which treats $& as a back reference to the full match and can therefore replicate matched content multiple times, leading to amplification.
The implementation only charges memory usage based on the original input string length and does not account for the actual size of the generated output.
This maps to CWE-400: Uncontrolled Resource Consumption, with a contributing factor of CWE-20: Improper Input Validation, because user-controlled template content is not constrained in a way that prevents pathological memory amplification behavior.
Potential impact
- Application process may consume excessive memory and be terminated by the runtime or container orchestrator.
- Rendering requests can hang or fail, leading to partial or full denial-of-service for affected endpoints.
- Shared infrastructure may experience reduced capacity or cascading failures if multiple services compete for exhausted memory.
- Watchdog mechanisms or health checks might restart processes frequently, degrading system stability and performance.
General mitigation guidance
Effective mitigation focuses on correcting resource accounting and reducing the potential for unbounded output expansion during template rendering.
Recommended defensive patterns include:
- Accounting for the actual length of rendered output when enforcing
memoryLimitor similar quotas, including intermediate strings produced by filters. - Introducing stricter limits on template complexity, maximum expansion depth, and output size, independent of input length.
- Restricting or sanitizing filter usage on untrusted input, especially filters that can cause string amplification (e.g., replace-like operations, loops, and concatenations).
- Applying defense-in-depth controls at the platform level: per-request timeouts, process-level memory limits, and isolation (e.g., separate workers) for template rendering.
- Designing template execution policies that distinguish between trusted templates (e.g., shipped with the application) and untrusted or user-generated templates, applying stricter safeguards to the latter.
How teams detect issues like this
Detection typically combines code analysis with runtime monitoring:
- Static analysis / code review:
- Look for template filters or helpers that call
String.prototype.replace()or similar APIs without accounting for potential output amplification. - Flag resource-limiting logic that bases limits only on input sizes instead of actual output size or overall memory usage.
- Review template engine wrappers for missing safeguards around user-supplied templates or filter arguments.
- Runtime monitoring:
- Track memory usage patterns during template rendering and alert on abnormal spikes correlated with specific templates or user inputs.
- Log and analyze requests that trigger unusually long render times or cause worker restarts due to out-of-memory conditions.
- Policy enforcement:
- Use automated security checks in CI/CD to enforce output-size caps and complexity limits in template engines.
- Integrate SAST or linting tools that know about dangerous template filter patterns or unchecked resource limits to prevent similar issues at review time.

