Skip to main content
Blue Moose logo Blue Moose Field notes
← All articles
twig

Twig 3.31.1 patches nine sandbox bypasses at once

A wooden raised garden bed with a hinged netted lid, like a literal sandbox

Most Twig security writing is about injection: someone gets a template string into your renderer. Twig 3.31.1, released on 24 November, is about the layer you reach for after you accept that risk. It patches nine sandbox-related issues in one go, and the advisories show how many small places a sandbox policy has to be enforced before it means anything.

What actually shipped

The 3.31.1 release notes list nine security entries: html_to_markdown converter limits, __toString() checks on spread arguments, test inputs and results and assignment expressions, the sandbox policy on dynamic embed parents, the Markup constructor, and property exposure through html_cva and the join filter. The advisory list published on 24 November rates most of them Low and three Moderate: the join filter, the embed parent and assignment/destructuring expressions. Affected versions are everything before 3.31.1.

Two I read in full

The embed one (GHSA-fmp9-6wxr-cfmq, CVE-2026-133767) is the clearest. When the parent of an embed tag is an expression rather than a plain string or variable, the sandbox never inspected it. Functions, filters and tests inside it were not checked against the policy, and neither were string conversions. The advisory's example is {% embed 'leak_' ~ app_secret() %}{% endembed %}. A sandboxed template allowed to use embed could call any function, filter or test, and trigger any __toString(). Method calls and property reads stay covered because those are checked at runtime. CVSS v4 is 5.3.

The join one (GHSA-p2x6-ppf3-32r3, CVE-2026-133768) is almost funny in how small it is. The filter converted a non-iterable value with (array) $value, and casting an object to an array exposes every property, private and protected included. So {{ user|join(',') }} could print properties the property allow-list was supposed to hide. The fix joins a non-iterable as a single item: a Stringable prints its checked string, anything else throws, matching what first and last already did. Also CVSS 5.3, confidentiality Low.

I only read those two advisories in full. For the other seven I have the titles and the release note lines, not the mechanisms, so I won't pretend to describe them further.

Do you care?

Only if you run a sandbox. Twig's sandbox is opt-in: it matters when untrusted or semi-trusted people write templates, for example editors in a CMS who are allowed Twig but not your whole object graph. Plain theme templates written by your own developers are not in the threat model. Both advisories also list the privilege requirement as low, not none, so this is about an authenticated template author, not an anonymous visitor.

That said, Craft CMS is the obvious place this lands for me. Its 5.12.3 release, a day later, updated Twig to 3.31.1 and fixed a low-severity sandbox bypass that let templates replace the content of Twig\Markup objects, which lines up with the constructor entry above. I'm connecting those two from the release notes' wording, not from a Craft advisory.

What I'm doing about it

  • Run composer show twig/twig on every project and bump anything below 3.31.1. Note that Twig 4.0.0-beta1 appeared the same day; a beta is not what you want for this.
  • Grep for SecurityPolicy or sandbox config to find out whether any of my sites actually use the sandbox.
  • If you do, review the allow-lists for embed, join and html_cva. The cheapest hardening is a shorter allow-list.

The takeaway for me: a sandbox is a long list of choke points, and every new filter or tag is another one to guard. Nine fixes in one release does not mean Twig is sloppy, it means someone methodically audited the policy. I'd rather have that than silence, and I'd rather not need the sandbox at all.

BM
Blue Moose
The moose behind Blue Moose. Full-stack PHP developer — Drupal by day, Symfony by night, tests always.