Problem/Motivation
In #2304969: Port private files access bypass from SA-CORE-2014-003, a new entity constraint was added to File and Image field types which ensures that a user can only add a reference to a file/image that they have access to.
This constraint is only supposed to verify access of the referenced file if it's a new/changed reference, meaning the user changed the reference from what it was to something else. However there is a bug in this code that forces the check to occur every time even if the reference didn't change.
I came across this bug when trying to add custom role-based view access control on media entities with private files:
- Create a media entity "Private Document" that has a private file schema
- Add a hook_media_access implementation that forbids "view" operation on entities of that media bundle to anyone without special role
- Add a new "Private Document" entity and upload a file and save
- Now go to edit the same entity, and try saving again. You get an error "You do not have access to the referenced entity (%type: %id)."
The problem occurs because the ReferenceAccessConstraint is now checking to ensure that the user has "view" access to the referenced private file on the file field. Well, since access of private file fields is delegated to the entity that references it, access is denied because the user that's editing the entity doesn't have that special role from step 2 above.
Proposed resolution
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-3009976
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
bkosborneSo the bug is that the code assumed that
$existing_entity->{$value->getFieldDefinition()->getName()}->referencedEntities();returned an array of referenced file entities indexed by the entity ID, but that's not true. It retursn an array of references indexed by the delta. This fixes it so that it's comparing the IDs properly.Comment #5
bkosborneRe-rolled for Drupal 8.8.x
Comment #7
dmouseThis patch work for me
Comment #8
sahal_va commentedI had similar issue while editing a custom entity with a file upload field.
Though the file entity was uploaded by the user, he wasn't able to edit the entity due to referenced file validation error.
This patch solved the issue.
Comment #9
jungle@bkosborne, thanks for filing this and working on it.
But bugs need a test, in general.
Comment #10
jungleApplying IS template
Comment #11
larowlanComment #18
bkosborneWhy is this unpublished?
Comment #20
bkosborneStill NW for tests I guess, but I created an MR at least.
Comment #21
larowlan