Problem/Motivation

While working on #2889855: Unpublished media entity can not be accessed by owner and update any media/delete any media access possibly cached by user we've noticed a couple of bugs in MediaAccessControlHandler regarding the cache for delete/update access.

  • 'edit own type media' permissions is checked and returned before 'update any media' permission, which causes cache per user when it's not needed
  • 'delete own type media' permissions is checked and returned before 'delete any media' permission, which causes cache per user when it's not needed
  • 'edit any type media' does not have the entity as cacheable dependency
  • 'delete any type media' does not have the entity as cacheable dependency

We also have issues when returning a neutral result - we need to be caching per user. We can't just cache per permissions when we return a neutral response as this would cache incorrectly for the use case where a user is denied access, then another user is meant to be granted access based on the fact they are the owner but they have identical permissions (see #9)

Steps to reproduce: Update any media cached by user

1. Create a roll with 'edit own $type media' and 'update any media' permission
2. Create a user and assign the role created in step 1.
3. Create a media item of $type
4. Login as the user from step 2
5. Try to edit the media item of step 3

Expected:
The access result is cached per permission.

Actual:
The access result is cached per user.

Steps to reproduce: Delete any media cached by user

1. Create a roll with 'delete own $type media' and 'delete any media' permission
2. Create a user and assign the role created in step 1.
3. Create a media item of $type
4. Login as the user from step 2
5. Try to delete the media item of step 3

Expected:
The access result is cached per permission.

Actual:
The access result is cached per user.

Steps to reproduce: Neutral result needs to be cached per user

1. Create a roll with 'delete own $type media'
2. Create 2 users and assign the role created in step 1.
3. Create a media item of $type and make user A the owner
4. Login as the user B from step 2
5. Try to delete the media item of step 3 and get access denied
6. Login as user A from step 2
7. Try to delete media item

Expected:
Access is granted.

Actual:
Access is not granted because previous result was cached only by permissions.

Proposed resolution

Fix update/delete access cache issues and add extensive tests for it.

  • Make sure the generic 'update any media' / 'edit any $type media' permissions are checked before the user specific 'update own media' / 'edit own $type media'.
  • Make sure the generic 'delete any media' / 'delete any $type media' permissions are checked before the user specific 'delete own media' / 'delete own $type media'.

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork drupal-2998824

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

seanB created an issue. See original summary.

seanb’s picture

Status: Active » Postponed
StatusFileSize
new37.04 KB

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

wim leers’s picture

seanb’s picture

Status: Needs work » Needs review
StatusFileSize
new37.04 KB

Rerolled.

wim leers’s picture

+++ b/core/modules/media/src/MediaAccessControlHandler.php
@@ -50,41 +50,63 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
+        return AccessResult::neutral("The following permissions are required: 'update any media' OR 'update own media' OR '$type: edit any media' OR '$type: edit own media'.")
+          ->cachePerPermissions();
...
+        return AccessResult::neutral("The following permissions are required: 'delete any media' OR 'delete own media' OR '$type: delete any media' OR '$type: delete own media'.")
+          ->cachePerPermissions();

This is missing addCacheableDependency($entity). It's easy to see that this is necessary because of the presence of $type in the reason string.

I'll review the test coverage tomorrow.

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

acbramley’s picture

This is missing addCacheableDependency($entity)

We're actually also missing the user context here and in other places.

We can't just cache per permissions when we return a neutral response as this would cache incorrectly for the use case where a user is denied access, then another user is meant to be granted access based on the fact they are the owner but they have identical permissions.

EDIT: Just to expand on the use case - I'm seeing this exact problem with media operations on the media overview view. Cacheability is broken for operations as per #2473873: Entity operations lack cacheability support, resulting in incorrect dropbuttons but with my latest patch on that issue it is somewhat fixed. However, since the access denied cacheability isn't varying by user currently, operations were getting cached when a user hit the page that didn't have access to edit (i.e they weren't the owner) then a following user that was the author wasn't seeing any operations links.

Adding related issues that add similar access controls to other modules, along with the operations caching issue which surfaced this bug for me.

Status: Needs review » Needs work

The last submitted patch, 9: 2998824-9.patch, failed testing. View results

acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new46.91 KB
new1.28 KB

This fixes a couple of the failures. Something is adding the user context on view even when the media entity is published which is causing the remainder of the failures. I have no idea what's causing this, testing locally even removing the cachePerUser() calls from the view operation wasn't fixing it.

EDIT: Just spitballing, but I'm wondering if the user context is coming from the denied edit/delete access based off the local tasks/links that would appear on the page for someone that had access?

Status: Needs review » Needs work

The last submitted patch, 11: 2998824-11.patch, failed testing. View results

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

acbramley’s picture

Looking into the test fails in #11 and the Drupal\Tests\jsonapi\Functional\MediaTest::testRevisions failure has me stumped.

Updating getExpectedUnauthorizedAccessCacheability to expect the user cache context is the right fix but that then fails with the inverse of what is failing now:

1) Drupal\Tests\jsonapi\Functional\MediaTest::testRevisions
Failed asserting that two arrays are identical.
--- Expected
+++ Actual
@@ @@
 Array &0 (
     0 => 'url.query_args:resourceVersion'
     1 => 'url.site'
-    2 => 'user.permissions'
+    2 => 'user'
 )

Very odd...

phenaproxima’s picture

Issue tags: +Bug Smash Initiative
phenaproxima’s picture

Priority: Normal » Major
Issue tags: +Triaged Media Initiative issue

I'd call this a major bug, bumping priority accordingly.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kim.pepper’s picture

Issue tags: +#pnx-sprint

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.

acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new44.28 KB

Looks like a good few of the changes from #11 were already committed in other issues. Let's see what fails now. Rerolled against 9.4.x

Status: Needs review » Needs work

The last submitted patch, 21: 2998824-21.patch, failed testing. View results

acbramley’s picture

Assigned: seanb » acbramley
Issue summary: View changes

Updated the IS to explain the issues in #9 - new green patch to follow :)

acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new46.38 KB
new4.89 KB
  1. Fixes missing cachePerUser on neutral result for view operation
  2. Fixes missed changes in re-roll when reordering update/delete any checks to the top
  3. Adds test coverage for 2 as this had obviously been missed since those specific things would have failed in #21
  4. Consolidates the "own" permission checks into 1 if statement

Status: Needs review » Needs work

The last submitted patch, 24: 2998824-24.patch, failed testing. View results

acbramley’s picture

Status: Needs work » Needs review

The remaining fails are tricky.

The jsonapi one for me is really hard to figure out how to fix. The problem is that we require the user cache context in some unauthorized scenarios and some we don't so we can't just blanket add it to getExpectedUnauthorizedAccessCacheability. I need to spend more time in ResourceTestBase::testRevisions to figure out exactly what happens before line 3085 and 2892 to figure out what conditions to check.

As for the MediaAccessTest, I think the issue is what I explained in #11 where the user cache context is being added by something else on the page (e.g an edit tab)

acbramley’s picture

Status: Needs review » Needs work
acbramley’s picture

Status: Needs work » Needs review
StatusFileSize
new48.42 KB
new2.19 KB

Confirmed my suspicions in #26 about the MediaAccessTest, we need user context in most places now.

Status: Needs review » Needs work

The last submitted patch, 28: 2998824-28.patch, failed testing. View results

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.

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.

acbramley’s picture

Assigned: acbramley » Unassigned

acbramley’s picture

Rebased onto an MR and attempted to fix the MediaTest::testRevisions failure. I got us past the cache context failure but now since the response is uncacheable due to the user context that bit fails... these tests extending ResourceTestBase are really tough to work with....

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.

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

mstrelan’s picture

Re-rolled MR for 11.x, we should probably close the old one and hide patches.

That data provider is massive, and even more so since #2911977: Add Media revision UI.

acbramley changed the visibility of the branch 2998824-mediaaccesscontrolhandler-updatedelete-access to hidden.

acbramley’s picture

acbramley’s picture

Rebased against 11.x and attempted yet again to fix the cache context issues.

We have even more issues now that we have the checkFieldAccess in EntityAccessControlerHandler checking the update operation which can randomly add the user context in too. I honestly can't see how these tests are fixable without a massive refactor to ResourceTestBase.

acbramley changed the visibility of the branch 11.x to hidden.

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

mxr576’s picture

Copy-pasting from Drupal Slack:

mxr576
Today at 12:49 PM
@acbramley
Maybe you will find some inspiration in my MR for fixing these test failures. testRevisions() killed my nerves so many times...
https://git.drupalcode.org/project/drupal/-/merge_requests/8198

mxr576
Just now
Pushed those changes from the other MR to yours, you can decide if you keep those or not. If we need these here as well either my PR should be merged first or these changes on ResourceTestBase has to be merged independently.
Test went far further now (previously it failed on core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php:2930), but it is still going to fail on something that should be work...
I also had to commit two changes in a dedicated commit over the ported changes and I would like to understand why those were necessary.
Generally speaking, the changes on core/modules/media/src/MediaAccessControlHandler.php because that makes many things uncacheable by adding the user context.
The ported changes on ResourceTestBase only warrants that assertions are aligned with expectations and not hardcoded on the header value.

mxr576’s picture

Issue summary: View changes
mxr576’s picture

Clean up GIT history in the MR, now we just need to figure out why the remaining tests fail...

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.