vendor/shopware/storefront/Framework/Csrf/CsrfRouteListener.php line 72

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Framework\Csrf;
  3. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  4. use Shopware\Core\Framework\Routing\KernelListenerPriorities;
  5. use Shopware\Core\PlatformRequest;
  6. use Shopware\Core\SalesChannelRequest;
  7. use Shopware\Storefront\Framework\Csrf\Exception\InvalidCsrfTokenException;
  8. use Shopware\Storefront\Framework\Routing\StorefrontRouteScope;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use Symfony\Component\Security\Csrf\CsrfToken;
  14. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  15. use Symfony\Contracts\Service\ResetInterface;
  16. use Symfony\Contracts\Translation\TranslatorInterface;
  17. /**
  18.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  19.  */
  20. class CsrfRouteListener implements EventSubscriberInterfaceResetInterface
  21. {
  22.     /**
  23.      * @deprecated tag:v6.5.0 - reason:visibility-change - Will become private and natively typed to bool
  24.      *
  25.      * @var bool
  26.      */
  27.     protected $csrfEnabled;
  28.     /**
  29.      * @deprecated tag:v6.5.0 - reason:visibility-change - Will become private and natively typed to string
  30.      *
  31.      * @var string
  32.      */
  33.     protected $csrfMode;
  34.     private CsrfTokenManagerInterface $csrfTokenManager;
  35.     /**
  36.      * Used to track if the csrf token has already been check for the request
  37.      */
  38.     private bool $csrfChecked false;
  39.     private TranslatorInterface $translator;
  40.     /**
  41.      * @internal
  42.      */
  43.     public function __construct(
  44.         CsrfTokenManagerInterface $csrfTokenManager,
  45.         bool $csrfEnabled,
  46.         string $csrfMode,
  47.         TranslatorInterface $translator
  48.     ) {
  49.         $this->csrfTokenManager $csrfTokenManager;
  50.         $this->csrfEnabled $csrfEnabled;
  51.         $this->translator $translator;
  52.         $this->csrfMode $csrfMode;
  53.     }
  54.     public static function getSubscribedEvents(): array
  55.     {
  56.         return [
  57.             KernelEvents::CONTROLLER => [
  58.                 ['csrfCheck'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_CONTEXT_RESOLVE_PRE],
  59.             ],
  60.         ];
  61.     }
  62.     public function csrfCheck(ControllerEvent $event): void
  63.     {
  64.         if (!$this->csrfEnabled || $this->csrfChecked === true) {
  65.             return;
  66.         }
  67.         $request $event->getRequest();
  68.         if ($request->attributes->get(SalesChannelRequest::ATTRIBUTE_CSRF_PROTECTEDtrue) === false) {
  69.             return;
  70.         }
  71.         if ($request->getMethod() !== Request::METHOD_POST) {
  72.             return;
  73.         }
  74.         /** @var RouteScope|list<string> $scopes */
  75.         $scopes $request->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, []);
  76.         if ($scopes instanceof RouteScope) {
  77.             $scopes $scopes->getScopes();
  78.         }
  79.         // Only check csrf token on storefront routes
  80.         if (!\in_array(StorefrontRouteScope::ID$scopestrue)) {
  81.             return;
  82.         }
  83.         $this->validateCsrfToken($request);
  84.     }
  85.     /**
  86.      * @deprecated tag:v6.5.0 - reason:visibility-change - method will become private in v6.5.0
  87.      */
  88.     public function validateCsrfToken(Request $request): void
  89.     {
  90.         $this->csrfChecked true;
  91.         $submittedCSRFToken = (string) $request->request->get('_csrf_token');
  92.         if ($this->csrfMode === CsrfModes::MODE_TWIG) {
  93.             $intent = (string) $request->attributes->get('_route');
  94.         } else {
  95.             $intent 'ajax';
  96.         }
  97.         $csrfCookies = (array) $request->cookies->get('csrf');
  98.         if (
  99.             (!isset($csrfCookies[$intent]) || $csrfCookies[$intent] !== $submittedCSRFToken)
  100.             && !$this->csrfTokenManager->isTokenValid(new CsrfToken($intent$submittedCSRFToken))
  101.         ) {
  102.             $session $request->getSession();
  103.             /* @see https://github.com/symfony/symfony/issues/41765 */
  104.             if (method_exists($session'getFlashBag')) {
  105.                 if ($request->isXmlHttpRequest()) {
  106.                     $session->getFlashBag()->add('danger'$this->translator->trans('error.message-403-ajax'));
  107.                 } else {
  108.                     $session->getFlashBag()->add('danger'$this->translator->trans('error.message-403'));
  109.                 }
  110.             }
  111.             throw new InvalidCsrfTokenException();
  112.         }
  113.     }
  114.     public function reset(): void
  115.     {
  116.         $this->csrfChecked false;
  117.     }
  118. }