An untrusted template rendered in Twig's sandbox can use filters, tags and functions your security policy doesn't allow. The sandbox checked a template's allowed filters, tags and functions once, when the template was first loaded, and kept that answer for the rest of the process.

Who is affected

Apps on twig/twig up to and including 3.26.0 that use the sandbox (SandboxExtension with a SecurityPolicy) and render both sandboxed and normal templates with the same Twig\Environment. The check could be skipped when:

  • enableSandbox() or disableSandbox() is called between renders
  • the policy is swapped with setSecurityPolicy()
  • a SourcePolicyInterface gives a different answer
  • a parent, macro or included template was first loaded outside the sandbox

The Twig advisory says long-running workers that share one environment are the most exposed: FrankenPHP, RoadRunner and Symfony Messenger consumers. Allowed methods, properties and __toString() are checked on every call, so those lists still held. Apps that don't use the sandbox aren't affected.

What to do

Update twig/twig to 3.27.0. Twig now runs the allow-list check every time a template is rendered, extended, included or imported from, using the current sandbox state. Older compiled template cache files keep working.

Reported and fixed by Fabien Potencier.