Skip to content

Commit c8d9581

Browse files
authored
Merge pull request #60569 from nextcloud/backport/60546/stable29
[stable29] fix: handle NAT64 addresses in isLocalAddress
2 parents bfc5f4b + 74e6433 commit c8d9581

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;
@@ -58,7 +59,15 @@ public function isLocalAddress(string $ip): bool {
5859
}
5960
/* Replace by normalized form */
6061
if ($parsedIp instanceof IPv6) {
61-
$ip = (string)($parsedIp->toIPv4() ?? $parsedIp);
62+
$ipv4 = $parsedIp->toIPv4();
63+
$ipv6Bytes = $parsedIp->getBytes();
64+
if ($ipv4) {
65+
$ip = (string)$ipv4;
66+
} elseif (array_slice($ipv6Bytes, 0, 4) === [0x00, 0x64, 0xFF, 0x9B]) {
67+
$ip = (string)IPv4::fromBytes(array_slice($ipv6Bytes, -4, 4));
68+
} else {
69+
$ip = (string)$parsedIp;
70+
}
6271
} else {
6372
$ip = (string)$parsedIp;
6473
}

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)