Skip to content

Commit

Permalink
Merge pull request #29852 from nextcloud/backport/29835/stable22
Browse files Browse the repository at this point in the history
[stable22] Fix getting subnet of ipv4 mapped ipv6 addresses
  • Loading branch information
PVince81 authored Nov 23, 2021
2 parents f5db8a1 + 74eeca3 commit fd883bd
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
41 changes: 41 additions & 0 deletions lib/private/Security/Normalizer/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,38 @@ private function getIPv6Subnet(string $ip, int $maskBits = 48): string {
return \inet_ntop($binary).'/'.$maskBits;
}

/**
* Returns the IPv4 address embedded in an IPv6 if applicable.
* The detected format is "::ffff:x.x.x.x" using the binary form.
*
* @return string|null embedded IPv4 string or null if none was found
*/
private function getEmbeddedIpv4(string $ipv6): ?string {
$binary = inet_pton($ipv6);
if (!$binary) {
return null;
}
for ($i = 0; $i <= 9; $i++) {
if (unpack('C', $binary[$i])[1] !== 0) {
return null;
}
}

for ($i = 10; $i <= 11; $i++) {
if (unpack('C', $binary[$i])[1] !== 255) {
return null;
}
}

$binary4 = '';
for ($i = 12; $i < 16; $i++) {
$binary4 .= $binary[$i];
}

return inet_ntop($binary4);
}


/**
* Gets either the /32 (IPv4) or the /64 (IPv6) subnet of an IP address
*
Expand All @@ -104,6 +136,15 @@ public function getSubnet(): string {
32
);
}

$ipv4 = $this->getEmbeddedIpv4($this->ip);
if ($ipv4 !== null) {
return $this->getIPv4Subnet(
$ipv4,
32
);
}

return $this->getIPv6Subnet(
$this->ip,
64
Expand Down
12 changes: 12 additions & 0 deletions tests/lib/Security/Normalizer/IpAddressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ public function subnetDataProvider() {
'192.168.0.123',
'192.168.0.123/32',
],
[
'::ffff:192.168.0.123',
'192.168.0.123/32',
],
[
'0:0:0:0:0:ffff:192.168.0.123',
'192.168.0.123/32',
],
[
'0:0:0:0:0:ffff:c0a8:7b',
'192.168.0.123/32',
],
[
'2001:0db8:85a3:0000:0000:8a2e:0370:7334',
'2001:db8:85a3::/64',
Expand Down

0 comments on commit fd883bd

Please sign in to comment.