/

CVE-2026-46640

Severity:

HIGH RISK

(8.8)

Code Injection (CWE-94) via Twig Dynamic Macro Resolution

CVE-2026-46640

Analyzed

Vulnerability type

Category:

Code Injection

CWE:

CWE-94

Impact:

Arbitrary Code Execution

Severity:

HIGH RISK

(8.8)

CVSS version:

3.1

Catch this CVE while code is being written

Detect vulnerable code directly in pull requests, so CVEs are fixed before merge.

Summary

Twig templates between versions 3.15.0 and 3.26.0 contain a code injection flaw in dynamic macro resolution using _self.(<string>) and import-alias attribute syntax.

If an attacker can influence the macro name string, they can cause raw PHP to be emitted into the compiled template and executed when the template is loaded.

Affected context

This vulnerability affects PHP applications that use Twig versions 3.15.0 through 3.25.x with templates relying on dynamic macro names via _self.(<string>) or imported alias attribute access.

It becomes relevant when untrusted or weakly validated input can flow into the macro name expression used to resolve template macros.

The issue appears during template compilation, where the macro reference is translated into underlying PHP code without sufficient identifier validation on the constructed macro name.

Exploit preconditions

  • Attack surface: Application routes or template-rendering paths where Twig templates are compiled and rendered.
  • Required access: Ability to control or significantly influence the string used as a dynamic macro name (e.g., request parameters, user-controlled configuration, or database-stored values rendered into templates).
  • Trigger point: Invocation of a template that uses _self.(<string>) or an import alias with a dynamic attribute expression to resolve a macro name.
  • Why exploitation is possible: Twig concatenates the attacker-controlled macro name into the generated PHP identifier without enforcing strict identifier rules or escaping, allowing injection of raw PHP fragments into the compiled template source.

Why this happens

The underlying bug stems from improper neutralization of user-influenced input in code generation logic, corresponding to CWE-94: Improper Control of Generation of Code.

Twig allows dynamic macro resolution using expressions like _self.(<string>) and import-alias attribute syntax, and these expressions are used to construct a MacroReferenceExpression that is later converted into PHP code.

Because the implementation did not sufficiently validate or restrict the macro identifier to safe characters, the engine effectively treated attacker-controlled strings as part of PHP code during template compilation.

This pattern converts data into executable code without a strict separation between template data and the underlying language, enabling arbitrary code execution when a crafted macro name is compiled and loaded.

Potential impact

  • Execution of arbitrary PHP code within the web server process.
  • Full compromise of the affected web application and its data.
  • Access to sensitive configuration, credentials, and environment variables.
  • Server-side pivoting to other internal services reachable from the application.
  • Defacement, data manipulation, or deployment of web shells for persistent access.

General mitigation guidance

Mitigations focus on preventing untrusted data from influencing code generation paths and tightening template macro usage patterns:

  • Avoid using dynamic macro names derived from user input; prefer static, hard-coded macro identifiers where possible.
  • Constrain macro name expressions to trusted sources or whitelist-validated values, enforcing strict allowed-character policies (e.g., alphanumerics and underscores only).
  • Separate presentation-level dynamic behavior (e.g., choosing which macro to call) from user-controlled values by mapping user input to a controlled, predefined set of macro names.
  • Configure template usage guidelines and code review practices that discourage passing raw request parameters or unvalidated model fields into template constructs that influence code generation.
  • Run PHP and template compilation under least-privilege accounts, reducing the impact scope of a successful code injection.

How teams detect issues like this

Detection strategies target both code-level patterns and runtime behavior:

  • Use static analysis or grep-based rules to locate Twig templates using _self.(...) or import-alias dynamic attribute access where the argument is not a literal string, especially when derived from request data or untrusted variables.
  • During code review, flag any use of dynamic macro resolution and require justification that inputs are strictly validated or drawn from trusted constants.
  • Introduce security-focused template linting that enforces policies around macro naming, disallowing expressions that can contain arbitrary characters or user-controlled content.
  • Employ application-level logging and monitoring to detect anomalous template compilation activity, such as unexpected changes in compiled template files or unusual PHP errors during template load.
  • Leverage automated security scanners that understand Twig syntax and can highlight potential code injection sinks within compiled or source templates.

Sources

SourceLink
github.comgithub.com/twigphp/Twig/commit/324fa60545694fa6abe85ded9befcb82e1066bc2
github.comgithub.com/twigphp/Twig/releases/tag/v3.26.0
github.comgithub.com/twigphp/Twig/security/advisories/GHSA-45vw-wh46-2vx8

Frequently asked

questions

Yes, it can be remotely exploitable if an attacker can control or significantly influence the dynamic macro name used in Twig templates, such as through HTTP parameters or fields persisted to the database and rendered into templates.

The attacker must be able to supply data that flows into the expression used for dynamic macro resolution. They typically do not need authenticated access if public-facing endpoints reflect user input into the relevant template constructs, although authentication requirements depend on the specific application.

The safest pattern is to avoid dynamic macro names altogether for untrusted data and restrict macro identifiers to static, predefined values. Where dynamic behavior is required, map user input to an internally maintained whitelist of macro names, and enforce strict validation and normalization of any value used in macro resolution.

To prevent similar problems, design templates so that user input never controls code-like constructs, including macro names, filters, or function calls. Establish secure coding guidelines for Twig usage, incorporate template-specific security checks into code review and CI pipelines, and rely on strict input validation and output encoding to maintain a clear separation between data and executable code.

Find out what's already
exploitable in your codebase.