vendor/shopware/core/Framework/Routing/CoreSubscriber.php line 34

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Routing;
  3. use Shopware\Core\PlatformRequest;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class CoreSubscriber implements EventSubscriberInterface
  9. {
  10.     /**
  11.      * @var string[]
  12.      */
  13.     private array $cspTemplates;
  14.     public function __construct($cspTemplates)
  15.     {
  16.         $this->cspTemplates = (array) $cspTemplates;
  17.     }
  18.     /**
  19.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  20.      */
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             KernelEvents::REQUEST => 'initializeCspNonce',
  25.             KernelEvents::RESPONSE => 'setSecurityHeaders',
  26.         ];
  27.     }
  28.     public function initializeCspNonce(RequestEvent $event): void
  29.     {
  30.         $nonce base64_encode(random_bytes(8));
  31.         $event->getRequest()->attributes->set(PlatformRequest::ATTRIBUTE_CSP_NONCE$nonce);
  32.     }
  33.     public function setSecurityHeaders(ResponseEvent $event): void
  34.     {
  35.         if (!$event->getResponse()->isSuccessful()) {
  36.             return;
  37.         }
  38.         $response $event->getResponse();
  39.         if ($event->getRequest()->isSecure()) {
  40.             $response->headers->set('Strict-Transport-Security''max-age=31536000; includeSubDomains');
  41.         }
  42.         $response->headers->set('X-Frame-Options''deny');
  43.         $response->headers->set('X-Content-Type-Options''nosniff');
  44.         $response->headers->set('Referrer-Policy''strict-origin-when-cross-origin');
  45.         $cspTemplate $this->cspTemplates['default'] ?? '';
  46.         $scope $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE);
  47.         if ($scope) {
  48.             foreach ($scope->getScopes() as $scope) {
  49.                 $cspTemplate $this->cspTemplates[$scope] ?? $cspTemplate;
  50.             }
  51.         }
  52.         $cspTemplate trim($cspTemplate);
  53.         if ($cspTemplate !== '' && !$response->headers->has('Content-Security-Policy')) {
  54.             $nonce $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_CSP_NONCE);
  55.             if (\is_string($nonce)) {
  56.                 $csp str_replace('%nonce%'$nonce$cspTemplate);
  57.                 $csp str_replace(["\n""\r"], ' '$csp);
  58.                 $response->headers->set('Content-Security-Policy'$csp);
  59.             }
  60.         }
  61.     }
  62. }