Prototype pollution
Description
A template that uses a key taken from its data to read or write a value reached the JavaScript prototype chain when that key named __proto__, constructor or prototype, or was a list holding one of those names. The forms concerned are a subscript such as {{ foo[k] }}, a call on one such as {{ foo[k](x) }}, the target of a native {% set obj[k].x = … %}, the bracket fragments a tag registered with setTag receives, and the attribute argument of swig-jinja2's groupby and sum. Depending on the template, such a key could:
- read the
ObjectandFunctionconstructors; - put a property on
Object.prototypefor the whole Node.js process, through a native set target, or through a value read with such a key that the template then writes (a dotted{% set %}in native, Twig and Jinja2, an{% include … with … only %}in native and Twig, Twig's{% with … only %}); - through a call on a value read this way, run text taken from the data as JavaScript.
A template is exposed when it indexes a value with a key the request controls, such as {{ labels[lang] }} or {{ row[column] }} with lang or column taken from the request.
Impact
Code execution and prototype pollution of the whole process, for an application whose templates index values with keys taken from the request. swig-django has no write form (it has no set tag, and its include builds a new context), but it shares the reads and the call.
Affected & patched
Every published release of the five packages before 2.12.1: @rhinostone/swig from 1.4.4 (the code dates from the original swig 1.4.2), @rhinostone/swig-core and @rhinostone/swig-twig from 2.0.0-alpha.3, @rhinostone/swig-jinja2 from 2.5.0 and @rhinostone/swig-django from 2.7.0, on the sync and the async render paths.
Fixed in 2.12.1:
- a key that names
__proto__,constructororprototype, compared as JavaScript converts a property key (so a list holding the name counts), reads as missing in a subscript, in a call on one, in a custom tag's bracket fragments and in the attribute argument of swig-jinja2'sgroupbyandsum; - a native set target given such a key throws « Unsafe assignment to "<name>" via bracket notation is not allowed (prototype-chain guard) on line N. » when the template runs;
- a prototype-chain name written as a variable inside a native set target's brackets fails at compile time.
The checks for a native set target and for a custom tag's bracket fragments are in the code the compiler emits: templates compiled by an earlier release (swig compile output, registerBundle bundles, stored precompile() results) keep the old behaviour until they are recompiled with 2.12.1, and so do subscripts compiled before 2.9.10. The 2.12.1 frontends require @rhinostone/swig-core 2.12.1, so upgrade the packages together.
Workarounds
- Upgrade to 2.12.1 or later and recompile precompiled templates.
- Otherwise, do not index template values with keys taken from the request, or check such keys before rendering against the names the template expects (reject
__proto__,constructor,prototypeand keys that are not strings).