Overview Several template filters used regular expressions that take time quadratic in the length of their input on some inputs
Description
Several template filters used regular expressions that take time quadratic in the length of their input on some inputs. A template that applied one of them to data supplied at render time (template locals) could be made to block the Node.js event loop for seconds:
striptags, in every flavor, on text with many<and no>after them; in swig-jinja2 also on many<!--with no-->after them.trim, in swig-twig and swig-jinja2, on a long run of the trimmed characters inside the text.- swig-jinja2
format, on a format string (the filter's input) with a%followed by a long run of zeros. - swig-django
truncatewordsandpluralizeon a long run of whitespace inside the value,slugifyon a long run of underscores, andfloatformaton an argument with a long run ofgorunot at its end.
Rendered through a template on 100 000 characters, each took 3 to 7.5 seconds in 2.9.0 (swig-jinja2 striptags on <!-- about 1.2 seconds, swig-jinja2 format about 23 seconds). In 2.9.1 the same inputs take a few milliseconds and give the same output.
Impact
Availability only (no confidentiality or integrity impact). In an application that applies one of these filters to request data, a single request carrying a crafted value of about 100 000 characters can block the process for seconds.
Affected & patched
- @rhinostone/swig < 2.9.1:
striptags. The regex dates from the original project, so every release has it. - @rhinostone/swig-twig >= 2.0.0-alpha.3, < 2.9.1:
striptags,trim. - @rhinostone/swig-jinja2 >= 2.5.0, < 2.9.1:
striptags,trim,format. - @rhinostone/swig-django >= 2.7.0, < 2.9.1:
striptags,truncatewords,slugify,floatformat, andpluralizesince 2.8.2.
Fixed in 2.9.1: the filters use linear-time helpers added to @rhinostone/swig-core 2.9.1 and give the same output as before. @rhinostone/swig-core has no vulnerable filter itself; the 2.9.1 frontends require swig-core 2.9.1, so upgrade the packages together.
Workarounds
- Upgrade to 2.9.1 or later.
- Otherwise, cap the length of request data before passing it to these filters, or do not apply them to untrusted values.
References
- swig-core helpers — github.com/
gina-io/ swig/ commit/ bfd9e447d33716b4b84cf17bc5fe50c2b5416e4f - swig — github.com/
gina-io/ swig/ commit/ 947852139523b26f4dc23ae8833f7a2004670f79 - swig-twig — github.com/
gina-io/ swig/ commit/ 732612ae75896dafa5c166ed5a0b2e20c83d4aac - swig-jinja2 — github.com/
gina-io/ swig/ commit/ 00154bedbbed0501074ba872e25f09d82558548b - swig-django — github.com/
gina-io/ swig/ commit/ f273f2ec66630778e684df5972f20bd845cad0ff - Release notes — github.com/
gina-io/ swig/ blob/ v2.9.1/ HISTORY.md