vendor/symfony/form/Extension/Csrf/EventListener/CsrfValidationListener.php line 65

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\Extension\Csrf\EventListener;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Component\Form\FormError;
  13. use Symfony\Component\Form\FormEvent;
  14. use Symfony\Component\Form\FormEvents;
  15. use Symfony\Component\Form\Util\ServerParams;
  16. use Symfony\Component\Security\Csrf\CsrfToken;
  17. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. /**
  20.  * @author Bernhard Schussek <bschussek@gmail.com>
  21.  */
  22. class CsrfValidationListener implements EventSubscriberInterface
  23. {
  24.     private $fieldName;
  25.     private $tokenManager;
  26.     private $tokenId;
  27.     private $errorMessage;
  28.     private $translator;
  29.     private $translationDomain;
  30.     private $serverParams;
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             FormEvents::PRE_SUBMIT => 'preSubmit',
  35.         ];
  36.     }
  37.     public function __construct(string $fieldNameCsrfTokenManagerInterface $tokenManagerstring $tokenIdstring $errorMessageTranslatorInterface $translator nullstring $translationDomain nullServerParams $serverParams null)
  38.     {
  39.         $this->fieldName $fieldName;
  40.         $this->tokenManager $tokenManager;
  41.         $this->tokenId $tokenId;
  42.         $this->errorMessage $errorMessage;
  43.         $this->translator $translator;
  44.         $this->translationDomain $translationDomain;
  45.         $this->serverParams $serverParams ?? new ServerParams();
  46.     }
  47.     public function preSubmit(FormEvent $event)
  48.     {
  49.         $form $event->getForm();
  50.         $postRequestSizeExceeded 'POST' === $form->getConfig()->getMethod() && $this->serverParams->hasPostMaxSizeBeenExceeded();
  51.         if ($form->isRoot() && $form->getConfig()->getOption('compound') && !$postRequestSizeExceeded) {
  52.             $data $event->getData();
  53.             $csrfValue \is_string($data[$this->fieldName] ?? null) ? $data[$this->fieldName] : null;
  54.             $csrfToken = new CsrfToken($this->tokenId$csrfValue);
  55.             if (null === $csrfValue || !$this->tokenManager->isTokenValid($csrfToken)) {
  56.                 $errorMessage $this->errorMessage;
  57.                 if (null !== $this->translator) {
  58.                     $errorMessage $this->translator->trans($errorMessage, [], $this->translationDomain);
  59.                 }
  60.                 $form->addError(new FormError($errorMessage$errorMessage, [], null$csrfToken));
  61.             }
  62.             if (\is_array($data)) {
  63.                 unset($data[$this->fieldName]);
  64.                 $event->setData($data);
  65.             }
  66.         }
  67.     }
  68. }