diff --git a/core/lib/Drupal/Core/Cache/DatabaseBackend.php b/core/lib/Drupal/Core/Cache/DatabaseBackend.php index 956662df40f..28da6cd7bf9 100644 --- a/core/lib/Drupal/Core/Cache/DatabaseBackend.php +++ b/core/lib/Drupal/Core/Cache/DatabaseBackend.php @@ -482,8 +482,11 @@ class DatabaseBackend implements CacheBackendInterface { */ protected function normalizeCid($cid) { // Nothing to do if the ID is a US ASCII string of 255 characters or less. + // Additionally check for trailing spaces in the cache ID because MySQL + // may or may not take these into account when making comparisons. + // @see https://dev.mysql.com/doc/refman/9.0/en/char.html $cid_is_ascii = mb_check_encoding($cid, 'ASCII'); - if (strlen($cid) <= 255 && $cid_is_ascii) { + if (strlen($cid) <= 255 && $cid_is_ascii && !str_ends_with($cid, ' ')) { return $cid; } // Return a string that uses as much as possible of the original cache ID diff --git a/core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php b/core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php index 4c970cf5b19..f30df8b054c 100644 --- a/core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php +++ b/core/tests/Drupal/KernelTests/Core/Cache/GenericCacheBackendUnitTestBase.php @@ -226,6 +226,13 @@ abstract class GenericCacheBackendUnitTestBase extends KernelTestBase { $this->assertEquals('value', $backend->get('TEST8')->data); $this->assertFalse($backend->get('test8')); + // Test a cid with and without a trailing space is treated as two different + // IDs. + $cid_nospace = 'trailing-space-test'; + $backend->set($cid_nospace, $cid_nospace); + $this->assertSame($cid_nospace, $backend->get($cid_nospace)->data); + $this->assertFalse($backend->get($cid_nospace . ' ')); + // Calling ::set() with invalid cache tags. This should fail an assertion. try { $backend->set('assertion_test', 'value', Cache::PERMANENT, ['node' => [3, 5, 7]]);