Skip to content

The publication for web craftspeople Wednesday, 25 November 2026

Security

Twig 3.31.1 Patches Nine Security Flaws, Seven in the Sandbox

Twig 3.31.1 fixes nine security flaws disclosed the same day, seven of them in 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.

Twig 3.31.1 corrige neuf failles de sécurité, dont sept dans le sandbox

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

ReferenceIssueSeveritySandbox required
CVE-2026-133763The is true test returned the tested object uncheckedModerateYes
CVE-2026-133765Assignment and destructuring results weren’t verifiedModerateYes
CVE-2026-133767The parent expression of an embed tag wasn’t checkedModerateYes
CVE-2026-133768The join filter exposed private properties of a non-iterable objectModerateYes
CVE-2026-133764Custom test input wasn’t verifiedLowYes
CVE-2026-133766Spread arguments weren’t checked before __toString()LowYes
CVE-2026-133770The Twig\Markup constructor remained reachable from the sandboxLowYes
CVE-2026-133769html_cva() (twig/html-extra) exposed private propertiesLowNo
CVE-2026-133773Unbounded converter cache in twig/markdown-extraLowNo

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.

As with the batch of fixes Laravel shipped the week before, none of these flaws got an isolated alert: the update is the protection, not a setting to tweak by hand. A project that sandboxes no template at all remains exposed only to CVE-2026-133769 and CVE-2026-133773, the two flaws that don’t require the sandbox.

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.

Read next