8000 [Validator] Add additional versions (`*_NO_PUBLIC`, `*_ONLY_PRIV` & `*_ONLY_RES`) in IP address constraint by Ninos · Pull Request #51777 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Add additional versions (*_NO_PUBLIC, *_ONLY_PRIV & *_ONLY_RES) in IP address constraint #51777

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 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a98f828
Enhancement: Add *_ONLY_PRIV & *_ONLY_RES as possible versions in IP …
Ninos Sep 28, 2023
9551dc3
Updated: Changelog
Ninos Sep 28, 2023
2b1e888
Updated: UPGRADE-6.4.md
Ninos Sep 28, 2023
24c73a3
CS
Ninos Sep 28, 2023
2200cd8
Added: Tests for `Mac` constraint
Ninos Sep 28, 2023
3cb3119
Fixed: Tests for `Mac` constraint
Ninos Sep 28, 2023
99e03ea
Added: OnlyPrivate & OnlyReserved tests
Ninos Sep 28, 2023
c11c261
Enhancement: OnlyPrivate & OnlyReserved tests
Ninos Sep 28, 2023
12f126e
CS
Ninos Sep 28, 2023
e4e6a24
Update src/Symfony/Component/Validator/Constraints/Mac.php
Ninos Oct 5, 2023
0e6cf5e
Update src/Symfony/Component/Validator/Constraints/Ip.php
Ninos Oct 5, 2023
0b7910f
Update src/Symfony/Component/Validator/Constraints/MacValidator.php
Ninos Oct 5, 2023
719be6c
Update src/Symfony/Component/Validator/Constraints/Mac.php
Ninos Oct 5, 2023
a6eb4f2
Update src/Symfony/Component/Validator/Constraints/Mac.php
Ninos Oct 5, 2023
4b4c5e4
Update src/Symfony/Component/Validator/Constraints/Mac.php
Ninos Oct 5, 2023
9cf46fc
Update src/Symfony/Component/Validator/Constraints/Mac.php
Ninos Oct 5, 2023
9775156
CS
Ninos Oct 5, 2023
646fd5f
Removed: Mac constraint
Ninos Oct 6, 2023
26e9f2a
[Validator] Remove `Mac` constraint
Ninos Oct 6, 2023
6e84494
[Validator] Fix IpTest
Ninos Oct 6, 2023
cfff49a
[Validator] Renamed constants to full-word names
Ninos Nov 5, 2023
3f58f6b
[Validator] Renamed constants to full-word names
Ninos Nov 5, 2023
a0419db
[Validator] Possibility to use all `Ip` constraint versions in `Cidr`…
Ninos Nov 6, 2023
ed09494
[Validator] Possibility to use all `Ip` constraint versions in `Cidr`…
Ninos Nov 6, 2023
65b3184
[Validator] Possibility to use all `Ip` constraint versions in `Cidr`…
Ninos Nov 6, 2023
b066087
[Validator] Possibility to use all `Ip` constraint versions in `Cidr`…
Ninos Nov 6, 2023
1991107
[Validator] Fixed tests & added `*_NO_PUBLIC`
Ninos Nov 6, 2023
cbdd81f
[Validator] Fixed tests
Ninos Nov 6, 2023
601809c
[Validator] Added normalizer to `Cidr` + better string support
Ninos Nov 7, 2023
cc930ef
Merge branch '7.1' into constraints-networking
Ninos Nov 20, 2023
0d12319
[Validator] Small fixes
Ninos Nov 20, 2023
b58bb03
[Validator] Small fixes
Ninos Nov 20, 2023
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
8000
Diff view
Prev Previous commit
Next Next commit
[Validator] Fixed tests & added *_NO_PUBLIC
  • Loading branch information
Ninos committed Nov 6, 2023
commit 1991107a136627e3d68ec7a72efa43b92c944ebd
2 changes: 1 addition & 1 deletion src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGELOG
6.4
---

* Add `*_ONLY_PRIVATE` and `*_ONLY_RESERVED` versions to `Ip` constraint
* Add `*_NO_PUBLIC`, `*_ONLY_PRIVATE` and `*_ONLY_RESERVED` versions to `Ip` constraint
* Possibility to use all `Ip` constraint versions for `Cidr` constraint
* Add `is_valid` function to the `Expression` constraint, its behavior is the same as `ValidatorInterface::validate`
* Allow single integer for the `versions` option of the `Uuid` constraint
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Cidr.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ class Cidr extends Constraint
Ip::V6 => 128,
Ip::ALL => 128,

Ip::V4_NO_PUBLIC => 32,
Ip::V6_NO_PUBLIC => 128,
Ip::ALL_NO_PUBLIC => 128,

Ip::V4_NO_PRIVATE => 32,
Ip::V6_NO_PRIVATE => 128,
Ip::ALL_NO_PRIVATE => 128,
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class Ip extends Constraint
public const V6 = '6';
public const ALL = 'all';

// adds inverse FILTER_FLAG_NO_RES_RANGE and FILTER_FLAG_NO_PRIV_RANGE flags (skip both)
public const V4_NO_PUBLIC = '4_no_public';
public const V6_NO_PUBLIC = '6_no_public';
public const ALL_NO_PUBLIC = 'all_no_public';

// adds FILTER_FLAG_NO_PRIV_RANGE flag (skip private ranges)
public const V4_NO_PRIVATE = '4_no_private';
public const V6_NO_PRIVATE = '6_no_private';
Expand Down Expand Up @@ -99,6 +104,10 @@ class Ip extends Constraint
self::V6,
self::ALL,

self::V4_NO_PUBLIC,
self::V6_NO_PUBLIC,
self::ALL_NO_PUBLIC,

self::V4_NO_PRIVATE,
self::V6_NO_PRIVATE,
self::ALL_NO_PRIVATE,
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Validator/Constraints/IpValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ class IpValidator extends ConstraintValidator
public static function checkIp(string $ip, mixed $version): bool
{
$flag = match ($version) {
Ip::V4, Ip::V4_ONLY_PRIVATE, Ip::V4_ONLY_RESERVED => \FILTER_FLAG_IPV4,
Ip::V6, Ip::V6_ONLY_PRIVATE, Ip::V6_ONLY_RESERVED => \FILTER_FLAG_IPV6,
Ip::V4, Ip::V4_NO_PUBLIC, Ip::V4_ONLY_PRIVATE, Ip::V4_ONLY_RESERVED => \FILTER_FLAG_IPV4,
Ip::V6, Ip::V6_NO_PUBLIC, Ip::V6_ONLY_PRIVATE, Ip::V6_ONLY_RESERVED => \FILTER_FLAG_IPV6,
Ip::V4_NO_PRIVATE => \FILTER_FLAG_IPV4 | \FILTER_FLAG_NO_PRIV_RANGE,
Ip::V6_NO_PRIVATE => \FILTER_FLAG_IPV6 | \FILTER_FLAG_NO_PRIV_RANGE,
Ip::ALL_NO_PRIVATE => \FILTER_FLAG_NO_PRIV_RANGE,
Expand All @@ -53,6 +53,7 @@ public static function checkIp(string $ip, mixed $version): bool
}

$inverseFlag = match ($version) {
Ip::V4_NO_PUBLIC, Ip::V6_NO_PUBLIC, Ip::ALL_NO_PUBLIC => \FILTER_FLAG_NO_PRIV_RANGE | \FILTER_FLAG_NO_RES_RANGE,
Ip::V4_ONLY_PRIVATE, Ip::V6_ONLY_PRIVATE, Ip::ALL_ONLY_PRIVATE => \FILTER_FLAG_NO_PRIV_RANGE,
Ip::V4_ONLY_RESERVED, Ip::V6_ONLY_RESERVED, Ip::ALL_ONLY_RESERVED => \FILTER_FLAG_NO_RES_RANGE,
default => 0,
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Validator/Tests/Constraints/CidrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ public function testForV6()

public function testWithInvalidVersion()
{
$availableVersions = [Ip::ALL, Ip::V4, Ip::V6];
$availableVersions = [
Ip::V4, Ip::V6, Ip::ALL,
Ip::V4_NO_PUBLIC, Ip::V6_NO_PUBLIC, Ip::ALL_NO_PUBLIC,
Ip::V4_NO_PRIVATE, Ip::V6_NO_PRIVATE, Ip::ALL_NO_PRIVATE,
Ip::V4_NO_RESERVED, Ip::V6_NO_RESERVED, Ip::ALL_NO_RESERVED,
Ip::V4_ONLY_PUBLIC, Ip::V6_ONLY_PUBLIC, Ip::ALL_ONLY_PUBLIC,
Ip::V4_ONLY_PRIVATE, Ip::V6_ONLY_PRIVATE, Ip::ALL_ONLY_PRIVATE,
Ip::V4_ONLY_RESERVED, Ip::V6_ONLY_RESERVED, Ip::ALL_ONLY_RESERVED
];

self::expectException(ConstraintDefinitionException::class);
self::expectExceptionMessage(sprintf('The option "version" must be one of "%s".', implode('", "', $availableVersions)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ public static function getWithInvalidMasksAndIps(): array
{
return [
['0.0.0.0/foobar'],
['10.0.0.0/128'],
['123.45.67.178/aaa'],
['172.16.0.0//'],
['172.16.0.0/a/'],
Expand All @@ -240,6 +239,7 @@ public static function getOutOfRangeNetmask(): array
{
return [
['10.0.0.0/24', Ip::V4, 10, 20],
['10.0.0.0/128'],
['2001:0DB8:85A3:0000:0000:8A2E:0370:7334/24', Ip::V6, 10, 20],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,24 @@ public function testInvalidIpsV4($ip)
->assertRaised();
}

/**
* @dataProvider getValidIpsV4
*/
public function testInvalidNoPublicIpsV4($ip)
{
$constraint = new Ip([
'version' => Ip::V4_NO_PUBLIC,
'message' => 'myMessage',
]);

$this->validator->validate($ip, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$ip.'"')
->setCode(Ip::INVALID_IP_ERROR)
->assertRaised();
}

public static function getValidPublicIpsV4()
{
return [
Expand Down
0