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.

CommentFileSizeAuthor
#3 3101664-3.patch594 bytesmatroskeen

Issue fork drupal-3101664

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Josh Waihi created an issue. See original summary.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

matroskeen’s picture

Version: 8.9.x-dev » 9.3.x-dev
Status: Active » Needs review
Issue tags: +Bug Smash Initiative
StatusFileSize
new594 bytes

I noticed this issue as a part of Bug Smash Initiative. My understanding is the same as it states in the description, so I'm curious to see what it breaks.

matroskeen’s picture

Issue summary: View changes
wim leers’s picture

Status: Needs review » Needs work

Woah! 😅 😂 The painful thing here is that fixing this will in fact lead to cache pollution and potentially information disclosure … 😱

So can we instead of using 0 and 1 use false and true? Otherwise we need to be absolutely certain that the cache gets wiped and locked while the code gets deployed.

P.S.: why Major? While confusing, this is not causing bugs AFAICT, it's purely cosmetic?

matroskeen’s picture

Priority: Major » Minor

Totally agree about the Priority, I think "Minor" is fair enough.

Otherwise we need to be absolutely certain that the cache gets wiped and locked while the code gets deployed.

I assume empty hook_post_update_NAME() won't give us this level of certainty to apply this cosmetic change, will it?

I'll try to research and find similar issues that were fixed in the past to see what was the strategy :)

matroskeen’s picture

I reviewed existing cache contexts and I didn't find similar changes in the commit history, so I'm not sure if there is any right way to be "absolutely certain that the cache gets wiped".

I also didn't find cases when the return value has true/false.

Given this is just a cosmetic change that might potentially lead to information disclosure, maybe it's a "won't fix"?

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

claudiu.cristea made their first commit to this issue’s fork.

claudiu.cristea’s picture

Priority: Minor » Normal
Status: Needs work » Needs review

@Matroskeen

Given this is just a cosmetic change that might potentially lead to information disclosure, maybe it's a "won't fix"?

The information disclosure might come in the case the cache is NOT wiped when this change will deployed on a site. Suddenly, all cached content for authenticated users will be exposed to anonymous and vice versa. Using a something different than 0 and 1 will prevent that and also there's no need for a "clear cache" post-update function.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative, +Needs tests

This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request as a guide.

Tagging for test case to show this issue.

Did not test patch.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bvoynick made their first commit to this issue’s fork.

bvoynick’s picture

I've rebased claudiu.cristea's MR (in a separate -11.x branch) and added a test.

If the MR can be changed to point to that branch, I think this would be ready for review.

bvoynick’s picture

Status: Needs work » Needs review

I didn't read docs closely enough, reusing an MR for a different branch is not possible.

Opened new MR #4170 for the -11.x branch.

claudiu.cristea’s picture

Status: Needs review » Needs work
Issue tags: -Needs tests

Added some remarks in MR. Tests were added, removing the tag

bvoynick’s picture

Status: Needs work » Needs review

Thanks, issues with MR 4170 should be addressed and ready for re-review.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems like a simple change

Verified the tests fail without the fix with this output
Failed asserting that two strings are identical.
Expected :'true'
Actual :'0'

Which is good.

LGTM

  • catch committed 2231e592 on 11.x
    Issue #3101664 by bvoynick, claudiu.cristea, Matroskeen, Josh Waihi,...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed 2231e59 and pushed to 11.x. Thanks!

Thought about backporting to 10.1.x but if someone was actually calling this directly somewhere and relying on the result (seems unlikely) it could cause issues, so leaving for 10.2.x for now.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.