vendor/nelmio/security-bundle/EventListener/ClickjackingListener.php line 40

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. use Symfony\Component\HttpKernel\KernelEvents;
  15. /**
  16.  * @final
  17.  */
  18. class ClickjackingListener extends AbstractContentTypeRestrictableListener
  19. {
  20.     private $paths;
  21.     public function __construct(array $paths, array $contentTypes = array())
  22.     {
  23.         parent::__construct($contentTypes);
  24.         $this->paths $paths;
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return array(KernelEvents::RESPONSE => 'onKernelResponse');
  29.     }
  30.     /**
  31.      * @param FilterResponseEvent|ResponseEvent $e
  32.      */
  33.     public function onKernelResponse($e)
  34.     {
  35.         // Compatibility with Symfony < 5 and Symfony >=5
  36.         if (!$e instanceof FilterResponseEvent && !$e instanceof ResponseEvent) {
  37.             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)));
  38.         }
  39.         if (HttpKernelInterface::MASTER_REQUEST !== $e->getRequestType()) {
  40.             return;
  41.         }
  42.         if (!$this->isContentTypeValid($e->getResponse())) {
  43.             return;
  44.         }
  45.         $response $e->getResponse();
  46.         if ($response->isRedirection()) {
  47.             return;
  48.         }
  49.         $currentPath $e->getRequest()->getRequestUri() ?: '/';
  50.         foreach ($this->paths as $path => $options) {
  51.             if (preg_match('{'.$path.'}i'$currentPath)) {
  52.                 if ('ALLOW' === $options['header']) {
  53.                     $response->headers->remove('X-Frame-Options');
  54.                 } else {
  55.                     $response->headers->set('X-Frame-Options'$options['header']);
  56.                 }
  57.                 return;
  58.             }
  59.         }
  60.     }
  61. }