mirror of
https://github.com/nextcloud/server.git
synced 2026-02-20 00:12:30 -05:00
Libcurl expects the value of the CURLOPT_RESOLVE configurations to be an array of strings, those strings containing a comma delimited list of resolved IPs for each host:port combination. The original code here does create that array with the host:port:ip combination, but multiple ips for a single host:port result in additional array entries, rather than adding them to the end of the string with a comma. Per the libcurl docs, the `CURLOPT_RESOLVE` array entries should match the syntax `host:port:address[,address]`. This creates a function-scoped associative array which uses `host:port` as the key (which are supposed to be unique and this ensures that), and the value is an array containing IP strings (ipv4 or ipv6). Once the associative array is populated, it is then set to the CURLOPT_RESOLVE array, imploding the ip arrays using a comma delimiter so the array syntax matches the expected by libcurl. Note that this reorders the "foreach ip" and "foreach port" loops. Rather than looping over ips then ports, we now loop over ports then ips, since ports are part of the unique host:port map, and multiple ips can exist therein. Signed-off-by: Aaron Ball <nullspoon@oper.io>
135 lines
3.7 KiB
PHP
135 lines
3.7 KiB
PHP
<?php
|
|
|
|
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/>.
|
|
*
|
|
*/
|
|
namespace OC\Http\Client;
|
|
|
|
use Psr\Http\Message\RequestInterface;
|
|
|
|
class DnsPinMiddleware {
|
|
/** @var NegativeDnsCache */
|
|
private $negativeDnsCache;
|
|
/** @var LocalAddressChecker */
|
|
private $localAddressChecker;
|
|
|
|
public function __construct(
|
|
NegativeDnsCache $negativeDnsCache,
|
|
LocalAddressChecker $localAddressChecker
|
|
) {
|
|
$this->negativeDnsCache = $negativeDnsCache;
|
|
$this->localAddressChecker = $localAddressChecker;
|
|
}
|
|
|
|
private function dnsResolve(string $target, int $recursionCount) : array {
|
|
if ($recursionCount >= 10) {
|
|
return [];
|
|
}
|
|
|
|
$recursionCount = $recursionCount++;
|
|
$targetIps = [];
|
|
|
|
$soaDnsEntry = dns_get_record($target, DNS_SOA);
|
|
if (isset($soaDnsEntry[0]) && isset($soaDnsEntry[0]['minimum-ttl'])) {
|
|
$dnsNegativeTtl = $soaDnsEntry[0]['minimum-ttl'];
|
|
} else {
|
|
$dnsNegativeTtl = null;
|
|
}
|
|
|
|
$dnsTypes = [DNS_A, DNS_AAAA, DNS_CNAME];
|
|
foreach ($dnsTypes as $key => $dnsType) {
|
|
if ($this->negativeDnsCache->isNegativeCached($target, $dnsType)) {
|
|
unset($dnsTypes[$key]);
|
|
continue;
|
|
}
|
|
|
|
$dnsResponses = dns_get_record($target, $dnsType);
|
|
$canHaveCnameRecord = true;
|
|
if (count($dnsResponses) > 0) {
|
|
foreach ($dnsResponses as $key => $dnsResponse) {
|
|
if (isset($dnsResponse['ip'])) {
|
|
$targetIps[] = $dnsResponse['ip'];
|
|
$canHaveCnameRecord = false;
|
|
} elseif (isset($dnsResponse['ipv6'])) {
|
|
$targetIps[] = $dnsResponse['ipv6'];
|
|
$canHaveCnameRecord = false;
|
|
} elseif (isset($dnsResponse['target']) && $canHaveCnameRecord) {
|
|
$targetIps = array_merge($targetIps, $this->dnsResolve($dnsResponse['target'], $recursionCount));
|
|
$canHaveCnameRecord = true;
|
|
}
|
|
}
|
|
} else {
|
|
if ($dnsNegativeTtl !== null) {
|
|
$this->negativeDnsCache->setNegativeCacheForDnsType($target, $dnsType, $dnsNegativeTtl);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $targetIps;
|
|
}
|
|
|
|
public function addDnsPinning() {
|
|
return function (callable $handler) {
|
|
return function (
|
|
RequestInterface $request,
|
|
array $options
|
|
) use ($handler) {
|
|
if ($options['nextcloud']['allow_local_address'] === true) {
|
|
return $handler($request, $options);
|
|
}
|
|
|
|
$hostName = (string)$request->getUri()->getHost();
|
|
$port = $request->getUri()->getPort();
|
|
|
|
$ports = [
|
|
'80',
|
|
'443',
|
|
];
|
|
|
|
if ($port !== null) {
|
|
$ports[] = (string)$port;
|
|
}
|
|
|
|
$targetIps = $this->dnsResolve($hostName, 0);
|
|
|
|
$curlResolves = [];
|
|
|
|
foreach ($ports as $port) {
|
|
$curlResolves["$hostName:$port"] = [];
|
|
|
|
foreach ($targetIps as $ip) {
|
|
$this->localAddressChecker->ThrowIfLocalIp($ip);
|
|
$curlResolves["$hostName:$port"][] = $ip;
|
|
}
|
|
}
|
|
|
|
// Coalesce the per-host:port ips back into a comma separated list
|
|
foreach ($curlResolves as $hostport => $ips) {
|
|
$options['curl'][CURLOPT_RESOLVE][] = "$hostport:" . implode(',', $ips);
|
|
}
|
|
|
|
return $handler($request, $options);
|
|
};
|
|
};
|
|
}
|
|
}
|