@rhinostone/swig-jinja2 and @rhinostone/swig-twig: unbounded loop (denial of service) in wordwrap and range from template data
Description
Two code paths could be driven into an unbounded loop by data supplied at render time (template locals), exhausting memory and hanging the Node.js process:
- swig-jinja2
wordwrapwith a width below 1 (0, a negative number, or a string such as"0") ran for seconds and consumed more than 2 GB of memory before a RangeError. Present since swig-jinja2 2.5.0, its first release. - swig-twig ranges
a..bwith an infinite bound (plain JSON yieldsInfinityfor a number such as1e999) looped until the array overflowed, taking seconds and more than 2 GB. Present since 2.0.0-alpha.5, in the shared swig-core range helper that swig-twig calls.
The native @rhinostone/swig frontend has neither the wordwrap filter nor the .. operator, and swig-django's wordwrap does not loop.
Impact
Availability only (no confidentiality or integrity impact). In an application that passes request data into a wordwrap width or a range bound, a single request can hang the process for seconds and make it allocate more than 2 GB.
Affected & patched
Affected: @rhinostone/swig-jinja2 < 2.8.2; @rhinostone/swig-twig and @rhinostone/swig-core >= 2.0.0-alpha.5, < 2.8.2. Fixed in 2.8.2: wordwrap returns the input unchanged for a width of 0 or less and breaks one character at a time for a width between 0 and 1; the range helper returns an empty range for a bound that is not finite, as it already did for NaN.
Workarounds
- Upgrade to 2.8.2 or later.
- Validate or clamp a
wordwrapwidth and a range bound taken from untrusted data.