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

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\Translation\TranslatorInterface;
  16. class CsrfRouteListener implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var bool
  20.      */
  21.     protected $csrfEnabled;
  22.     /**
  23.      * @var string
  24.      */
  25.     protected $csrfMode;
  26.     private CsrfTokenManagerInterface $csrfTokenManager;
  27.     /**
  28.      * Used to track if the csrf token has already been check for the request
  29.      */
  30.     private bool $csrfChecked false;
  31.     /**
  32.      * @var TranslatorInterface
  33.      */
  34.     private $translator;
  35.     public function __construct(
  36.         CsrfTokenManagerInterface $csrfTokenManager,
  37.         bool $csrfEnabled,
  38.         string $csrfMode,
  39.         TranslatorInterface $translator
  40.     ) {
  41.         $this->csrfTokenManager $csrfTokenManager;
  42.         $this->csrfEnabled $csrfEnabled;
  43.         $this->translator $translator;
  44.         $this->csrfMode $csrfMode;
  45.     }
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             KernelEvents::CONTROLLER => [
  50.                 ['csrfCheck'KernelListenerPriorities::KERNEL_CONTROLLER_EVENT_CONTEXT_RESOLVE_PRE],
  51.             ],
  52.         ];
  53.     }
  54.     public function csrfCheck(ControllerEvent $event): void
  55.     {
  56.         if (!$this->csrfEnabled || $this->csrfChecked === true) {
  57.             return;
  58.         }
  59.         $request $event->getRequest();
  60.         if ($request->attributes->get(SalesChannelRequest::ATTRIBUTE_CSRF_PROTECTEDtrue) === false) {
  61.             return;
  62.         }
  63.         if ($request->getMethod() !== Request::METHOD_POST) {
  64.             return;
  65.         }
  66.         /** @var RouteScope|array $scopes */
  67.         $scopes $request->attributes->get(PlatformRequest::ATTRIBUTE_ROUTE_SCOPE, []);
  68.         if ($scopes instanceof RouteScope) {
  69.             $scopes $scopes->getScopes();
  70.         }
  71.         // Only check csrf token on storefront routes
  72.         if (!\in_array(StorefrontRouteScope::ID$scopestrue)) {
  73.             return;
  74.         }
  75.         $this->validateCsrfToken($request);
  76.     }
  77.     public function validateCsrfToken(Request $request): void
  78.     {
  79.         $this->csrfChecked true;
  80.         $submittedCSRFToken = (string) $request->request->get('_csrf_token');
  81.         if ($this->csrfMode === CsrfModes::MODE_TWIG) {
  82.             $intent = (string) $request->attributes->get('_route');
  83.         } else {
  84.             $intent 'ajax';
  85.         }
  86.         $csrfCookies = (array) $request->cookies->get('csrf');
  87.         if (
  88.             (!isset($csrfCookies[$intent]) || $csrfCookies[$intent] !== $submittedCSRFToken)
  89.             && !$this->csrfTokenManager->isTokenValid(new CsrfToken($intent$submittedCSRFToken))
  90.         ) {
  91.             $session $request->getSession();
  92.             /* @see https://github.com/symfony/symfony/issues/41765 */
  93.             if (method_exists($session'getFlashBag')) {
  94.                 if ($request->isXmlHttpRequest()) {
  95.                     $session->getFlashBag()->add('danger'$this->translator->trans('error.message-403-ajax'));
  96.                 } else {
  97.                     $session->getFlashBag()->add('danger'$this->translator->trans('error.message-403'));
  98.                 }
  99.             }
  100.             throw new InvalidCsrfTokenException();
  101.         }
  102.     }
  103. }