-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[FrameworkBundle] Allow default action in configuration #57653
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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
71bc5d6
8b9f9f4
29f9749
d63e583
4744fe2
6a05b1b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -80,6 +80,7 @@ | |
use Symfony\Component\Form\FormTypeGuesserInterface; | ||
use Symfony\Component\Form\FormTypeInterface; | ||
use Symfony\Component\HtmlSanitizer\HtmlSanitizer; | ||
use Symfony\Component\HtmlSanitizer\HtmlSanitizerAction; | ||
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig; | ||
use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface; | ||
use Symfony\Component\HttpClient\Messenger\PingWebhookMessageHandler; | ||
|
@@ -3006,6 +3007,17 @@ private function registerHtmlSanitizerConfiguration(array $config, ContainerBuil | |
$def = $container->register($configId, HtmlSanitizerConfig::class); | ||
|
||
// Base | ||
if ($sanitizerConfig['default_action'] ?? false) { | ||
if (!class_exists(HtmlSanitizerAction::class)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is dead code because of the way the config is done at the moment: if there is a value, it must be one of the allowed cases, which are derived from the enum |
||
throw new LogicException(\sprintf( | ||
'Default action requires the HtmlSanitizer component to be installed in version >=7.2 (%s currently installed). Try running "composer require symfony/html-sanitizer".', | ||
InstalledVersions::getVersion('symfony/html-sanitizer') | ||
)); | ||
} | ||
|
||
$def->addMethodCall('defaultAction', [HtmlSanitizerAction::from($sanitizerConfig['default_action'])], true); | ||
} | ||
|
||
if ($sanitizerConfig['allow_safe_elements']) { | ||
$def->addMethodCall('allowSafeElements', [], true); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -918,6 +918,7 @@ | |
<xsd:attribute name="allow-relative-links" type="xsd:boolean" /> | ||
<xsd:attribute name="allow-relative-medias" type="xsd:boolean" /> | ||
<xsd:attribute name="max-input-length" type="xsd:positiveInteger" /> | ||
<xsd:attribute name="default-action" type="xsd:string" /> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we restrict the possible values to the ones allowed by the enum? |
||
</xsd:complexType> | ||
|
||
<xsd:complexType name="element-option"> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
$container->loadFromExtension('framework', [ | ||
'annotations' => false, | ||
'http_method_override' => false, | ||
'handle_all_throwables' => true, | ||
'php_errors' => ['log' => true], | ||
'html_sanitizer' => [ | ||
'sanitizers' => [ | ||
'custom' => [ | ||
'allow_safe_elements' => true, | ||
'allow_static_elements' => true, | ||
'allow_elements' => [ | ||
'iframe' => 'src', | ||
'custom-tag' => ['data-attr', 'data-attr-1'], | ||
'custom-tag-2' => '*', | ||
], | ||
'block_elements' => ['section'], | ||
'drop_elements' => ['video'], | ||
'allow_attributes' => [ | ||
'src' => ['iframe'], | ||
'data-attr' => '*', | ||
], | ||
'drop_attributes' => [ | ||
'data-attr' => ['custom-tag'], | ||
'data-attr-1' => [], | ||
'data-attr-2' => '*', | ||
], | ||
'force_attributes' => [ | ||
'a' => ['rel' => 'noopener noreferrer'], | ||
'h1' => ['class' => 'bp4-heading'], | ||
], | ||
'force_https_urls' => true, | ||
'allowed_link_schemes' => ['http', 'https', 'mailto'], | ||
'allowed_link_hosts' => ['symfony.com'], | ||
'allow_relative_links' => true, | ||
'allowed_media_schemes' => ['http', 'https', 'data'], | ||
'allowed_media_hosts' => ['symfony.com'], | ||
'allow_relative_medias' => true, | ||
'with_attribute_sanitizers' => [ | ||
'App\\Sanitizer\\CustomAttributeSanitizer', | ||
], | ||
'without_attribute_sanitizers' => [ | ||
'App\\Sanitizer\\OtherCustomAttributeSanitizer', | ||
], | ||
], | ||
'all.sanitizer' => null, | ||
], | ||
], | ||
]); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
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"> | ||
|
||
<config xmlns="http://symfony.com/schema/dic/symfony" http-method-override="false" handle-all-throwables="true"> | ||
<annotations enabled="false" /> | ||
<php-errors log="true" /> | ||
<html-sanitizer> | ||
<sanitizer name="custom" | ||
allow-safe-elements="true" | ||
allow-static-elements="true" | ||
force-https-urls="true" | ||
allow-relative-links="true" | ||
allow-relative-medias="true" | ||
> | ||
<allow-element name="iframe"> | ||
<attribute>src</attribute> | ||
</allow-element> | ||
<allow-element name="custom-tag"> | ||
<attribute>data-attr</attribute> | ||
<attribute>data-attr-1</attribute> | ||
</allow-element> | ||
<allow-element name="custom-tag-2"> | ||
<attribute>*</attribute> | ||
</allow-element> | ||
<block-element>section</block-element> | ||
<drop-element>video</drop-element> | ||
<allow-attribute name="src"> | ||
<element>iframe</element> | ||
</allow-attribute> | ||
<allow-attribute name="data-attr"> | ||
<element>*</element> | ||
</allow-attribute> | ||
<drop-attribute name="data-attr"> | ||
<element>custom-tag</element> | ||
</drop-attribute> | ||
<drop-attribute name="data-attr-1" /> | ||
<drop-attribute name="data-attr-2"> | ||
<element>*</element> | ||
</drop-attribute> | ||
<force-attribute name="a"> | ||
<attribute name="rel">noopener noreferrer</attribute> | ||
</force-attribute> | ||
<force-attribute name="h1"> | ||
<attribute name="class">bp4-heading</attribute> | ||
</force-attribute> | ||
<allowed-link-scheme>http</allowed-link-scheme> | ||
<allowed-link-scheme>https</allowed-link-scheme> | ||
<allowed-link-scheme>mailto</allowed-link-scheme> | ||
<allowed-link-host>symfony.com</allowed-link-host> | ||
<allowed-media-scheme>http</allowed-media-scheme> | ||
<allowed-media-scheme>https</allowed-media-scheme> | ||
<allowed-media-scheme>data</allowed-media-scheme> | ||
<allowed-media-host>symfony.com</allowed-media-host> | ||
<with-attribute-sanitizer>App\Sanitizer\CustomAttributeSanitizer</with-attribute-sanitizer> | ||
<without-attribute-sanitizer>App\Sanitizer\OtherCustomAttributeSanitizer</without-attribute-sanitizer> | ||
</sanitizer> | ||
|
||
<sanitizer name="all.sanitizer"/> | ||
</html-sanitizer> | ||
</config> | ||
</container> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
framework: | ||
annotations: false | ||
http_method_override: false | ||
handle_all_throwables: true | ||
php_errors: | ||
log: true | ||
html_sanitizer: | ||
sanitizers: | ||
custom: | ||
allow_safe_elements: true | ||
allow_static_elements: true | ||
allow_elements: | ||
iframe: 'src' | ||
custom-tag: ['data-attr', 'data-attr-1'] | ||
custom-tag-2: '*' | ||
block_elements: | ||
- section | ||
drop_elements: | ||
- video | ||
allow_attributes: | ||
src: ['iframe'] | ||
data-attr: '*' | ||
drop_attributes: | ||
data-attr: [custom-tag] | ||
data-attr-1: [] | ||
data-attr-2: '*' | ||
force_attributes: | ||
a: | ||
rel: noopener noreferrer | ||
h1: | ||
class: bp4-heading | ||
force_https_urls: true | ||
allowed_link_schemes: ['http', 'https', 'mailto'] | ||
allowed_link_hosts: ['symfony.com'] | ||
allow_relative_links: true | ||
allowed_media_schemes: ['http', 'https', 'data'] | ||
allowed_media_hosts: ['symfony.com'] | ||
allow_relative_medias: true | ||
with_attribute_sanitizers: | ||
- App\Sanitizer\CustomAttributeSanitizer | ||
without_attribute_sanitizers: | ||
- App\Sanitizer\OtherCustomAttributeSanitizer | ||
|
||
all.sanitizer: null |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but this won't work if the component is not installed, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes good point. Just came here to say #57686 might allow this to be cleaner, but indeed if the enum class is missing what do we do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so now that #57686 is merged, I would say this could probably be changed to:
->enumNode('default_action')->enumFqcn(HtmlSanitizerAction::class)->end()
The question of what to do when
!class_exists(HtmlSanitizerAction::class)
.. I would say make the enum node conditional, and only declare it if the class exists, so that if html-sanitizer is outdated and you try to configure a default_action the config will error with an unknown option default_action. For better DX we could also if the enum class is not there define it as a dummy null value but throw with a message about html-sanitizer bieng outdated if any value is passed in.