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

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