Skip to content

Commit

Permalink
Merge pull request #43588 from nextcloud/feat/woff2-check-setupcheck
Browse files Browse the repository at this point in the history
  • Loading branch information
skjnldsv authored Feb 15, 2024
2 parents 76111e9 + 6db75d9 commit 19bfbe3
Show file tree
Hide file tree
Showing 11 changed files with 214 additions and 91 deletions.
2 changes: 2 additions & 0 deletions apps/settings/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
'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',
Expand Down Expand Up @@ -114,6 +115,7 @@
'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',
Expand Down
2 changes: 2 additions & 0 deletions apps/settings/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions apps/settings/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
use OCA\Settings\SetupChecks\SystemIs64bit;
use OCA\Settings\SetupChecks\TempSpaceAvailable;
use OCA\Settings\SetupChecks\TransactionIsolation;
use OCA\Settings\SetupChecks\Woff2Loading;
use OCA\Settings\UserMigration\AccountMigrator;
use OCA\Settings\WellKnown\ChangePasswordHandler;
use OCA\Settings\WellKnown\SecurityTxtHandler;
Expand Down Expand Up @@ -213,6 +214,7 @@ public function register(IRegistrationContext $context): void {
$context->registerSetupCheck(TempSpaceAvailable::class);
$context->registerSetupCheck(TransactionIsolation::class);
$context->registerSetupCheck(PushService::class);
$context->registerSetupCheck(Woff2Loading::class);

$context->registerUserMigrator(AccountMigrator::class);
}
Expand Down
104 changes: 104 additions & 0 deletions apps/settings/lib/SetupChecks/CheckServerResponseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Ferdinand Thiessen <[email protected]>
*
* @author Ferdinand Thiessen <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
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<int, IResponse>
*/
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;
}
}
41 changes: 17 additions & 24 deletions apps/settings/lib/SetupChecks/JavaScriptModules.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
* 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,
) {
}

Expand All @@ -56,28 +58,19 @@ public function getName(): string {

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.'));

}
}
82 changes: 82 additions & 0 deletions apps/settings/lib/SetupChecks/Woff2Loading.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Ferdinand Thiessen <[email protected]>
*
* @author Ferdinand Thiessen <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Settings\SetupChecks;

use OCP\Http\Client\IClientService;
use OCP\IConfig;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\SetupCheck\ISetupCheck;
use OCP\SetupCheck\SetupResult;
use Psr\Log\LoggerInterface;

/**
* Check whether the WOFF2 URLs works
*/
class Woff2Loading implements ISetupCheck {
use CheckServerResponseTrait;

public function __construct(
protected IL10N $l10n,
protected IConfig $config,
protected IURLGenerator $urlGenerator,
protected IClientService $clientService,
protected LoggerInterface $logger,
) {
}

public function getCategory(): string {
return 'network';
}

public function getName(): string {
return $this->l10n->t('WOFF2 file loading');
}

public function run(): SetupResult {
$url = $this->urlGenerator->linkTo('', 'core/fonts/NotoSans-Regular-latin.woff2');
$noResponse = true;
$responses = $this->runHEAD($url);
foreach ($responses as $response) {
$noResponse = false;
if ($response->getStatusCode() === 200) {
return SetupResult::success();
}
}

if ($noResponse) {
return SetupResult::info(
$this->l10n->t('Could not check for WOFF2 loading support. Please check manually if your webserver serves `.woff2` files.') . "\n" . $this->serverConfigHelp(),
$this->urlGenerator->linkToDocs('admin-nginx'),
);
}
return SetupResult::warning(
$this->l10n->t('Your web server is not properly set up to deliver .woff2 files. This is typically an issue with the Nginx configuration. For Nextcloud 15 it needs an adjustement to also deliver .woff2 files. Compare your Nginx configuration to the recommended configuration in our documentation.'),
$this->urlGenerator->linkToDocs('admin-nginx'),
);

}
}
5 changes: 2 additions & 3 deletions apps/settings/src/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,9 @@ window.addEventListener('DOMContentLoaded', () => {
OC.SetupChecks.checkProviderUrl(OC.getRootPath() + '/ocs-provider/', OC.theme.docPlaceholderUrl, $('#postsetupchecks').data('check-wellknown') === true),
OC.SetupChecks.checkSetup(),
OC.SetupChecks.checkGeneric(),
OC.SetupChecks.checkWOFF2Loading(OC.filePath('core', '', 'fonts/NotoSans-Regular-latin.woff2'), OC.theme.docPlaceholderUrl),
OC.SetupChecks.checkDataProtected(),
).then((check1, check2, check3, check4, check5, check6, check7, check8, check9, check10, check11) => {
const messages = [].concat(check1, check2, check3, check4, check5, check6, check7, check8, check9, check10, check11)
).then((check1, check2, check3, check4, check5, check6, check7, check8, check9, check10) => {
const messages = [].concat(check1, check2, check3, check4, check5, check6, check7, check8, check9, check10)
const $el = $('#postsetupchecks')
$('#security-warning-state-loading').addClass('hidden')

Expand Down
34 changes: 0 additions & 34 deletions core/js/setupchecks.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,40 +136,6 @@
return deferred.promise();
},


/**
* Check whether the WOFF2 URLs works.
*
* @param url the URL to test
* @param placeholderUrl the placeholder URL - can be found at OC.theme.docPlaceholderUrl
* @return $.Deferred object resolved with an array of error messages
*/
checkWOFF2Loading: function(url, placeholderUrl) {
var deferred = $.Deferred();

var afterCall = function(xhr) {
var messages = [];
if (xhr.status !== 200) {
var docUrl = placeholderUrl.replace('PLACEHOLDER', 'admin-nginx');
messages.push({
msg: t('core', 'Your web server is not properly set up to deliver .woff2 files. This is typically an issue with the Nginx configuration. For Nextcloud 15 it needs an adjustement to also deliver .woff2 files. Compare your Nginx configuration to the recommended configuration in our {linkstart}documentation ↗{linkend}.', { docLink: docUrl, url: url })
.replace('{linkstart}', '<a target="_blank" rel="noreferrer noopener" class="external" href="' + docUrl + '">')
.replace('{linkend}', '</a>'),
type: OC.SetupChecks.MESSAGE_TYPE_WARNING
});
}
deferred.resolve(messages);
};

$.ajax({
type: 'GET',
url: url,
complete: afterCall,
allowAuthErrors: true
});
return deferred.promise();
},

/**
* Runs setup checks on the server side
*
Expand Down
27 changes: 0 additions & 27 deletions core/js/tests/specs/setupchecksSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,33 +143,6 @@ describe('OC.SetupChecks tests', function() {
});
});

describe('checkWOFF2Loading', function() {
it('should fail with another response status code than the expected one', function(done) {
var async = OC.SetupChecks.checkWOFF2Loading(OC.filePath('core', '', 'fonts/NotoSans-Regular-latin.woff2'), 'http://example.org/PLACEHOLDER');

suite.server.requests[0].respond(302);

async.done(function( data, s, x ){
expect(data).toEqual([{
msg: 'Your web server is not properly set up to deliver .woff2 files. This is typically an issue with the Nginx configuration. For Nextcloud 15 it needs an adjustement to also deliver .woff2 files. Compare your Nginx configuration to the recommended configuration in our <a target="_blank" rel="noreferrer noopener" class="external" href="http://example.org/admin-nginx">documentation ↗</a>.',
type: OC.SetupChecks.MESSAGE_TYPE_WARNING
}]);
done();
});
});

it('should return no error with the expected response status code', function(done) {
var async = OC.SetupChecks.checkWOFF2Loading(OC.filePath('core', '', 'fonts/NotoSans-Regular-latin.woff2'), 'http://example.org/PLACEHOLDER');

suite.server.requests[0].respond(200);

async.done(function( data, s, x ){
expect(data).toEqual([]);
done();
});
});
});

describe('checkDataProtected', function() {

oc_dataURL = "data";
Expand Down
Loading

0 comments on commit 19bfbe3

Please sign in to comment.