From cf9b13242542e11511830adc8dc6decf10b739ab Mon Sep 17 00:00:00 2001 From: Daniel Burger <48986191+danielburger1337@users.noreply.github.com> Date: Tue, 21 Mar 2023 17:30:23 +0100 Subject: [PATCH] [HttpFoundation] Use separate caches for IpUtils checkIp4 and checkIp6 --- src/Symfony/Component/HttpFoundation/IpUtils.php | 4 ++-- .../HttpFoundation/Tests/IpUtilsTest.php | 15 +++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/HttpFoundation/IpUtils.php b/src/Symfony/Component/HttpFoundation/IpUtils.php index 2f31284e36c69..49d9a9d74c251 100644 --- a/src/Symfony/Component/HttpFoundation/IpUtils.php +++ b/src/Symfony/Component/HttpFoundation/IpUtils.php @@ -73,7 +73,7 @@ public static function checkIp4(?string $requestIp, string $ip) return false; } - $cacheKey = $requestIp.'-'.$ip; + $cacheKey = $requestIp.'-'.$ip.'-v4'; if (isset(self::$checkedIps[$cacheKey])) { return self::$checkedIps[$cacheKey]; } @@ -126,7 +126,7 @@ public static function checkIp6(?string $requestIp, string $ip) return false; } - $cacheKey = $requestIp.'-'.$ip; + $cacheKey = $requestIp.'-'.$ip.'-v6'; if (isset(self::$checkedIps[$cacheKey])) { return self::$checkedIps[$cacheKey]; } diff --git a/src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php b/src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php index 085790cf606a8..f5ac4053b62a6 100644 --- a/src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php +++ b/src/Symfony/Component/HttpFoundation/Tests/IpUtilsTest.php @@ -19,6 +19,21 @@ class IpUtilsTest extends TestCase { use ExpectDeprecationTrait; + public function testSeparateCachesPerProtocol() + { + $ip = '192.168.52.1'; + $subnet = '192.168.0.0/16'; + + $this->assertFalse(IpUtils::checkIp6($ip, $subnet)); + $this->assertTrue(IpUtils::checkIp4($ip, $subnet)); + + $ip = '2a01:198:603:0:396e:4789:8e99:890f'; + $subnet = '2a01:198:603:0::/65'; + + $this->assertFalse(IpUtils::checkIp4($ip, $subnet)); + $this->assertTrue(IpUtils::checkIp6($ip, $subnet)); + } + /** * @dataProvider getIpv4Data */