Twig 3.31.1 shipped on November 24, 2026, alongside an unusual batch of eleven security advisories published the same day. Nine of these flaws are fixed by this release, seven of them affecting the sandbox engine used to run untrusted templates. The first beta of Twig 4.0, released in parallel, locks that sandbox down further by default.
Eleven security advisories in a single day
The Symfony blog, which hosts Twig’s security advisories, published eleven CVE entries on November 24, from CVE-2026-133763 to CVE-2026-133773. Two of them, CVE-2026-133771 and CVE-2026-133772, had already been fixed by version 3.31.0; the other nine are addressed in 3.31.1. Fabien Potencier notes that applications using no sandboxed templates at all are only affected by two of these nine flaws.
What 3.31.1 fixes, in detail
| Reference | Issue | Severity | Sandbox required |
|---|---|---|---|
| CVE-2026-133763 | The is true test returned the tested object unchecked | Moderate | Yes |
| CVE-2026-133765 | Assignment and destructuring results weren’t verified | Moderate | Yes |
| CVE-2026-133767 | The parent expression of an embed tag wasn’t checked | Moderate | Yes |
| CVE-2026-133768 | The join filter exposed private properties of a non-iterable object | Moderate | Yes |
| CVE-2026-133764 | Custom test input wasn’t verified | Low | Yes |
| CVE-2026-133766 | Spread arguments weren’t checked before __toString() | Low | Yes |
| CVE-2026-133770 | The Twig\Markup constructor remained reachable from the sandbox | Low | Yes |
| CVE-2026-133769 | html_cva() (twig/html-extra) exposed private properties | Low | No |
| CVE-2026-133773 | Unbounded converter cache in twig/markdown-extra | Low | No |
The clearest case: __toString() calls nobody thought were still reachable
Twig’s sandbox exists to run a template supplied by an untrusted source (a third-party theme, user-submitted content) without letting it call arbitrary PHP methods. Several of the fixed flaws share the same pattern: a syntactic detour was enough to trigger a __toString() call the security policy was supposed to block, bypassing the exact path the sandbox was watching.
{# Before 3.31.1: destructuring bypassed the sandbox policy #}
{% set [a, b] = [forbidden_object, 'value'] %}
{{ a }} {# could trigger __toString() with no check #}
{# Same pattern via a call's spread arguments #}
{{ some_function(...[forbidden_object]) }}
A sandbox that checks one execution path out of two isn’t half as safe: it’s a sandbox you should assume can be bypassed.
Twig 4.0.0-beta1, released the same day, changes how sandboxing works
Twig 4.0’s first beta makes the sandbox a first-class citizen: a render_sandboxed() function replaces the sandboxed argument of include(), and the parent(), block() and attribute() functions now go through the security policy instead of bypassing it by design. for loops that don’t use the loop variable now iterate natively, cutting executed instructions by more than 20% on a reference Symfony template.
{% for user in users if user.active %}
- {{ user.username }}
{% else %}
- No active user
{% endfor %}
For extension authors, the most concrete break concerns cloning a Twig\Environment, which now throws an exception, and the TwigCallableInterface, which gains an isAlwaysAllowedInSandbox() method to implement. The recommended migration path is still to clear every deprecation flagged by the latest 3.x release before moving to the beta.
Updating without waiting
Moving to 3.31.1 is an ordinary patch update, with no documented breaking change. For any project that renders sandboxed templates (customizable themes, content previews), it deserves to be prioritized rather than folded into the usual update cycle, using the same command-line tools as any ordinary Composer update.
composer update twig/twig twig/html-extra twig/markdown-extra
php bin/console cache:clear
What to remember
Twig 3.31.1 closes nine security flaws in a sandbox many projects treat as a protection you can set and forget. Seven of them only affect sandboxed templates, but that’s precisely the use case the sandbox exists for. The Twig 4.0 beta, shipped the same day, confirms that this hardening continues beyond the 3.x branch.
Eleven security advisories on the same day looks like a one-off incident, but it’s the opposite: it signals a thorough audit of the whole sandbox rather than a single fix following an external report. On any project exposing customizable templates to untrusted users, this update is worth treating as urgent, even without a known exploit in the wild. — Simon Janvier
Further reading: the full 3.31.1 release announcement and its linked security advisories are available on the Symfony blog.
