CVE-2026-46640 — Vulnerability in twig/twig — CVE Details
Twig: Arbitrary PHP code execution via `_self.(<string>)` macro-reference compilation
What Should I Do?
Address in Next Maintenance Window
Low exploitation probability (0.64%). Schedule for your next planned update.
Does your project use an affected twig/twig version?
Paste your composer.lock to check it for CVE-2026-46640 and every other known vulnerability, with CISA KEV and EPSS on each finding. Free with no signup for up to 10 packages, or up to 500 per scan with a free account, which also keeps up to 200 packages in saved projects.
Summary
Description
The obj.(expr) dynamic-attribute syntax (added in 3.15.0 as the replacement for the deprecated attribute() function) lets the attribute be an arbitrary expression. When the receiver is _self (or any {% import %} alias) and the parenthesised expression is a string literal, DotExpressionParser short-circuits to the macro-call path and concatenates the attacker-controlled string into a MacroReferenceExpression name with no identifier validation. MacroReferenceExpression::compile() then emits that name raw into the generated PHP source.
An attacker who can supply template source can inject arbitrary PHP into the compiled template and execute it at template-load time, before checkSecurity() is ever called. This is a complete bypass of SandboxExtension, including a globally-enabled sandbox with an empty SecurityPolicy allowlist.
Resolution
The parser now validates that the dynamic attribute resolves to a valid macro identifier before routing through MacroReferenceExpression, and the macro-reference compiler emits the name through a properly escaped path.
Credits
Twig would like to thank Claude Mythos Preview (via Project Glasswing) for reporting the issue and providing the fix.
Advisory Timeline
- First published May 21, 2026 (189 days ago).
- Last revised September 10, 2026 — 111 days after initial publication.
Remediation
Upgrade to the fixed version using your package manager.
composer require "twig/twig:^3.26.0"
After upgrading, scan your dependencies again to confirm the vulnerability is resolved.
Affected Packages (1)
Affects 1 ecosystem: Packagist (1).
| Package | Fixed In |
|---|---|
| twig/twig Packagist · Affected: v3.15.0, v3.16.0, v3.17.0, v3.17.1 (+11 more) | 3.26.0 |
Vulnerability Classification
Common Weakness Enumeration (CWE) identifiers for this vulnerability type.
- CWE-94Code InjectionMITRE
CVSS Score Breakdown
What the CVSS (Common Vulnerability Scoring System) 8.8 score means for each attack dimension.
CVSS:4.0/AV:N/AC:L/AT:N/PR:L/UI:N/VC:H/VI:H/VA:H/SC:N/SI:N/SA:N
References
- https://github.com/twigphp/Twig/security/advisories/GHSA-45vw-wh46-2vx8WEB
- https://github.com/FriendsOfPHP/security-advisories/blob/master/twig/twig/CVE-2026-46640.yamlWEB
- https://github.com/twigphp/TwigPACKAGE
- https://github.com/vladko312/extras/blob/main/CVE-2026-46640.pyWEB
- https://symfony.com/cve-2026-46640WEB
Frequently Asked Questions
- What is CVE-2026-46640?
- Twig: Arbitrary PHP code execution via `_self.(<string>)` macro-reference compilation. This vulnerability has been assigned a severity rating of HIGH (CVSS score: 8.8/10).
- How do I check if my project is affected by CVE-2026-46640?
- CVE-2026-46640 affects twig/twig. It has a 0.6% probability of exploitation within 30 days (EPSS score). Use GeekWala's free vulnerability scanner to check your dependencies against CVE-2026-46640 and 200,000+ other known vulnerabilities.
Severity & Exploitability
Significant impact if exploited, or exploitable with modest effort.
This is a below-median EPSS score — only 45.4% of tracked vulnerabilities score at or below it, so most tracked vulnerabilities are considered more likely to be exploited than this one.
Also Known As
Related CVEs
- CVE-2026-24425HIGH
Twig: Possible sandbox bypass when using a source policy
- CVE-2026-46639HIGH
Twig: Sandbox property and method bypass via object-destructuring assignment
- CVE-2026-47732HIGH
Twig: Sandbox: multiple `__toString()` policy bypasses via unguarded string coercion points
- CVE-2026-46634MEDIUM
Twig: `template_from_string()` escapes a SourcePolicy-driven sandbox via synthesized template name
- CVE-2026-47730LOW
Twig: XSS in profiler HtmlDumper via unescaped template and profile names
- CVE-2026-46628LOW
Twig: The `spaceless` filter implicitly marks its output as safe
- CVE-2026-48805LOW
Twig: Sandbox state regression in deprecated internal wrappers in `src/Resources/core.php`
- CVE-2026-46635LOW
Twig: Sandbox property allowlist bypass via the `column` filter (array_column on objects)