Skip to content

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

Open
jack-worman wants to merge 2 commits into
twigphp:3.xfrom
jack-worman:fix-import-alias-autoescape
Open

jack-worman wants to merge 2 commits into
twigphp:3.xfrom
jack-worman:fix-import-alias-autoescape

Conversation

@jack-worman

Copy link
Copy Markdown

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.

…rt alias of another scope

Since 3.0, calls on an import alias compile to macro references, which are
always safe, so the import alias "safe variables" rule only ever matched
ordinary variables sharing the alias name, such as a loop variable or macro
argument in another block or macro.
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.

1 participant