Skip to content

Commit 77321bc

Browse files
authored
Merge pull request #47944 from nextcloud/backport/47883/stable30
[stable30] fix(setup-checks): Ensure URL with webroot works
2 parents dd4caaf + 71d870d commit 77321bc

8 files changed

Lines changed: 317 additions & 56 deletions

File tree

apps/settings/lib/SetupChecks/CheckServerResponseTrait.php

Lines changed: 47 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,85 +37,93 @@ protected function serverConfigHelp(): string {
3737
* Get all possible URLs that need to be checked for a local request test.
3838
* This takes all `trusted_domains` and the CLI overwrite URL into account.
3939
*
40-
* @param string $url The relative URL to test starting with a /
41-
* @return string[] List of possible absolute URLs
40+
* @param string $url The absolute path (absolute URL without host but with web-root) to test starting with a /
41+
* @param bool $isRootRequest Set to remove the web-root from URL and host (e.g. when requesting a path in the domain root like '/.well-known')
42+
* @return list<string> List of possible absolute URLs
4243
*/
43-
protected function getTestUrls(string $url, bool $removeWebroot): array {
44-
$testUrls = [];
44+
protected function getTestUrls(string $url, bool $isRootRequest = false): array {
45+
$url = '/' . ltrim($url, '/');
4546

4647
$webroot = rtrim($this->urlGenerator->getWebroot(), '/');
48+
if ($isRootRequest === false && $webroot !== '' && str_starts_with($url, $webroot)) {
49+
// The URL contains the web-root but also the base url does so,
50+
// so we need to remove the web-root from the URL.
51+
$url = substr($url, strlen($webroot));
52+
}
4753

48-
/* Try overwrite.cli.url first, it’s supposed to be how the server contacts itself */
49-
$cliUrl = $this->config->getSystemValueString('overwrite.cli.url', '');
54+
// Base URLs to test
55+
$baseUrls = [];
5056

57+
// Try overwrite.cli.url first, it’s supposed to be how the server contacts itself
58+
$cliUrl = $this->config->getSystemValueString('overwrite.cli.url', '');
5159
if ($cliUrl !== '') {
52-
$cliUrl = $this->normalizeUrl(
60+
// The CLI URL already contains the web-root, so we need to normalize it if requested
61+
$baseUrls[] = $this->normalizeUrl(
5362
$cliUrl,
54-
$webroot,
55-
$removeWebroot
63+
$isRootRequest
5664
);
57-
58-
$testUrls[] = $cliUrl . $url;
5965
}
6066

61-
/* Try URL generator second */
62-
$baseUrl = $this->normalizeUrl(
67+
// Try URL generator second
68+
// The base URL also contains the webroot so also normalize it
69+
$baseUrls[] = $this->normalizeUrl(
6370
$this->urlGenerator->getBaseUrl(),
64-
$webroot,
65-
$removeWebroot
71+
$isRootRequest
6672
);
6773

68-
if ($baseUrl !== $cliUrl) {
69-
$testUrls[] = $baseUrl . $url;
70-
}
71-
7274
/* Last resort: trusted domains */
73-
$hosts = $this->config->getSystemValue('trusted_domains', []);
74-
foreach ($hosts as $host) {
75+
$trustedDomains = $this->config->getSystemValue('trusted_domains', []);
76+
foreach ($trustedDomains as $host) {
7577
if (str_contains($host, '*')) {
7678
/* Ignore domains with a wildcard */
7779
continue;
7880
}
79-
$hosts[] = 'https://' . $host . $url;
80-
$hosts[] = 'http://' . $host . $url;
81+
$baseUrls[] = $this->normalizeUrl("https://$host$webroot", $isRootRequest);
82+
$baseUrls[] = $this->normalizeUrl("http://$host$webroot", $isRootRequest);
8183
}
8284

83-
return $testUrls;
85+
return array_map(fn (string $host) => $host . $url, array_values(array_unique($baseUrls)));
8486
}
8587

8688
/**
8789
* Strip a trailing slash and remove the webroot if requested.
90+
* @param string $url The URL to normalize. Should be an absolute URL containing scheme, host and optionally web-root.
91+
* @param bool $removeWebroot If set the web-root is removed from the URL and an absolute URL with only the scheme and host (optional port) is returned
8892
*/
89-
protected function normalizeUrl(string $url, string $webroot, bool $removeWebroot): string {
90-
$url = rtrim($url, '/');
91-
if ($removeWebroot && str_ends_with($url, $webroot)) {
92-
$url = substr($url, -strlen($webroot));
93+
protected function normalizeUrl(string $url, bool $removeWebroot): string {
94+
if ($removeWebroot) {
95+
$segments = parse_url($url);
96+
$port = isset($segments['port']) ? (':' . $segments['port']) : '';
97+
return $segments['scheme'] . '://' . $segments['host'] . $port;
9398
}
9499
return rtrim($url, '/');
95100
}
96101

97102
/**
98103
* Run a HTTP request to check header
99104
* @param string $method The HTTP method to use
100-
* @param string $url The relative URL to check
101-
* @param array{ignoreSSL?: bool, httpErrors?: bool, options?: array} $options Additional options, like
102-
* [
103-
* // Ignore invalid SSL certificates (e.g. self signed)
104-
* 'ignoreSSL' => true,
105-
* // Ignore requests with HTTP errors (will not yield if request has a 4xx or 5xx response)
106-
* 'httpErrors' => true,
107-
* ]
105+
* @param string $url The absolute path (URL with webroot but without host) to check, can be the output of `IURLGenerator`
106+
* @param bool $isRootRequest If set the webroot is removed from URLs to make the request target the host's root. Example usage are the /.well-known URLs in the root path.
107+
* @param array{ignoreSSL?: bool, httpErrors?: bool, options?: array} $options HTTP client related options, like
108+
* [
109+
* // Ignore invalid SSL certificates (e.g. self signed)
110+
* 'ignoreSSL' => true,
111+
* // Ignore requests with HTTP errors (will not yield if request has a 4xx or 5xx response)
112+
* 'httpErrors' => true,
113+
* // Additional options for the HTTP client (see `IClient`)
114+
* 'options' => [],
115+
* ]
108116
*
109117
* @return Generator<int, IResponse>
110118
*/
111-
protected function runRequest(string $method, string $url, array $options = [], bool $removeWebroot = false): Generator {
119+
protected function runRequest(string $method, string $url, array $options = [], bool $isRootRequest = false): Generator {
112120
$options = array_merge(['ignoreSSL' => true, 'httpErrors' => true], $options);
113121

114122
$client = $this->clientService->newClient();
115123
$requestOptions = $this->getRequestOptions($options['ignoreSSL'], $options['httpErrors']);
116124
$requestOptions = array_merge($requestOptions, $options['options'] ?? []);
117125

118-
foreach ($this->getTestUrls($url, $removeWebroot) as $testURL) {
126+
foreach ($this->getTestUrls($url, $isRootRequest) as $testURL) {
119127
try {
120128
yield $client->request($method, $testURL, $requestOptions);
121129
} catch (\Throwable $e) {
@@ -126,7 +134,7 @@ protected function runRequest(string $method, string $url, array $options = [],
126134

127135
/**
128136
* Run a HEAD request to check header
129-
* @param string $url The relative URL to check
137+
* @param string $url The relative URL to check (e.g. output of IURLGenerator)
130138
* @param bool $ignoreSSL Ignore SSL certificates
131139
* @param bool $httpErrors Ignore requests with HTTP errors (will not yield if request has a 4xx or 5xx response)
132140
* @return Generator<int, IResponse>

apps/settings/lib/SetupChecks/DataDirectoryProtected.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ public function getName(): string {
4040
}
4141

4242
public function run(): SetupResult {
43-
$datadir = str_replace(\OC::$SERVERROOT . '/', '', $this->config->getSystemValue('datadirectory', ''));
44-
45-
$dataUrl = $this->urlGenerator->getWebroot() . '/' . $datadir . '/.ncdata';
43+
$dataDir = str_replace(\OC::$SERVERROOT . '/', '', $this->config->getSystemValueString('datadirectory', ''));
44+
$dataUrl = $this->urlGenerator->linkTo('', $dataDir . '/.ncdata');
4645

4746
$noResponse = true;
4847
foreach ($this->runRequest('GET', $dataUrl, [ 'httpErrors' => false ]) as $response) {

apps/settings/lib/SetupChecks/WellKnownUrls.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,10 @@ public function run(): SetupResult {
5050
['propfind', '/.well-known/carddav', [207], false],
5151
];
5252

53+
$requestOptions = ['httpErrors' => false, 'options' => ['allow_redirects' => ['track_redirects' => true]]];
5354
foreach ($urls as [$verb,$url,$validStatuses,$checkCustomHeader]) {
5455
$works = null;
55-
foreach ($this->runRequest($verb, $url, ['httpErrors' => false, 'options' => ['allow_redirects' => ['track_redirects' => true]]], removeWebroot: true) as $response) {
56+
foreach ($this->runRequest($verb, $url, $requestOptions, isRootRequest: true) as $response) {
5657
// Check that the response status matches
5758
$works = in_array($response->getStatusCode(), $validStatuses);
5859
// and (if needed) the custom Nextcloud header is set
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
namespace OCA\Settings\Tests\SetupChecks;
10+
11+
use OCA\Settings\SetupChecks\CheckServerResponseTrait;
12+
use OCP\Http\Client\IClientService;
13+
use OCP\IConfig;
14+
use OCP\IL10N;
15+
use OCP\IURLGenerator;
16+
use Psr\Log\LoggerInterface;
17+
18+
/**
19+
* Dummy implementation for CheckServerResponseTraitTest
20+
*/
21+
class CheckServerResponseTraitImplementation {
22+
23+
use CheckServerResponseTrait {
24+
CheckServerResponseTrait::getRequestOptions as public;
25+
CheckServerResponseTrait::runHEAD as public;
26+
CheckServerResponseTrait::runRequest as public;
27+
CheckServerResponseTrait::normalizeUrl as public;
28+
CheckServerResponseTrait::getTestUrls as public;
29+
}
30+
31+
public function __construct(
32+
protected IL10N $l10n,
33+
protected IConfig $config,
34+
protected IURLGenerator $urlGenerator,
35+
protected IClientService $clientService,
36+
protected LoggerInterface $logger,
37+
) {
38+
}
39+
40+
}

0 commit comments

Comments
 (0)