@rhinostone/swig-twig excessive memory allocation
Description
Three template features built an array whose length came from a number in the data supplied at render time (template locals), with no upper limit:
- swig-twig ranges
a..b(the range helper in @rhinostone/swig-core) build one element per value fromatob, so a bound such as1e8built an array of 100 million numbers. A range whose values reach 2^53 (9007199254740992) never ended: from 2^53 on, adding 1 no longer changes a JavaScript number, so the loop kept adding the same value. 2.8.2 made a range with an infinite bound empty (GHSA-48rg-vg49-r5vm), but a bound at or beyond 2^53 still never ended. batchin swig-twig and swig-jinja2 pads the last chunk up to the batch size when a fill value is given, so a size such as1e8built a chunk of that length.- swig-jinja2
slicebuilds one column per count, so a count such as1e7built that many columns.
The array grows until the V8 heap limit, and the process then aborts with "JavaScript heap out of memory", which a program cannot catch. Under a 256 MB heap each of these aborted the process within a second (a slice count of 1e7, the other sizes 1e8). With a larger heap a larger number is needed, except for a range whose values reach 2^53, which aborts the process whatever the heap.
Impact
Availability only (no confidentiality or integrity impact). In an application that passes a number from the request into a swig-twig range bound, a batch size or a swig-jinja2 slice count, a single request can abort the Node.js process.
Affected & patched
- @rhinostone/swig-twig >= 2.0.0-alpha.3, < 2.9.2:
batchwith a fill value since 2.0.0-alpha.3, ranges (a..b) since 2.0.0-alpha.5. - @rhinostone/swig-jinja2 >= 2.5.0, < 2.9.2:
slice, andbatchwith a fill value. - @rhinostone/swig-core >= 2.0.0-alpha.5, < 2.9.2: the range helper behind swig-twig's
a..b.
@rhinostone/swig and @rhinostone/swig-django have none of these features and are not affected.
Fixed in 2.9.2: a range of more than 1,000,000 elements is empty, as for an infinite bound, which includes a range whose values reach 2^53; swig-jinja2 slice with more than 1,000,000 columns returns an empty list, as for an infinite count; and batch leaves the last chunk unpadded for a size above 1,000,000. Smaller ranges, slices and batches are unchanged. The 2.9.2 frontends require @rhinostone/swig-core 2.9.2, so upgrade the packages together.
Workarounds
- Upgrade to 2.9.2 or later.
- Otherwise, limit numbers taken from untrusted data before they reach a range bound, a
batchsize or aslicecount, for example to between -1,000,000 and 1,000,000.
References
- swig-core range helper and swig-twig — github.com/
gina-io/ swig/ commit/ 7288240dd7d08b08946d182054588cde9b2e6f6a - swig-jinja2 — github.com/
gina-io/ swig/ commit/ 02f1d10ba7eabf45a12ea34b55b3fbb62b9caf85 - Release notes — github.com/
gina-io/ swig/ blob/ v2.9.2/ HISTORY.md - Earlier fix for an infinite range bound — github.com/
gina-io/ swig/ security/ advisories/ GHSA-48rg-vg49-r5vm