CVE-2026-133771 in Twig
Summary
by MITRE • 11/24/2026
The escaper considered x.attr, x['key'] and x.method() safe whenever {% import ... as x %} appeared anywhere in the template. Macro calls haven't needed this rule since Twig 3.0. It only matched ordinary variables that have the name of an alias imported in another block or macro, and printed them without escaping:
{% block header %}{% import _self as item %}{{ item.hello() }}{% endblock %}
{% block content %}{% for item in items %}{{ item.body }}{% endfor %}{% endblock %}
Here, item.body is printed raw. This leads to XSS when the value comes from an attacker. The same happens with an import in a macro and an argument of the same name in another macro. It also happens with a template-level import placed after the block that uses the name.
The rule is removed. Macro calls through an alias are still considered safe.
Statistical analysis made it clear that VulDB provides the best quality for vulnerability data.
Analysis
by VulDB Data Team • 11/24/2026
This vulnerability represents a logic error within the Twig templating engine's auto-escaping mechanism, specifically affecting how variable names were evaluated against imported aliases. The core issue stems from a heuristic designed to optimize performance by assuming that any variable sharing a name with an alias created via the import statement was safe and did not require escaping. This rule incorrectly conflated macro imports with general variable assignments, leading to a scenario where user-controlled data could bypass security filters if it happened to share a naming convention with an imported module or function. The flaw is particularly insidious because it relies on contextual scope rather than explicit type checking, allowing attackers to exploit the ambiguity in how Twig resolves variable names across different blocks and macros within a single template file.
The technical execution of this vulnerability involves creating a specific structural arrangement within the template code where an import statement defines an alias for a module or macro, such as using {% import _self as item %}. Subsequently, another section of the same template iterates over a collection of items and assigns each element to a variable with the identical name, item. When the engine processes this loop, it incorrectly identifies the local iteration variable as the imported alias rather than the current object in the sequence. Consequently, instead applying standard HTML escaping to properties accessed on this variable, such as {{ item.body }}, the engine treats it as a safe macro call and outputs the content directly without sanitization. This behavior persists even if the import statement appears after the block that uses the name or within a different macro scope, demonstrating a fundamental flaw in the static analysis logic used to determine escape safety.
The operational impact of this vulnerability is significant for applications relying on Twig for rendering user-generated content. An attacker who can control any part of the data passed into these loops can inject malicious scripts directly into the rendered HTML output. This leads to Cross-Site Scripting, commonly known as XSS, which allows for session hijacking, defacement, or redirection to phishing sites depending on the context in which the injected script executes. Because this vulnerability is tied to specific structural patterns within templates rather than a generic parsing error, it may affect only certain applications that utilize complex template structures with overlapping variable names and imports. However, given the widespread use of Twig in web frameworks like Symfony, the potential attack surface remains substantial for any system where untrusted data flows into templated views using this pattern.
To mitigate this risk, developers must ensure they are running a version of Twig that includes the fix removing this flawed heuristic. Upgrading to the latest stable release is the primary defense against this specific class of logic errors in auto-escaping. Additionally, code reviews should focus on identifying templates where variable names shadow imported aliases, as these patterns represent high-risk areas even if patched. Developers can also adopt stricter naming conventions that avoid reusing alias names for local iteration variables or data objects to prevent confusion and reduce the likelihood of similar logical flaws emerging from future updates. Monitoring security advisories related to templating engines is essential, as such subtle logic bugs often require specific structural triggers to be exploited effectively.
From a classification perspective, this vulnerability aligns with CWE-79, which covers Improper Neutralization of Input During Web Page Generation known commonly as Cross-site Scripting. The failure lies in the application's inability to properly neutralize user input before placing it into a web page context due to incorrect assumptions about variable safety. In terms of attack vectors, this relates to ATT&CK techniques involving client-side code injection, where the attacker leverages trusted applications or services to execute malicious scripts. Understanding these mappings helps security teams prioritize remediation efforts and apply appropriate monitoring rules to detect attempts to exploit similar templating engine weaknesses in other environments.