Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Validator] prevent hash collisions caused by reused object hashes #38387

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

Merged
merged 2 commits into from
Nov 13, 2020
Merged
Changes from 1 commit
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
Next Next commit
[Validator][RecursiveContextualValidator] Prevent validated hash coll…
…isions
  • Loading branch information
fancyweb authored and xabbuh committed Nov 12, 2020
commit 9645fa39ece71f41c583ab5153cfda209bec4c8c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\All;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Collection;
use Symfony\Component\Validator\Constraints\Expression;
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;
Expand All @@ -26,6 +29,7 @@
use Symfony\Component\Validator\Constraints\Required;
use Symfony\Component\Validator\Constraints\Traverse;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\ConstraintValidatorFactory;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\Context\ExecutionContextFactory;
Expand Down Expand Up @@ -2135,4 +2139,47 @@ public function testOptionalConstraintIsIgnored()

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

public function testValidatedConstraintsHashesDoNotCollide()
{
$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 TestConstraintHashesDoNotCollide()));
}
}

final class TestConstraintHashesDoNotCollide extends Constraint
{
}

final class TestConstraintHashesDoNotCollideValidator 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']));
}
}