CVE-2026-133765: Sandbox `__toString()` policy bypass via assignment and destructuring expressions
The sandbox checks that a template may call toString() on an object before converting it to a string. Since Twig 3.23.0, assignments like x = obj, and the destructuring forms [a] = list and {a} = obj, evaluate to the value that they assign. The sandbox didn't check that value, so a sandboxed template could call a denied toString() method:
{{ (x = obj) }} {{ ({secret: s} = obj)|join }}
No tag needs to be allowed.
The sandbox now checks the result of assignments like the result of filters and functions. {% do x = obj %} and {% if (x = obj) %} still work, since they don't convert the value to a string.
Affected Software
Event History
Frequently Asked Questions
Who can exploit this issue?
An attacker needs the ability to render a sandboxed Twig template and access to an object whose toString() method is denied by the sandbox policy. The bypass occurs when an assignment or destructuring expression evaluates to that object and its result is converted to a string.
Does an attacker need any Twig tags to be allowed?
No. The issue can be exploited without any tag being allowed.
Which template patterns are relevant when assessing exposure?
Review rendered assignment expressions such as {{ (x = obj) }} and destructuring expressions whose result is passed through a string-converting operation, such as {{ ({secret: s} = obj)|join }}. Twig versions since 3.23.0 are described as evaluating these expressions to their assigned value.
Are assignments safe when they are not rendered as strings?
{% do x = obj %} and {% if (x = obj) %} still work because those contexts do not convert the assigned value to a string. The vulnerable behavior concerns assignment or destructuring results that are converted to strings.