Skip to content

Commit 11d9a17

Browse files
committed
Allow using [] in the relaxed escaping as well
1 parent f805215 commit 11d9a17

File tree

3 files changed

+7
-7
lines changed

3 files changed

+7
-7
lines changed

doc/filters/escape.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ documents:
5757
also when used as the value of an HTML attribute **without quotes**
5858
(e.g. ``data-attribute={{ some_value }}``).
5959

60-
* ``html_attr_relaxed``: like ``html_attr``, but **does not** escape ``@`` and ``:``
61-
characters. You may want to use this in combination with front-end frameworks that
62-
use attribute names like ``v-bind:href`` or ``@click``. But, be aware that in some
63-
processing contexts like XML, characters like the colon ``:`` may have meaning like
64-
for XML namespace separation.
60+
* ``html_attr_relaxed``: like ``html_attr``, but **does not** escape the ``@``, ``:``,
61+
``[`` and ``]`` characters. You may want to use this in combination with front-end
62+
frameworks that use attribute names like ``v-bind:href`` or ``@click``. But, be
63+
aware that in some processing contexts like XML, characters like the colon ``:``
64+
may have meaning like for XML namespace separation.
6565

6666
Note that doing contextual escaping in HTML documents is hard and choosing the
6767
right escaping strategy depends on a lot of factors. Please, read related

src/Runtime/EscaperRuntime.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ public function escape($string, string $strategy = 'html', ?string $charset = nu
267267

268268
$regex = match ($strategy) {
269269
'html_attr' => '#[^a-zA-Z0-9,\.\-_]#Su',
270-
'html_attr_relaxed' => '#[^a-zA-Z0-9,\._:@]#Su',
270+
'html_attr_relaxed' => '#[^a-zA-Z0-9,\._:@\[\]]#Su',
271271
};
272272

273273
$string = preg_replace_callback($regex, function ($matches) {

tests/Runtime/EscaperRuntimeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ public function testHtmlAttributeEscapingEscapesOwaspRecommendedRanges()
339339

340340
public function testHtmlAttributeRelaxedEscapingEscapesOwaspRecommendedRanges()
341341
{
342-
$immune = [',', '.', '-', '_', ':', '@']; // Exceptions to escaping ranges
342+
$immune = [',', '.', '-', '_', ':', '@', '[', ']']; // Exceptions to escaping ranges
343343
for ($chr = 0; $chr < 0xFF; ++$chr) {
344344
if ($chr >= 0x30 && $chr <= 0x39
345345
|| $chr >= 0x41 && $chr <= 0x5A

0 commit comments

Comments
 (0)