Skip to content

Commit 16b28ab

Browse files
plfortnicolas-grekas
authored andcommitted
[HtmlSanitizer] Allow null for sanitizer option allowed_link_hosts and allowed_media_hosts
1 parent 4d6c868 commit 16b28ab

File tree

5 files changed

+50
-5
lines changed

5 files changed

+50
-5
lines changed

DependencyInjection/Configuration.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,7 +1912,7 @@ private function addHttpClientRetrySection()
19121912
->integerNode('max_delay')->defaultValue(0)->min(0)->info('Max time in ms that a retry should ever be delayed (0 = infinite)')->end()
19131913
->floatNode('jitter')->defaultValue(0.1)->min(0)->max(1)->info('Randomness in percent (between 0 and 1) to apply to the delay')->end()
19141914
->end()
1915-
;
1915+
;
19161916
}
19171917

19181918
private function addMailerSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone)
@@ -2223,9 +2223,13 @@ private function addHtmlSanitizerSection(ArrayNodeDefinition $rootNode, callable
22232223
->info('Allows only a given list of schemes to be used in links href attributes.')
22242224
->scalarPrototype()->end()
22252225
->end()
2226-
->arrayNode('allowed_link_hosts')
2226+
->variableNode('allowed_link_hosts')
22272227
->info('Allows only a given list of hosts to be used in links href attributes.')
2228-
->scalarPrototype()->end()
2228+
->defaultValue(null)
2229+
->validate()
2230+
->ifTrue(function ($v) { return !\is_array($v) && null !== $v; })
2231+
->thenInvalid('The "allowed_link_hosts" parameter must be an array or null')
2232+
->end()
22292233
->end()
22302234
->booleanNode('allow_relative_links')
22312235
->info('Allows relative URLs to be used in links href attributes.')
@@ -2235,9 +2239,13 @@ private function addHtmlSanitizerSection(ArrayNodeDefinition $rootNode, callable
22352239
->info('Allows only a given list of schemes to be used in media source attributes (img, audio, video, ...).')
22362240
->scalarPrototype()->end()
22372241
->end()
2238-
->arrayNode('allowed_media_hosts')
2242+
->variableNode('allowed_media_hosts')
22392243
->info('Allows only a given list of hosts to be used in media source attributes (img, audio, video, ...).')
2240-
->scalarPrototype()->end()
2244+
->defaultValue(null)
2245+
->validate()
2246+
->ifTrue(function ($v) { return !\is_array($v) && null !== $v; })
2247+
->thenInvalid('The "allowed_media_hosts" parameter must be an array or null')
2248+
->end()
22412249
->end()
22422250
->booleanNode('allow_relative_medias')
22432251
->info('Allows relative URLs to be used in media source attributes (img, audio, video, ...).')
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', [
4+
'http_method_override' => false,
5+
'html_sanitizer' => [
6+
'sanitizers' => [
7+
'custom_default' => null,
8+
],
9+
],
10+
]);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd
6+
http://symfony.com/schema/dic/symfony https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
7+
8+
<config xmlns="http://symfony.com/schema/dic/symfony" http-method-override="false">
9+
<html-sanitizer>
10+
<sanitizer name="custom_default"/>
11+
</html-sanitizer>
12+
</config>
13+
</container>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
framework:
2+
http_method_override: false
3+
html_sanitizer:
4+
sanitizers:
5+
custom_default: ~

Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,15 @@ static function ($call) {
21032103
$this->assertFalse($container->hasAlias(HtmlSanitizerInterface::class.' $default'));
21042104
}
21052105

2106+
public function testHtmlSanitizerDefaultNullAllowedLinkMediaHost()
2107+
{
2108+
$container = $this->createContainerFromFile('html_sanitizer_default_allowed_link_and_media_hosts');
2109+
2110+
$calls = $container->getDefinition('html_sanitizer.config.custom_default')->getMethodCalls();
2111+
$this->assertContains(['allowLinkHosts', [null], true], $calls);
2112+
$this->assertContains(['allowMediaHosts', [null], true], $calls);
2113+
}
2114+
21062115
public function testHtmlSanitizerDefaultConfig()
21072116
{
21082117
$container = $this->createContainerFromFile('html_sanitizer_default_config');

0 commit comments

Comments
 (0)