From 3059cb4a303dc623d133dcfbd6bf61dfca440357 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 15 Apr 2026 18:59:18 +0200 Subject: [PATCH] fix: handle NAT64 addresses in isLocalAddress Signed-off-by: Robin Appelman --- lib/private/Net/IpAddressClassifier.php | 11 ++++++++++- tests/lib/Net/IpAddressClassifierTest.php | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/private/Net/IpAddressClassifier.php b/lib/private/Net/IpAddressClassifier.php index b73d41fd79b7e..9d701b21e4199 100644 --- a/lib/private/Net/IpAddressClassifier.php +++ b/lib/private/Net/IpAddressClassifier.php @@ -9,6 +9,7 @@ namespace OC\Net; +use IPLib\Address\IPv4; use IPLib\Address\IPv6; use IPLib\Factory; use IPLib\ParseStringFlag; @@ -42,7 +43,15 @@ public function isLocalAddress(string $ip): bool { } /* 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 6014eb9dffe65..47aa424ec9ef8 100644 --- a/tests/lib/Net/IpAddressClassifierTest.php +++ b/tests/lib/Net/IpAddressClassifierTest.php @@ -51,6 +51,7 @@ public static function localIpAddressData(): array { ['::1'], ['100.100.100.200'], ['192.0.0.1'], + ['64:ff9b::a9fe:a9fe'], // NAT64 of 169.254.169.254 ]; }