10000 [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 20 commits
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
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.4
---

* Add `*_ONLY_PRIV` and `*_ONLY_RES` versions to `Ip` 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
* Allow single constraint to be passed to the `constraints` option of the `When` constraint
Expand Down
19 changes: 19 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Ip.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Joseph Bielawski <stloyd@gmail.com>
* @author Ninos Ego <me@ninosego.de>
*/
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::IS_REPEATABLE)]
class Ip extends Constraint
Expand All @@ -46,6 +47,16 @@ class Ip extends Constraint
public const V6_ONLY_PUBLIC = '6_public';
public const ALL_ONLY_PUBLIC = 'all_public';

// adds inverse FILTER_FLAG_NO_PRIV_RANGE
public const V4_ONLY_PRIV = '4_priv';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PRIV is an abbreviation for "PRIVATE" ? If yes I would suffix the constants with PRIVATE instead of PRIV

Same for the other constants.

Excepts this is commonly used like this, what I don't know

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He reused the same syntax as the existing *_NO_PRIV

public const V6_ONLY_PRIV = '6_priv';
public const ALL_ONLY_PRIV = 'all_priv';

// adds inverse FILTER_FLAG_NO_RES_RANGE
public const V4_ONLY_RES = '4_res';
public const V6_ONLY_RES = '6_res';
public const ALL_ONLY_RES = 'all_res';

public const INVALID_IP_ERROR = 'b1b427ae-9f6f-41b0-aa9b-84511fbb3c5b';

protected const VERSIONS = [
Expand All @@ -64,6 +75,14 @@ class Ip extends Constraint
self::V4_ONLY_PUBLIC,
self::V6_ONLY_PUBLIC,
self::ALL_ONLY_PUBLIC,

self::V4_ONLY_PRIV,
self::V6_ONLY_PRIV,
self::ALL_ONLY_PRIV,

self::V4_ONLY_RES,
self::V6_ONLY_RES,
self::ALL_ONLY_RES,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RES is super cryptic, let's go with the full RESERVED word

Let's duplicate all consts to add these full-word names as aliases (PRIV also)

];

protected const ERROR_NAMES = [
Expand Down
20 changes: 18 additions & 2 deletions src/Symfony/Component/Validator/Constraints/IpValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*
* @author Bernhard Schussek <bschussek@gmail.com>
* @author Joseph Bielawski <stloyd@gmail.com>
* @author Ninos Ego <me@ninosego.de>
*/
class IpValidator extends ConstraintValidator
{
Expand Down Expand Up @@ -48,8 +49,8 @@ public function validate(mixed $value, Constraint $constraint)
}

$flag = match ($constraint->version) {
Ip::V4 => \FILTER_FLAG_IPV4,
Ip::V6 => \FILTER_FLAG_IPV6,
Ip::V4, Ip::V4_ONLY_PRIV, Ip::V4_ONLY_RES => \FILTER_FLAG_IPV4,
Ip::V6, Ip::V6_ONLY_PRIV, Ip::V6_ONLY_RES => \FILTER_FLAG_IPV6,
Ip::V4_NO_PRIV => \FILTER_FLAG_IPV4 | \FILTER_FLAG_NO_PRIV_RANGE,
Ip::V6_NO_PRIV => \FILTER_FLAG_IPV6 | \FILTER_FLAG_NO_PRIV_RANGE,
Ip::ALL_NO_PRIV => \FILTER_FLAG_NO_PRIV_RANGE,
Expand All @@ -67,6 +68,21 @@ public function validate(mixed $value, Constraint $constraint)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Ip::INVALID_IP_ERROR)
->addViolation();

return;
}

$inverseFlag = match ($constraint->version) {
Ip::V4_ONLY_PRIV, Ip::V6_ONLY_PRIV, Ip::ALL_ONLY_PRIV => \FILTER_FLAG_NO_PRIV_RANGE,
Ip::V4_ONLY_RES, Ip::V6_ONLY_RES, Ip::ALL_ONLY_RES => \FILTER_FLAG_NO_RES_RANGE,
default => 0,
};

if ($inverseFlag && filter_var($value, \FILTER_VALIDATE_IP, $inverseFlag)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setCode(Ip::INVALID_IP_ERROR)
->addViolation();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testValidIpV6WithWhitespacesNamed()
{
$this->validator->validate(
"\n\t2001:0db8:85a3:0000:0000:8a2e:0370:7334\r\n",
new Ip(version: \Symfony\Component\Validator\Constraints\Ip::V6, normalizer: 'trim')
new Ip(version: Ip::V6, normalizer: 'trim')
);

$this->assertNoViolation();
Expand Down Expand Up @@ -185,6 +185,15 @@ public function testInvalidIpsV4($ip)
->assertRaised();
}

public static function getValidPublicIpsV4()
{
return [
['8.0.0.0'],
['90.0.0.0'],
['110.0.0.110'],
];
}

public static function getInvalidIpsV4()
{
return [
Expand All @@ -201,7 +210,19 @@ public static function getInvalidIpsV4()
}

/**
* @dataProvider getInvalidPrivateIpsV4
* @dataProvider getValidPrivateIpsV4
*/
public function testValidPrivateIpsV4($ip)
{
$this->validator->validate($ip, new Ip([
'version' => Ip::V4_ONLY_PRIV,
]));

$this->assertNoViolation();
}

/**
* @dataProvider getValidPrivateIpsV4
*/
public function testInvalidPrivateIpsV4($ip)
{
Expand All @@ -218,7 +239,25 @@ public function testInvalidPrivateIpsV4($ip)
->assertRaised();
}

public static function getInvalidPrivateIpsV4()
/**
* @dataProvider getInvalidPrivateIpsV4
*/
public function testInvalidOnlyPrivateIpsV4($ip)
{
$constraint = new Ip([
'version' => Ip::V4_ONLY_PRIV,
'message' => 'myMessage',
]);

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

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

public static function getValidPrivateIpsV4()
{
return [
['10.0.0.0'],
Expand All @@ -227,8 +266,25 @@ public static function getInvalidPrivateIpsV4()
];
}

public static function getInvalidPrivateIpsV4()
{
return array_merge(self::getValidPublicIpsV4(), self::getValidReservedIpsV4());
}

/**
* @dataProvider getInvalidReservedIpsV4
* @dataProvider getValidReservedIpsV4
*/
public function testValidReservedIpsV4($ip)
{
$this->validator->validate($ip, new Ip([
'version' => Ip::V4_ONLY_RES,
]));

$this->assertNoViolation();
}

/**
* @dataProvider getValidReservedIpsV4
*/
public function testInvalidReservedIpsV4($ip)
{
Expand All @@ -245,7 +301,25 @@ public function testInvalidReservedIpsV4($ip)
->assertRaised();
}

public static function getInvalidReservedIpsV4()
/**
* @dataProvider getInvalidReservedIpsV4
*/
public function testInvalidOnlyReservedIpsV4($ip)
{
$constraint = new Ip([
'version' => Ip::V4_ONLY_RES,
'message' => 'myMessage',
]);

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

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

public static function getValidReservedIpsV4()
{
return [
['0.0.0.0'],
Expand All @@ -254,6 +328,11 @@ public static function getInvalidReservedIpsV4()
];
}

public static function getInvalidReservedIpsV4()
{
return array_merge(self::getValidPublicIpsV4(), self::getValidPrivateIpsV4());
}

/**
* @dataProvider getInvalidPublicIpsV4
*/
Expand All @@ -274,7 +353,7 @@ public function testInvalidPublicIpsV4($ip)

public static function getInvalidPublicIpsV4()
{
return array_merge(self::getInvalidPrivateIpsV4(), self::getInvalidReservedIpsV4());
return array_merge(self::getValidPrivateIpsV4(), self::getValidReservedIpsV4());
}

/**
Expand Down Expand Up @@ -433,7 +512,7 @@ public function testInvalidPrivateIpsAll($ip)

public static function getInvalidPrivateIpsAll()
{
return array_merge(self::getInvalidPrivateIpsV4(), self::getInvalidPrivateIpsV6());
return array_merge(self::getValidPrivateIpsV4(), self::getInvalidPrivateIpsV6());
}

/**
Expand All @@ -456,7 +535,7 @@ public function testInvalidReservedIpsAll($ip)

public static function getInvalidReservedIpsAll()
{
return array_merge(self::getInvalidReservedIpsV4(), self::getInvalidReservedIpsV6());
return array_merge(self::getValidReservedIpsV4(), self::getInvalidReservedIpsV6());
}

/**
Expand Down
0