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

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 array<string>
  13.      */
  14.     private array $cspTemplates;
  15.     /**
  16.      * @internal
  17.      */
  18.     public function __construct($cspTemplates)
  19.     {
  20.         $this->cspTemplates = (array) $cspTemplates;
  21.     }
  22.     /**
  23.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  24.      */
  25.     public static function getSubscribedEvents()
  26.     {
  27.         return [
  28.             KernelEvents::REQUEST => 'initializeCspNonce',
  29.             KernelEvents::RESPONSE => 'setSecurityHeaders',
  30.         ];
  31.     }
  32.     public function initializeCspNonce(RequestEvent $event): void
  33.     {
  34.         $nonce base64_encode(random_bytes(8));
  35.         $event->getRequest()->attributes->set(PlatformRequest::ATTRIBUTE_CSP_NONCE$nonce);
  36.     }
  37.     public function setSecurityHeaders(ResponseEvent $event): void
  38.     {
  39.         if (!$event->getResponse()->isSuccessful()) {
  40.             return;
  41.         }
  42.         $response $event->getResponse();
  43.         if ($event->getRequest()->isSecure()) {
  44.             $response->headers->set('Strict-Transport-Security''max-age=31536000; includeSubDomains');
  45.         }
  46.         $response->headers->set('X-Frame-Options''deny');
  47.         $response->headers->set('X-Content-Type-Options''nosniff');
  48.         $response->headers->set('Referrer-Policy''strict-origin-when-cross-origin');
  49.         $cspTemplate $this->cspTemplates['default'] ?? '';
  50.         $scopes $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, []);
  51.         if ($scopes instanceof RouteScopeAnnotation) {
  52.             $scopes $scopes->getScopes();
  53.         }
  54.         foreach ($scopes as $scope) {
  55.             $cspTemplate $this->cspTemplates[$scope] ?? $cspTemplate;
  56.         }
  57.         $cspTemplate trim($cspTemplate);
  58.         if ($cspTemplate !== '' && !$response->headers->has('Content-Security-Policy')) {
  59.             $nonce $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_CSP_NONCE);
  60.             if (\is_string($nonce)) {
  61.                 $csp str_replace('%nonce%'$nonce$cspTemplate);
  62.                 $csp str_replace(["\n""\r"], ' '$csp);
  63.                 $response->headers->set('Content-Security-Policy'$csp);
  64.             }
  65.         }
  66.     }
  67. }