Skip to content

Commit 0107529

Browse files
authored
Replace yiisoft/validator dependency to yiisoft/network-utilities (#42)
1 parent 253cf5c commit 0107529

7 files changed

Lines changed: 56 additions & 48 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 1.0.2 under development
44

5-
- no changes in this release.
5+
- Enh #42: Replace `yiisoft/validator` dependency to `yiisoft/network-utilities` (@vjik)
66

77
## 1.0.1 June 02, 2024
88

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
"psr/http-server-handler": "^1.0",
3636
"psr/http-server-middleware": "^1.0",
3737
"yiisoft/http": "^1.2",
38-
"yiisoft/validator": "^1.0"
38+
"yiisoft/network-utilities": "^1.1"
3939
},
4040
"require-dev": {
4141
"httpsoft/http-message": "^1.0",

src/IpValidator.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\ProxyMiddleware;
6+
7+
use Yiisoft\NetworkUtilities\IpHelper;
8+
use Yiisoft\NetworkUtilities\IpRanges;
9+
10+
/**
11+
* @internal
12+
*/
13+
final class IpValidator
14+
{
15+
public static function isIp(string $value): bool
16+
{
17+
return self::isIpV4($value) || self::isIpV6($value);
18+
}
19+
20+
public static function isIpV6(string $value): bool
21+
{
22+
return preg_match(IpHelper::IPV6_REGEXP, $value) === 1;
23+
}
24+
25+
/**
26+
* @param string[] $ranges
27+
*/
28+
public static function inRanges(string $value, array $ranges): bool
29+
{
30+
return (new IpRanges($ranges))->isAllowed($value);
31+
}
32+
33+
private static function isIpV4(string $value): bool
34+
{
35+
return preg_match(IpHelper::IPV4_REGEXP, $value) === 1;
36+
}
37+
}

src/TrustedHostsNetworkResolver.php

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
use Psr\Http\Server\RequestHandlerInterface;
1313
use RuntimeException;
1414
use Yiisoft\Http\HeaderValueHelper;
15+
use Yiisoft\NetworkUtilities\IpRanges;
1516
use Yiisoft\ProxyMiddleware\Exception\InvalidConnectionChainItemException;
1617
use Yiisoft\ProxyMiddleware\Exception\RfcProxyParseException;
17-
use Yiisoft\Validator\Rule\Ip;
18-
use Yiisoft\Validator\ValidatorInterface;
1918

2019
use function count;
2120
use function in_array;
@@ -130,7 +129,6 @@ class TrustedHostsNetworkResolver implements MiddlewareInterface
130129

131130
/**
132131
* @psalm-var list<non-empty-string>
133-
* @psalm-suppress PropertyNotSetInConstructor
134132
*/
135133
private array $trustedIps = [];
136134
/**
@@ -146,7 +144,7 @@ class TrustedHostsNetworkResolver implements MiddlewareInterface
146144
*/
147145
private ?string $connectionChainItemsAttribute = null;
148146

149-
public function __construct(private ValidatorInterface $validator)
147+
public function __construct()
150148
{
151149
}
152150

@@ -165,7 +163,7 @@ public function withTrustedIps(array $trustedIps): self
165163
foreach ($trustedIps as $ip) {
166164
$this->assertIsNonEmptyString($ip, 'Trusted IP');
167165

168-
if (!$this->isIp($ip)) {
166+
if (!IpValidator::isIp($ip)) {
169167
throw new InvalidArgumentException("\"$ip\" is not a valid IP.");
170168
}
171169

@@ -672,7 +670,7 @@ private function parseProxiesFromRfcHeader(array $proxyItems): array
672670
if (isset($matches['ipv6']) && !empty($matches['ipv6'])) {
673671
$ip = $matches['ipv6'];
674672

675-
if (!$this->isIpv6($ip)) {
673+
if (!IpValidator::isIpV6($ip)) {
676674
$message = "Enclosing in square brackets assumes presence of valid IPv6, \"$ip\" given.";
677675

678676
throw new RfcProxyParseException($message);
@@ -722,7 +720,7 @@ private function getConnectionChainItem(
722720
bool $validateIp = true,
723721
bool $validateProtocol = true,
724722
): array {
725-
if ($ip !== null && $validateIp && !$this->isIp($ip)) {
723+
if ($ip !== null && $validateIp && !IpValidator::isIp($ip)) {
726724
throw new InvalidConnectionChainItemException("\"$ip\" is not a valid IP.");
727725
}
728726

@@ -847,47 +845,25 @@ private function assertReverseObfuscatedIpData(?array $ipData): void
847845
}
848846
}
849847

850-
if (!$this->isIp($ipData[0])) {
848+
if (!IpValidator::isIp($ipData[0])) {
851849
throw new RuntimeException('IP returned from reverse-obfuscated IP data is not valid.');
852850
}
853851
}
854852

855-
/**
856-
* @psalm-assert non-empty-string $value
857-
*/
858-
private function isIp(string $value): bool
859-
{
860-
return $this
861-
->validator
862-
->validate($value, [new Ip()])
863-
->isValid();
864-
}
865-
866-
/**
867-
* @psalm-assert non-empty-string $value
868-
*/
869-
private function isIpv6(string $value): bool
870-
{
871-
return $this
872-
->validator
873-
->validate($value, [new Ip(allowIpv4: false)])
874-
->isValid();
875-
}
876-
877853
/**
878854
* @psalm-param non-empty-string $value
879855
*/
880856
private function isPrivateIp(string $value): bool
881857
{
882-
return (new Ip(ranges: ['private']))->isAllowed($value);
858+
return IpValidator::inRanges($value, [IpRanges::PRIVATE]);
883859
}
884860

885861
/**
886862
* @psalm-param non-empty-string $value
887863
*/
888864
private function isTrustedIp(string $value): bool
889865
{
890-
return !empty($this->trustedIps) && (new Ip(ranges: $this->trustedIps))->isAllowed($value);
866+
return !empty($this->trustedIps) && IpValidator::inRanges($value, $this->trustedIps);
891867
}
892868

893869
/**

tests/TrustedHostsNetworkResolver/ProcessTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Yiisoft\Http\Status;
1010
use Yiisoft\ProxyMiddleware\Tests\Support\MockRequestHandler;
1111
use Yiisoft\ProxyMiddleware\TrustedHostsNetworkResolver;
12-
use Yiisoft\Validator\Validator;
1312

1413
final class ProcessTest extends TestCase
1514
{
@@ -1326,7 +1325,7 @@ public function dataProcess(): iterable
13261325
],
13271326
],
13281327
yield 'RFC header, IP related data, hidden IP, obfuscated, reverse-obfuscating' => [
1329-
(new class (new Validator()) extends TrustedHostsNetworkResolver {
1328+
(new class () extends TrustedHostsNetworkResolver {
13301329
protected function reverseObfuscateIpIdentifier(
13311330
string $ipIdentifier,
13321331
array $validatedConnectionChainItems,

tests/TrustedHostsNetworkResolver/RuntimeExceptionTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Yiisoft\ProxyMiddleware\Exception\RfcProxyParseException;
1212
use Yiisoft\ProxyMiddleware\Tests\Support\MockRequestHandler;
1313
use Yiisoft\ProxyMiddleware\TrustedHostsNetworkResolver;
14-
use Yiisoft\Validator\Validator;
1514

1615
final class RuntimeExceptionTest extends TestCase
1716
{
@@ -509,7 +508,7 @@ public function dataReverseObfuscateIpIdentifierException(): iterable
509508
{
510509
return [
511510
yield 'empty array' => [
512-
(new class (new Validator()) extends TrustedHostsNetworkResolver {
511+
(new class () extends TrustedHostsNetworkResolver {
513512
protected function reverseObfuscateIpIdentifier(
514513
string $ipIdentifier,
515514
array $validatedConnectionChainItems,
@@ -533,7 +532,7 @@ protected function reverseObfuscateIpIdentifier(
533532
'Reverse-obfuscated IP data can\'t be empty.',
534533
],
535534
yield 'wrong items count' => [
536-
(new class (new Validator()) extends TrustedHostsNetworkResolver {
535+
(new class () extends TrustedHostsNetworkResolver {
537536
protected function reverseObfuscateIpIdentifier(
538537
string $ipIdentifier,
539538
array $validatedConnectionChainItems,
@@ -557,7 +556,7 @@ protected function reverseObfuscateIpIdentifier(
557556
'Invalid array keys for reverse-obfuscated IP data. The allowed and required keys are: "0", "1".',
558557
],
559558
yield 'IP: not a string' => [
560-
(new class (new Validator()) extends TrustedHostsNetworkResolver {
559+
(new class () extends TrustedHostsNetworkResolver {
561560
protected function reverseObfuscateIpIdentifier(
562561
string $ipIdentifier,
563562
array $validatedConnectionChainItems,
@@ -581,7 +580,7 @@ protected function reverseObfuscateIpIdentifier(
581580
'IP returned from reverse-obfuscated IP data must be non-empty string.',
582581
],
583582
yield 'IP: empty string' => [
584-
(new class (new Validator()) extends TrustedHostsNetworkResolver {
583+
(new class () extends TrustedHostsNetworkResolver {
585584
protected function reverseObfuscateIpIdentifier(
586585
string $ipIdentifier,
587586
array $validatedConnectionChainItems,
@@ -605,7 +604,7 @@ protected function reverseObfuscateIpIdentifier(
605604
'IP returned from reverse-obfuscated IP data must be non-empty string.',
606605
],
607606
yield 'IP: invalid' => [
608-
(new class (new Validator()) extends TrustedHostsNetworkResolver {
607+
(new class () extends TrustedHostsNetworkResolver {
609608
protected function reverseObfuscateIpIdentifier(
610609
string $ipIdentifier,
611610
array $validatedConnectionChainItems,
@@ -629,7 +628,7 @@ protected function reverseObfuscateIpIdentifier(
629628
'IP returned from reverse-obfuscated IP data is not valid.',
630629
],
631630
yield 'port: empty string' => [
632-
(new class (new Validator()) extends TrustedHostsNetworkResolver {
631+
(new class () extends TrustedHostsNetworkResolver {
633632
protected function reverseObfuscateIpIdentifier(
634633
string $ipIdentifier,
635634
array $validatedConnectionChainItems,
@@ -653,7 +652,7 @@ protected function reverseObfuscateIpIdentifier(
653652
'Port returned from reverse-obfuscated IP data must be non-empty string.',
654653
],
655654
yield 'IP: valid port instead of IP, port: invalid' => [
656-
(new class (new Validator()) extends TrustedHostsNetworkResolver {
655+
(new class () extends TrustedHostsNetworkResolver {
657656
protected function reverseObfuscateIpIdentifier(
658657
string $ipIdentifier,
659658
array $validatedConnectionChainItems,

tests/TrustedHostsNetworkResolver/TestCase.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
use PHPUnit\Framework\TestCase as PHPUnitTestCase;
99
use Psr\Http\Message\ServerRequestInterface;
1010
use Yiisoft\ProxyMiddleware\TrustedHostsNetworkResolver;
11-
use Yiisoft\Validator\Validator;
1211

1312
class TestCase extends PHPUnitTestCase
1413
{
1514
protected function createMiddleware(): TrustedHostsNetworkResolver
1615
{
17-
$validator = new Validator();
18-
19-
return new TrustedHostsNetworkResolver($validator);
16+
return new TrustedHostsNetworkResolver();
2017
}
2118

2219
protected function createRequest(

0 commit comments

Comments
 (0)