Skip to content

Commit bf5ae83

Browse files
committed
Add full HTML Sanitizer configuration reference
1 parent 5529407 commit bf5ae83

File tree

3 files changed

+195
-8
lines changed

3 files changed

+195
-8
lines changed

reference/configuration/framework.rst

Lines changed: 190 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,10 +1352,197 @@ thrown by the application and will turn them into HTTP responses.
13521352
html_sanitizer
13531353
~~~~~~~~~~~~~~
13541354

1355-
The ``html_sanitizer`` option (and its children) are used to configure
1356-
custom HTML sanitizers. Read more about the options in the
1355+
The ``html_sanitizer`` option is used to configure custom HTML sanitizers.
1356+
Read more about the usage in the
13571357
:ref:`HTML sanitizer documentation <html-sanitizer-configuration>`.
13581358

1359+
sanitizers
1360+
..........
1361+
1362+
**type**: ``prototype``
1363+
1364+
Defines the list of custom HTML sanitizers. Each key is the sanitizer name
1365+
(used as a service identifier), and the value is the sanitizer configuration.
1366+
1367+
default_action
1368+
""""""""""""""
1369+
1370+
**type**: ``string`` **possible values**: ``'drop'``, ``'block'`` or ``'allow'``
1371+
1372+
Defines how the sanitizer must behave by default for elements not explicitly
1373+
configured. ``drop`` removes the element and its children, ``block`` removes
1374+
the element but keeps its children, and ``allow`` keeps the element.
1375+
1376+
allow_safe_elements
1377+
"""""""""""""""""""
1378+
1379+
**type**: ``boolean`` **default**: ``false``
1380+
1381+
Allows all "safe" elements and attributes. This includes all static elements
1382+
except those that can lead to CSS injection or click-jacking.
1383+
1384+
allow_static_elements
1385+
"""""""""""""""""""""
1386+
1387+
**type**: ``boolean`` **default**: ``false``
1388+
1389+
Allows all static elements and attributes from the `W3C Sanitizer API standard`_.
1390+
1391+
allow_elements
1392+
""""""""""""""
1393+
1394+
**type**: ``array``
1395+
1396+
Configures the elements that the sanitizer should retain from the input.
1397+
The key is the element name, the value is either ``*`` to allow the
1398+
default set of attributes or a list of allowed attribute names:
1399+
1400+
.. code-block:: yaml
1401+
1402+
# config/packages/html_sanitizer.yaml
1403+
framework:
1404+
html_sanitizer:
1405+
sanitizers:
1406+
app.post_sanitizer:
1407+
allow_elements:
1408+
i: '*'
1409+
a: ['title']
1410+
span: 'class'
1411+
1412+
block_elements
1413+
""""""""""""""
1414+
1415+
**type**: ``array``
1416+
1417+
Configures elements as blocked. Blocked elements are elements the sanitizer
1418+
should remove from the input, but retain their children.
1419+
1420+
drop_elements
1421+
"""""""""""""
1422+
1423+
**type**: ``array``
1424+
1425+
Configures elements as dropped. Dropped elements are elements the sanitizer
1426+
should remove from the input, including their children.
1427+
1428+
allow_attributes
1429+
""""""""""""""""
1430+
1431+
**type**: ``array``
1432+
1433+
Configures attributes as allowed globally. Allowed attributes are attributes
1434+
the sanitizer should retain from the input. The key is the attribute name,
1435+
the value is a list of elements on which this attribute is allowed (or ``*``
1436+
to allow on all elements).
1437+
1438+
drop_attributes
1439+
"""""""""""""""
1440+
1441+
**type**: ``array``
1442+
1443+
Configures attributes as dropped globally. Dropped attributes are attributes
1444+
the sanitizer should remove from the input. The key is the attribute name,
1445+
the value is a list of elements on which this attribute is dropped (or ``*``
1446+
to drop on all elements).
1447+
1448+
force_attributes
1449+
""""""""""""""""
1450+
1451+
**type**: ``array``
1452+
1453+
Forcefully sets the values of certain attributes on certain elements. The key
1454+
is the element name, the value is an array of attribute name/value pairs:
1455+
1456+
.. code-block:: yaml
1457+
1458+
# config/packages/html_sanitizer.yaml
1459+
framework:
1460+
html_sanitizer:
1461+
sanitizers:
1462+
app.post_sanitizer:
1463+
force_attributes:
1464+
a:
1465+
rel: noopener noreferrer
1466+
1467+
force_https_urls
1468+
""""""""""""""""
1469+
1470+
**type**: ``boolean`` **default**: ``false``
1471+
1472+
Transforms URLs using the HTTP scheme to use the HTTPS scheme instead.
1473+
1474+
allowed_link_schemes
1475+
""""""""""""""""""""
1476+
1477+
**type**: ``array``
1478+
1479+
Allows only a given list of schemes to be used in links ``href`` attributes.
1480+
1481+
allowed_link_hosts
1482+
""""""""""""""""""
1483+
1484+
**type**: ``array`` **default**: ``null``
1485+
1486+
Allows only a given list of hosts to be used in links ``href`` attributes.
1487+
By default (``null``), all hosts are allowed.
1488+
1489+
allow_relative_links
1490+
""""""""""""""""""""
1491+
1492+
**type**: ``boolean`` **default**: ``false``
1493+
1494+
Allows relative URLs to be used in links ``href`` attributes.
1495+
1496+
allowed_media_schemes
1497+
"""""""""""""""""""""
1498+
1499+
**type**: ``array``
1500+
1501+
Allows only a given list of schemes to be used in media source attributes
1502+
(``img``, ``audio``, ``video``, ...).
1503+
1504+
allowed_media_hosts
1505+
"""""""""""""""""""
1506+
1507+
**type**: ``array`` **default**: ``null``
1508+
1509+
Allows only a given list of hosts to be used in media source attributes
1510+
(``img``, ``audio``, ``video``, ...). By default (``null``), all hosts are
1511+
allowed.
1512+
1513+
allow_relative_medias
1514+
"""""""""""""""""""""
1515+
1516+
**type**: ``boolean`` **default**: ``false``
1517+
1518+
Allows relative URLs to be used in media source attributes (``img``,
1519+
``audio``, ``video``, ...).
1520+
1521+
with_attribute_sanitizers
1522+
"""""""""""""""""""""""""
1523+
1524+
**type**: ``array``
1525+
1526+
Registers custom attribute sanitizer services. Each entry is a service
1527+
identifier implementing
1528+
:class:`Symfony\\Component\\HtmlSanitizer\\Visitor\\AttributeSanitizer\\AttributeSanitizerInterface`.
1529+
1530+
without_attribute_sanitizers
1531+
""""""""""""""""""""""""""""
1532+
1533+
**type**: ``array``
1534+
1535+
Unregisters custom attribute sanitizer services that were previously
1536+
registered (e.g. by the default configuration).
1537+
1538+
max_input_length
1539+
""""""""""""""""
1540+
1541+
**type**: ``integer`` **default**: ``0``
1542+
1543+
The maximum length allowed for the sanitized input. When set to ``0`` (the
1544+
default), the length is unlimited.
1545+
13591546
.. _configuration-framework-http_cache:
13601547

13611548
http_cache
@@ -4300,3 +4487,4 @@ to know their differences.
43004487
.. _`PHP attributes`: https://www.php.net/manual/en/language.attributes.overview.php
43014488
.. _`shared cache`: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#shared_cache
43024489
.. _`private cache`: https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Caching#private_caches
4490+
.. _`W3C Sanitizer API standard`: https://wicg.github.io/sanitizer-api/#default-configuration

reference/configuration/security.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ allow_if_equal_granted_denied
194194

195195
This option is only used by the ``consensus`` strategy. If the number of
196196
:doc:`voters </security/voters>` granting access is equal to the number of
197-
voters denying access, this option determines the final decision. If ``true``
198-
(the default), access is granted in case of a tie.
197+
voters denying access, this option determines the final decision. If ``true``
198+
(the default), access is granted in case of a tie.
199199

200200
service
201201
.......

routing.rst

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,9 @@ the ``list()`` method of the ``BlogController`` class.
8282
.. warning::
8383

8484
If you define multiple PHP classes in the same file, Symfony only loads the
85-
routes of the first class, ignoring all the other routes.The route attribute
86-
is always wins over route with yaml, xml or PHP file and Symfony will always
87-
load the route attribute.
88-
85+
routes of the first class, ignoring all the other routes. The route attribute
86+
always wins over routes defined in YAML, XML or PHP files and Symfony will
87+
always load the route attribute.
8988

9089
The route name (``blog_list``) is not important for now, but it will be
9190
essential later when :ref:`generating URLs <routing-generating-urls>`. You only

0 commit comments

Comments
 (0)