Twig: PHP code injection via `{% use %}` template name
Published 21 May 2026 · Updated 4 Nov 2026
Is your project on an affected twig/twig?
Look after several Laravel or PHP apps? Indie watches up to 15 repos, private ones too, for $5 a month. See Indie
Affected package
- Affected
- >=1.15.1 <3.26.0
- Fixed in
- 3.26.0
composer require twig/twig:^3.26.0Details
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.
Is GHSA-7p85-w9px-jpjp in any of your client repos?
Checking one package by hand is fine. Checking it across forty client projects is not. NPMscan reads the lockfiles in every GitHub repo you pick, finds each project with an affected twig/twig, and gives you the command that fixes it.
More advisories for twig/twig
- HighCVE-2026-49981Twig: Sandbox filter, tag and function allow-list bypass when sandbox state changes between renders for a cached `Template` · 1 Jul 2026
- MediumCVE-2026-48808Twig: Sandbox property allowlist bypass via the `column` filter under `SourcePolicyInterface` · 30 Jun 2026
- MediumCVE-2026-48807Twig: Sandbox `__toString()` policy bypass via `Traversable` in `join` and `replace` filters · 30 Jun 2026
- MediumCVE-2026-48806Twig: Sandbox `__toString()` policy bypass via dynamic mapping keys · 30 Jun 2026
- LowCVE-2026-48805Twig: Sandbox state regression in deprecated internal wrappers in `src/Resources/core.php` · 30 Jun 2026
- HighCVE-2026-47732Twig: Sandbox: multiple `__toString()` policy bypasses via unguarded string coercion points · 5 Jun 2026
References
- Linkgithub.com/twigphp/Twig/security/advisories/GHSA-7p85-w9px-jpjp
- Advisorynvd.nist.gov/vuln/detail/CVE-2026-46633
- Linkgithub.com/twigphp/Twig/commit/679447fa29083043665482ccf7d64372472621b8
- Linkgithub.com/twigphp/Twig/commit/a2c6db7ea7d2c90923e6708019ec1442526d1c30
- Linkgithub.com/twigphp/Twig/commit/e9ff55f6910832428e48a35b2e0748189ad49ae3
- Linkgist.github.com/vladko312/d61b950f168c791db4786b83dc7a81e6
- Linkgithub.com/FriendsOfPHP/security-advisories/blob/master/twig/twig/CVE-2026-46633.yaml
- Packagegithub.com/twigphp/Twig
- Linkgithub.com/twigphp/Twig/releases/tag/v3.26.0
- Linksymfony.com/cve-2026-46633
Advisory data from OSV.dev (opens OSV.dev in a new tab), refreshed daily.