Skip to content

Commit 32e2e6a

Browse files
authored
Merge pull request #60572 from nextcloud/backport/60546/stable26
[stable26] fix: handle NAT64 addresses in isLocalAddress
2 parents e0bf742 + 361b1cf commit 32e2e6a

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

lib/private/Net/IpAddressClassifier.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
namespace OC\Net;
2727

28+
use IPLib\Address\IPv4;
2829
use IPLib\Address\IPv6;
2930
use IPLib\Factory;
3031
use IPLib\ParseStringFlag;
@@ -62,7 +63,15 @@ public function isLocalAddress(string $ip): bool {
6263
}
6364
/* Replace by normalized form */
6465
if ($parsedIp instanceof IPv6) {
65-
$ip = (string)($parsedIp->toIPv4() ?? $parsedIp);
66+
$ipv4 = $parsedIp->toIPv4();
67+
$ipv6Bytes = $parsedIp->getBytes();
68+
if ($ipv4) {
69+
$ip = (string)$ipv4;
70+
} elseif (array_slice($ipv6Bytes, 0, 4) === [0x00, 0x64, 0xFF, 0x9B]) {
71+
$ip = (string)IPv4::fromBytes(array_slice($ipv6Bytes, -4, 4));
72+
} else {
73+
$ip = (string)$parsedIp;
74+
}
6675
} else {
6776
$ip = (string)$parsedIp;
6877
}

tests/lib/Net/IpAddressClassifierTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function localIpAddressData(): array {
6666
['::1'],
6767
['100.100.100.200'],
6868
['192.0.0.1'],
69+
['64:ff9b::a9fe:a9fe'], // NAT64 of 169.254.169.254
6970
];
7071
}
7172

0 commit comments

Comments
 (0)