vendor/nelmio/security-bundle/src/EventListener/ContentTypeListener.php line 27

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\HttpKernel\Event\ResponseEvent;
  13. final class ContentTypeListener
  14. {
  15.     private bool $nosniff;
  16.     public function __construct(bool $nosniff)
  17.     {
  18.         $this->nosniff $nosniff;
  19.     }
  20.     public function onKernelResponse(ResponseEvent $e): void
  21.     {
  22.         if (!$e->isMainRequest()) {
  23.             return;
  24.         }
  25.         if (!$this->nosniff) {
  26.             return;
  27.         }
  28.         $response $e->getResponse();
  29.         if ($response->isRedirection()) {
  30.             return;
  31.         }
  32.         $response->headers->add(['X-Content-Type-Options' => 'nosniff']);
  33.     }
  34. }