Skip to content

Commit fad93db

Browse files
Merge branch '4.2'
* 4.2: fix merge [FrameworkBundle] fix xsd [FrameworkBundle] update xsd to match the 4.2 configuration [FrameworkBundle] Update the xsd to match the actual session configuration [Form] CsrfValidationListener marks the token as invalid if it is not a string [Routing] fix perf issue when dumping large number of routes Fix wrong value in file id attribute for Xliff 2.0 [VarDumper] Fixed phpDoc [PhpUnitBridge] fix PHP 5.3 compat [Messenger] Fix DataCollector template [Filesystem] Fixed some docblocks and typos bumped Symfony version to 4.2.4 updated VERSION for 4.2.3 updated CHANGELOG for 4.2.3 bumped Symfony version to 3.4.23 updated VERSION for 3.4.22 update CONTRIBUTORS for 3.4.22 updated CHANGELOG for 3.4.22 fix some minor typos do not overwrite the constraint being evaluated
2 parents d52da26 + 98cabc9 commit fad93db

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

Extension/Csrf/EventListener/CsrfValidationListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ public function preSubmit(FormEvent $event)
6666
if ($form->isRoot() && $form->getConfig()->getOption('compound') && !$postRequestSizeExceeded) {
6767
$data = $event->getData();
6868

69-
$csrfToken = new CsrfToken($this->tokenId, $data[$this->fieldName] ?? null);
70-
if (!isset($data[$this->fieldName]) || !$this->tokenManager->isTokenValid($csrfToken)) {
69+
$csrfValue = \is_string($data[$this->fieldName] ?? null) ? $data[$this->fieldName] : null;
70+
$csrfToken = new CsrfToken($this->tokenId, $csrfValue);
71+
72+
if (null === $csrfValue || !$this->tokenManager->isTokenValid($csrfToken)) {
7173
$errorMessage = $this->errorMessage;
7274

7375
if (null !== $this->translator) {

Extension/Validator/Constraints/FormValidator.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ class FormValidator extends ConstraintValidator
2626
/**
2727
* {@inheritdoc}
2828
*/
29-
public function validate($form, Constraint $constraint)
29+
public function validate($form, Constraint $formConstraint)
3030
{
31-
if (!$constraint instanceof Form) {
32-
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Form');
31+
if (!$formConstraint instanceof Form) {
32+
throw new UnexpectedTypeException($formConstraint, __NAMESPACE__.'\Form');
3333
}
3434

3535
if (!$form instanceof FormInterface) {
@@ -62,8 +62,8 @@ public function validate($form, Constraint $constraint)
6262
// Otherwise validate a constraint only once for the first
6363
// matching group
6464
foreach ($groups as $group) {
65-
if (\in_array($group, $constraint->groups)) {
66-
$validator->atPath('data')->validate($form->getData(), $constraint, $group);
65+
if (\in_array($group, $formConstraint->groups)) {
66+
$validator->atPath('data')->validate($form->getData(), $formConstraint, $group);
6767
if (\count($this->context->getViolations()) > 0) {
6868
break;
6969
}
@@ -113,7 +113,7 @@ public function validate($form, Constraint $constraint)
113113
? (string) $form->getViewData()
114114
: \gettype($form->getViewData());
115115

116-
$this->context->setConstraint($constraint);
116+
$this->context->setConstraint($formConstraint);
117117
$this->context->buildViolation($config->getOption('invalid_message'))
118118
->setParameters(array_replace(['{{ value }}' => $clientDataAsString], $config->getOption('invalid_message_parameters')))
119119
->setInvalidValue($form->getViewData())
@@ -125,7 +125,7 @@ public function validate($form, Constraint $constraint)
125125

126126
// Mark the form with an error if it contains extra fields
127127
if (!$config->getOption('allow_extra_fields') && \count($form->getExtraData()) > 0) {
128-
$this->context->setConstraint($constraint);
128+
$this->context->setConstraint($formConstraint);
129129
$this->context->buildViolation($config->getOption('extra_fields_message'))
130130
->setParameter('{{ extra_fields }}', '"'.implode('", "', array_keys($form->getExtraData())).'"')
131131
->setInvalidValue($form->getExtraData())

Tests/Extension/Csrf/EventListener/CsrfValidationListenerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ public function testStringFormData()
6464
$this->assertSame($data, $event->getData());
6565
}
6666

67+
public function testArrayCsrfToken()
68+
{
69+
$event = new FormEvent($this->form, ['csrf' => []]);
70+
71+
$validation = new CsrfValidationListener('csrf', $this->tokenManager, 'unknown', 'Invalid.');
72+
$validation->preSubmit($event);
73+
74+
$this->assertNotEmpty($this->form->getErrors());
75+
}
76+
6777
public function testMaxPostSizeExceeded()
6878
{
6979
$serverParams = $this

Tests/Extension/Validator/Constraints/FormValidatorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,20 @@
1313

1414
use Symfony\Component\Form\CallbackTransformer;
1515
use Symfony\Component\Form\Exception\TransformationFailedException;
16+
use Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper;
1617
use Symfony\Component\Form\Extension\Validator\Constraints\Form;
1718
use Symfony\Component\Form\Extension\Validator\Constraints\FormValidator;
1819
use Symfony\Component\Form\FormBuilder;
1920
use Symfony\Component\Form\FormInterface;
2021
use Symfony\Component\Form\SubmitButtonBuilder;
22+
use Symfony\Component\Translation\IdentityTranslator;
2123
use Symfony\Component\Validator\Constraints\GroupSequence;
2224
use Symfony\Component\Validator\Constraints\NotBlank;
2325
use Symfony\Component\Validator\Constraints\NotNull;
2426
use Symfony\Component\Validator\Constraints\Valid;
27+
use Symfony\Component\Validator\Context\ExecutionContext;
2528
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
29+
use Symfony\Component\Validator\Validation;
2630

2731
/**
2832
* @author Bernhard Schussek <[email protected]>
@@ -649,6 +653,27 @@ public function getValidationGroups(FormInterface $form)
649653
return ['group1', 'group2'];
650654
}
651655

656+
public function testCauseForNotAllowedExtraFieldsIsTheFormConstraint()
657+
{
658+
$form = $this
659+
->getBuilder('form', null, ['constraints' => [new NotBlank(['groups' => ['foo']])]])
660+
->setCompound(true)
661+
->setDataMapper(new PropertyPathMapper())
662+
->getForm();
663+
$form->submit([
664+
'extra_data' => 'foo',
665+
]);
666+
667+
$context = new ExecutionContext(Validation::createValidator(), $form, new IdentityTranslator());
668+
$constraint = new Form();
669+
670+
$this->validator->initialize($context);
671+
$this->validator->validate($form, $constraint);
672+
673+
$this->assertCount(1, $context->getViolations());
674+
$this->assertSame($constraint, $context->getViolations()->get(0)->getConstraint());
675+
}
676+
652677
private function getMockExecutionContext()
653678
{
654679
$context = $this->getMockBuilder('Symfony\Component\Validator\Context\ExecutionContextInterface')->getMock();

0 commit comments

Comments
 (0)