Skip to main content
Menu
Advisories

Overview Several template filters used regular expressions that take time quadratic in the length of their input on some inputs

GHSA-RVX7-JQR9-C242Published Updated GitHub

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:

  1. striptags, in every flavor, on text with many < and no > after them; in swig-jinja2 also on many <!-- with no --> after them.
  2. trim, in swig-twig and swig-jinja2, on a long run of the trimmed characters inside the text.
  3. swig-jinja2 format, on a format string (the filter's input) with a % followed by a long run of zeros.
  4. swig-django truncatewords and pluralize on a long run of whitespace inside the value, slugify on a long run of underscores, and floatformat on an argument with a long run of g or u not 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, and pluralize since 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

Affected versions

References

GitHub · gina-io/swig