Skip to content

Commit 8ce133e

Browse files
authored
Merge pull request #60565 from nextcloud/backport/60546/stable33
[stable33] fix: handle NAT64 addresses in isLocalAddress
2 parents 3747ded + 3059cb4 commit 8ce133e

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
@@ -9,6 +9,7 @@
99

1010
namespace OC\Net;
1111

12+
use IPLib\Address\IPv4;
1213
use IPLib\Address\IPv6;
1314
use IPLib\Factory;
1415
use IPLib\ParseStringFlag;
@@ -42,7 +43,15 @@ public function isLocalAddress(string $ip): bool {
4243
}
4344
/* Replace by normalized form */
4445
if ($parsedIp instanceof IPv6) {
45-
$ip = (string)($parsedIp->toIPv4() ?? $parsedIp);
46+
$ipv4 = $parsedIp->toIPv4();
47+
$ipv6Bytes = $parsedIp->getBytes();
48+
if ($ipv4) {
49+
$ip = (string)$ipv4;
50+
} elseif (array_slice($ipv6Bytes, 0, 4) === [0x00, 0x64, 0xFF, 0x9B]) {
51+
$ip = (string)IPv4::fromBytes(array_slice($ipv6Bytes, -4, 4));
52+
} else {
53+
$ip = (string)$parsedIp;
54+
}
4655
} else {
4756
$ip = (string)$parsedIp;
4857
}

tests/lib/Net/IpAddressClassifierTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public static function localIpAddressData(): array {
5151
['::1'],
5252
['100.100.100.200'],
5353
['192.0.0.1'],
54+
['64:ff9b::a9fe:a9fe'], // NAT64 of 169.254.169.254
5455
];
5556
}
5657

0 commit comments

Comments
 (0)