From 7fbd518452570e1e3c77815acaf6ab0242b00d08 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Fri, 13 Sep 2024 14:18:25 +0200 Subject: [PATCH] feat: Make `CheckServerResponseTrait` public and provide as `OCP\SetupCheck\CheckServerResponseTrait` 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 --- apps/dav/lib/SetupChecks/WebdavEndpoint.php | 2 +- .../composer/composer/autoload_classmap.php | 1 - .../composer/composer/autoload_static.php | 1 - .../SetupChecks/DataDirectoryProtected.php | 1 + .../lib/SetupChecks/JavaScriptModules.php | 3 +- .../lib/SetupChecks/JavaScriptSourceMaps.php | 3 +- .../settings/lib/SetupChecks/OcxProviders.php | 1 + .../lib/SetupChecks/SecurityHeaders.php | 5 +- .../lib/SetupChecks/WellKnownUrls.php | 1 + .../settings/lib/SetupChecks/Woff2Loading.php | 3 +- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + .../SetupCheck}/CheckServerResponseTrait.php | 59 ++++++++++--------- ...CheckServerResponseTraitImplementation.php | 5 +- .../CheckServerResponseTraitTest.php | 2 +- 15 files changed, 49 insertions(+), 40 deletions(-) rename {apps/settings/lib/SetupChecks => lib/public/SetupCheck}/CheckServerResponseTrait.php (84%) rename {apps/settings/tests/SetupChecks => tests/lib/SetupCheck}/CheckServerResponseTraitImplementation.php (85%) rename {apps/settings/tests/SetupChecks => tests/lib/SetupCheck}/CheckServerResponseTraitTest.php (99%) diff --git a/apps/dav/lib/SetupChecks/WebdavEndpoint.php b/apps/dav/lib/SetupChecks/WebdavEndpoint.php index f5a387836fb77..c2574202fcd02 100644 --- a/apps/dav/lib/SetupChecks/WebdavEndpoint.php +++ b/apps/dav/lib/SetupChecks/WebdavEndpoint.php @@ -9,11 +9,11 @@ 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 7a3618ecb5134..1b3c4c25552af 100644 --- a/apps/settings/composer/composer/autoload_classmap.php +++ b/apps/settings/composer/composer/autoload_classmap.php @@ -82,7 +82,6 @@ '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 18882ed9fc233..5de0bb31fd6b4 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 051494adb6258..4280457ced03a 100644 --- a/apps/settings/lib/SetupChecks/DataDirectoryProtected.php +++ b/apps/settings/lib/SetupChecks/DataDirectoryProtected.php @@ -12,6 +12,7 @@ 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 ae19eacec7b3f..e09dc459dc885 100644 --- a/apps/settings/lib/SetupChecks/JavaScriptModules.php +++ b/apps/settings/lib/SetupChecks/JavaScriptModules.php @@ -12,6 +12,7 @@ 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 @@ public function run(): SetupResult { $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 85cbe8723395c..dcfc40192b93a 100644 --- a/apps/settings/lib/SetupChecks/JavaScriptSourceMaps.php +++ b/apps/settings/lib/SetupChecks/JavaScriptSourceMaps.php @@ -12,6 +12,7 @@ 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 @@ public function getName(): string { 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 84da99dbfb097..191341b0ee431 100644 --- a/apps/settings/lib/SetupChecks/OcxProviders.php +++ b/apps/settings/lib/SetupChecks/OcxProviders.php @@ -12,6 +12,7 @@ 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 a6dbc631b5cde..b85ab9b401804 100644 --- a/apps/settings/lib/SetupChecks/SecurityHeaders.php +++ b/apps/settings/lib/SetupChecks/SecurityHeaders.php @@ -13,6 +13,7 @@ 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 @@ public function run(): SetupResult { } } - $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 623b9fae90c6e..9fdaca996b867 100644 --- a/apps/settings/lib/SetupChecks/WellKnownUrls.php +++ b/apps/settings/lib/SetupChecks/WellKnownUrls.php @@ -13,6 +13,7 @@ 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 769653c46180d..27aff4ea9993a 100644 --- a/apps/settings/lib/SetupChecks/Woff2Loading.php +++ b/apps/settings/lib/SetupChecks/Woff2Loading.php @@ -12,6 +12,7 @@ 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 @@ public function run(): SetupResult { 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 37013ecc1aecd..5510143197ac8 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -700,6 +700,7 @@ '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 293e79f80c64d..7b8584b543826 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 similarity index 84% rename from apps/settings/lib/SetupChecks/CheckServerResponseTrait.php rename to lib/public/SetupCheck/CheckServerResponseTrait.php index 3080829cb00c4..29a2215aa673e 100644 --- a/apps/settings/lib/SetupChecks/CheckServerResponseTrait.php +++ b/lib/public/SetupCheck/CheckServerResponseTrait.php @@ -6,31 +6,33 @@ * 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 @@ protected function serverConfigHelp(): string { * @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 List of possible absolute URLs + * @since 31.0.0 */ protected function getTestUrls(string $url, bool $isRootRequest = false): array { $url = '/' . ltrim($url, '/'); @@ -85,20 +88,6 @@ protected function getTestUrls(string $url, bool $isRootRequest = false): array return array_map(fn (string $host) => $host . $url, array_values(array_unique($baseUrls))); } - /** - * 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 @@ -115,6 +104,7 @@ protected function normalizeUrl(string $url, bool $removeWebroot): string { * ] * * @return Generator + * @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 @@ protected function runRequest(string $method, string $url, array $options = [], } /** - * 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 + * 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 @@ protected function getRequestOptions(bool $ignoreSSL, bool $httpErrors): array { } 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 similarity index 85% rename from apps/settings/tests/SetupChecks/CheckServerResponseTraitImplementation.php rename to tests/lib/SetupCheck/CheckServerResponseTraitImplementation.php index 6c8b65855cc93..1119c5ed0d986 100644 --- a/apps/settings/tests/SetupChecks/CheckServerResponseTraitImplementation.php +++ b/tests/lib/SetupCheck/CheckServerResponseTraitImplementation.php @@ -6,13 +6,13 @@ * 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 similarity index 99% rename from apps/settings/tests/SetupChecks/CheckServerResponseTraitTest.php rename to tests/lib/SetupCheck/CheckServerResponseTraitTest.php index e51546c1cf174..32fbce64ce626 100644 --- a/apps/settings/tests/SetupChecks/CheckServerResponseTraitTest.php +++ b/tests/lib/SetupCheck/CheckServerResponseTraitTest.php @@ -6,7 +6,7 @@ * 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;