vendor/pimcore/pimcore/bundles/AdminBundle/EventListener/AdminSecurityListener.php line 54

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\ContentSecurityPolicyHandler;
  16. use Pimcore\Bundle\CoreBundle\EventListener\Traits\PimcoreContextAwareTrait;
  17. use Pimcore\Config;
  18. use Pimcore\Http\Request\Resolver\PimcoreContextResolver;
  19. use Pimcore\Http\RequestHelper;
  20. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  21. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  22. use Symfony\Component\HttpKernel\KernelEvents;
  23. /**
  24.  * @internal
  25.  */
  26. class AdminSecurityListener implements EventSubscriberInterface
  27. {
  28.     use PimcoreContextAwareTrait;
  29.     /**
  30.      * @param ContentSecurityPolicyHandler $contentSecurityPolicyHandler
  31.      */
  32.     public function __construct(
  33.         protected RequestHelper $requestHelper,
  34.         protected ContentSecurityPolicyHandler $contentSecurityPolicyHandler,
  35.         protected Config $config
  36.     ) {
  37.     }
  38.     /**
  39.      * {@inheritdoc}
  40.      */
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             KernelEvents::RESPONSE => 'onKernelResponse',
  45.         ];
  46.     }
  47.     public function onKernelResponse(ResponseEvent $event)
  48.     {
  49.         if (!$this->config['admin_csp_header']['enabled']) {
  50.             return;
  51.         }
  52.         $request $event->getRequest();
  53.         if (!$event->isMainRequest()) {
  54.             return;
  55.         }
  56.         if (!$this->matchesPimcoreContext($requestPimcoreContextResolver::CONTEXT_ADMIN)) {
  57.             return;
  58.         }
  59.         if ($this->requestHelper->isFrontendRequestByAdmin($request)) {
  60.             return;
  61.         }
  62.         if (!empty($this->config['admin_csp_header']['exclude_paths'])) {
  63.             $requestUri $request->getRequestUri();
  64.             foreach ($this->config['admin_csp_header']['exclude_paths'] as $path) {
  65.                 if (@preg_match($path$requestUri)) {
  66.                     return;
  67.                 }
  68.             }
  69.         }
  70.         $response $event->getResponse();
  71.         // set CSP header with random nonce string to the response
  72.         $response->headers->set('Content-Security-Policy'$this->contentSecurityPolicyHandler->getCspHeader());
  73.     }
  74. }