GitHub Advisoryadvisory · Packagist
GHSA-7p85-w9px-jpjp Twig: PHP code injection via `{% use %}` template name
CRITICAL
Official page ↗
- Aliases
- CVE-2026-46633
- Ecosystem
- Packagist
- Packages
- twig/twig
- Severity
- Critical
- CVSS vector
- CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:H/I:H/A:H
- Weaknesses
- CWE-94
- Reviewed
- 2026-05-21
Affected packages
• Packagist twig/twig: >= 1.15.1, < 3.26.0 → fixed in 3.26.0
Description
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.
Related
Alias · CVE (1)
- CVE-2026-46633 Twig is a template language for PHP. vulnerability
Weakness · CWE (1)
- CWE-94 Improper Control of Generation of Code weakness
References
- https://github.com/twigphp/Twig/security/advisories/GHSA-7p85-w9px-jpjp web
- https://nvd.nist.gov/vuln/detail/CVE-2026-46633 advisory
- https://github.com/twigphp/Twig/commit/679447fa29083043665482ccf7d64372472621b8 web
- https://github.com/twigphp/Twig/commit/a2c6db7ea7d2c90923e6708019ec1442526d1c30 web
- https://github.com/twigphp/Twig/commit/e9ff55f6910832428e48a35b2e0748189ad49ae3 web
- https://gist.github.com/vladko312/d61b950f168c791db4786b83dc7a81e6 web
- https://github.com/FriendsOfPHP/security-advisories/blob/master/twig/twig/CVE-2026-46633.yaml web
- https://github.com/twigphp/Twig package
- https://github.com/twigphp/Twig/releases/tag/v3.26.0 web
- https://symfony.com/cve-2026-46633 web