The exploit leverages Twig's 'filter' filter, which accepts a callback. In vulnerable versions, the security policy failed to adequately validate the callback if it was a built-in PHP function like 'system'. By passing an array containing the command (e.g., 'id') and applying the 'filter' with 'system' as the argument, the attacker can execute arbitrary code. This bypasses the intended restriction on direct function calls within the sandbox.
{{ ['id']|filter('system') }}
{# Logic: Bypassing the security policy by using the 'filter' filter to call 'system' #}
{# The bypass relies on Twig's internal handling of callbacks within filters. #}
The fix involves hardening the 'SecurityPolicy' class to explicitly check callbacks against a whitelist of allowed functions and ensuring that dangerous functions like 'system', 'exec', and 'passthru' are strictly prohibited, even when called indirectly through filters.