Verified Commit 71b0922a authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #3368537 by catch, TwoD, alexpott, cmlara, sboden: Ensure trailing...

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

(cherry picked from commit fb2469d3)
parent 357d207d
Loading
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -482,8 +482,11 @@ protected function catchException(\Exception $e, $table_name = NULL) {
   */
  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
+7 −0
Original line number Diff line number Diff line
@@ -226,6 +226,13 @@ public function testSetGet(): void {
    $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]]);