diff --git a/lib/private/Net/IpAddressClassifier.php b/lib/private/Net/IpAddressClassifier.php index b73d41fd79b..9d701b21e41 100644 --- a/lib/private/Net/IpAddressClassifier.php +++ b/lib/private/Net/IpAddressClassifier.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace OC\Net; +use IPLib\Address\IPv4; use IPLib\Address\IPv6; use IPLib\Factory; use IPLib\ParseStringFlag; @@ -42,7 +43,15 @@ class IpAddressClassifier { } /* Replace by normalized form */ if ($parsedIp instanceof IPv6) { - $ip = (string)($parsedIp->toIPv4() ?? $parsedIp); + $ipv4 = $parsedIp->toIPv4(); + $ipv6Bytes = $parsedIp->getBytes(); + if ($ipv4) { + $ip = (string)$ipv4; + } elseif (array_slice($ipv6Bytes, 0, 4) === [0x00, 0x64, 0xFF, 0x9B]) { + $ip = (string)IPv4::fromBytes(array_slice($ipv6Bytes, -4, 4)); + } else { + $ip = (string)$parsedIp; + } } else { $ip = (string)$parsedIp; } diff --git a/tests/lib/Net/IpAddressClassifierTest.php b/tests/lib/Net/IpAddressClassifierTest.php index 755b9171c79..d4d8bf5cd22 100644 --- a/tests/lib/Net/IpAddressClassifierTest.php +++ b/tests/lib/Net/IpAddressClassifierTest.php @@ -52,6 +52,7 @@ class IpAddressClassifierTest extends TestCase { ['::1'], ['100.100.100.200'], ['192.0.0.1'], + ['64:ff9b::a9fe:a9fe'], // NAT64 of 169.254.169.254 ]; }