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

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