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.

