8000 [Validator][RecursiveContextualValidator] Prevent validated hash coll… · symfony/symfony@c175d67 · GitHub
[go: up one dir, main page]

Skip to content

Commit c175d67

Browse files
fancywebxabbuh
authored andcommitted
[Validator][RecursiveContextualValidator] Prevent validated hash collisions
1 parent f4a1885 commit c175d67

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/Symfony/Component/Validator/Tests/Validator/RecursiveValidatorTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,20 @@
1212
namespace Symfony\Component\Validator\Tests\Validator;
1313

1414
use Symfony\Component\Translation\IdentityTranslator;
15+
use Symfony\Component\Validator\Constraint;
1516
use Symfony\Component\Validator\Constraints\All;
1617
use Symfony\Component\Validator\Constraints\Collection;
1718
use Symfony\Component\Validator\Constraints\GroupSequence;
19+
use Symfony\Component\Validator\Constraints\IsFalse;
20+
use Symfony\Component\Validator\Constraints\IsNull;
1821
use Symfony\Component\Validator\Constraints\IsTrue;
1922
use Symfony\Component\Validator\Constraints\Length;
2023
use Symfony\Component\Validator\Constraints\NotBlank;
2124
use Symfony\Component\Validator\Constraints\NotNull;
2225
use Symfony\Component\Validator\Constraints\Optional;
2326
use Symfony\Component\Validator\Constraints\Required;
2427
use Symfony\Component\Validator\Constraints\Valid;
28+
use Symfony\Component\Validator\ConstraintValidator;
2529
use Symfony\Component\Validator\ConstraintValidatorFactory;
2630
use Symfony\Component\Validator\Context\ExecutionContextFactory;
2731
use Symfony\Component\Validator\Mapping\ClassMetadata;
@@ -200,4 +204,47 @@ public function testOptionalConstraintIsIgnored()
200204

201205
$this->assertCount(0, $violations);
202206
}
207+
208+
public function testValidatedConstraintsHashesDoNotCollide()
209+
{
210+
$metadata = new ClassMetadata(Entity::class);
211+
$metadata->addPropertyConstraint('initialized', new NotNull(['groups' => 'should_pass']));
212+
$metadata->addPropertyConstraint('initialized', new IsNull(['groups' => 'should_fail']));
213+
214+
$this->metadataFactory->addMetadata($metadata);
215+
216+
$entity = new Entity();
217+
$entity->data = new \stdClass();
218+
219+
$this->assertCount(2, $this->validator->validate($entity, new TestConstraintHashesDoNotCollide()));
220+
}
221+
}
222+
223+
final class TestConstraintHashesDoNotCollide extends Constraint
224+
{
225+
}
226+
227+
final class TestConstraintHashesDoNotCollideValidator extends ConstraintValidator
228+
{
229+
/**
230+
* {@inheritdoc}
231+
*/
232+
public function validate($value, Constraint $constraint)
233+
{
234+
if (!$value instanceof Entity) {
235+
throw new \LogicException();
236+
}
237+
238+
$this->context->getValidator()
239+
->inContext($this->context)
240+
->atPath('data')
241+
->validate($value, new NotNull())
242+
->validate($value, new NotNull())
243+
->validate($value, new IsFalse());
244+
245+
$this->context->getValidator()
246+
->inContext($this->context)
247+
->validate($value, null, new GroupSequence(['should_pass']))
248+
->validate($value, null, new GroupSequence(['should_fail']));
249+
}
203250
}

0 commit comments

Comments
 (0)
0