CVE-2026-46633: Twig: PHP code injection via `{% use %}` template name
Description
Compiler::string() escapes ", $, \, NUL and TAB when generating PHP double-quoted string literals, but does not escape single quotes. In ModuleNode::compileConstructor(), the template name from a {% use %} tag is compiled via subcompile() -> string() and placed inside a surrounding PHP single-quoted string literal. A template name containing a single quote terminates that surrounding string early, allowing arbitrary PHP expressions to be injected into the compiled cache file.
The injected code executes within the PHP process when the cache file is first loaded, bypassing the Twig sandbox entirely and achieving remote code execution. SecurityPolicy unconditionally allows {% use %} regardless of the configured allowedTags, so this primitive is reachable from sandboxed templates as well.
Resolution
Compiler::string() now also escapes single quotes so that template names placed inside single-quoted PHP literals can no longer break out of the surrounding context.
Credits
Twig would like to thank Anvil Secure in collaboration with Claude and Anthropic Research for reporting the issue and providing the fix.
Other sources
Twig is a template language for PHP. Prior to 3.26.0, Compiler::string() does not escape single quotes when a template name from a {% use %} tag is placed inside a PHP single-quoted string literal, allowing a crafted template name to terminate the string and inject arbitrary PHP expressions into the compiled cache file. This issue is fixed in version 3.26.0.
— MITRE
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
composer/twig/twigto a version that resolves this vulnerability.Fixed in 3.26.0 - Upgrade
Upgrade
Twig (Twig template engine for PHP)to a version that resolves this vulnerability.Fixed in 3.26.0 - Configuration
Update Twig so Compiler::string() escapes single quotes when template names (from `{% use %}` tags) are embedded inside surrounding PHP single-quoted string literals, preventing breakout into the compiled cache file.
Twig Compiler::string() escaping of single quotes in generated PHP single-quoted string literals = escape single quotes (')
Event History
Frequently Asked Questions
What is the severity of CVE-2026-46633?
CVE-2026-46633 has a high severity rating of 8.7.
How do I fix CVE-2026-46633?
To fix CVE-2026-46633, update the Twig library to the latest version provided by the maintainers.
What type of vulnerability is CVE-2026-46633?
CVE-2026-46633 is classified as a Code Injection vulnerability.
Which software is affected by CVE-2026-46633?
CVE-2026-46633 affects the Composer Twig library and Symfony Twig.
What component of Twig is vulnerable in CVE-2026-46633?
The vulnerability occurs in the handling of template names within the `{% use %}` tag in Twig.