Skip to content

Document HtmlSanitizer default action configuration #21226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions html_sanitizer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,100 @@
->dropElement('figure')
);

.. _html-sanitizer-default-action:

Default Action for Unconfigured Elements

Check failure on line 478 in html_sanitizer.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Please ensure title "Default Action for Unconfigured Elements" and underline length are matching

Check failure on line 478 in html_sanitizer.rst

View workflow job for this annotation

GitHub Actions / Lint (DOCtor-RST)

Please ensure title "Default Action for Unconfigured Elements" and underline length are matching
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. versionadded:: 7.2

The ``defaultAction()`` method was introduced in Symfony 7.2.

By default, elements that are not explicitly allowed, blocked, or dropped
will be removed along with their children. You can change this behavior
by configuring the default action using the ``defaultAction()`` method.

The method accepts an ``HtmlSanitizerAction`` enum with three possible values:

* ``HtmlSanitizerAction::Drop`` (default): Remove the element and its children
* ``HtmlSanitizerAction::Block``: Remove the element but keep its children
* ``HtmlSanitizerAction::Allow``: Keep the element (without any attributes)

.. configuration-block::

.. code-block:: yaml

# config/packages/html_sanitizer.yaml
framework:
html_sanitizer:
sanitizers:
app.post_sanitizer:
# set the default action for unconfigured elements
default_action: 'block' # or 'allow', 'drop'
allow_elements:
p: '*'

Check failure on line 507 in html_sanitizer.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Cache Warmup] In ArrayNode.php line 304: Unrecognized option "default_action" under "framework.html_sanitizer.saniti zers.app.post_sanitizer". Available options are "allow_attributes", "allow_ elements", "allow_relative_links", "allow_relative_medias", "allow_safe_ele ments", "allow_static_elements", "allowed_link_hosts", "allowed_link_scheme s", "allowed_media_hosts", "allowed_media_schemes", "block_elements", "drop _attributes", "drop_elements", "force_attributes", "force_https_urls", "max _input_length", "with_attribute_sanitizers", "without_attribute_sanitizers" .

.. code-block:: xml

<!-- config/packages/html_sanitizer.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:framework="http://symfony.com/schema/dic/symfony"
xsi:schemaLocation="http://symfony.com/schema/dic/services
https://symfony.com/schema/dic/services/services-1.0.xsd
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:html-sanitizer>
<!-- set the default action for unconfigured elements -->
<framework:sanitizer name="app.post_sanitizer" default-action="block">
<framework:allow-element name="p">
<framework:attribute>*</framework:attribute>
</framework:allow-element>
</framework:sanitizer>
</framework:html-sanitizer>
</framework:config>
</container>

.. code-block:: php

// config/packages/framework.php
use Symfony\Config\FrameworkConfig;

return static function (FrameworkConfig $framework): void {
$framework->htmlSanitizer()
->sanitizer('app.post_sanitizer')
// set the default action for unconfigured elements
->defaultAction('block') // or 'allow', 'drop'
->allowElement('p', '*')
;
};

Check failure on line 544 in html_sanitizer.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Cache Warmup] 2025-07-21T11:23:09+00:00 [critical] Uncaught Error: Call to undefined method Symfony\Config\Framework\HtmlSanitizer\SanitizerConfig::defaultAction()

.. code-block:: php-standalone

use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerAction;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;

$postSanitizer = new HtmlSanitizer(
(new HtmlSanitizerConfig())
// set the default action for unconfigured elements
->defaultAction(HtmlSanitizerAction::Block)
->allowElement('p')
);

With this configuration, if the input HTML contains an element that is not
explicitly configured (e.g., ``<span>``), the sanitizer will remove the
``<span>`` element but keep its children, instead of dropping both the
element and its children.

.. note::

When using ``HtmlSanitizerAction::Allow`` as the default action, the
unconfigured elements will be preserved but without any attributes for
security reasons.

Allow Attributes
~~~~~~~~~~~~~~~~

Expand Down
Loading