From a907b74c2ac40a6d12976056cbc21eab09312936 Mon Sep 17 00:00:00 2001 From: Côme Chilliet Date: Tue, 20 Sep 2022 12:20:35 +0200 Subject: Add missing urldecode and idn_to_utf8 calls to local address checker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The call to idn_to_utf8 call is actually to apply normalization Signed-off-by: Côme Chilliet --- lib/private/Http/Client/DnsPinMiddleware.php | 2 +- lib/private/Http/Client/LocalAddressChecker.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'lib') diff --git a/lib/private/Http/Client/DnsPinMiddleware.php b/lib/private/Http/Client/DnsPinMiddleware.php index ee0ea053dcf..f5e6214a4ab 100644 --- a/lib/private/Http/Client/DnsPinMiddleware.php +++ b/lib/private/Http/Client/DnsPinMiddleware.php @@ -125,7 +125,7 @@ class DnsPinMiddleware { $ports[] = (string)$port; } - $targetIps = $this->dnsResolve($hostName, 0); + $targetIps = $this->dnsResolve(idn_to_utf8($hostName), 0); $curlResolves = []; diff --git a/lib/private/Http/Client/LocalAddressChecker.php b/lib/private/Http/Client/LocalAddressChecker.php index f4fea503ab9..b0de54b430f 100644 --- a/lib/private/Http/Client/LocalAddressChecker.php +++ b/lib/private/Http/Client/LocalAddressChecker.php @@ -72,7 +72,7 @@ class LocalAddressChecker { throw new LocalServerException('Could not detect any host'); } - $host = strtolower($host); + $host = idn_to_utf8(strtolower(urldecode($host))); // Remove brackets from IPv6 addresses if (strpos($host, '[') === 0 && substr($host, -1) === ']') { $host = substr($host, 1, -1); -- cgit v1.2.3 From 7ac688a2e5c74c890417f25683afc5c0a9a1e000 Mon Sep 17 00:00:00 2001 From: Côme Chilliet Date: Tue, 20 Sep 2022 12:34:04 +0200 Subject: Use new dependency to normalize IPs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/Http/Client/LocalAddressChecker.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'lib') diff --git a/lib/private/Http/Client/LocalAddressChecker.php b/lib/private/Http/Client/LocalAddressChecker.php index b0de54b430f..274186d0a5a 100644 --- a/lib/private/Http/Client/LocalAddressChecker.php +++ b/lib/private/Http/Client/LocalAddressChecker.php @@ -25,6 +25,8 @@ declare(strict_types=1); */ namespace OC\Http\Client; +use IPLib\Factory; +use IPLib\ParseStringFlag; use OCP\Http\Client\LocalServerException; use Psr\Log\LoggerInterface; use Symfony\Component\HttpFoundation\IpUtils; @@ -37,6 +39,17 @@ class LocalAddressChecker { } public function ThrowIfLocalIp(string $ip) : void { + $parsedIp = Factory::parseAddressString( + $ip, + ParseStringFlag::IPV4_MAYBE_NON_DECIMAL | ParseStringFlag::IPV4ADDRESS_MAYBE_NON_QUAD_DOTTED + ); + if ($parsedIp === null) { + /* Not an IP */ + return; + } + /* Replace by normalized form */ + $ip = (string)$parsedIp; + $localRanges = [ '100.64.0.0/10', // See RFC 6598 '192.0.0.0/24', // See RFC 6890 -- cgit v1.2.3 From 31117fa7c70de9beb8aa39615e780214b251f519 Mon Sep 17 00:00:00 2001 From: Côme Chilliet Date: Tue, 20 Sep 2022 12:46:22 +0200 Subject: Fix tests for nested v4 in v6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/Http/Client/LocalAddressChecker.php | 20 ++++++-------------- tests/lib/Http/Client/LocalAddressCheckerTest.php | 4 ++-- 2 files changed, 8 insertions(+), 16 deletions(-) (limited to 'lib') diff --git a/lib/private/Http/Client/LocalAddressChecker.php b/lib/private/Http/Client/LocalAddressChecker.php index 274186d0a5a..13a7d062de3 100644 --- a/lib/private/Http/Client/LocalAddressChecker.php +++ b/lib/private/Http/Client/LocalAddressChecker.php @@ -25,6 +25,7 @@ declare(strict_types=1); */ namespace OC\Http\Client; +use IPLib\Address\IPv6; use IPLib\Factory; use IPLib\ParseStringFlag; use OCP\Http\Client\LocalServerException; @@ -48,7 +49,11 @@ class LocalAddressChecker { return; } /* Replace by normalized form */ - $ip = (string)$parsedIp; + if ($parsedIp instanceof IPv6) { + $ip = (string)($parsedIp->toIPv4() ?? $parsedIp); + } else { + $ip = (string)$parsedIp; + } $localRanges = [ '100.64.0.0/10', // See RFC 6598 @@ -63,19 +68,6 @@ class LocalAddressChecker { $this->logger->warning("Host $ip was not connected to because it violates local access rules"); throw new LocalServerException('Host violates local access rules'); } - - // Also check for IPv6 IPv4 nesting, because that's not covered by filter_var - if ((bool)filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && substr_count($ip, '.') > 0) { - $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) || - 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'); - } - } } public function ThrowIfLocalAddress(string $uri) : void { diff --git a/tests/lib/Http/Client/LocalAddressCheckerTest.php b/tests/lib/Http/Client/LocalAddressCheckerTest.php index 991801f043d..8c8e64eddf9 100644 --- a/tests/lib/Http/Client/LocalAddressCheckerTest.php +++ b/tests/lib/Http/Client/LocalAddressCheckerTest.php @@ -91,7 +91,7 @@ class LocalAddressCheckerTest extends \Test\TestCase { return [ ['192.168.0.1'], ['fe80::200:5aee:feaa:20a2'], - ['0:0:0:0:0:0:10.0.0.1'], + ['0:0:0:0:0:ffff:10.0.0.1'], ['0:0:0:0:0:ffff:127.0.0.0'], ['10.0.0.1'], ['::'], @@ -112,7 +112,7 @@ class LocalAddressCheckerTest extends \Test\TestCase { ['172.16.42.1'], ['[fdf8:f53b:82e4::53]/secret.ics'], ['[fe80::200:5aee:feaa:20a2]/secret.ics'], - ['[0:0:0:0:0:0:10.0.0.1]/secret.ics'], + ['[0:0:0:0:0:ffff:10.0.0.1]/secret.ics'], ['[0:0:0:0:0:ffff:127.0.0.0]/secret.ics'], ['10.0.0.1'], ['another-host.local'], -- cgit v1.2.3