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
- Wait until #3473374: Improve Dynamic Page Cache header assertions in JSON:API tests gets merged that improves JSONAPI tests reliability, or try to solve this issue without it. Currently, latest changes from that issue are cherry picked to MR#5697.
User interface changes
API changes
Data model changes
| Comment | File | Size | Author |
|---|
Issue fork drupal-2998824
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
Comment #2
seanbThis patch is rolled on top of #2889855-132: Unpublished media entity can not be accessed by owner and update any media/delete any media access possibly cached by user. Let's trigger the testbot when that one is in.
Comment #3
seanbComment #5
wim leers#2889855: Unpublished media entity can not be accessed by owner and update any media/delete any media access possibly cached by user landed, this is now unblocked.
Comment #6
seanbRerolled.
Comment #7
wim leersThis is missing
addCacheableDependency($entity). It's easy to see that this is necessary because of the presence of$typein the reason string.I'll review the test coverage tomorrow.
Comment #9
acbramley commentedWe'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.
Comment #11
acbramley commentedThis 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?
Comment #14
acbramley commentedLooking into the test fails in #11 and the
Drupal\Tests\jsonapi\Functional\MediaTest::testRevisionsfailure has me stumped.Updating
getExpectedUnauthorizedAccessCacheabilityto expect the user cache context is the right fix but that then fails with the inverse of what is failing now:Very odd...
Comment #15
phenaproximaComment #16
phenaproximaI'd call this a major bug, bumping priority accordingly.
Comment #19
kim.pepperComment #21
acbramley commentedLooks 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
Comment #23
acbramley commentedUpdated the IS to explain the issues in #9 - new green patch to follow :)
Comment #24
acbramley commentedComment #26
acbramley commentedThe 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)
Comment #27
acbramley commentedComment #28
acbramley commentedConfirmed my suspicions in #26 about the MediaAccessTest, we need user context in most places now.
Comment #32
acbramley commentedComment #34
acbramley commentedRebased 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....
Comment #38
mstrelan commentedRe-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.
Comment #40
acbramley commentedComment #41
acbramley commentedRebased 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.
Comment #44
mxr576Copy-pasting from Drupal Slack:
Comment #45
mxr576Comment #46
mxr576Clean up GIT history in the MR, now we just need to figure out why the remaining tests fail...