CVE-2026-46640: Twig Arbitrary PHP Code Execution Vulnerability

HIGH
Advisory Identifier: GHSA-45vw-wh46-2vx8
CWE-94
Published
Advisory Summary

CVE-2026-46640 is a HIGH arbitrary php code execution vulnerability (CWE-94: Improper Control of Generation of Code ('Code Injection')) affecting Twig libraries in the Packagist ecosystem. The issue involves arbitrary php code execution via `_self.(<string>)` macro-reference compilation. Remediation is available by upgrading affected packages to version 3.26.0.

CVE-2026-46640 at a Glance

CVE Identifier CVE-2026-46640
GHSA Identifier GHSA-45vw-wh46-2vx8
Severity HIGH
CVSS v3.1 Base Score N/A
CWE Classification CWE-94: Improper Control of Generation of Code ('Code Injection')
Affected Product Twig
Package Ecosystem Packagist
Published Date
Last Updated

How to Fix CVE-2026-46640

To remediate this vulnerability, upgrade all affected Twig dependencies to a patched release. Both direct and transitive dependencies should be checked, as frameworks or security libraries may pull in vulnerable versions transitively.

twig/twig fixed: 3.26.0
Upgrade twig/twig to 3.26.0
Verify Direct and Transitive Dependencies

Run the following command in your project directory to verify that all resolved dependencies use the patched release:

inspect dependencies for twig/twig

How to Check if You Are Affected

To determine whether your application is affected by CVE-2026-46640, inspect your project dependencies for Twig:

inspect dependencies for twig/twig

Analyze the terminal output to locate any version of Twig in your dependency tree. If any resolved version matches the vulnerable ranges listed in the table below (for example, versions earlier than 3.26.0), your application is affected.

Note: Verify declared build manifests and deployed runtime container environments.

Technical Details

Vulnerability Mechanism & Technical Analysis

This vulnerability is classified under CWE-94: Improper Control of Generation of Code ('Code Injection'), where the product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment. In Twig, software state validation or input boundaries are circumvented during processing. Review the official source advisory statement below for specific affected components, classes, and reproduction details.

Source Advisory Statement

### 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.

CVE-2026-46640 Affected Twig Versions

1 component(s)
Fixed Versions:
3.26.0
Product / Package Ecosystem Affected Versions Fixed Version
twig/twig
Packagist
≥ 3.15.0
< 3.26.0
3.26.0

References

Frequently Asked Questions

What is CVE-2026-46640?
CVE-2026-46640 is a security vulnerability in Twig involving Arbitrary PHP code execution via `_self.(<string>)` macro-reference compilation under CWE-94: Improper Control of Generation of Code ('Code Injection').
Which Twig versions are affected?
Affected versions include releases prior to 3.26.0 across affected packages: twig/twig.
What version fixes CVE-2026-46640?
Upgrading to 3.26.0 (or the latest patched release for your respective branch) resolves the vulnerability.
Is twig/twig affected?
Yes, twig/twig is affected in releases prior to 3.26.0. Upgrade your dependency to 3.26.0 or later.
What CWE is associated with CVE-2026-46640?
CVE-2026-46640 is classified under CWE-94: Improper Control of Generation of Code ('Code Injection') (The product constructs all or part of a code segment using externally-influenced input from an upstream component, but it does not neutralize or incorrectly neutralizes special elements that could modify the syntax or behavior of the intended code segment.).
How can I check Packagist dependencies for CVE-2026-46640?
Run the dependency tree command: 'inspect dependencies for twig/twig'. This command lists direct and transitive dependencies in your build configuration so you can verify if a vulnerable version is resolved.
Machine-readable OSV JSON OSV v1.5 Schema
Normalized schema output
{
  "schema_version" : "1.4.0",
  "id" : "GHSA-45vw-wh46-2vx8",
  "modified" : "2026-06-05T20:33:13Z",
  "published" : "2026-05-21T21:31:08Z",
  "aliases" : [ "CVE-2026-46640" ],
  "summary" : "Twig: Arbitrary PHP code execution via `_self.(<string>)` macro-reference compilation",
  "details" : "### Description\n\nThe `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.\n\nAn 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.\n\n### Resolution\n\nThe 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.\n\n### Credits\n\nTwig would like to thank Claude Mythos Preview (via Project Glasswing) for reporting the issue and providing the fix.",
  "severity" : [ {
    "type" : "CVSS_V4",
    "score" : "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"
  } ],
  "affected" : [ {
    "package" : {
      "ecosystem" : "Packagist",
      "name" : "twig/twig"
    },
    "ranges" : [ {
      "type" : "ECOSYSTEM",
      "events" : [ {
        "introduced" : "3.15.0"
      }, {
        "fixed" : "3.26.0"
      } ]
    } ]
  } ],
  "references" : [ {
    "type" : "WEB",
    "url" : "https://github.com/twigphp/Twig/security/advisories/GHSA-45vw-wh46-2vx8"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/FriendsOfPHP/security-advisories/blob/master/twig/twig/CVE-2026-46640.yaml"
  }, {
    "type" : "PACKAGE",
    "url" : "https://github.com/twigphp/Twig"
  }, {
    "type" : "WEB",
    "url" : "https://github.com/vladko312/extras/blob/main/CVE-2026-46640.py"
  }, {
    "type" : "WEB",
    "url" : "https://symfony.com/cve-2026-46640"
  } ],
  "database_specific" : {
    "cwe_ids" : [ "CWE-94" ],
    "severity" : "HIGH",
    "github_reviewed" : true,
    "github_reviewed_at" : "2026-05-21T21:31:08Z",
    "nvd_published_at" : null
  }
}