Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorblizzz <blizzz@arthur-schiwon.de>2022-08-02 13:36:10 +0300
committerGitHub <noreply@github.com>2022-08-02 13:36:10 +0300
commit039fd75a88967abf3e56a01967c0bbdfe77a1a87 (patch)
tree2d906f8a61c9e46df7a6395313cf45006a01c14f
parent66d4f6744e0ac07daae0aeba36e2edeade4d4dc4 (diff)
parent38eb0df6714933eaee94ea0abc619ce5715eaaa1 (diff)
Merge pull request #33353 from nextcloud/backport/33031/stable24
[stable24] Improve local IP detection
-rw-r--r--lib/composer/composer/autoload_classmap.php1
-rw-r--r--lib/composer/composer/autoload_static.php1
-rw-r--r--lib/private/Http/Client/LocalAddressChecker.php16
-rw-r--r--lib/private/Http/IpUtils.php185
-rw-r--r--tests/lib/Http/Client/LocalAddressCheckerTest.php5
5 files changed, 206 insertions, 2 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php
index b3b31cae4cc..59939b9cb1f 100644
--- a/lib/composer/composer/autoload_classmap.php
+++ b/lib/composer/composer/autoload_classmap.php
@@ -1251,6 +1251,7 @@ return array(
'OC\\Http\\Client\\NegativeDnsCache' => $baseDir . '/lib/private/Http/Client/NegativeDnsCache.php',
'OC\\Http\\Client\\Response' => $baseDir . '/lib/private/Http/Client/Response.php',
'OC\\Http\\CookieHelper' => $baseDir . '/lib/private/Http/CookieHelper.php',
+ 'OC\\Http\\IpUtils' => $baseDir . '/lib/private/Http/IpUtils.php',
'OC\\Http\\WellKnown\\RequestManager' => $baseDir . '/lib/private/Http/WellKnown/RequestManager.php',
'OC\\InitialStateService' => $baseDir . '/lib/private/InitialStateService.php',
'OC\\Installer' => $baseDir . '/lib/private/Installer.php',
diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php
index fd2d072e9a8..57ba09046b3 100644
--- a/lib/composer/composer/autoload_static.php
+++ b/lib/composer/composer/autoload_static.php
@@ -1280,6 +1280,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
'OC\\Http\\Client\\NegativeDnsCache' => __DIR__ . '/../../..' . '/lib/private/Http/Client/NegativeDnsCache.php',
'OC\\Http\\Client\\Response' => __DIR__ . '/../../..' . '/lib/private/Http/Client/Response.php',
'OC\\Http\\CookieHelper' => __DIR__ . '/../../..' . '/lib/private/Http/CookieHelper.php',
+ 'OC\\Http\\IpUtils' => __DIR__ . '/../../..' . '/lib/private/Http/IpUtils.php',
'OC\\Http\\WellKnown\\RequestManager' => __DIR__ . '/../../..' . '/lib/private/Http/WellKnown/RequestManager.php',
'OC\\InitialStateService' => __DIR__ . '/../../..' . '/lib/private/InitialStateService.php',
'OC\\Installer' => __DIR__ . '/../../..' . '/lib/private/Installer.php',
diff --git a/lib/private/Http/Client/LocalAddressChecker.php b/lib/private/Http/Client/LocalAddressChecker.php
index c69d1007a16..da641b57c04 100644
--- a/lib/private/Http/Client/LocalAddressChecker.php
+++ b/lib/private/Http/Client/LocalAddressChecker.php
@@ -27,6 +27,7 @@ namespace OC\Http\Client;
use OCP\Http\Client\LocalServerException;
use Psr\Log\LoggerInterface;
+use OC\Http\IpUtils;
class LocalAddressChecker {
private LoggerInterface $logger;
@@ -36,7 +37,16 @@ class LocalAddressChecker {
}
public function ThrowIfLocalIp(string $ip) : void {
- if ((bool)filter_var($ip, FILTER_VALIDATE_IP) && !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
+ $localRanges = [
+ '100.64.0.0/10', // See RFC 6598
+ '192.0.0.0/24', // See RFC 6890
+ ];
+ if (
+ (bool)filter_var($ip, FILTER_VALIDATE_IP) &&
+ (
+ !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) ||
+ IpUtils::checkIp($ip, $localRanges)
+ )) {
$this->logger->warning("Host $ip was not connected to because it violates local access rules");
throw new LocalServerException('Host violates local access rules');
}
@@ -46,7 +56,9 @@ class LocalAddressChecker {
$delimiter = strrpos($ip, ':'); // Get last colon
$ipv4Address = substr($ip, $delimiter + 1);
- if (!filter_var($ipv4Address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
+ if (
+ !filter_var($ipv4Address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) ||
+ IpUtils::checkIp($ip, $localRanges)) {
$this->logger->warning("Host $ip was not connected to because it violates local access rules");
throw new LocalServerException('Host violates local access rules');
}
diff --git a/lib/private/Http/IpUtils.php b/lib/private/Http/IpUtils.php
new file mode 100644
index 00000000000..97a3cb9dba1
--- /dev/null
+++ b/lib/private/Http/IpUtils.php
@@ -0,0 +1,185 @@
+<?php
+
+/*
+ * This file comes from Symfony. Copied here to avoid backporting a new dependency.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+namespace OC\Http;
+
+/**
+ * Http utility functions.
+ *
+ * @author Fabien Potencier <fabien@symfony.com>
+ */
+class IpUtils {
+ /**
+ * @var array<string,bool>
+ */
+ private static $checkedIps = [];
+
+ /**
+ * This class should not be instantiated.
+ */
+ private function __construct() {
+ }
+
+ /**
+ * Checks if an IPv4 or IPv6 address is contained in the list of given IPs or subnets.
+ *
+ * @param string|array $ips List of IPs or subnets (can be a string if only a single one)
+ *
+ * @return bool
+ */
+ public static function checkIp(string $requestIp, $ips) {
+ if (!\is_array($ips)) {
+ $ips = [$ips];
+ }
+
+ $method = substr_count($requestIp, ':') > 1 ? 'checkIp6' : 'checkIp4';
+
+ foreach ($ips as $ip) {
+ if (self::$method($requestIp, $ip)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Compares two IPv4 addresses.
+ * In case a subnet is given, it checks if it contains the request IP.
+ *
+ * @param string $ip IPv4 address or subnet in CIDR notation
+ *
+ * @return bool Whether the request IP matches the IP, or whether the request IP is within the CIDR subnet
+ */
+ public static function checkIp4(string $requestIp, string $ip) {
+ $cacheKey = $requestIp.'-'.$ip;
+ if (isset(self::$checkedIps[$cacheKey])) {
+ return self::$checkedIps[$cacheKey];
+ }
+
+ if (!filter_var($requestIp, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4)) {
+ return self::$checkedIps[$cacheKey] = false;
+ }
+
+ if (str_contains($ip, '/')) {
+ [$address, $netmask] = explode('/', $ip, 2);
+
+ if ('0' === $netmask) {
+ return self::$checkedIps[$cacheKey] = (bool)filter_var($address, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4);
+ }
+
+ $netmask = (int)$netmask;
+
+ if ($netmask < 0 || $netmask > 32) {
+ return self::$checkedIps[$cacheKey] = false;
+ }
+ } else {
+ $address = $ip;
+ $netmask = 32;
+ }
+
+ if (false === ip2long($address)) {
+ return self::$checkedIps[$cacheKey] = false;
+ }
+
+ return self::$checkedIps[$cacheKey] = 0 === substr_compare(sprintf('%032b', ip2long($requestIp)), sprintf('%032b', ip2long($address)), 0, $netmask);
+ }
+
+ /**
+ * Compares two IPv6 addresses.
+ * In case a subnet is given, it checks if it contains the request IP.
+ *
+ * @author David Soria Parra <dsp at php dot net>
+ *
+ * @see https://github.com/dsp/v6tools
+ *
+ * @param string $ip IPv6 address or subnet in CIDR notation
+ *
+ * @return bool
+ *
+ * @throws \RuntimeException When IPV6 support is not enabled
+ */
+ public static function checkIp6(string $requestIp, string $ip) {
+ $cacheKey = $requestIp.'-'.$ip;
+ if (isset(self::$checkedIps[$cacheKey])) {
+ return self::$checkedIps[$cacheKey];
+ }
+
+ if (!((\extension_loaded('sockets') && \defined('AF_INET6')) || @inet_pton('::1'))) {
+ throw new \RuntimeException('Unable to check Ipv6. Check that PHP was not compiled with option "disable-ipv6".');
+ }
+
+ if (str_contains($ip, '/')) {
+ [$address, $netmask] = explode('/', $ip, 2);
+
+ if ('0' === $netmask) {
+ return (bool) unpack('n*', @inet_pton($address));
+ }
+
+ $netmask = (int)$netmask;
+
+ if ($netmask < 1 || $netmask > 128) {
+ return self::$checkedIps[$cacheKey] = false;
+ }
+ } else {
+ $address = $ip;
+ $netmask = 128;
+ }
+
+ $bytesAddr = unpack('n*', @inet_pton($address));
+ $bytesTest = unpack('n*', @inet_pton($requestIp));
+
+ if (!$bytesAddr || !$bytesTest) {
+ return self::$checkedIps[$cacheKey] = false;
+ }
+
+ for ($i = 1, $ceil = ceil($netmask / 16); $i <= $ceil; ++$i) {
+ $left = $netmask - 16 * ($i - 1);
+ $left = ($left <= 16) ? $left : 16;
+ $mask = ~(0xFFFF >> $left) & 0xFFFF;
+ if (($bytesAddr[$i] & $mask) != ($bytesTest[$i] & $mask)) {
+ return self::$checkedIps[$cacheKey] = false;
+ }
+ }
+
+ return self::$checkedIps[$cacheKey] = true;
+ }
+
+ /**
+ * Anonymizes an IP/IPv6.
+ *
+ * Removes the last byte for v4 and the last 8 bytes for v6 IPs
+ */
+ public static function anonymize(string $ip): string {
+ $wrappedIPv6 = false;
+ if ('[' === substr($ip, 0, 1) && ']' === substr($ip, -1, 1)) {
+ $wrappedIPv6 = true;
+ $ip = substr($ip, 1, -1);
+ }
+
+ $packedAddress = inet_pton($ip);
+ if (4 === \strlen($packedAddress)) {
+ $mask = '255.255.255.0';
+ } elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff:ffff'))) {
+ $mask = '::ffff:ffff:ff00';
+ } elseif ($ip === inet_ntop($packedAddress & inet_pton('::ffff:ffff'))) {
+ $mask = '::ffff:ff00';
+ } else {
+ $mask = 'ffff:ffff:ffff:ffff:0000:0000:0000:0000';
+ }
+ $ip = inet_ntop($packedAddress & inet_pton($mask));
+
+ if ($wrappedIPv6) {
+ $ip = '['.$ip.']';
+ }
+
+ return $ip;
+ }
+}
diff --git a/tests/lib/Http/Client/LocalAddressCheckerTest.php b/tests/lib/Http/Client/LocalAddressCheckerTest.php
index 0bba1cee5f4..9f2f6c72993 100644
--- a/tests/lib/Http/Client/LocalAddressCheckerTest.php
+++ b/tests/lib/Http/Client/LocalAddressCheckerTest.php
@@ -96,6 +96,8 @@ class LocalAddressCheckerTest extends \Test\TestCase {
['10.0.0.1'],
['::'],
['::1'],
+ ['100.100.100.200'],
+ ['192.0.0.1'],
];
}
@@ -116,6 +118,9 @@ class LocalAddressCheckerTest extends \Test\TestCase {
['another-host.local'],
['service.localhost'],
['!@#$'], // test invalid url
+ ['100.100.100.200'],
+ ['192.0.0.1'],
+ ['randomdomain.internal'],
];
}