|
25 | 25 | use Symfony\Component\DependencyInjection\ContainerBuilder;
|
26 | 26 | use Symfony\Component\DependencyInjection\Exception\LogicException;
|
27 | 27 | use Symfony\Component\Form\Form;
|
| 28 | +use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface; |
28 | 29 | use Symfony\Component\HttpClient\HttpClient;
|
29 | 30 | use Symfony\Component\HttpFoundation\Cookie;
|
30 | 31 | use Symfony\Component\Lock\Lock;
|
@@ -167,6 +168,7 @@ public function getConfigTreeBuilder(): TreeBuilder
|
167 | 168 | $this->addNotifierSection($rootNode, $enableIfStandalone);
|
168 | 169 | $this->addRateLimiterSection($rootNode, $enableIfStandalone);
|
169 | 170 | $this->addUidSection($rootNode, $enableIfStandalone);
|
| 171 | + $this->addHtmlSanitizerSection($rootNode, $enableIfStandalone); |
170 | 172 |
|
171 | 173 | return $treeBuilder;
|
172 | 174 | }
|
@@ -2106,4 +2108,147 @@ private function addUidSection(ArrayNodeDefinition $rootNode, callable $enableIf
|
2106 | 2108 | ->end()
|
2107 | 2109 | ;
|
2108 | 2110 | }
|
| 2111 | + |
| 2112 | + private function addHtmlSanitizerSection(ArrayNodeDefinition $rootNode, callable $enableIfStandalone) |
| 2113 | + { |
| 2114 | + $rootNode |
| 2115 | + ->children() |
| 2116 | + ->arrayNode('html_sanitizer') |
| 2117 | + ->info('HtmlSanitizer configuration') |
| 2118 | + ->{$enableIfStandalone('symfony/html-sanitizer', HtmlSanitizerInterface::class)}() |
| 2119 | + ->fixXmlConfig('sanitizer') |
| 2120 | + ->children() |
| 2121 | + ->scalarNode('default') |
| 2122 | + ->defaultNull() |
| 2123 | + ->info('Default sanitizer to use when injecting without named binding.') |
| 2124 | + ->end() |
| 2125 | + ->arrayNode('sanitizers') |
| 2126 | + ->useAttributeAsKey('name') |
| 2127 | + ->arrayPrototype() |
| 2128 | + ->fixXmlConfig('allow_element') |
| 2129 | + ->fixXmlConfig('block_element') |
| 2130 | + ->fixXmlConfig('drop_element') |
| 2131 | + ->fixXmlConfig('allow_attribute') |
| 2132 | + ->fixXmlConfig('drop_attribute') |
| 2133 | + ->fixXmlConfig('force_attribute') |
| 2134 | + ->fixXmlConfig('allowed_link_scheme') |
| 2135 | + ->fixXmlConfig('allowed_link_host') |
| 2136 | + ->fixXmlConfig('allowed_media_scheme') |
| 2137 | + ->fixXmlConfig('allowed_media_host') |
| 2138 | + ->fixXmlConfig('with_attribute_sanitizer') |
| 2139 | + ->fixXmlConfig('without_attribute_sanitizer') |
| 2140 | + ->children() |
| 2141 | + ->booleanNode('allow_safe_elements') |
| 2142 | + ->info('Allows "safe" elements and attributes.') |
| 2143 | + ->defaultFalse() |
| 2144 | + ->end() |
| 2145 | + ->booleanNode('allow_all_static_elements') |
| 2146 | + ->info('Allows all static elements and attributes from the W3C Sanitizer API standard.') |
| 2147 | + ->defaultFalse() |
| 2148 | + ->end() |
| 2149 | + ->arrayNode('allow_elements') |
| 2150 | + ->info('Configures the elements that the sanitizer should retain from the input. The element name is the key, the value is either a list of allowed attributes for this element or "*" to allow the default set of attributes (https://wicg.github.io/sanitizer-api/#default-configuration).') |
| 2151 | + ->example(['i' => '*', 'a' => ['title'], 'span' => 'class']) |
| 2152 | + ->normalizeKeys(false) |
| 2153 | + ->useAttributeAsKey('name') |
| 2154 | + ->variablePrototype() |
| 2155 | + ->beforeNormalization() |
| 2156 | + ->ifArray()->then(fn ($n) => $n['attribute'] ?? $n) |
| 2157 | + ->end() |
| 2158 | + ->validate() |
| 2159 | + ->ifTrue(fn ($n): bool => !\is_string($n) && !\is_array($n)) |
| 2160 | + ->thenInvalid('The value must be either a string or an array of strings.') |
| 2161 | + ->end() |
| 2162 | + ->end() |
| 2163 | + ->end() |
| 2164 | + ->arrayNode('block_elements') |
| 2165 | + ->info('Configures elements as blocked. Blocked elements are elements the sanitizer should remove from the input, but retain their children.') |
| 2166 | + ->beforeNormalization() |
| 2167 | + ->ifString() |
| 2168 | + ->then(fn (string $n): array => (array) $n) |
| 2169 | + ->end() |
| 2170 | + ->scalarPrototype()->end() |
| 2171 | + ->end() |
| 2172 | + ->arrayNode('drop_elements') |
| 2173 | + ->info('Configures elements as dropped. Dropped elements are elements the sanitizer should remove from the input, including their children.') |
| 2174 | + ->beforeNormalization() |
| 2175 | + ->ifString() |
| 2176 | + ->then(fn (string $n): array => (array) $n) |
| 2177 | + ->end() |
| 2178 | + ->scalarPrototype()->end() |
| 2179 | + ->end() |
| 2180 | + ->arrayNode('allow_attributes') |
| 2181 | + ->info('Configures attributes as allowed. Allowed attributes are attributes the sanitizer should retain from the input.') |
| 2182 | + ->normalizeKeys(false) |
| 2183 | + ->useAttributeAsKey('name') |
| 2184 | + ->variablePrototype() |
| 2185 | + ->beforeNormalization() |
| 2186 | + ->ifArray()->then(fn ($n) => $n['element'] ?? $n) |
| 2187 | + ->end() |
| 2188 | + ->end() |
| 2189 | + ->end() |
| 2190 | + ->arrayNode('drop_attributes') |
| 2191 | + ->info('Configures attributes as dropped. Dropped attributes are attributes the sanitizer should remove from the input.') |
| 2192 | + ->normalizeKeys(false) |
| 2193 | + ->useAttributeAsKey('name') |
| 2194 | + ->variablePrototype() |
| 2195 | + ->beforeNormalization() |
| 2196 | + ->ifArray()->then(fn ($n) => $n['element'] ?? $n) |
| 2197 | + ->end() |
| 2198 | + ->end() |
| 2199 | + ->end() |
| 2200 | + ->arrayNode('force_attributes') |
| 2201 | + ->info('Forcefully set the values of certain attributes on certain elements.') |
| 2202 | + ->normalizeKeys(false) |
| 2203 | + ->useAttributeAsKey('name') |
| 2204 | + ->arrayPrototype() |
| 2205 | + ->normalizeKeys(false) |
| 2206 | + ->useAttributeAsKey('name') |
| 2207 | + ->scalarPrototype()->end() |
| 2208 | + ->end() |
| 2209 | + ->end() |
| 2210 | + ->booleanNode('force_https_urls') |
| 2211 | + ->info('Transforms URLs using the HTTP scheme to use the HTTPS scheme instead.') |
| 2212 | + ->defaultFalse() |
| 2213 | + ->end() |
| 2214 | + ->arrayNode('allowed_link_schemes') |
| 2215 | + ->info('Allows only a given list of schemes to be used in links href attributes.') |
| 2216 | + ->scalarPrototype()->end() |
| 2217 | + ->end() |
| 2218 | + ->arrayNode('allowed_link_hosts') |
| 2219 | + ->info('Allows only a given list of hosts to be used in links href attributes.') |
| 2220 | + ->scalarPrototype()->end() |
| 2221 | + ->end() |
| 2222 | + ->booleanNode('allow_relative_links') |
| 2223 | + ->info('Allows relative URLs to be used in links href attributes.') |
| 2224 | + ->defaultFalse() |
| 2225 | + ->end() |
| 2226 | + ->arrayNode('allowed_media_schemes') |
| 2227 | + ->info('Allows only a given list of schemes to be used in media source attributes (img, audio, video, ...).') |
| 2228 | + ->scalarPrototype()->end() |
| 2229 | + ->end() |
| 2230 | + ->arrayNode('allowed_media_hosts') |
| 2231 | + ->info('Allows only a given list of hosts to be used in media source attributes (img, audio, video, ...).') |
| 2232 | + ->scalarPrototype()->end() |
| 2233 | + ->end() |
| 2234 | + ->booleanNode('allow_relative_medias') |
| 2235 | + ->info('Allows relative URLs to be used in media source attributes (img, audio, video, ...).') |
| 2236 | + ->defaultFalse() |
| 2237 | + ->end() |
| 2238 | + ->arrayNode('with_attribute_sanitizers') |
| 2239 | + ->info('Registers custom attribute sanitizers.') |
| 2240 | + ->scalarPrototype()->end() |
| 2241 | + ->end() |
| 2242 | + ->arrayNode('without_attribute_sanitizers') |
| 2243 | + ->info('Unregisters custom attribute sanitizers.') |
| 2244 | + ->scalarPrototype()->end() |
| 2245 | + ->end() |
| 2246 | + ->end() |
| 2247 | + ->end() |
| 2248 | + ->end() |
| 2249 | + ->end() |
| 2250 | + ->end() |
| 2251 | + ->end() |
| 2252 | + ; |
| 2253 | + } |
2109 | 2254 | }
|
0 commit comments