File tree Expand file tree Collapse file tree 4 files changed +12
-17
lines changed
Expand file tree Collapse file tree 4 files changed +12
-17
lines changed Original file line number Diff line number Diff line change 77
88 The ``html_attr `` function was added in Twig 3.24.
99
10- The ``html_attr `` function renders HTML attributes from one or more mappings.
11- The mappings contain the names of HTML attributes as keys, and the corresponding
12- values represent the attributes' values. Escaping is applied for the attribute
10+ The ``html_attr `` function renders HTML attributes from one or more mappings,
11+ taking care of proper escaping. The mappings contain the names of HTML
12+ attributes as keys, and the corresponding values represent the attributes'
1313values.
1414
1515.. note ::
1616
17- Attribute names are **not ** escaped, to allow using names ``v-bind:href ``
18- or ``@click `` required by some front-end frameworks that would be escaped
19- by the ``html_attr `` strategy. Avoid using attribute names from user input
20- or untrusted sources, or apply escaping with ``|e('html_attr') `` _before_
21- using such data for attribute names.
17+ Attribute names are escaped using the ``html_attr_relaxed `` strategy.
2218
2319.. code-block :: html+twig
2420
Original file line number Diff line number Diff line change @@ -248,7 +248,7 @@ public static function htmlAttr(Environment $env, iterable|string|false|null ...
248248 continue ;
249249 }
250250
251- $ result .= $ name .'=" ' .$ runtime ->escape ($ value ).'" ' ;
251+ $ result .= $ runtime -> escape ( $ name, ' html_attr_relaxed ' ) .'=" ' .$ runtime ->escape ($ value ).'" ' ;
252252 }
253253
254254 return trim ($ result );
Original file line number Diff line number Diff line change 22"html_attr" function
33--TEMPLATE--
44Simple attributes: <tag {{ html_attr({ foo: 'bar' }, { bar: 'baz' }) }}/>
5- {% set untrusted_input = 'unsafe="yes" usage' %}
6- Escaping of the value only: <tag {{ html_attr({ (untrusted_input): '<>&\'"' }) }}/>
7- Properly escaped attribute name: <tag {{ html_attr({ (untrusted_input|e('html_attr')): '<>&\'"' }) }}/>
5+ Relaxed attribute name escaping: <tag {{ html_attr({ 'v-bind:href': 'url', ':[key]': 'url', '@click': 'doSomething' }) }} />
6+ Appropriate escaping: <tag {{ html_attr({ 'untrusted name': '<>&\'"' }) }}/>
87Empty attribute list: <tag {{ html_attr([], {}, null) }}/>
98Using a short ternary: <tag {{ html_attr({foo: 'bar'}, false ? {bar: 'baz'}) }}/>
109boolean true attribute: <tag {{ html_attr({ checked: true}) }}/>
@@ -23,8 +22,8 @@ merging a "comma separated token list" value with more array values: <img {{ htm
2322return []
2423--EXPECT--
2524Simple attributes: <tag foo="bar" bar="baz"/>
26- Escaping of the value only : <tag unsafe="yes" usage="<>&'"" />
27- Properly escaped attribute name : <tag unsafe="yes"&# x20;usage ="<>&'""/>
25+ Relaxed attribute name escaping : <tag v-bind:href="url" @[key]="url" @click="doSomething" />
26+ Appropriate escaping : <tag untrusted&# x20;name ="<>&'""/>
2827Empty attribute list: <tag />
2928Using a short ternary: <tag foo="bar"/>
3029boolean true attribute: <tag checked=""/>
Original file line number Diff line number Diff line change @@ -141,10 +141,10 @@ public static function htmlAttrProvider(): \Generator
141141 ];
142142
143143 // Escaping
144- yield 'attribute name is _not_ escaped ' => [
145- 'this is dangerous: fail @ xss ="123" ' ,
144+ yield 'attribute name is escaped ' => [
145+ 'data-user id ="123" ' ,
146146 [
147- ['this is dangerous: fail @ xss ' => '123 ' ],
147+ ['data-user id ' => '123 ' ],
148148 ],
149149 ];
150150
You can’t perform that action at this time.
0 commit comments