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

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