Skip to content

Commit 9645fa3

Browse files
fancywebxabbuh
authored andcommitted
[Validator][RecursiveContextualValidator] Prevent validated hash collisions
1 parent cdfa9c2 commit 9645fa3

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
@@ -13,11 +13,14 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Translation\IdentityTranslator;
16+
use Symfony\Component\Validator\Constraint;
1617
use Symfony\Component\Validator\Constraints\All;
1718
use Symfony\Component\Validator\Constraints\Callback;
1819
use Symfony\Component\Validator\Constraints\Collection;
1920
use Symfony\Component\Validator\Constraints\Expression;
2021
use Symfony\Component\Validator\Constraints\GroupSequence;
22+
use Symfony\Component\Validator\Constraints\IsFalse;
23+
use Symfony\Component\Validator\Constraints\IsNull;
2124
use Symfony\Component\Validator\Constraints\IsTrue;
2225
use Symfony\Component\Validator\Constraints\Length;
2326
use Symfony\Component\Validator\Constraints\NotBlank;
@@ -26,6 +29,7 @@
2629
use Symfony\Component\Validator\Constraints\Required;
2730
use Symfony\Component\Validator\Constraints\Traverse;
2831
use Symfony\Component\Validator\Constraints\Valid;
32+
use Symfony\Component\Validator\ConstraintValidator;
2933
use Symfony\Component\Validator\ConstraintValidatorFactory;
3034
use Symfony\Component\Validator\ConstraintViolationInterface;
3135
use Symfony\Component\Validator\Context\ExecutionContextFactory;
@@ -2135,4 +2139,47 @@ public function testOptionalConstraintIsIgnored()
21352139

21362140
$this->assertCount(0, $violations);
21372141
}
2142+
2143+
public function testValidatedConstraintsHashesDoNotCollide()
2144+
{
2145+
$metadata = new ClassMetadata(Entity::class);
2146+
$metadata->addPropertyConstraint('initialized', new NotNull(['groups' => 'should_pass']));
2147+
$metadata->addPropertyConstraint('initialized', new IsNull(['groups' => 'should_fail']));
2148+
2149+
$this->metadataFactory->addMetadata($metadata);
2150+
2151+
$entity = new Entity();
2152+
$entity->data = new \stdClass();
2153+
2154+
$this->assertCount(2, $this->validator->validate($entity, new TestConstraintHashesDoNotCollide()));
2155+
}
2156+
}
2157+
2158+
final class TestConstraintHashesDoNotCollide extends Constraint
2159+
{
2160+
}
2161+
2162+
final class TestConstraintHashesDoNotCollideValidator extends ConstraintValidator
2163+
{
2164+
/**
2165+
* {@inheritdoc}
2166+
*/
2167+
public function validate($value, Constraint $constraint)
2168+
{
2169+
if (!$value instanceof Entity) {
2170+
throw new \LogicException();
2171+
}
2172+
2173+
$this->context->getValidator()
2174+
->inContext($this->context)
2175+
->atPath('data')
2176+
->validate($value, new NotNull())
2177+
->validate($value, new NotNull())
2178+
->validate($value, new IsFalse());
2179+
2180+
$this->context->getValidator()
2181+
->inContext($this->context)
2182+
->validate($value, null, new GroupSequence(['should_pass']))
2183+
->validate($value, null, new GroupSequence(['should_fail']));
2184+
}
21382185
}

0 commit comments

Comments
 (0)