Twig RCE via Macro-Reference Compilation (CVE-2026-46640)
A vulnerability in Twig versions 3.15.0 to 3.26.0 (CVE-2026-46640) allows arbitrary PHP code execution via the `_self.(<string>)` macro-reference compilation, enabling attackers to inject and execute arbitrary PHP code by supplying malicious template source, bypassing the SandboxExtension.
A critical security flaw exists in Twig, a templating engine for PHP, specifically affecting versions 3.15.0 up to (but not including) 3.26.0. The vulnerability, identified as CVE-2026-46640, stems from the obj.(expr) dynamic-attribute syntax, which was introduced in version 3.15.0 as a replacement for the deprecated attribute() function. When the receiver is _self or an imported alias, and the expression is a string literal, the DotExpressionParser incorrectly concatenates the attacker-controlled string into a MacroReferenceExpression without proper validation. This bypasses the SandboxExtension, even with a globally-enabled sandbox and an empty SecurityPolicy allowlist. An attacker who can control the template source can inject arbitrary PHP code into the compiled template, resulting in code execution at template-load time.
Attack Chain
- The attacker gains the ability to supply template source code to the Twig engine. This could be achieved through methods like exploiting an existing file upload vulnerability or directly manipulating template files if access is available.
- The attacker crafts a malicious template containing the
_self.(<string>)syntax, where<string>is a PHP code injection payload. For example,_self.("system('whoami')"). - The Twig engine parses the malicious template, and the
DotExpressionParserhandles the_self.(<string>)expression. - The
DotExpressionParserincorrectly concatenates the attacker-controlled string into aMacroReferenceExpressionname without identifier validation, creating a malicious macro reference. - The
MacroReferenceExpression::compile()method then emits this raw, unvalidated name directly into the generated PHP source code. - The Twig engine loads and compiles the generated PHP source code, effectively executing the injected PHP code.
- The injected PHP code executes system commands (e.g.,
whoami) or performs other malicious actions. - The attacker gains unauthorized access to the system or data, potentially leading to further compromise, such as data exfiltration or lateral movement.
Impact
Successful exploitation of this vulnerability allows an attacker to execute arbitrary PHP code on the server, leading to complete system compromise. This can result in data breaches, service disruption, and potential financial loss. Given the widespread use of Twig in PHP-based web applications, a significant number of systems are potentially vulnerable. The bypass of SandboxExtension makes this particularly dangerous, as it circumvents common security measures intended to restrict code execution.
Recommendation
- Upgrade to Twig version 3.26.0 or later to patch CVE-2026-46640, as recommended in the GitHub advisory (https://github.com/advisories/GHSA-45vw-wh46-2vx8).
- Implement strict input validation and sanitization on any user-supplied data used in Twig templates to mitigate code injection risks.
- Deploy the Sigma rule “Detect Twig RCE via Macro Injection (CVE-2026-46640)” to identify exploitation attempts in web server logs.
Detection coverage 2
Detect Twig RCE via Macro Injection (CVE-2026-46640)
highDetects CVE-2026-46640 exploitation — attempts to inject PHP code via Twig macro injection by identifying suspicious patterns in HTTP requests targeting Twig templates.
Detect Twig RCE via eval-like Functions (CVE-2026-46640)
highDetects CVE-2026-46640 exploitation — usage of eval, assert, or create_function within Twig templates, indicating a potential RCE attempt
Detection queries are available on the platform. Get full rules →