Twig's sandbox lets you render templates written by people you don't fully trust, under a security policy that limits which methods they can call. Before this fix, a sandboxed template could call __toString() on an object even when the policy didn't allow it, as long as the object was inside an array or passed as an argument to a function or filter.

Who is affected

Apps that render untrusted templates with Twig's SandboxExtension, on twig/twig below 3.11.2, or 3.12.0 and later below 3.14.1. The attacker needs to be able to write templates, and gets whatever the blocked __toString() methods return. Apps that don't use the sandbox are not affected. Twig 1.x and 2.x are no longer maintained and stay vulnerable.

What to do

Update to 3.11.2 on the 3.11 branch, or to 3.14.1. The sandbox now checks __toString() calls on all objects. On Twig 1.x or 2.x, move to 3.x. See Twig's advisory and the fix.

Reported by Jamie Schouten.