UserRolesCacheContext reports wrong user.roles

Created on 17 December 2019, about 5 years ago
Updated 1 August 2023, over 1 year ago

Problem/Motivation

When user.roles cache contexts are used for anonymous or authenticated users, the values for these contexts are used around the wrong way.
Anonymous users will have a user.roles:authenticated=1 cache context while authenticated users while have user.roles:anonymous=1 and user.roles:authenticated=0.

The code snippet below is from Drupal\Core\Cache\Context\UserRolesCacheContext. Note the final return statement where the 1 and 0 on the ternary statement are around the wrong way.

 /**
   * {@inheritdoc}
   */
  public function getContext($role = NULL) {
    // User 1 does not actually have any special behavior for roles; this is
    // added as additional security and backwards compatibility protection for
    // SA-CORE-2015-002.
    // @todo Remove in Drupal 9.0.0.
    if ($this->user->id() == 1) {
      return 'is-super-user';
    }
    if ($role === NULL) {
      return implode(',', $this->user->getRoles());
    }
    else {
      return (in_array($role, $this->user->getRoles()) ? '0' : '1');
    }
  }

Since values remain unique, this issue is largely semantic in core. However, I've been trying to integrate user.roles with Varnish which relies on the these values by correct.

Proposed resolution

Swap the return numbers, so anonymous users will have a user.roles:authenticated=0 or user.roles:anonymous=1.

πŸ› Bug report
Status

Fixed

Version

11.0 πŸ”₯

Component
CacheΒ  β†’

Last updated 3 days ago

Created by

πŸ‡³πŸ‡ΏNew Zealand Josh Waihi

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024