diff --git a/apps/settings/composer/composer/autoload_classmap.php b/apps/settings/composer/composer/autoload_classmap.php index ef81604ca0e..7727d8ff9ec 100644 --- a/apps/settings/composer/composer/autoload_classmap.php +++ b/apps/settings/composer/composer/autoload_classmap.php @@ -78,6 +78,7 @@ return array( 'OCA\\Settings\\Settings\\Personal\\ServerDevNotice' => $baseDir . '/../lib/Settings/Personal/ServerDevNotice.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', @@ -114,6 +115,7 @@ return array( 'OCA\\Settings\\SetupChecks\\SystemIs64bit' => $baseDir . '/../lib/SetupChecks/SystemIs64bit.php', 'OCA\\Settings\\SetupChecks\\TempSpaceAvailable' => $baseDir . '/../lib/SetupChecks/TempSpaceAvailable.php', 'OCA\\Settings\\SetupChecks\\TransactionIsolation' => $baseDir . '/../lib/SetupChecks/TransactionIsolation.php', + 'OCA\\Settings\\SetupChecks\\Woff2Loading' => $baseDir . '/../lib/SetupChecks/Woff2Loading.php', 'OCA\\Settings\\UserMigration\\AccountMigrator' => $baseDir . '/../lib/UserMigration/AccountMigrator.php', 'OCA\\Settings\\UserMigration\\AccountMigratorException' => $baseDir . '/../lib/UserMigration/AccountMigratorException.php', 'OCA\\Settings\\WellKnown\\ChangePasswordHandler' => $baseDir . '/../lib/WellKnown/ChangePasswordHandler.php', diff --git a/apps/settings/composer/composer/autoload_static.php b/apps/settings/composer/composer/autoload_static.php index fe23224d214..f565a34c5ee 100644 --- a/apps/settings/composer/composer/autoload_static.php +++ b/apps/settings/composer/composer/autoload_static.php @@ -93,6 +93,7 @@ class ComposerStaticInitSettings 'OCA\\Settings\\Settings\\Personal\\ServerDevNotice' => __DIR__ . '/..' . '/../lib/Settings/Personal/ServerDevNotice.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', @@ -129,6 +130,7 @@ class ComposerStaticInitSettings 'OCA\\Settings\\SetupChecks\\SystemIs64bit' => __DIR__ . '/..' . '/../lib/SetupChecks/SystemIs64bit.php', 'OCA\\Settings\\SetupChecks\\TempSpaceAvailable' => __DIR__ . '/..' . '/../lib/SetupChecks/TempSpaceAvailable.php', 'OCA\\Settings\\SetupChecks\\TransactionIsolation' => __DIR__ . '/..' . '/../lib/SetupChecks/TransactionIsolation.php', + 'OCA\\Settings\\SetupChecks\\Woff2Loading' => __DIR__ . '/..' . '/../lib/SetupChecks/Woff2Loading.php', 'OCA\\Settings\\UserMigration\\AccountMigrator' => __DIR__ . '/..' . '/../lib/UserMigration/AccountMigrator.php', 'OCA\\Settings\\UserMigration\\AccountMigratorException' => __DIR__ . '/..' . '/../lib/UserMigration/AccountMigratorException.php', 'OCA\\Settings\\WellKnown\\ChangePasswordHandler' => __DIR__ . '/..' . '/../lib/WellKnown/ChangePasswordHandler.php', diff --git a/apps/settings/lib/SetupChecks/CheckServerResponseTrait.php b/apps/settings/lib/SetupChecks/CheckServerResponseTrait.php new file mode 100644 index 00000000000..30ffa9a2977 --- /dev/null +++ b/apps/settings/lib/SetupChecks/CheckServerResponseTrait.php @@ -0,0 +1,104 @@ + + * + * @author Ferdinand Thiessen + * + * @license AGPL-3.0-or-later + * + * 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 . + * + */ +namespace OCA\Settings\SetupChecks; + +use Generator; +use OCP\Http\Client\IClientService; +use OCP\Http\Client\IResponse; +use OCP\IConfig; +use OCP\IL10N; +use OCP\IURLGenerator; + +/** + * Common trait for setup checks that need to use requests to the same server and check the response + */ +trait CheckServerResponseTrait { + protected IConfig $config; + protected IURLGenerator $urlGenerator; + protected IClientService $clientService; + protected IL10N $l10n; + + /** + * Common helper string in case a check could not fetch any results + */ + protected function serverConfigHelp(): string { + return $this->l10n->t('To allow this check to run you have to make sure that your webserver can connect to itself. Therefor it must be able to resolve and connect to at least one its `trusted_domains` or the `overwrite.cli.url`.'); + } + + /** + * Get all possible URLs that need to be checked for a local request test. + * This takes all `trusted_domains` and the CLI overwrite URL into account. + * + * @param string $url The relative URL to test + * @return string[] List of possible absolute URLs + */ + protected function getTestUrls(string $url): array { + $hosts = $this->config->getSystemValue('trusted_domains', []); + $cliUrl = $this->config->getSystemValue('overwrite.cli.url', ''); + if ($cliUrl !== '') { + $hosts[] = $cliUrl; + } + + $testUrls = array_merge( + [$this->urlGenerator->getAbsoluteURL($url)], + array_map(fn (string $host): string => $host . $url, $hosts), + ); + + return $testUrls; + } + + /** + * Run a HEAD request to check header + * @param string $url The relative URL to check + * @param bool $ignoreSSL Ignore SSL certificates + * @return Generator + */ + protected function runHEAD(string $url, bool $ignoreSSL = true): Generator { + $client = $this->clientService->newClient(); + $requestOptions = $this->getRequestOptions($ignoreSSL); + + foreach ($this->getTestUrls($url) as $testURL) { + try { + yield $client->head($testURL, $requestOptions); + } catch (\Throwable $e) { + $this->logger->debug('Can not connect to local server for running setup checks', ['exception' => $e, 'url' => $testURL]); + } + } + } + + protected function getRequestOptions(bool $ignoreSSL): array { + $requestOptions = [ + 'connect_timeout' => 10, + 'nextcloud' => [ + 'allow_local_address' => true, + ], + ]; + if ($ignoreSSL) { + $requestOptions['verify'] = false; + } + return $requestOptions; + } +} diff --git a/apps/settings/lib/SetupChecks/JavaScriptModules.php b/apps/settings/lib/SetupChecks/JavaScriptModules.php index 92e0035eedd..81f580a37bc 100644 --- a/apps/settings/lib/SetupChecks/JavaScriptModules.php +++ b/apps/settings/lib/SetupChecks/JavaScriptModules.php @@ -37,12 +37,14 @@ use Psr\Log\LoggerInterface; * Checks if the webserver serves '.mjs' files using the correct MIME type */ class JavaScriptModules implements ISetupCheck { + use CheckServerResponseTrait; + public function __construct( - private IL10N $l10n, - private IConfig $config, - private IURLGenerator $urlGenerator, - private IClientService $clientService, - private LoggerInterface $logger, + protected IL10N $l10n, + protected IConfig $config, + protected IURLGenerator $urlGenerator, + protected IClientService $clientService, + protected LoggerInterface $logger, ) { } @@ -56,28 +58,19 @@ class JavaScriptModules implements ISetupCheck { public function run(): SetupResult { $testFile = $this->urlGenerator->linkTo('settings', 'js/esm-test.mjs'); - $testURLs = array_merge( - [$this->urlGenerator->getAbsoluteURL($testFile)], - array_map(fn (string $host): string => $host . $testFile, $this->config->getSystemValue('trusted_domains', [])) - ); - foreach ($testURLs as $testURL) { - try { - $client = $this->clientService->newClient(); - $response = $client->head($testURL, [ - 'connect_timeout' => 10, - 'nextcloud' => [ - 'allow_local_address' => true, - ], - ]); - if (preg_match('/(text|application)\/javascript/i', $response->getHeader('Content-Type'))) { - return SetupResult::success(); - } - } catch (\Throwable $e) { - $this->logger->debug('Can not connect to local server for checking JavaScript modules support', ['exception' => $e, 'url' => $testURL]); - return SetupResult::warning($this->l10n->t('Could not check for JavaScript support. Please check manually if your webserver serves `.mjs` files using the JavaScript MIME type.')); + $noResponse = true; + foreach ($this->runHEAD($testFile) as $response) { + $noResponse = false; + if (preg_match('/(text|application)\/javascript/i', $response->getHeader('Content-Type'))) { + return SetupResult::success(); } } + + if ($noResponse) { + return SetupResult::warning($this->l10n->t('Could not check for JavaScript support. Please check manually if your webserver serves `.mjs` files using the JavaScript MIME type.') . "\n" . $this->serverConfigHelp()); + } return SetupResult::error($this->l10n->t('Your webserver does not serve `.mjs` files using the JavaScript MIME type. This will break some apps by preventing browsers from executing the JavaScript files. You should configure your webserver to serve `.mjs` files with either the `text/javascript` or `application/javascript` MIME type.')); + } }