Skip to content

Fix a variable not being escaped when it has the name of a macro import alias of another scope - #5039

Merged
fabpot merged 1 commit into
twigphp:3.xfrom
jack-worman:fix-import-alias-autoescape
Nov 18, 2026
Merged

fabpot merged 1 commit into
twigphp:3.xfrom
jack-worman:fix-import-alias-autoescape

Conversation

@jack-worman

Copy link
Copy Markdown
Contributor

The escaper treats x.attr, x['key'] and x.method() as safe whenever {% import … as x %} appears anywhere in the template. That rule (8832b66) dates from when macro calls compiled to attribute reads on the alias. Since 3.0 they compile to macro references (MacroReferenceExpression today), which are already safe, so the rule only matches ordinary variables that share the alias name in another block or macro, and prints them unescaped:

{% block header %}{% import _self as item %}{{ item.hello() }}{% endblock %}
{% block content %}{% for item in items %}{{ item.body }}{% endfor %}{% endblock %}

item.body is output raw. The same happens with an import in one macro and an argument of that name in a later macro, and with a top-level import placed after the block that uses the name.

This removes the rule. Macro calls through an alias stay safe through MacroReferenceExpression, and attribute(alias, 'name') can't reach macros, since aliases live in $macros, not in the context.

@fabpot
fabpot force-pushed the fix-import-alias-autoescape branch from d35e945 to 3a6745a Compare November 18, 2026 07:30
@fabpot

fabpot commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Thank you @jack-worman.

@fabpot
fabpot merged commit 31667a0 into twigphp:3.x Nov 18, 2026
@jack-worman
jack-worman deleted the fix-import-alias-autoescape branch November 18, 2026 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants