|
71 | 71 | use Symfony\Component\Form\FormTypeExtensionInterface;
|
72 | 72 | use Symfony\Component\Form\FormTypeGuesserInterface;
|
73 | 73 | use Symfony\Component\Form\FormTypeInterface;
|
| 74 | +use Symfony\Component\HtmlSanitizer\HtmlSanitizer; |
| 75 | +use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig; |
| 76 | +use Symfony\Component\HtmlSanitizer\HtmlSanitizerInterface; |
74 | 77 | use Symfony\Component\HttpClient\MockHttpClient;
|
75 | 78 | use Symfony\Component\HttpClient\Retry\GenericRetryStrategy;
|
76 | 79 | use Symfony\Component\HttpClient\RetryableHttpClient;
|
@@ -488,6 +491,14 @@ public function load(array $configs, ContainerBuilder $container)
|
488 | 491 | $this->registerUidConfiguration($config['uid'], $container, $loader);
|
489 | 492 | }
|
490 | 493 |
|
| 494 | + if ($this->isConfigEnabled($container, $config['html_sanitizer'])) { |
| 495 | + if (!class_exists(HtmlSanitizerConfig::class)) { |
| 496 | + throw new LogicException('HtmlSanitizer support cannot be enabled as the HtmlSanitizer component is not installed. Try running "composer require symfony/html-sanitizer".'); |
| 497 | + } |
| 498 | + |
| 499 | + $this->registerHtmlSanitizerConfiguration($config['html_sanitizer'], $container, $loader); |
| 500 | + } |
| 501 | + |
491 | 502 | $this->addAnnotatedClassesToCompile([
|
492 | 503 | '**\\Controller\\',
|
493 | 504 | '**\\Entity\\',
|
@@ -2549,6 +2560,100 @@ private function registerUidConfiguration(array $config, ContainerBuilder $conta
|
2549 | 2560 | }
|
2550 | 2561 | }
|
2551 | 2562 |
|
| 2563 | + private function registerHtmlSanitizerConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader) |
| 2564 | + { |
| 2565 | + $loader->load('html_sanitizer.php'); |
| 2566 | + |
| 2567 | + foreach ($config['sanitizers'] as $sanitizerName => $sanitizerConfig) { |
| 2568 | + $def = $container->register($sanitizerName.'.config', HtmlSanitizerConfig::class); |
| 2569 | + |
| 2570 | + // Base configuration |
| 2571 | + if ($sanitizerConfig['allow_safe_elements'] ?? false) { |
| 2572 | + $def->addMethodCall('allowSafeElements', [], true); |
| 2573 | + } |
| 2574 | + |
| 2575 | + if ($sanitizerConfig['allow_all_static_elements'] ?? false) { |
| 2576 | + $def->addMethodCall('allowAllStaticElements', [], true); |
| 2577 | + } |
| 2578 | + |
| 2579 | + // Configures elements |
| 2580 | + foreach ($sanitizerConfig['allow_elements'] ?? [] as $element => $attributes) { |
| 2581 | + $def->addMethodCall('allowElement', [$element, $attributes], true); |
| 2582 | + } |
| 2583 | + |
| 2584 | + foreach ($sanitizerConfig['block_elements'] ?? [] as $element) { |
| 2585 | + $def->addMethodCall('blockElement', [$element], true); |
| 2586 | + } |
| 2587 | + |
| 2588 | + foreach ($sanitizerConfig['drop_elements'] ?? [] as $element) { |
| 2589 | + $def->addMethodCall('dropElement', [$element], true); |
| 2590 | + } |
| 2591 | + |
| 2592 | + // Configures attributes |
| 2593 | + foreach ($sanitizerConfig['allow_attributes'] ?? [] as $attribute => $elements) { |
| 2594 | + $def->addMethodCall('allowAttribute', [$attribute, $elements], true); |
| 2595 | + } |
| 2596 | + |
| 2597 | + foreach ($sanitizerConfig['drop_attributes'] ?? [] as $attribute => $elements) { |
| 2598 | + $def->addMethodCall('dropAttribute', [$attribute, $elements], true); |
| 2599 | + } |
| 2600 | + |
| 2601 | + // Force attributes |
| 2602 | + foreach ($sanitizerConfig['force_attributes'] ?? [] as $element => $attributes) { |
| 2603 | + foreach ($attributes as $attrName => $attrValue) { |
| 2604 | + $def->addMethodCall('forceAttribute', [$element, $attrName, $attrValue], true); |
| 2605 | + } |
| 2606 | + } |
| 2607 | + |
| 2608 | + // Settings |
| 2609 | + if (isset($sanitizerConfig['force_https_urls'])) { |
| 2610 | + $def->addMethodCall('forceHttpsUrls', [$sanitizerConfig['force_https_urls']], true); |
| 2611 | + } |
| 2612 | + |
| 2613 | + if (isset($sanitizerConfig['allowed_link_schemes'])) { |
| 2614 | + $def->addMethodCall('allowLinkSchemes', [$sanitizerConfig['allowed_link_schemes']], true); |
| 2615 | + } |
| 2616 | + |
| 2617 | + if (isset($sanitizerConfig['allowed_link_hosts'])) { |
| 2618 | + $def->addMethodCall('allowLinkHosts', [$sanitizerConfig['allowed_link_hosts']], true); |
| 2619 | + } |
| 2620 | + |
| 2621 | + if (isset($sanitizerConfig['allow_relative_links'])) { |
| 2622 | + $def->addMethodCall('allowRelativeLinks', [$sanitizerConfig['allow_relative_links']], true); |
| 2623 | + } |
| 2624 | + |
| 2625 | + if (isset($sanitizerConfig['allowed_media_schemes'])) { |
| 2626 | + $def->addMethodCall('allowMediaSchemes', [$sanitizerConfig['allowed_media_schemes']], true); |
| 2627 | + } |
| 2628 | + |
| 2629 | + if (isset($sanitizerConfig['allowed_media_hosts'])) { |
| 2630 | + $def->addMethodCall('allowMediaHosts', [$sanitizerConfig['allowed_media_hosts']], true); |
| 2631 | + } |
| 2632 | + |
| 2633 | + if (isset($sanitizerConfig['allow_relative_medias'])) { |
| 2634 | + $def->addMethodCall('allowRelativeMedias', [$sanitizerConfig['allow_relative_medias']], true); |
| 2635 | + } |
| 2636 | + |
| 2637 | + // Custom attribute sanitizers |
| 2638 | + foreach ($sanitizerConfig['with_attribute_sanitizers'] ?? [] as $serviceName) { |
| 2639 | + $def->addMethodCall('withAttributeSanitizer', [new Reference($serviceName)], true); |
| 2640 | + } |
| 2641 | + |
| 2642 | + foreach ($sanitizerConfig['without_attribute_sanitizers'] ?? [] as $serviceName) { |
| 2643 | + $def->addMethodCall('withoutAttributeSanitizer', [new Reference($serviceName)], true);<
10000
/div> |
| 2644 | + } |
| 2645 | + |
| 2646 | + // Create the sanitizer and link its config |
| 2647 | + $container->register($sanitizerName, HtmlSanitizer::class) |
| 2648 | + ->addArgument(new Reference($sanitizerName.'.config')) |
| 2649 | + ; |
| 2650 | + |
| 2651 | + $container->registerAliasForArgument($sanitizerName, HtmlSanitizerInterface::class, $sanitizerName); |
| 2652 | + } |
| 2653 | + |
| 2654 | + $container->setAlias(HtmlSanitizerInterface::class, new Reference($config['default'])); |
| 2655 | + } |
| 2656 | + |
2552 | 2657 | private function resolveTrustedHeaders(array $headers): int
|
2553 | 2658 | {
|
2554 | 2659 | $trustedHeaders = 0;
|
|
0 commit comments