Home/ Engineering Insights/ Twig Sandbox Bypasses (CVE-2026-133763 to CVE-2026-133773): Upgrade to 3.31.1

Twig Sandbox Bypasses (CVE-2026-133763 to CVE-2026-133773): Upgrade to 3.31.1

Source. This article is based on Symfony Blog: CVE-2026-133764 Twig sandbox __toString() policy bypass (batch of Twig advisories). Details were checked against it on November 25, 2026; check the source for later changes.

On 24 November 2026 the Symfony project published a batch of security advisories for Twig, the PHP template engine. Eleven CVEs sit in the batch, from CVE-2026-133763 to CVE-2026-133773. Nine of them concern the Twig sandbox, one is an escaping bug that can lead to XSS, and one is unbounded memory growth in a Markdown extension. The advisories say the fixes ship in Twig 3.31.0 and 3.31.1, so 3.31.1 is the version to aim for. We read the advisories on symfony.com and the Packagist advisory feed. None of the advisories we read gave a severity rating, so we give none either.

Start from the first one: CVE-2026-133764 on symfony.com. Each CVE has its own page under the same pattern.

Who is affected

Two groups of applications are affected, and the split matters.

  • Everyone who renders templates with an attacker-influenced value. CVE-2026-133771 is a missing-autoescape bug in Twig before 3.31.0. It does not need the sandbox.
  • Applications that use the Twig sandbox, for example to let editors or customers write templates. Most of the other CVEs let a sandboxed template do something its policy should forbid. If you never enable the sandbox, those do not apply to you, though upgrading is still sensible.

The Packagist feed lists the affected range for most of these as below 3.31.1. Two of them, CVE-2026-133771 and CVE-2026-133772, are listed as below 3.31.0. Several entries also cover the 1.x and 2.x lines, so an old pinned Twig is not a safe place to hide.

Check your installed version

composer show twig/twig | grep versions
composer show 'twig/*' | grep -E 'twig/(twig|html-extra|markdown-extra)'

If you only have a composer.lock and no shell on the server, search it for "name": "twig/twig" and read the version on the next lines.

To find out whether the sandbox is in use, search the code base:

grep -rn "SandboxExtension\|SecurityPolicy\|enableSandbox\|{% sandbox" src templates

What the advisories describe

We summarise each in our own words. The advisory pages have the exact examples.

  • __toString() policy bypasses. The sandbox is supposed to check that a template may convert an object to a string. CVE-2026-133763 (the is true test), CVE-2026-133764 (input of custom tests), CVE-2026-133765 (assignment and destructuring expressions) and CVE-2026-133766 (spread arguments) each describe a path where that check was skipped. The advisories give 3.21.0, 3.23.0 and 3.28.0 as the starting versions for the first, third and fourth. CVE-2026-133764 is listed as affecting versions before 3.31.1. For CVE-2026-133764 and CVE-2026-133766 the advisory says an application is affected only if it registers and allows a suitable test, function or filter, and that no built-in callable is affected.
  • Embed parent expression (CVE-2026-133767). The advisory says that when the parent of an embed tag is an expression, the sandbox did not check the functions, filters and tests inside it. A sandboxed template allowed to use embed could call any of them.
  • Private property exposure (CVE-2026-133768 and CVE-2026-133769). The join filter and the html_cva() function in twig/html-extra cast non-array values with (array). For an object, the advisory says that exposes private and protected properties that the policy denies.
  • Markup constructor (CVE-2026-133770). The sandbox allowed calling __construct() on Twig\Markup objects. The advisory points to application-owned objects that are also rendered in trusted pages, such as a global in a long-running worker.
  • Guard tag (CVE-2026-133772). A branch of a guard tag that is not selected was still parsed. Tags inside it, such as extends or macro, registered their effects even though the policy did not allow those tags.
  • Autoescaping (CVE-2026-133771). A variable named like a macro import alias in another scope was treated as safe and printed without escaping. The advisory describes this as leading to XSS when the value comes from an attacker.
  • Markdown cache (CVE-2026-133773). The html_to_markdown filter in twig/markdown-extra kept a converter per distinct options array in a static, unbounded cache. The advisory says this can fill the memory of long-running workers. The fix caps it at 100 converters.

The fix

Per the advisories, upgrade Twig to 3.31.1. For the two extension packages the fixed release is also 3.31.1.

  1. Record the current versions: composer show twig/twig twig/html-extra twig/markdown-extra (the last two only if installed).
  2. Update only the Twig packages:
    composer update twig/twig twig/html-extra twig/markdown-extra --with-dependencies
    If a package is not installed, drop it from the command.
  3. If Composer refuses, read its message. A symfony/twig-bridge or framework constraint may hold Twig back. Raise that constraint rather than forcing Twig.
  4. Clear the compiled template cache and deploy. In Symfony that is php bin/console cache:clear. In other setups, delete the directory you pass as the Twig cache option.

The advisories name the patched branch as 3.x. We did not find a patched 1.x or 2.x release in the sources we read. If you are on those lines, the way out is an upgrade to the 3.x line, which in turn needs a supported PHP version. Check the Twig requirements on its documentation before you plan that.

If you cannot upgrade today

The advisories give no general workaround, so we do not invent one. What they do say can still narrow your exposure:

  • Several issues apply only when a custom test, function or filter is allowed in the sandbox policy. Review your allow-lists and remove entries you do not need.
  • CVE-2026-133767 needs a sandboxed template that is allowed to use embed. CVE-2026-133772 needs guard. Removing those tags from the policy removes those two paths. That is our reading of the advisories, not a statement from the maintainers.
  • Do not let untrusted users write templates until you have upgraded.

How to confirm the fix

composer show twig/twig | grep versions
composer audit

composer audit reads the Packagist advisory data, so it should stop reporting these ids once you are on 3.31.1. We have not run these checks against a real project for this post. Also run your own template and sandbox tests, and read the Twig changelog for any behaviour change you could hit. The advisories say, for example, that the join filter now treats a non-iterable value as a single item and throws for objects that are not Stringable, which could change output in a template that relied on the old behaviour.

When this points to a bigger problem

A batch like this is routine for a maintained project. It becomes a problem when the project is pinned to an old Twig because the framework, the PHP version or the theme layer cannot move. The same pin usually blocks other security fixes, such as the Symfony Config XXE fix and the Symfony MIME header injection fix. If you are in that position, our legacy PHP modernisation services cover the PHP upgrade, the dependency chain and the tests that make the next update a small job. Our guide to upgrading from PHP 5.6 or 7.x to PHP 8 and our modernisation roadmap show the order we work in.

Frequently asked questions

Which Twig version fixes CVE-2026-133763 to CVE-2026-133773?

The advisories name Twig 3.31.1 as the fix for most of them, and 3.31.0 for CVE-2026-133771 and CVE-2026-133772. Upgrading to 3.31.1 covers all of them.

Am I affected if I do not use the Twig sandbox?

Most of the batch concerns sandboxed templates only. CVE-2026-133771, the missing autoescaping issue, is described without the sandbox, so check your version either way.

Pinned to an old Twig or Symfony?

We upgrade legacy PHP applications and their template layer, and add the tests that make the next security update routine.

See our Legacy PHP Modernisation Services
Ravi Raghav, Founder & CEO of AllScript Lab
Written by Ravi Raghav Founder & CEO