vendor/nelmio/security-bundle/EventListener/ContentTypeListener.php line 33

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Nelmio SecurityBundle.
  4.  *
  5.  * (c) Nelmio <hello@nelm.io>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Nelmio\SecurityBundle\EventListener;
  11. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  12. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  13. use Symfony\Component\HttpKernel\HttpKernelInterface;
  14. /**
  15.  * @final
  16.  */
  17. class ContentTypeListener
  18. {
  19.     protected $nosniff;
  20.     public function __construct($nosniff)
  21.     {
  22.         $this->nosniff $nosniff;
  23.     }
  24.     /**
  25.      * @param FilterResponseEvent|ResponseEvent $e
  26.      */
  27.     public function onKernelResponse($e)
  28.     {
  29.         // Compatibility with Symfony < 5 and Symfony >=5
  30.         if (!$e instanceof FilterResponseEvent && !$e instanceof ResponseEvent) {
  31.             throw new \InvalidArgumentException(\sprintf('Expected instance of type %s, %s given'\class_exists(ResponseEvent::class) ? ResponseEvent::class : FilterResponseEvent::class, \is_object($e) ? \get_class($e) : \gettype($e)));
  32.         }
  33.         if (HttpKernelInterface::MASTER_REQUEST !== $e->getRequestType()) {
  34.             return;
  35.         }
  36.         if (!$this->nosniff) {
  37.             return;
  38.         }
  39.         $response $e->getResponse();
  40.         if ($response->isRedirection()) {
  41.             return;
  42.         }
  43.         $response->headers->add(array('X-Content-Type-Options' => 'nosniff'));
  44.     }
  45. }