/

CVE-2026-46633

Severity:

CRITICAL RISK

(9.8)

Code Injection (CWE-94) via Twig template name handling

CVE-2026-46633

Modified

Vulnerability type

Category:

Code Injection

CWE:

CWE-94

Impact:

Arbitrary Code Execution

Severity:

CRITICAL RISK

(9.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 template names used in {% use %} tags are improperly escaped when compiled into PHP single-quoted string literals, enabling crafted names to break out of the string context.

Under conditions where attackers can influence template names, this flaw can allow arbitrary PHP expression injection into the compiled cache file and lead to full code execution.

Affected context

This vulnerability appears in PHP applications that use Twig templates and rely on the template compilation cache.

It becomes relevant when template names from {% use %} tags can be influenced by data outside the template author's direct control, such as dynamic template resolution or user-configurable template references.

The issue manifests specifically when these names are compiled into PHP single-quoted strings without proper escaping, creating an opportunity for injected PHP expressions.

Exploit preconditions

  • Attack surface: Twig template compilation pipeline in PHP applications.
  • Required access: Ability to supply or modify the template name referenced in a {% use %} tag (e.g., via configuration, plugin systems, or user-controlled template selection mechanisms).
  • Trigger point: Compilation of a Twig template into a cached PHP file where the crafted template name is embedded in a single-quoted string literal.
  • Why exploitation is possible: Compiler::string() fails to escape single quotes in template names, allowing the attacker-controlled value to terminate the string and append arbitrary PHP expressions that execute when the cache file is loaded.

Why this happens

The underlying bug is an improper handling of untrusted data when generating executable code, aligning with CWE-94: Improper Control of Generation of Code ('Code Injection').

Twig's compiler takes template names and embeds them directly into PHP single-quoted string literals without escaping single quotes.

This creates a code-generation sink where data that should be treated as opaque identifiers is instead treated as part of the program source, enabling attackers to inject PHP expressions by breaking out of the string context during compilation.

Potential impact

  • Attackers can inject and execute arbitrary PHP code within the application context.
  • Compromise of sensitive application data and business logic.
  • Escalation to full server compromise depending on PHP and system permissions.
  • Tampering with application behavior, including disabling security checks or modifying output.
  • Establishment of persistent backdoors via manipulated compiled cache files.

General mitigation guidance

Effective mitigation focuses on controlling how data flows into code generation paths and reducing trust in template identifiers.

Recommended patterns include:

  • Treat template names and similar identifiers as data, not source code; ensure they are safely encoded or escaped before inclusion in generated code.
  • Centralize template resolution through a trusted mapping layer that never uses raw user input as a template name.
  • Enforce strict validation whitelists for template names, limiting them to known, static values.
  • Run PHP applications with least-privilege configurations, reducing impact if code-generation issues occur.
  • Prefer compilation and caching mechanisms that separate metadata from executable code rather than concatenating raw identifiers into source.

How teams detect issues like this

Detection can leverage both static and review-based approaches:

  • Static analysis targeting code-generation routines that concatenate unescaped identifiers or input into PHP source, especially within template engines and compilers.
  • Code review checklists that flag any use of unescaped variables in generated PHP strings or eval-like constructs.
  • Security policies requiring explicit escaping functions for any data injected into compiled templates or cache files.
  • Automated tooling integrated into CI/CD pipelines to search for patterns where user-influenced values are written into executable PHP cache files.

Sources

SourceLink
gist.github.comgist.github.com/vladko312/d61b950f168c791db4786b83dc7a81e6
github.comgithub.com/FriendsOfPHP/security-advisories/blob/master/twig/twig/CVE-2026-46633.yaml
github.comgithub.com/twigphp/Twig/commit/679447fa29083043665482ccf7d64372472621b8
github.comgithub.com/twigphp/Twig/commit/a2c6db7ea7d2c90923e6708019ec1442526d1c30
github.comgithub.com/twigphp/Twig/commit/e9ff55f6910832428e48a35b2e0748189ad49ae3
github.comgithub.com/twigphp/Twig/releases/tag/v3.26.0
github.comgithub.com/twigphp/Twig/security/advisories/GHSA-7p85-w9px-jpjp
symfony.comsymfony.com/cve-2026-46633

Frequently asked

questions

Exploitability depends on whether attackers can control template names used in {% use %} tags. In environments where template selection or naming is user or plugin-driven, the scope can be wide and lead to full code execution.

An attacker needs the capability to influence or set the template name that ultimately appears in a {% use %} tag during compilation. This could be through configuration files, extension points, or functionality that binds user input to template resolution.

The safest approach is to ensure all identifiers embedded into generated PHP code are properly escaped or encoded before insertion, and to strictly validate template names against a fixed set of allowed values managed by the application.

Prevent future occurrences by adopting secure code-generation practices: never mix untrusted data into executable source, enforce validation and escaping for all compilation inputs, and include template engines and cache writers in regular security reviews and automated scanning.

Find out what's already
exploitable in your codebase.