Skip to content

Commit

Permalink
[BUGFIX] Avoid empty cache identifiers
Browse files Browse the repository at this point in the history
Currently it is possible to add an invalid cache configuration with
empty identifiers. This change avoids this behavior by checking the
indentifiers while changing the cache configuration at the
CacheManager.

Resolves: #91959
Resolves: #91773
Releases: master, 10.4
Change-Id: Icfb25ab52418da166582dba4bca389250325097b
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/65229
Tested-by: Christian Kuhn <lolli@schwarzbu.ch>
Tested-by: TYPO3com <noreply@typo3.com>
Tested-by: Daniel Haupt <mail@danielhaupt.de>
Tested-by: Benni Mack <benni@typo3.org>
Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch>
Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de>
Reviewed-by: Daniel Haupt <mail@danielhaupt.de>
Reviewed-by: Benni Mack <benni@typo3.org>
  • Loading branch information
gilbertsoft authored and bmack committed Aug 10, 2020
1 parent 84cbe10 commit 688425e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
8 changes: 7 additions & 1 deletion typo3/sysext/core/Classes/Cache/CacheManager.php
Expand Up @@ -96,13 +96,19 @@ public function setCacheConfigurations(array $cacheConfigurations)
$newConfiguration = [];
$migratedConfiguration = [];
foreach ($cacheConfigurations as $identifier => $configuration) {
if (empty($identifier)) {
throw new \InvalidArgumentException('A cache identifier was not set.', 1596980032);
}
if (!is_array($configuration)) {
throw new \InvalidArgumentException('The cache configuration for cache "' . $identifier . '" was not an array as expected.', 1231259656);
}
// Fallback layer, will be removed in TYPO3 v11.0.
if (strpos($identifier, 'cache_') === 0) {
trigger_error('Accessing a cache with the "cache_" prefix as in "' . $identifier . '" is not necessary anymore, and should be called without the cache prefix.', E_USER_DEPRECATED);
$identifier = substr($identifier, 6);
if (empty($identifier)) {
throw new \InvalidArgumentException('A cache identifier was not set.', 1596980033);
}
trigger_error('Accessing a cache with the "cache_" prefix as in "' . $identifier . '" is not necessary anymore, and should be called without the cache prefix.', E_USER_DEPRECATED);
$migratedConfiguration[$identifier] = $configuration;
} else {
$newConfiguration[$identifier] = $configuration;
Expand Down
44 changes: 44 additions & 0 deletions typo3/sysext/core/Tests/Unit/Cache/CacheManagerTest.php
Expand Up @@ -537,4 +537,48 @@ public function setCacheConfigurationsMergesLegacyConfigCorrectly()
$manager->setCacheConfigurations($rawConfiguration);
self::assertEquals($expectedConfiguration, $manager->_get('cacheConfigurations'));
}

/**
* @test
*/
public function setCacheConfigurationsThrowsExceptionIfConfiguredCacheDoesNotHaveAnIdentifier()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1596980032);

/** @var \PHPUnit\Framework\MockObject\MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|CacheManager $manager */
$manager = $this->getAccessibleMock(CacheManager::class, ['dummy']);
$manager->setCacheConfigurations([
'' => [
'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class,
'backend' => \TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend::class,
'options' => [
'compression' => true,
],
'groups' => ['pages'],
]
]);
}

/**
* @test
*/
public function setCacheConfigurationsThrowsExceptionIfMigratedConfiguredCacheDoesNotHaveAnIdentifier()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionCode(1596980033);

/** @var \PHPUnit\Framework\MockObject\MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface|CacheManager $manager */
$manager = $this->getAccessibleMock(CacheManager::class, ['dummy']);
$manager->setCacheConfigurations([
'cache_' => [
'frontend' => \TYPO3\CMS\Core\Cache\Frontend\VariableFrontend::class,
'backend' => \TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend::class,
'options' => [
'compression' => true,
],
'groups' => ['pages'],
]
]);
}
}

0 comments on commit 688425e

Please sign in to comment.