Skip to content

Commit

Permalink
do not modify a constraint during validation to not leak its context
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jun 4, 2024
1 parent ecd1d92 commit 34c12b7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/Symfony/Component/Validator/Constraints/CidrValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ public function validate($value, Constraint $constraint): void
return;
}

if (filter_var($ipAddress, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4) && $constraint->netmaskMax > 32) {
$constraint->netmaskMax = 32;
$netmaskMax = $constraint->netmaskMax;

if (filter_var($ipAddress, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4) && $netmaskMax > 32) {
$netmaskMax = 32;
}

if ($netmask < $constraint->netmaskMin || $netmask > $constraint->netmaskMax) {
if ($netmask < $constraint->netmaskMin || $netmask > $netmaskMax) {
$this->context
->buildViolation($constraint->netmaskRangeViolationMessage)
->setParameter('{{ min }}', $constraint->netmaskMin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,17 @@ public static function getWithWrongVersion(): array
['2001:0db8:85a3:0000:0000:8a2e:0370:7334/13', Ip::V4],
];
}

public function testDoesNotModifyContextBetweenValidations()
{
$constraint = new Cidr();

$this->validator->validate('1.2.3.4/28', $constraint);

$this->assertNoViolation();

$this->validator->validate('::1/64', $constraint);

$this->assertNoViolation();
}
}

0 comments on commit 34c12b7

Please sign in to comment.