Issue #3368537 by catch, TwoD, alexpott, cmlara, sboden: Ensure trailing whitespace at the end of a cache ID results in a unique cache item

merge-requests/6699/merge
Lee Rowlands 2024-08-03 09:56:10 +10:00
parent 5702b1893c
commit fb2469d334
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
2 changed files with 11 additions and 1 deletions

View File

@ -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

View File

@ -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]]);