vendor/pimcore/pimcore/bundles/AdminBundle/EventListener/CsrfProtectionListener.php line 64

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\AdminBundle\EventListener;
  15. use Pimcore\Bundle\AdminBundle\Security\CsrfProtectionHandler;
  16. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  17. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpKernel\Event\RequestEvent;
  20. use Symfony\Component\HttpKernel\KernelEvents;
  21. use Twig\Environment;
  22. /**
  23.  * @internal
  24.  */
  25. class CsrfProtectionListener implements EventSubscriberInterface
  26. {
  27.     use PimcoreContextAwareTrait;
  28.     /**
  29.      * @var Environment
  30.      */
  31.     protected $twig;
  32.     /**
  33.      * @var CsrfProtectionHandler $handler
  34.      */
  35.     protected $csrfProtectionHandler;
  36.     /**
  37.      * @param CsrfProtectionHandler $csrfProtectionHandler
  38.      */
  39.     public function __construct(CsrfProtectionHandler $csrfProtectionHandler)
  40.     {
  41.         $this->csrfProtectionHandler $csrfProtectionHandler;
  42.     }
  43.     /**
  44.      * {@inheritdoc}
  45.      */
  46.     public static function getSubscribedEvents()
  47.     {
  48.         return [
  49.             KernelEvents::REQUEST => ['handleRequest'11],
  50.         ];
  51.     }
  52.     /**
  53.      * @param RequestEvent $event
  54.      */
  55.     public function handleRequest(RequestEvent $event)
  56.     {
  57.         $request $event->getRequest();
  58.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_ADMIN)) {
  59.             return;
  60.         }
  61.         $this->csrfProtectionHandler->generateCsrfToken();
  62.         if ($request->isMethodCacheable()) {
  63.             return;
  64.         }
  65.         $exludedRoutes = [
  66.             // WebDAV
  67.             'pimcore_admin_webdav',
  68.             // external applications
  69.             'pimcore_admin_external_opcache_index',
  70.             'pimcore_admin_external_adminer_adminer''pimcore_admin_external_adminer_proxy',
  71.             'pimcore_admin_external_adminer_proxy_1''pimcore_admin_external_adminer_proxy_2',
  72.         ];
  73.         $route $request->attributes->get('_route');
  74.         if (in_array($route$exludedRoutes) || in_array($route$this->csrfProtectionHandler->getExcludedRoutes())) {
  75.             return;
  76.         }
  77.         $this->csrfProtectionHandler->checkCsrfToken($request);
  78.     }
  79. }