8000 [Validator][RecursiveContextualValidator] Prevent validated hash collisions by fancyweb · Pull Request #36415 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator][RecursiveContextualValidator] Prevent validated hash collisions #36415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
namespace Symfony\Component\Validator\Tests\Validator;

use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\IsFalse;
use Symfony\Component\Validator\Constraints\IsNull;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Optional;
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
use Symfony\Component\Validator\Mapping\ClassMetadata;
Expand Down Expand Up @@ -200,4 +204,47 @@ public function testOptionalConstraintIsIgnored()

$this->assertCount(0, $violations);
}

public function testValidatedConstraintsHashesDontCollide()
{
$metadata = new ClassMetadata(Entity::class);
$metadata->addPropertyConstraint('initialized', new NotNull(['groups' => 'should_pass']));
$metadata->addPropertyConstraint('initialized', new IsNull(['groups' => 'should_fail']));

$this->metadataFactory->addMetadata($metadata);

$entity = new Entity();
$entity->data = new \stdClass();

$this->assertCount(2, $this->validator->validate($entity, new TestConstraintHashesDontCollide()));
}
}

final class TestConstraintHashesDontCollide extends Constraint
{
}

final class TestConstraintHashesDontCollideValidator extends ConstraintValidator
{
/**
* {@inheritdoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$value instanceof Entity) {
throw new \LogicException();
}

$this->context->getValidator()
->inContext($this->context)
->atPath('data')
->validate($value, new NotNull())
->validate($value, new NotNull())
->validate($value, new IsFalse());

$this->context->getValidator()
->inContext($this->context)
->validate($value, null, new GroupSequence(['should_pass']))
->validate($value, null, new GroupSequence(['should_fail']));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class RecursiveContextualValidator implements ContextualValidatorInterface
private $validatorFactory;
private $objectInitializers;

private $validatedConstraintsReferences = [];
private $initializedObjectsReferences = [];

/**
* Creates a validator for the given context.
*
Expand Down Expand Up @@ -443,6 +446,10 @@ private function validateClassNode($object, $cacheKey, ClassMetadataInterface $m
{
$context->setNode($object, $object, $metadata, $propertyPath);

if (!isset($this->initializedObjectsReferences[$cacheKey])) {
$this->initializedObjectsReferences[$cacheKey] = $object;
}

if (!$context->isObjectInitialized($cacheKey)) {
foreach ($this->objectInitializers as $initializer) {
$initializer->initialize($object);
Expand All @@ -456,9 +463,7 @@ private function validateClassNode($object, $cacheKey, ClassMetadataInterface $m
// to cascade the "Default" group when traversing the group
// sequence
$defaultOverridden = false;

// Use the object hash for group sequences
$groupHash = \is_object($group) ? spl_object_hash($group) : $group;
$groupHash = serialize($group);

if ($context->isGroupValidated($cacheKey, $groupHash)) {
// Skip this group when validating the properties and when
Expand Down Expand Up @@ -803,6 +808,10 @@ private function validateInGroup($value, $cacheKey, MetadataInterface $metadata,
// that constraints belong to multiple validated groups
if (null !== $cacheKey) {
$constraintHash = spl_object_hash($constraint);
if (!isset($this->validatedConstraintsReferences[$constraintHash])) {
$this->validatedConstraintsReferences[$constraintHash] = $constraint;
}

// instanceof Valid: In case of using a Valid constraint with many groups
// it makes a reference object get validated by each group
if ($constraint instanceof Composite || $constraint instanceof Valid) {
Expand Down
0