The exploit leverages the '_self' variable, which in some Twig versions provides access to the Twig_Environment object. By registering a callback for undefined filters, the attacker can map an arbitrary PHP function (like 'exec') to a filter name. When the 'filter' is subsequently called, the engine executes the registered PHP function with the provided arguments, bypassing the sandbox's function whitelist.
{{ _self.env.registerUndefinedFilterCallback('exec') }}
{{ _self.env.getFilter('id') }}
Modern Twig versions (and patches for CVE-2026-46640) explicitly block access to the 'env' and 'template' properties of the '_self' object within the sandbox. Additionally, the 'registerUndefinedFilterCallback' method is now restricted to administrative contexts only.