Skip to content

Commit

Permalink
Simplify IP address normalizer with IP masks
Browse files Browse the repository at this point in the history
Remove dead code

Signed-off-by: Benjamin Gaussorgues <[email protected]>
  • Loading branch information
Altahrim committed Jul 26, 2023
1 parent b76b0bb commit 38b4009
Showing 1 changed file with 17 additions and 63 deletions.
80 changes: 17 additions & 63 deletions lib/private/Security/Normalizer/IpAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,59 +37,30 @@
* @package OC\Security\Normalizer
*/
class IpAddress {
/** @var string */
private $ip;

/**
* @param string $ip IP to normalized
* @param string $ip IP to normalize
*/
public function __construct(string $ip) {
$this->ip = $ip;
}

/**
* Return the given subnet for an IPv4 address and mask bits
*
* @param string $ip
* @param int $maskBits
* @return string
*/
private function getIPv4Subnet(string $ip, int $maskBits = 32): string {
$binary = \inet_pton($ip);
for ($i = 32; $i > $maskBits; $i -= 8) {
$j = \intdiv($i, 8) - 1;
$k = \min(8, $i - $maskBits);
$mask = (0xff - ((2 ** $k) - 1));
$int = \unpack('C', $binary[$j]);
$binary[$j] = \pack('C', $int[1] & $mask);
}
return \inet_ntop($binary).'/'.$maskBits;
public function __construct(
private string $ip,
) {
}

/**
* Return the given subnet for an IPv6 address and mask bits
*
* @param string $ip
* @param int $maskBits
* @return string
* Return the given subnet for an IPv6 address (64 first bits)
*/
private function getIPv6Subnet(string $ip, int $maskBits = 48): string {
private function getIPv6Subnet(string $ip): string {
if ($ip[0] === '[' && $ip[-1] === ']') { // If IP is with brackets, for example [::1]
$ip = substr($ip, 1, strlen($ip) - 2);
}
$pos = strpos($ip, '%'); // if there is an explicit interface added to the IP, e.g. fe80::ae2d:d1e7:fe1e:9a8d%enp2s0
if ($pos !== false) {
$ip = substr($ip, 0, $pos - 1);
}

$binary = \inet_pton($ip);
for ($i = 128; $i > $maskBits; $i -= 8) {
$j = \intdiv($i, 8) - 1;
$k = \min(8, $i - $maskBits);
$mask = (0xff - ((2 ** $k) - 1));
$int = \unpack('C', $binary[$j]);
$binary[$j] = \pack('C', $int[1] & $mask);
}
return \inet_ntop($binary).'/'.$maskBits;
$mask = inet_pton('FFFF:FFFF:FFFF:FFFF::');

return inet_ntop($binary & $mask).'/64';
}

/**
Expand All @@ -103,24 +74,13 @@ private function getEmbeddedIpv4(string $ipv6): ?string {
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];
$mask = inet_pton('::FFFF:FFFF');
if (($binary & ~$mask) !== inet_pton('::FFFF:0.0.0.0')) {
return null;
}

return inet_ntop($binary4);
return inet_ntop(substr($binary, -4));
}


Expand All @@ -130,19 +90,13 @@ private function getEmbeddedIpv4(string $ipv6): ?string {
* @return string
*/
public function getSubnet(): string {
if (\preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $this->ip)) {
return $this->getIPv4Subnet(
$this->ip,
32
);
if (filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
return $this->ip.'/32';
}

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

return $this->getIPv6Subnet(

Check failure on line 102 in lib/private/Security/Normalizer/IpAddress.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

TooManyArguments

lib/private/Security/Normalizer/IpAddress.php:102:17: TooManyArguments: Too many arguments for method OC\Security\Normalizer\IpAddress::getipv6subnet - saw 2 (see https://psalm.dev/026)
Expand Down

0 comments on commit 38b4009

Please sign in to comment.