Skip to content

Commit c224b8c

Browse files
committed
fix(setupchecks): Test overwrite.cli url first, then generated one, and
trusted domains as last fallback. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
1 parent 4ce4d7b commit c224b8c

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

apps/settings/lib/SetupChecks/CheckServerResponseTrait.php

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,36 +35,49 @@ protected function serverConfigHelp(): string {
3535

3636
/**
3737
* Get all possible URLs that need to be checked for a local request test.
38+
* This takes all `trusted_domains` and the CLI overwrite URL into account.
3839
*
39-
* @param string $url The relative URL to test
40+
* @param string $url The relative URL to test starting with a /
4041
* @return string[] List of possible absolute URLs
4142
*/
4243
protected function getTestUrls(string $url, bool $removeWebroot): array {
4344
$testUrls = [];
4445

45-
$webroot = $this->urlGenerator->getWebroot();
46+
$webroot = rtrim($this->urlGenerator->getWebroot(), '/');
4647

47-
$baseUrl = $this->normalizeUrl(
48-
$this->urlGenerator->getBaseUrl(),
49-
$webroot,
50-
$removeWebroot
51-
);
48+
/* Try overwrite.cli.url first, it’s supposed to be how the server contacts itself */
49+
$cliUrl = $this->config->getSystemValueString('overwrite.cli.url', '');
5250

53-
$testUrls[] = $baseUrl . $url;
51+
if ($cliUrl !== '') {
52+
$cliUrl = $this->normalizeUrl(
53+
$cliUrl,
54+
$webroot,
55+
$removeWebroot
56+
);
5457

55-
$cliUrl = $this->config->getSystemValueString('overwrite.cli.url', '');
56-
if ($cliUrl === '') {
57-
return $testUrls;
58+
$testUrls[] = $cliUrl . $url;
5859
}
5960

60-
$cliUrl = $this->normalizeUrl(
61-
$cliUrl,
61+
/* Try URL generator second */
62+
$baseUrl = $this->normalizeUrl(
63+
$this->urlGenerator->getBaseUrl(),
6264
$webroot,
6365
$removeWebroot
6466
);
6567

66-
if ($cliUrl !== $baseUrl) {
67-
$testUrls[] = $cliUrl . $url;
68+
if ($baseUrl !== $cliUrl) {
69+
$testUrls[] = $baseUrl . $url;
70+
}
71+
72+
/* Last resort: trusted domains */
73+
$hosts = $this->config->getSystemValue('trusted_domains', []);
74+
foreach ($hosts as $host) {
75+
if (str_contains($host, '*')) {
76+
/* Ignore domains with a wildcard */
77+
continue;
78+
}
79+
$hosts[] = 'https://' . $host . $url;
80+
$hosts[] = 'http://' . $host . $url;
6881
}
6982

7083
return $testUrls;
@@ -74,7 +87,8 @@ protected function getTestUrls(string $url, bool $removeWebroot): array {
7487
* Strip a trailing slash and remove the webroot if requested.
7588
*/
7689
protected function normalizeUrl(string $url, string $webroot, bool $removeWebroot): string {
77-
if ($removeWebroot && str_contains($url, $webroot)) {
90+
$url = rtrim($url, '/');
91+
if ($removeWebroot && str_ends_with($url, $webroot)) {
7892
$url = substr($url, -strlen($webroot));
7993
}
8094
return rtrim($url, '/');

0 commit comments

Comments
 (0)