Twig 3.31.1 is a security release that fixes nine vulnerabilities, seven of them in the sandbox. This post also covers two more, which 3.31.0 already fixed. Applications that don't render sandboxed templates are only exposed to two of these issues. One is the missing escaping of variables named like a macro alias, the other is the memory growth of the html_to_markdown filter.

Security fixes

Most of these issues let a sandboxed template reach what its security policy denies. That can be a __toString() method, a property, or a function, filter or test.

Medium

Since 3.21.0, the is true test returns the object it tests, and the sandbox didn't check that result. A sandboxed template could therefore print an object through a denied __toString() method. The sandbox now checks the result of tests like the result of filters and functions (CVE-2026-133763).

Since 3.23.0, assignments like x = obj and the destructuring forms evaluate to the value they assign. The sandbox didn't check that value either, so a sandboxed template could call a denied __toString() method through an assignment (CVE-2026-133765).

When the parent of an embed tag was an expression other than a string or a variable, the sandbox didn't see that expression. A sandboxed template allowed to use embed could therefore call any function, filter or test, and any __toString() method. The sandbox now checks this expression like any other one (CVE-2026-133767).

The join filter converted an object that isn't iterable to an array, which gives all its properties, the private and protected ones included. A sandboxed template allowed to use join could print properties that the policy denies. Such a value is now joined as a single item (CVE-2026-133768).

Low

The sandbox didn't check the input of tests. The PHP callable of a test can convert its input to a string, for example through a string parameter. PHP then called __toString() on an object without any check. The input of tests is now checked like the input of filters (CVE-2026-133764).

Since 3.28.0, the sandbox skips the __toString() check of an argument whose parameter can't hold a string. A spread argument was mapped to a single parameter, although it fills every parameter from its position on. It's now checked unless all these parameters have a safe type (CVE-2026-133766).

html_cva(), in twig/html-extra, converted the values of variants to arrays. A sandboxed template could therefore print the private and protected properties of any object of its context. Objects are now dropped and never match a variant (CVE-2026-133769).

The sandbox allowed every method of Twig\Markup objects, their constructor included, so a sandboxed template could replace the content of any Markup object it reaches. Calling this constructor is no longer allowed (CVE-2026-133770).

The html_to_markdown filter, in twig/markdown-extra, kept a converter for each mapping of options it received, without any limit. A template passing options taken from the input could fill the memory of long-running workers. The filter now keeps at most 100 converters (CVE-2026-133773).

Two more issues were fixed in 3.31.0, and their advisories are published today. The escaper considered x.attr safe whenever a macro alias named x was imported anywhere in the template. Variables with this name in other blocks and macros were then printed without escaping (CVE-2026-133771). And the branch of a guard tag that isn't selected still registered its extends, use, macro and block tags, which the sandbox didn't check (CVE-2026-133772).

Hardening

This release also makes the sandbox stricter in places where no vulnerability is known:

  • compiled templates now depend on the autoescaping strategy and on the callables that are always allowed in the sandbox, so environments that differ by these options don't share them anymore;
  • the cache tag checks the sandbox policy before it converts its key and its tags to strings.

Full Changelog

  • CVE-2026-133763 Check __toString() on the result of tests in the sandbox
  • CVE-2026-133764 Check __toString() on the input of tests in the sandbox
  • CVE-2026-133765 Check __toString() on the result of assignment expressions in the sandbox
  • CVE-2026-133766 Check __toString() on spread arguments in the sandbox
  • CVE-2026-133767 Check the sandbox policy against dynamic embed parent expressions
  • CVE-2026-133768 Stop the join filter from exposing the properties of non-iterable objects
  • CVE-2026-133769 Stop html_cva from exposing the properties of objects used as classes
  • CVE-2026-133770 Prevent sandboxed templates from calling the Markup constructor
  • CVE-2026-133773 Limit the number of converters kept by html_to_markdown
  • #5046 Include the autoescape strategy and always-allowed sandbox registrations in compiled template keys (@fabpot)
  • #5047 Check the sandbox policy before the cache tag coerces its key and tags to strings (@fabpot)
  • #5049 Fix arrow functions in the parent expression of extends and embed tags (@nicolas-grekas)
Published in #Releases #Twig