vendor/nelmio/security-bundle/src/EventListener/XssProtectionListener.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Nelmio SecurityBundle.
  5.  *
  6.  * (c) Nelmio <hello@nelm.io>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Nelmio\SecurityBundle\EventListener;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. final class XssProtectionListener implements EventSubscriberInterface
  16. {
  17.     private bool $enabled;
  18.     private bool $modeBlock;
  19.     private ?string $reportUri;
  20.     public function __construct(bool $enabledbool $modeBlock, ?string $reportUri null)
  21.     {
  22.         $this->enabled $enabled;
  23.         $this->modeBlock $modeBlock;
  24.         $this->reportUri $reportUri;
  25.     }
  26.     public function onKernelResponse(ResponseEvent $e): void
  27.     {
  28.         if (!$e->isMainRequest()) {
  29.             return;
  30.         }
  31.         $response $e->getResponse();
  32.         if ($response->isRedirection()) {
  33.             return;
  34.         }
  35.         $value '0';
  36.         if ($this->enabled) {
  37.             $value '1';
  38.             if ($this->modeBlock) {
  39.                 $value .= '; mode=block';
  40.             }
  41.             if (null !== $this->reportUri) {
  42.                 $value .= '; report='.$this->reportUri;
  43.             }
  44.         }
  45.         $response->headers->set('X-XSS-Protection'$value);
  46.     }
  47.     public static function getSubscribedEvents(): array
  48.     {
  49.         return [KernelEvents::RESPONSE => 'onKernelResponse'];
  50.     }
  51.     /**
  52.      * @phpstan-param array{enabled: bool, mode_block: bool, report_uri: string|null} $config
  53.      */
  54.     public static function fromConfig(array $config): self
  55.     {
  56.         $enabled $config['enabled'];
  57.         $modeBlock $config['mode_block'];
  58.         $reportUri $config['report_uri'];
  59.         return new self($enabled$modeBlock$reportUri);
  60.     }
  61. }