Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Shipped apps should include the Nextcloud version in the cache buster #48650

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2693,12 +2693,8 @@
<code><![CDATA[string]]></code>
</InvalidParamDefault>
<InvalidScalarArgument>
<code><![CDATA[$appName]]></code>
<code><![CDATA[$appName]]></code>
<code><![CDATA[$appId]]></code>
</InvalidScalarArgument>
<UndefinedInterfaceMethod>
<code><![CDATA[getInitialStates]]></code>
</UndefinedInterfaceMethod>
</file>
<file src="lib/private/URLGenerator.php">
<InvalidReturnStatement>
Expand Down
104 changes: 57 additions & 47 deletions lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,45 +31,38 @@

class TemplateLayout extends \OC_Template {
private static $versionHash = '';
/** @var string[] */
private static $cacheBusterCache = [];

/** @var CSSResourceLocator|null */
public static $cssLocator = null;

/** @var JSResourceLocator|null */
public static $jsLocator = null;

/** @var IConfig */
private $config;

/** @var IInitialStateService */
private $initialState;

/** @var INavigationManager */
private $navigationManager;
private IConfig $config;
private IAppManager $appManager;
private InitialStateService $initialState;
private INavigationManager $navigationManager;

/**
* @param string $renderAs
* @param string $appId application id
*/
public function __construct($renderAs, $appId = '') {
/** @var IConfig */
$this->config = \OC::$server->get(IConfig::class);

/** @var IInitialStateService */
$this->initialState = \OC::$server->get(IInitialStateService::class);
$this->config = \OCP\Server::get(IConfig::class);
$this->appManager = \OCP\Server::get(IAppManager::class);
$this->initialState = \OCP\Server::get(InitialStateService::class);
$this->navigationManager = \OCP\Server::get(INavigationManager::class);

// Add fallback theming variables if theming is disabled
if ($renderAs !== TemplateResponse::RENDER_AS_USER
|| !\OC::$server->getAppManager()->isEnabledForUser('theming')) {
// Add fallback theming variables if not rendered as user
if ($renderAs !== TemplateResponse::RENDER_AS_USER) {
// TODO cache generated default theme if enabled for fallback if server is erroring ?
Util::addStyle('theming', 'default');
}

// Decide which page we show
if ($renderAs === TemplateResponse::RENDER_AS_USER) {
/** @var INavigationManager */
$this->navigationManager = \OC::$server->get(INavigationManager::class);

parent::__construct('core', 'layout.user');
if (in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
$this->assign('bodyid', 'body-settings');
Expand All @@ -90,7 +83,7 @@ public function __construct($renderAs, $appId = '') {
}
// Set body data-theme
$this->assign('enabledThemes', []);
if (\OC::$server->getAppManager()->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
if ($this->appManager->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
/** @var \OCA\Theming\Service\ThemesService */
$themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
$this->assign('enabledThemes', $themesService->getEnabledThemes());
Expand All @@ -101,8 +94,8 @@ public function __construct($renderAs, $appId = '') {
$this->assign('logoUrl', $logoUrl);

// Set default entry name
$defaultEntryId = \OCP\Server::get(INavigationManager::class)->getDefaultEntryIdForUser();
$defaultEntry = \OCP\Server::get(INavigationManager::class)->get($defaultEntryId);
$defaultEntryId = $this->navigationManager->getDefaultEntryIdForUser();
$defaultEntry = $this->navigationManager->get($defaultEntryId);
$this->assign('defaultAppName', $defaultEntry['name']);

// Add navigation entry
Expand Down Expand Up @@ -182,8 +175,7 @@ public function __construct($renderAs, $appId = '') {
$showSimpleSignup = true;
}

$appManager = \OCP\Server::get(IAppManager::class);
if ($appManager->isEnabledForUser('registration')) {
if ($this->appManager->isEnabledForUser('registration')) {
$urlGenerator = \OCP\Server::get(IURLGenerator::class);
$signUpLink = $urlGenerator->getAbsoluteURL('/index.php/apps/registration/');
}
Expand All @@ -203,7 +195,7 @@ public function __construct($renderAs, $appId = '') {
$this->assign('locale', $locale);
$this->assign('direction', $direction);

if (\OC::$server->getSystemConfig()->getValue('installed', false)) {
if ($this->config->getSystemValueBool('installed', false)) {
if (empty(self::$versionHash)) {
$v = \OC_App::getAppVersions();
$v['core'] = implode('.', \OCP\Util::getVersion());
Expand All @@ -224,7 +216,7 @@ public function __construct($renderAs, $appId = '') {
\OCP\Server::get(ServerVersion::class),
\OCP\Util::getL10N('lib'),
\OCP\Server::get(Defaults::class),
\OC::$server->getAppManager(),
$this->appManager,
\OC::$server->getSession(),
\OC::$server->getUserSession()->getUser(),
$this->config,
Expand Down Expand Up @@ -314,34 +306,52 @@ protected function getVersionHashSuffix($path = false, $file = false) {
// allows chrome workspace mapping in debug mode
return '';
}
$themingSuffix = '';
$v = [];

if ($this->config->getSystemValueBool('installed', false)) {
if (\OC::$server->getAppManager()->isInstalled('theming')) {
$themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
}
$v = \OC_App::getAppVersions();
if ($this->config->getSystemValueBool('installed', false) === false) {
// if not installed just return the version hash
return '?v=' . self::$versionHash;
}

// Try the webroot path for a match
if ($path !== false && $path !== '') {
$appName = $this->getAppNamefromPath($path);
if (array_key_exists($appName, $v)) {
$appVersion = $v[$appName];
return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
}
$hash = false;
// Try the web-root first
if (is_string($path) && $path !== '') {
$hash = $this->getVersionHashByPath($path);
}
// If not found try the file
if ($hash === false && is_string($file) && $file !== '') {
$hash = $this->getVersionHashByPath($file);
}
// fallback to the file path instead
if ($file !== false && $file !== '') {
$appName = $this->getAppNamefromPath($file);
if (array_key_exists($appName, $v)) {
$appVersion = $v[$appName];
return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
// As a last resort we use the server version hash
if ($hash === false) {
$hash = self::$versionHash;
}

// The theming app is force-enabled thus the cache buster is always available
$themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');

return '?v=' . $hash . $themingSuffix;
}

private function getVersionHashByPath(string $path): string|false {
if (array_key_exists($path, self::$cacheBusterCache) === false) {
// Not yet cached, so lets find the cache buster string
$appId = $this->getAppNamefromPath($path);
if ($appId === false || $appId === null) {
// No app Id could be guessed
return false;
}

$appVersion = $this->appManager->getAppVersion($appId);
// For shipped apps the app version is not a single source of truth, we rather also need to consider the Nextcloud version
if ($this->appManager->isShipped($appId)) {
$appVersion .= '-' . self::$versionHash;
}

$hash = substr(md5($appVersion), 0, 8);
self::$cacheBusterCache[$path] = $hash;
}

return '?v=' . self::$versionHash . $themingSuffix;
return self::$cacheBusterCache[$path];
}

/**
Expand Down
84 changes: 84 additions & 0 deletions tests/lib/TemplateLayoutTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace Test;

use OC\InitialStateService;
use OC\TemplateLayout;
use OCP\App\IAppManager;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\IConfig;

class TemplateLayoutTest extends \Test\TestCase {


/** @dataProvider dataVersionHash */
public function testVersionHash($path, $file, $installed, $debug, $expected): void {
$appManager = $this->createMock(IAppManager::class);
$appManager->expects(self::any())
->method('getAppVersion')
->willReturnCallback(fn ($appId) => match ($appId) {
'shippedApp' => 'shipped_1',
'otherApp' => 'other_2',
default => "$appId",
});
$appManager->expects(self::any())
->method('isShipped')
->willReturnCallback(fn (string $app) => $app === 'shippedApp');

$config = $this->createMock(IConfig::class);
$config->expects(self::atLeastOnce())
->method('getSystemValueBool')
->willReturnMap([
['installed', false, $installed],
['debug', false, $debug],
]);
$config->expects(self::any())
->method('getAppValue')
->with('theming', 'cachebuster', '0')
->willReturn('42');

$initialState = $this->createMock(InitialStateService::class);

$this->overwriteService(IConfig::class, $config);
$this->overwriteService(IAppManager::class, $appManager);
$this->overwriteService(InitialStateService::class, $initialState);

$layout = $this->getMockBuilder(TemplateLayout::class)
->onlyMethods(['getAppNamefromPath'])
->setConstructorArgs([TemplateResponse::RENDER_AS_ERROR])
->getMock();

self::invokePrivate(TemplateLayout::class, 'versionHash', ['version_hash']);

$layout->expects(self::any())
->method('getAppNamefromPath')
->willReturnCallback(fn ($appName) => match($appName) {
'apps/shipped' => 'shippedApp',
'other/app.css' => 'otherApp',
default => false,
});

$hash = self::invokePrivate($layout, 'getVersionHashSuffix', [$path, $file]);
self::assertEquals($expected, $hash);
}

public static function dataVersionHash() {
return [
'no hash if in debug mode' => ['apps/shipped', 'style.css', true, true, ''],
'only version hash if not installed' => ['apps/shipped', 'style.css', false, false, '?v=version_hash'],
'version hash with cache buster if app not found' => ['unknown/path', '', true, false, '?v=version_hash-42'],
'version hash with cache buster if neither path nor file provided' => [false, false, true, false, '?v=version_hash-42'],
'app version hash if external app' => ['', 'other/app.css', true, false, '?v=' . substr(md5('other_2'), 0, 8) . '-42'],
'app version and version hash if shipped app' => ['apps/shipped', 'style.css', true, false, '?v=' . substr(md5('shipped_1-version_hash'), 0, 8) . '-42'],
'prefer path over file' => ['apps/shipped', 'other/app.css', true, false, '?v=' . substr(md5('shipped_1-version_hash'), 0, 8) . '-42'],
];
}

}
Loading