aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Http/Client/DnsPinMiddleware.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Http/Client/DnsPinMiddleware.php')
-rw-r--r--lib/private/Http/Client/DnsPinMiddleware.php67
1 files changed, 29 insertions, 38 deletions
diff --git a/lib/private/Http/Client/DnsPinMiddleware.php b/lib/private/Http/Client/DnsPinMiddleware.php
index c6a58972fdd..96e0f71adbe 100644
--- a/lib/private/Http/Client/DnsPinMiddleware.php
+++ b/lib/private/Http/Client/DnsPinMiddleware.php
@@ -3,25 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2021, Lukas Reschke <lukas@statuscode.ch>
- *
- * @author Lukas Reschke <lukas@statuscode.ch>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Http\Client;
@@ -30,23 +13,15 @@ use OCP\Http\Client\LocalServerException;
use Psr\Http\Message\RequestInterface;
class DnsPinMiddleware {
- /** @var NegativeDnsCache */
- private $negativeDnsCache;
- private IpAddressClassifier $ipAddressClassifier;
public function __construct(
- NegativeDnsCache $negativeDnsCache,
- IpAddressClassifier $ipAddressClassifier
+ private NegativeDnsCache $negativeDnsCache,
+ private IpAddressClassifier $ipAddressClassifier,
) {
- $this->negativeDnsCache = $negativeDnsCache;
- $this->ipAddressClassifier = $ipAddressClassifier;
}
/**
* Fetch soa record for a target
- *
- * @param string $target
- * @return array|null
*/
private function soaRecord(string $target): ?array {
$labels = explode('.', $target);
@@ -55,7 +30,7 @@ class DnsPinMiddleware {
$second = array_pop($labels);
$hostname = $second . '.' . $top;
- $responses = dns_get_record($hostname, DNS_SOA);
+ $responses = $this->dnsGetRecord($hostname, DNS_SOA);
if ($responses === false || count($responses) === 0) {
return null;
@@ -74,15 +49,21 @@ class DnsPinMiddleware {
$soaDnsEntry = $this->soaRecord($target);
$dnsNegativeTtl = $soaDnsEntry['minimum-ttl'] ?? null;
+ $canHaveCnameRecord = true;
- $dnsTypes = [DNS_A, DNS_AAAA, DNS_CNAME];
+ $dnsTypes = \defined('AF_INET6') || @inet_pton('::1')
+ ? [DNS_A, DNS_AAAA, DNS_CNAME]
+ : [DNS_A, DNS_CNAME];
foreach ($dnsTypes as $dnsType) {
+ if ($canHaveCnameRecord === false && $dnsType === DNS_CNAME) {
+ continue;
+ }
+
if ($this->negativeDnsCache->isNegativeCached($target, $dnsType)) {
continue;
}
- $dnsResponses = dns_get_record($target, $dnsType);
- $canHaveCnameRecord = true;
+ $dnsResponses = $this->dnsGetRecord($target, $dnsType);
if ($dnsResponses !== false && count($dnsResponses) > 0) {
foreach ($dnsResponses as $dnsResponse) {
if (isset($dnsResponse['ip'])) {
@@ -93,7 +74,6 @@ class DnsPinMiddleware {
$canHaveCnameRecord = false;
} elseif (isset($dnsResponse['target']) && $canHaveCnameRecord) {
$targetIps = array_merge($targetIps, $this->dnsResolve($dnsResponse['target'], $recursionCount));
- $canHaveCnameRecord = true;
}
}
} elseif ($dnsNegativeTtl !== null) {
@@ -104,17 +84,24 @@ class DnsPinMiddleware {
return $targetIps;
}
- public function addDnsPinning() {
+ /**
+ * Wrapper for dns_get_record
+ */
+ protected function dnsGetRecord(string $hostname, int $type): array|false {
+ return \dns_get_record($hostname, $type);
+ }
+
+ public function addDnsPinning(): callable {
return function (callable $handler) {
return function (
RequestInterface $request,
- array $options
+ array $options,
) use ($handler) {
if ($options['nextcloud']['allow_local_address'] === true) {
return $handler($request, $options);
}
- $hostName = (string)$request->getUri()->getHost();
+ $hostName = $request->getUri()->getHost();
$port = $request->getUri()->getPort();
$ports = [
@@ -128,6 +115,10 @@ class DnsPinMiddleware {
$targetIps = $this->dnsResolve(idn_to_utf8($hostName), 0);
+ if (empty($targetIps)) {
+ throw new LocalServerException('No DNS record found for ' . $hostName);
+ }
+
$curlResolves = [];
foreach ($ports as $port) {
@@ -136,7 +127,7 @@ class DnsPinMiddleware {
foreach ($targetIps as $ip) {
if ($this->ipAddressClassifier->isLocalAddress($ip)) {
// TODO: continue with all non-local IPs?
- throw new LocalServerException('Host violates local access rules');
+ throw new LocalServerException('Host "' . $ip . '" (' . $hostName . ':' . $port . ') violates local access rules');
}
$curlResolves["$hostName:$port"][] = $ip;
}