CVE-2026-46633: 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
PHP code injection via {% use %} template name
— Packagist
Twig is a template language for PHP. Starting in 1.15.1 and 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
debian/php-twigto a version that resolves this vulnerability.Fixed in 3.5.1-1+deb12u3Fixed in 3.27.0-0+deb13u1Fixed in 3.30.0-1 - Upgrade
Upgrade
debian/php-twigto a version that resolves this vulnerability.Fixed in 3.5.1-1+deb12u3 - Upgrade
Upgrade
debian/php-twigto a version that resolves this vulnerability.Fixed in 3.27.0-0+deb13u1 - Upgrade
Upgrade
debian/php-twigto a version that resolves this vulnerability.Fixed in 3.26.0-1 - Upgrade
Upgrade to a fixed release to a version that resolves this vulnerability.
Fixed in 3.26.0
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.