ReferenceAccessConstraintValidator check is broken for existing entities

Created on 4 April 2019, over 6 years ago
Updated 6 September 2025, 7 days ago

Problem/Motivation

Users get violation errors when trying to add add a file to a field which contains existing files they do not have access to. This could be the case with anonymous users, as they may only have access to files they have uploaded themselves.

Details

\Drupal\Core\Entity\Plugin\Validation\Constraint\ReferenceAccessConstraintValidator::validate is supposed to check whether you have view access to entities being added to an entity reference and is used by ImageItem and FileItem.

The logic is that existing references are skipped, but new references have their access checked. However, I believe there are two bugs in the code:

  1. The code assumes $referenced_entities is keyed by entity ID, but it is actually keyed by delta
  2. The code checks whether the existing referenced entity is in the array of existing referenced entities, when it should check whether the value being validated is in the array

The combination of both of these bugs means I do not believe this is a security risk, as it will only affect validating existing references, which this check is supposed to ignore entirely.

Specifically the bugs are in:

        $referenced_entities = $existing_entity->{$value->getFieldDefinition()->getName()}->referencedEntities();
        // Check permission if we are not already referencing the entity.
        foreach ($referenced_entities as $ref) {
          if (isset($referenced_entities[$ref->id()])) {

$referenced_entities is keyed by delta, not by entity ID, so checking $referenced_entities[$ref->id()] is the wrong thing to do here.

We are supposed to be checking whether $id is in the list (the item we're validating). However, we're checking whether $ref->id() is in the list, which (aside from the above bug), would always be the case, as we've retrieved it from the list one line above.

πŸ› Bug report
Status

Needs work

Version

11.0 πŸ”₯

Component

typed data system

Created by

πŸ‡¬πŸ‡§United Kingdom andrewbelcher

Live updates comments and jobs are added and updated live.
  • Novice

    It would make a good project for someone who is new to the Drupal contribution process. It's preferred over Newbie.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

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