diff --git a/Constraints/CidrValidator.php b/Constraints/CidrValidator.php index 4fc78a78..1c6f4c0b 100644 --- a/Constraints/CidrValidator.php +++ b/Constraints/CidrValidator.php @@ -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) diff --git a/Tests/Constraints/CidrValidatorTest.php b/Tests/Constraints/CidrValidatorTest.php index 9274d81d..5ac53109 100644 --- a/Tests/Constraints/CidrValidatorTest.php +++ b/Tests/Constraints/CidrValidatorTest.php @@ -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(); + } }