diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-09-13 14:18:25 +0200 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2024-09-16 14:58:21 +0200 |
commit | 7fbd518452570e1e3c77815acaf6ab0242b00d08 (patch) | |
tree | 76330afc5e2b841a2155778954f6931d5fe8d022 | |
parent | ff0cab5956c289bad60db54716b148c8879566a4 (diff) | |
download | nextcloud-server-7fbd518452570e1e3c77815acaf6ab0242b00d08.tar.gz nextcloud-server-7fbd518452570e1e3c77815acaf6ab0242b00d08.zip |
feat: Make `CheckServerResponseTrait` public and provide as `OCP\SetupCheck\CheckServerResponseTrait`feat/make-setup-check-trait-public
This trait is used by other apps for creating setup checks,
so we should provide it instead apps using private API.
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
15 files changed, 49 insertions, 40 deletions
diff --git a/apps/dav/lib/SetupChecks/WebdavEndpoint.php b/apps/dav/lib/SetupChecks/WebdavEndpoint.php index f5a387836fb..c2574202fcd 100644 --- a/apps/dav/lib/SetupChecks/WebdavEndpoint.php +++ b/apps/dav/lib/SetupChecks/WebdavEndpoint.php @@ -9,11 +9,11 @@ declare(strict_types=1); namespace OCA\DAV\SetupChecks; -use OCA\Settings\SetupChecks\CheckServerResponseTrait; use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\SetupCheck\CheckServerResponseTrait; use OCP\SetupCheck\ISetupCheck; use OCP\SetupCheck\SetupResult; use Psr\Log\LoggerInterface; diff --git a/apps/settings/composer/composer/autoload_classmap.php b/apps/settings/composer/composer/autoload_classmap.php index 7a3618ecb51..1b3c4c25552 100644 --- a/apps/settings/composer/composer/autoload_classmap.php +++ b/apps/settings/composer/composer/autoload_classmap.php @@ -82,7 +82,6 @@ return array( 'OCA\\Settings\\SetupChecks\\AllowedAdminRanges' => $baseDir . '/../lib/SetupChecks/AllowedAdminRanges.php', 'OCA\\Settings\\SetupChecks\\AppDirsWithDifferentOwner' => $baseDir . '/../lib/SetupChecks/AppDirsWithDifferentOwner.php', 'OCA\\Settings\\SetupChecks\\BruteForceThrottler' => $baseDir . '/../lib/SetupChecks/BruteForceThrottler.php', - 'OCA\\Settings\\SetupChecks\\CheckServerResponseTrait' => $baseDir . '/../lib/SetupChecks/CheckServerResponseTrait.php', 'OCA\\Settings\\SetupChecks\\CheckUserCertificates' => $baseDir . '/../lib/SetupChecks/CheckUserCertificates.php', 'OCA\\Settings\\SetupChecks\\CodeIntegrity' => $baseDir . '/../lib/SetupChecks/CodeIntegrity.php', 'OCA\\Settings\\SetupChecks\\CronErrors' => $baseDir . '/../lib/SetupChecks/CronErrors.php', diff --git a/apps/settings/composer/composer/autoload_static.php b/apps/settings/composer/composer/autoload_static.php index 18882ed9fc2..5de0bb31fd6 100644 --- a/apps/settings/composer/composer/autoload_static.php +++ b/apps/settings/composer/composer/autoload_static.php @@ -97,7 +97,6 @@ class ComposerStaticInitSettings 'OCA\\Settings\\SetupChecks\\AllowedAdminRanges' => __DIR__ . '/..' . '/../lib/SetupChecks/AllowedAdminRanges.php', 'OCA\\Settings\\SetupChecks\\AppDirsWithDifferentOwner' => __DIR__ . '/..' . '/../lib/SetupChecks/AppDirsWithDifferentOwner.php', 'OCA\\Settings\\SetupChecks\\BruteForceThrottler' => __DIR__ . '/..' . '/../lib/SetupChecks/BruteForceThrottler.php', - 'OCA\\Settings\\SetupChecks\\CheckServerResponseTrait' => __DIR__ . '/..' . '/../lib/SetupChecks/CheckServerResponseTrait.php', 'OCA\\Settings\\SetupChecks\\CheckUserCertificates' => __DIR__ . '/..' . '/../lib/SetupChecks/CheckUserCertificates.php', 'OCA\\Settings\\SetupChecks\\CodeIntegrity' => __DIR__ . '/..' . '/../lib/SetupChecks/CodeIntegrity.php', 'OCA\\Settings\\SetupChecks\\CronErrors' => __DIR__ . '/..' . '/../lib/SetupChecks/CronErrors.php', diff --git a/apps/settings/lib/SetupChecks/DataDirectoryProtected.php b/apps/settings/lib/SetupChecks/DataDirectoryProtected.php index 051494adb62..4280457ced0 100644 --- a/apps/settings/lib/SetupChecks/DataDirectoryProtected.php +++ b/apps/settings/lib/SetupChecks/DataDirectoryProtected.php @@ -12,6 +12,7 @@ use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\SetupCheck\CheckServerResponseTrait; use OCP\SetupCheck\ISetupCheck; use OCP\SetupCheck\SetupResult; use Psr\Log\LoggerInterface; diff --git a/apps/settings/lib/SetupChecks/JavaScriptModules.php b/apps/settings/lib/SetupChecks/JavaScriptModules.php index ae19eacec7b..e09dc459dc8 100644 --- a/apps/settings/lib/SetupChecks/JavaScriptModules.php +++ b/apps/settings/lib/SetupChecks/JavaScriptModules.php @@ -12,6 +12,7 @@ use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\SetupCheck\CheckServerResponseTrait; use OCP\SetupCheck\ISetupCheck; use OCP\SetupCheck\SetupResult; use Psr\Log\LoggerInterface; @@ -43,7 +44,7 @@ class JavaScriptModules implements ISetupCheck { $testFile = $this->urlGenerator->linkTo('settings', 'js/esm-test.mjs'); $noResponse = true; - foreach ($this->runHEAD($testFile) as $response) { + foreach ($this->runRequest('HEAD', $testFile) as $response) { $noResponse = false; if (preg_match('/(text|application)\/javascript/i', $response->getHeader('Content-Type'))) { return SetupResult::success(); diff --git a/apps/settings/lib/SetupChecks/JavaScriptSourceMaps.php b/apps/settings/lib/SetupChecks/JavaScriptSourceMaps.php index 85cbe872339..dcfc40192b9 100644 --- a/apps/settings/lib/SetupChecks/JavaScriptSourceMaps.php +++ b/apps/settings/lib/SetupChecks/JavaScriptSourceMaps.php @@ -12,6 +12,7 @@ use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\SetupCheck\CheckServerResponseTrait; use OCP\SetupCheck\ISetupCheck; use OCP\SetupCheck\SetupResult; use Psr\Log\LoggerInterface; @@ -42,7 +43,7 @@ class JavaScriptSourceMaps implements ISetupCheck { public function run(): SetupResult { $testFile = $this->urlGenerator->linkTo('settings', 'js/map-test.js.map'); - foreach ($this->runHEAD($testFile) as $response) { + foreach ($this->runRequest('HEAD', $testFile) as $response) { return SetupResult::success(); } diff --git a/apps/settings/lib/SetupChecks/OcxProviders.php b/apps/settings/lib/SetupChecks/OcxProviders.php index 84da99dbfb0..191341b0ee4 100644 --- a/apps/settings/lib/SetupChecks/OcxProviders.php +++ b/apps/settings/lib/SetupChecks/OcxProviders.php @@ -12,6 +12,7 @@ use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\SetupCheck\CheckServerResponseTrait; use OCP\SetupCheck\ISetupCheck; use OCP\SetupCheck\SetupResult; use Psr\Log\LoggerInterface; diff --git a/apps/settings/lib/SetupChecks/SecurityHeaders.php b/apps/settings/lib/SetupChecks/SecurityHeaders.php index a6dbc631b5c..b85ab9b4018 100644 --- a/apps/settings/lib/SetupChecks/SecurityHeaders.php +++ b/apps/settings/lib/SetupChecks/SecurityHeaders.php @@ -13,6 +13,7 @@ use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\SetupCheck\CheckServerResponseTrait; use OCP\SetupCheck\ISetupCheck; use OCP\SetupCheck\SetupResult; use Psr\Log\LoggerInterface; @@ -71,8 +72,8 @@ class SecurityHeaders implements ISetupCheck { } } - $xssfields = array_map('trim', explode(';', $response->getHeader('X-XSS-Protection'))); - if (!in_array('1', $xssfields) || !in_array('mode=block', $xssfields)) { + $xssFields = array_map('trim', explode(';', $response->getHeader('X-XSS-Protection'))); + if (!in_array('1', $xssFields) || !in_array('mode=block', $xssFields)) { $msg .= $this->l10n->t('- The `%1$s` HTTP header does not contain `%2$s`. This is a potential security or privacy risk, as it is recommended to adjust this setting accordingly.', ['X-XSS-Protection', '1; mode=block'])."\n"; } diff --git a/apps/settings/lib/SetupChecks/WellKnownUrls.php b/apps/settings/lib/SetupChecks/WellKnownUrls.php index 623b9fae90c..9fdaca996b8 100644 --- a/apps/settings/lib/SetupChecks/WellKnownUrls.php +++ b/apps/settings/lib/SetupChecks/WellKnownUrls.php @@ -13,6 +13,7 @@ use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\SetupCheck\CheckServerResponseTrait; use OCP\SetupCheck\ISetupCheck; use OCP\SetupCheck\SetupResult; use Psr\Log\LoggerInterface; diff --git a/apps/settings/lib/SetupChecks/Woff2Loading.php b/apps/settings/lib/SetupChecks/Woff2Loading.php index 769653c4618..27aff4ea999 100644 --- a/apps/settings/lib/SetupChecks/Woff2Loading.php +++ b/apps/settings/lib/SetupChecks/Woff2Loading.php @@ -12,6 +12,7 @@ use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\SetupCheck\CheckServerResponseTrait; use OCP\SetupCheck\ISetupCheck; use OCP\SetupCheck\SetupResult; use Psr\Log\LoggerInterface; @@ -49,7 +50,7 @@ class Woff2Loading implements ISetupCheck { protected function checkFont(string $fileExtension, string $url): SetupResult { $noResponse = true; - $responses = $this->runHEAD($url); + $responses = $this->runRequest('HEAD', $url); foreach ($responses as $response) { $noResponse = false; if ($response->getStatusCode() === 200) { diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 37013ecc1ae..5510143197a 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -700,6 +700,7 @@ return array( 'OCP\\Settings\\IManager' => $baseDir . '/lib/public/Settings/IManager.php', 'OCP\\Settings\\ISettings' => $baseDir . '/lib/public/Settings/ISettings.php', 'OCP\\Settings\\ISubAdminSettings' => $baseDir . '/lib/public/Settings/ISubAdminSettings.php', + 'OCP\\SetupCheck\\CheckServerResponseTrait' => $baseDir . '/lib/public/SetupCheck/CheckServerResponseTrait.php', 'OCP\\SetupCheck\\ISetupCheck' => $baseDir . '/lib/public/SetupCheck/ISetupCheck.php', 'OCP\\SetupCheck\\ISetupCheckManager' => $baseDir . '/lib/public/SetupCheck/ISetupCheckManager.php', 'OCP\\SetupCheck\\SetupResult' => $baseDir . '/lib/public/SetupCheck/SetupResult.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 293e79f80c6..7b8584b5438 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -733,6 +733,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Settings\\IManager' => __DIR__ . '/../../..' . '/lib/public/Settings/IManager.php', 'OCP\\Settings\\ISettings' => __DIR__ . '/../../..' . '/lib/public/Settings/ISettings.php', 'OCP\\Settings\\ISubAdminSettings' => __DIR__ . '/../../..' . '/lib/public/Settings/ISubAdminSettings.php', + 'OCP\\SetupCheck\\CheckServerResponseTrait' => __DIR__ . '/../../..' . '/lib/public/SetupCheck/CheckServerResponseTrait.php', 'OCP\\SetupCheck\\ISetupCheck' => __DIR__ . '/../../..' . '/lib/public/SetupCheck/ISetupCheck.php', 'OCP\\SetupCheck\\ISetupCheckManager' => __DIR__ . '/../../..' . '/lib/public/SetupCheck/ISetupCheckManager.php', 'OCP\\SetupCheck\\SetupResult' => __DIR__ . '/../../..' . '/lib/public/SetupCheck/SetupResult.php', diff --git a/apps/settings/lib/SetupChecks/CheckServerResponseTrait.php b/lib/public/SetupCheck/CheckServerResponseTrait.php index 3080829cb00..29a2215aa67 100644 --- a/apps/settings/lib/SetupChecks/CheckServerResponseTrait.php +++ b/lib/public/SetupCheck/CheckServerResponseTrait.php @@ -6,31 +6,33 @@ declare(strict_types=1); * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\Settings\SetupChecks; +namespace OCP\SetupCheck; use Generator; use OCP\Http\Client\IClientService; use OCP\Http\Client\IResponse; use OCP\IConfig; -use OCP\IL10N; use OCP\IURLGenerator; +use OCP\L10N\IFactory; use Psr\Log\LoggerInterface; /** * Common trait for setup checks that need to use requests to the same server and check the response + * @since 31.0.0 */ trait CheckServerResponseTrait { protected IConfig $config; protected IURLGenerator $urlGenerator; protected IClientService $clientService; - protected IL10N $l10n; protected LoggerInterface $logger; /** * Common helper string in case a check could not fetch any results + * @since 31.0.0 */ protected function serverConfigHelp(): string { - return $this->l10n->t('To allow this check to run you have to make sure that your Web server can connect to itself. Therefore it must be able to resolve and connect to at least one of its `trusted_domains` or the `overwrite.cli.url`. This failure may be the result of a server-side DNS mismatch or outbound firewall rule.'); + $l10n = \OCP\Server::get(IFactory::class)->get('lib'); + return $l10n->t('To allow this check to run you have to make sure that your Web server can connect to itself. Therefore it must be able to resolve and connect to at least one of its `trusted_domains` or the `overwrite.cli.url`. This failure may be the result of a server-side DNS mismatch or outbound firewall rule.'); } /** @@ -40,6 +42,7 @@ trait CheckServerResponseTrait { * @param string $url The absolute path (absolute URL without host but with web-root) to test starting with a / * @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') * @return list<string> List of possible absolute URLs + * @since 31.0.0 */ protected function getTestUrls(string $url, bool $isRootRequest = false): array { $url = '/' . ltrim($url, '/'); @@ -86,20 +89,6 @@ trait CheckServerResponseTrait { } /** - * Strip a trailing slash and remove the webroot if requested. - * @param string $url The URL to normalize. Should be an absolute URL containing scheme, host and optionally web-root. - * @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 - */ - protected function normalizeUrl(string $url, bool $removeWebroot): string { - if ($removeWebroot) { - $segments = parse_url($url); - $port = isset($segments['port']) ? (':' . $segments['port']) : ''; - return $segments['scheme'] . '://' . $segments['host'] . $port; - } - return rtrim($url, '/'); - } - - /** * Run a HTTP request to check header * @param string $method The HTTP method to use * @param string $url The absolute path (URL with webroot but without host) to check, can be the output of `IURLGenerator` @@ -115,6 +104,7 @@ trait CheckServerResponseTrait { * ] * * @return Generator<int, IResponse> + * @since 31.0.0 */ protected function runRequest(string $method, string $url, array $options = [], bool $isRootRequest = false): Generator { $options = array_merge(['ignoreSSL' => true, 'httpErrors' => true], $options); @@ -133,17 +123,11 @@ trait CheckServerResponseTrait { } /** - * Run a HEAD request to check header - * @param string $url The relative URL to check (e.g. output of IURLGenerator) - * @param bool $ignoreSSL Ignore SSL certificates - * @param bool $httpErrors Ignore requests with HTTP errors (will not yield if request has a 4xx or 5xx response) - * @return Generator<int, IResponse> + * Get HTTP client options + * @param bool $ignoreSSL If set SSL errors are ignored (e.g. self-signed certificates) + * @since 31.0.0 */ - protected function runHEAD(string $url, bool $ignoreSSL = true, bool $httpErrors = true): Generator { - return $this->runRequest('HEAD', $url, ['ignoreSSL' => $ignoreSSL, 'httpErrors' => $httpErrors]); - } - - protected function getRequestOptions(bool $ignoreSSL, bool $httpErrors): array { + private function getRequestOptions(bool $ignoreSSL, bool $httpErrors): array { $requestOptions = [ 'connect_timeout' => 10, 'http_errors' => $httpErrors, @@ -156,4 +140,23 @@ trait CheckServerResponseTrait { } return $requestOptions; } + + /** + * Strip a trailing slash and remove the webroot if requested. + * @param string $url The URL to normalize. Should be an absolute URL containing scheme, host and optionally web-root. + * @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 + * @since 31.0.0 + */ + private function normalizeUrl(string $url, bool $removeWebroot): string { + if ($removeWebroot) { + $segments = parse_url($url); + if (!isset($segments['scheme']) || !isset($segments['host'])) { + throw new \InvalidArgumentException('URL is missing scheme or host'); + } + + $port = isset($segments['port']) ? (':' . $segments['port']) : ''; + return $segments['scheme'] . '://' . $segments['host'] . $port; + } + return rtrim($url, '/'); + } } diff --git a/apps/settings/tests/SetupChecks/CheckServerResponseTraitImplementation.php b/tests/lib/SetupCheck/CheckServerResponseTraitImplementation.php index 6c8b65855cc..1119c5ed0d9 100644 --- a/apps/settings/tests/SetupChecks/CheckServerResponseTraitImplementation.php +++ b/tests/lib/SetupCheck/CheckServerResponseTraitImplementation.php @@ -6,13 +6,13 @@ declare(strict_types=1); * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\Settings\Tests\SetupChecks; +namespace Test\SetupCheck; -use OCA\Settings\SetupChecks\CheckServerResponseTrait; use OCP\Http\Client\IClientService; use OCP\IConfig; use OCP\IL10N; use OCP\IURLGenerator; +use OCP\SetupCheck\CheckServerResponseTrait; use Psr\Log\LoggerInterface; /** @@ -22,7 +22,6 @@ class CheckServerResponseTraitImplementation { use CheckServerResponseTrait { CheckServerResponseTrait::getRequestOptions as public; - CheckServerResponseTrait::runHEAD as public; CheckServerResponseTrait::runRequest as public; CheckServerResponseTrait::normalizeUrl as public; CheckServerResponseTrait::getTestUrls as public; diff --git a/apps/settings/tests/SetupChecks/CheckServerResponseTraitTest.php b/tests/lib/SetupCheck/CheckServerResponseTraitTest.php index e51546c1cf1..32fbce64ce6 100644 --- a/apps/settings/tests/SetupChecks/CheckServerResponseTraitTest.php +++ b/tests/lib/SetupCheck/CheckServerResponseTraitTest.php @@ -6,7 +6,7 @@ declare(strict_types=1); * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors * SPDX-License-Identifier: AGPL-3.0-or-later */ -namespace OCA\Settings\Tests\SetupChecks; +namespace Test\SetupCheck; use OCP\Http\Client\IClientService; use OCP\IConfig; |