Overview On the async render path (renderFile(path, locals, cb) with a loader that sets loader.async, or compileFile with codegenMode: 'async'), nothing limited how deeply templates could nest
Description
On the async render path (renderFile(path, locals, cb) with a loader that sets loader.async, or compileFile with codegenMode: 'async'), nothing limited how deeply templates could nest. A template that includes, imports or extends itself, directly or through other templates, therefore loaded and compiled templates without end:
- an extends cycle (
a.htmlextendsb.html, which extendsa.html), or a dynamic{% extends layout %}whose value names the template itself; - a template that includes itself, with a literal path or with a path taken from the template data (
{% include partial %}); - an import or from-import cycle (
a.htmlimportsb.html, which importsa.html).
Each round loads one more template and the heap keeps growing until V8 aborts the process with "JavaScript heap out of memory", which a program cannot catch. Under a 64 MB heap, @rhinostone/swig 2.9.4 aborted after 11 to 14 seconds and 22,000 to 26,000 template loads on an extends cycle, on a template that includes itself through a path from the data, and on an import cycle; with a larger heap it takes longer and ends the same way.
The sync render path is not affected: it ends these renders (an extends cycle fails with "Illegal circular extends", a template that includes or imports itself with a stack overflow).
Impact
Availability only (no confidentiality or integrity impact). In an application that renders on the async path and takes an include or extends path from the request, such as a page or partial name, a single request naming a template that includes or extends itself can abort the Node.js process. A template mistake has the same effect without any request data.
Affected & patched
- @rhinostone/swig-core >= 2.2.0, < 2.9.5: the async render path's template loading.
- @rhinostone/swig >= 2.2.0, < 2.9.5 and @rhinostone/swig-twig >= 2.2.0, < 2.9.5.
- @rhinostone/swig-jinja2 >= 2.5.0, < 2.9.5 and @rhinostone/swig-django >= 2.7.0, < 2.9.5 (since their first releases).
Fixed in 2.9.5: each top-level async render counts the templates it has in progress. Loading a template while 1000 are in progress fails the render with Template "<name>" is nested more than 1000 levels deep: a template probably includes, imports or extends itself., and an extends cycle fails at once with Illegal circular extends of "<name>"., the sync path's message. A recursive include therefore renders 1000 levels deep and fails at 1001, which rendered before. An include marked ignore missing swallows that error, as it swallows any error of the template it includes, so a template that includes itself under ignore missing now ends after 1000 levels. The 2.9.5 frontends require @rhinostone/swig-core 2.9.5, so upgrade the packages together.
Workarounds
- Upgrade to 2.9.5 or later.
- Otherwise, on the async render path, do not pass a template path taken from untrusted data to an include or an extends.