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

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