diff --git a/CHANGELOG.md b/CHANGELOG.md index 0698cff..3437b31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 1.0.2 under development -- no changes in this release. +- Enh #42: Replace `yiisoft/validator` dependency to `yiisoft/network-utilities` (@vjik) ## 1.0.1 June 02, 2024 diff --git a/composer.json b/composer.json index ce0dcf6..1414345 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,7 @@ "psr/http-server-handler": "^1.0", "psr/http-server-middleware": "^1.0", "yiisoft/http": "^1.2", - "yiisoft/validator": "^1.0" + "yiisoft/network-utilities": "^1.1" }, "require-dev": { "httpsoft/http-message": "^1.0", diff --git a/src/IpValidator.php b/src/IpValidator.php new file mode 100644 index 0000000..05d9f13 --- /dev/null +++ b/src/IpValidator.php @@ -0,0 +1,37 @@ +isAllowed($value); + } + + private static function isIpV4(string $value): bool + { + return preg_match(IpHelper::IPV4_REGEXP, $value) === 1; + } +} diff --git a/src/TrustedHostsNetworkResolver.php b/src/TrustedHostsNetworkResolver.php index d55192e..444b4dd 100644 --- a/src/TrustedHostsNetworkResolver.php +++ b/src/TrustedHostsNetworkResolver.php @@ -12,10 +12,9 @@ use Psr\Http\Server\RequestHandlerInterface; use RuntimeException; use Yiisoft\Http\HeaderValueHelper; +use Yiisoft\NetworkUtilities\IpRanges; use Yiisoft\ProxyMiddleware\Exception\InvalidConnectionChainItemException; use Yiisoft\ProxyMiddleware\Exception\RfcProxyParseException; -use Yiisoft\Validator\Rule\Ip; -use Yiisoft\Validator\ValidatorInterface; use function count; use function in_array; @@ -130,7 +129,6 @@ class TrustedHostsNetworkResolver implements MiddlewareInterface /** * @psalm-var list - * @psalm-suppress PropertyNotSetInConstructor */ private array $trustedIps = []; /** @@ -146,7 +144,7 @@ class TrustedHostsNetworkResolver implements MiddlewareInterface */ private ?string $connectionChainItemsAttribute = null; - public function __construct(private ValidatorInterface $validator) + public function __construct() { } @@ -165,7 +163,7 @@ public function withTrustedIps(array $trustedIps): self foreach ($trustedIps as $ip) { $this->assertIsNonEmptyString($ip, 'Trusted IP'); - if (!$this->isIp($ip)) { + if (!IpValidator::isIp($ip)) { throw new InvalidArgumentException("\"$ip\" is not a valid IP."); } @@ -672,7 +670,7 @@ private function parseProxiesFromRfcHeader(array $proxyItems): array if (isset($matches['ipv6']) && !empty($matches['ipv6'])) { $ip = $matches['ipv6']; - if (!$this->isIpv6($ip)) { + if (!IpValidator::isIpV6($ip)) { $message = "Enclosing in square brackets assumes presence of valid IPv6, \"$ip\" given."; throw new RfcProxyParseException($message); @@ -722,7 +720,7 @@ private function getConnectionChainItem( bool $validateIp = true, bool $validateProtocol = true, ): array { - if ($ip !== null && $validateIp && !$this->isIp($ip)) { + if ($ip !== null && $validateIp && !IpValidator::isIp($ip)) { throw new InvalidConnectionChainItemException("\"$ip\" is not a valid IP."); } @@ -847,39 +845,17 @@ private function assertReverseObfuscatedIpData(?array $ipData): void } } - if (!$this->isIp($ipData[0])) { + if (!IpValidator::isIp($ipData[0])) { throw new RuntimeException('IP returned from reverse-obfuscated IP data is not valid.'); } } - /** - * @psalm-assert non-empty-string $value - */ - private function isIp(string $value): bool - { - return $this - ->validator - ->validate($value, [new Ip()]) - ->isValid(); - } - - /** - * @psalm-assert non-empty-string $value - */ - private function isIpv6(string $value): bool - { - return $this - ->validator - ->validate($value, [new Ip(allowIpv4: false)]) - ->isValid(); - } - /** * @psalm-param non-empty-string $value */ private function isPrivateIp(string $value): bool { - return (new Ip(ranges: ['private']))->isAllowed($value); + return IpValidator::inRanges($value, [IpRanges::PRIVATE]); } /** @@ -887,7 +863,7 @@ private function isPrivateIp(string $value): bool */ private function isTrustedIp(string $value): bool { - return !empty($this->trustedIps) && (new Ip(ranges: $this->trustedIps))->isAllowed($value); + return !empty($this->trustedIps) && IpValidator::inRanges($value, $this->trustedIps); } /** diff --git a/tests/TrustedHostsNetworkResolver/ProcessTest.php b/tests/TrustedHostsNetworkResolver/ProcessTest.php index ac2c1b0..22f2205 100644 --- a/tests/TrustedHostsNetworkResolver/ProcessTest.php +++ b/tests/TrustedHostsNetworkResolver/ProcessTest.php @@ -9,7 +9,6 @@ use Yiisoft\Http\Status; use Yiisoft\ProxyMiddleware\Tests\Support\MockRequestHandler; use Yiisoft\ProxyMiddleware\TrustedHostsNetworkResolver; -use Yiisoft\Validator\Validator; final class ProcessTest extends TestCase { @@ -1326,7 +1325,7 @@ public function dataProcess(): iterable ], ], yield 'RFC header, IP related data, hidden IP, obfuscated, reverse-obfuscating' => [ - (new class (new Validator()) extends TrustedHostsNetworkResolver { + (new class () extends TrustedHostsNetworkResolver { protected function reverseObfuscateIpIdentifier( string $ipIdentifier, array $validatedConnectionChainItems, diff --git a/tests/TrustedHostsNetworkResolver/RuntimeExceptionTest.php b/tests/TrustedHostsNetworkResolver/RuntimeExceptionTest.php index 5f7c6e9..8ac15ad 100644 --- a/tests/TrustedHostsNetworkResolver/RuntimeExceptionTest.php +++ b/tests/TrustedHostsNetworkResolver/RuntimeExceptionTest.php @@ -11,7 +11,6 @@ use Yiisoft\ProxyMiddleware\Exception\RfcProxyParseException; use Yiisoft\ProxyMiddleware\Tests\Support\MockRequestHandler; use Yiisoft\ProxyMiddleware\TrustedHostsNetworkResolver; -use Yiisoft\Validator\Validator; final class RuntimeExceptionTest extends TestCase { @@ -509,7 +508,7 @@ public function dataReverseObfuscateIpIdentifierException(): iterable { return [ yield 'empty array' => [ - (new class (new Validator()) extends TrustedHostsNetworkResolver { + (new class () extends TrustedHostsNetworkResolver { protected function reverseObfuscateIpIdentifier( string $ipIdentifier, array $validatedConnectionChainItems, @@ -533,7 +532,7 @@ protected function reverseObfuscateIpIdentifier( 'Reverse-obfuscated IP data can\'t be empty.', ], yield 'wrong items count' => [ - (new class (new Validator()) extends TrustedHostsNetworkResolver { + (new class () extends TrustedHostsNetworkResolver { protected function reverseObfuscateIpIdentifier( string $ipIdentifier, array $validatedConnectionChainItems, @@ -557,7 +556,7 @@ protected function reverseObfuscateIpIdentifier( 'Invalid array keys for reverse-obfuscated IP data. The allowed and required keys are: "0", "1".', ], yield 'IP: not a string' => [ - (new class (new Validator()) extends TrustedHostsNetworkResolver { + (new class () extends TrustedHostsNetworkResolver { protected function reverseObfuscateIpIdentifier( string $ipIdentifier, array $validatedConnectionChainItems, @@ -581,7 +580,7 @@ protected function reverseObfuscateIpIdentifier( 'IP returned from reverse-obfuscated IP data must be non-empty string.', ], yield 'IP: empty string' => [ - (new class (new Validator()) extends TrustedHostsNetworkResolver { + (new class () extends TrustedHostsNetworkResolver { protected function reverseObfuscateIpIdentifier( string $ipIdentifier, array $validatedConnectionChainItems, @@ -605,7 +604,7 @@ protected function reverseObfuscateIpIdentifier( 'IP returned from reverse-obfuscated IP data must be non-empty string.', ], yield 'IP: invalid' => [ - (new class (new Validator()) extends TrustedHostsNetworkResolver { + (new class () extends TrustedHostsNetworkResolver { protected function reverseObfuscateIpIdentifier( string $ipIdentifier, array $validatedConnectionChainItems, @@ -629,7 +628,7 @@ protected function reverseObfuscateIpIdentifier( 'IP returned from reverse-obfuscated IP data is not valid.', ], yield 'port: empty string' => [ - (new class (new Validator()) extends TrustedHostsNetworkResolver { + (new class () extends TrustedHostsNetworkResolver { protected function reverseObfuscateIpIdentifier( string $ipIdentifier, array $validatedConnectionChainItems, @@ -653,7 +652,7 @@ protected function reverseObfuscateIpIdentifier( 'Port returned from reverse-obfuscated IP data must be non-empty string.', ], yield 'IP: valid port instead of IP, port: invalid' => [ - (new class (new Validator()) extends TrustedHostsNetworkResolver { + (new class () extends TrustedHostsNetworkResolver { protected function reverseObfuscateIpIdentifier( string $ipIdentifier, array $validatedConnectionChainItems, diff --git a/tests/TrustedHostsNetworkResolver/TestCase.php b/tests/TrustedHostsNetworkResolver/TestCase.php index e62898f..efa2219 100644 --- a/tests/TrustedHostsNetworkResolver/TestCase.php +++ b/tests/TrustedHostsNetworkResolver/TestCase.php @@ -8,15 +8,12 @@ use PHPUnit\Framework\TestCase as PHPUnitTestCase; use Psr\Http\Message\ServerRequestInterface; use Yiisoft\ProxyMiddleware\TrustedHostsNetworkResolver; -use Yiisoft\Validator\Validator; class TestCase extends PHPUnitTestCase { protected function createMiddleware(): TrustedHostsNetworkResolver { - $validator = new Validator(); - - return new TrustedHostsNetworkResolver($validator); + return new TrustedHostsNetworkResolver(); } protected function createRequest(