IpAddress.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Security\Normalizer;
  8. /**
  9. * Class IpAddress is used for normalizing IPv4 and IPv6 addresses in security
  10. * relevant contexts in Nextcloud.
  11. *
  12. * @package OC\Security\Normalizer
  13. */
  14. class IpAddress {
  15. /**
  16. * @param string $ip IP to normalize
  17. */
  18. public function __construct(
  19. private string $ip,
  20. ) {
  21. }
  22. /**
  23. * Return the given subnet for an IPv6 address (64 first bits)
  24. */
  25. private function getIPv6Subnet(string $ip): string {
  26. if ($ip[0] === '[' && $ip[-1] === ']') { // If IP is with brackets, for example [::1]
  27. $ip = substr($ip, 1, strlen($ip) - 2);
  28. }
  29. $pos = strpos($ip, '%'); // if there is an explicit interface added to the IP, e.g. fe80::ae2d:d1e7:fe1e:9a8d%enp2s0
  30. if ($pos !== false) {
  31. $ip = substr($ip, 0, $pos - 1);
  32. }
  33. $binary = \inet_pton($ip);
  34. $mask = inet_pton('FFFF:FFFF:FFFF:FFFF::');
  35. return inet_ntop($binary & $mask).'/64';
  36. }
  37. /**
  38. * Returns the IPv4 address embedded in an IPv6 if applicable.
  39. * The detected format is "::ffff:x.x.x.x" using the binary form.
  40. *
  41. * @return string|null embedded IPv4 string or null if none was found
  42. */
  43. private function getEmbeddedIpv4(string $ipv6): ?string {
  44. $binary = inet_pton($ipv6);
  45. if (!$binary) {
  46. return null;
  47. }
  48. $mask = inet_pton('::FFFF:FFFF');
  49. if (($binary & ~$mask) !== inet_pton('::FFFF:0.0.0.0')) {
  50. return null;
  51. }
  52. return inet_ntop(substr($binary, -4));
  53. }
  54. /**
  55. * Gets either the /32 (IPv4) or the /64 (IPv6) subnet of an IP address
  56. */
  57. public function getSubnet(): string {
  58. if (filter_var($this->ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  59. return $this->ip.'/32';
  60. }
  61. $ipv4 = $this->getEmbeddedIpv4($this->ip);
  62. if ($ipv4 !== null) {
  63. return $ipv4.'/32';
  64. }
  65. return $this->getIPv6Subnet($this->ip);
  66. }
  67. /**
  68. * Returns the specified IP address
  69. */
  70. public function __toString(): string {
  71. return $this->ip;
  72. }
  73. }