avoid fatal error when group argument to GroupAccessResult:: allowedIfHasGroupPermissions() is NULL

Created on 24 January 2023, over 2 years ago

Problem/Motivation

We're seeing fatal errors when reindexing our groups after having deleted some groups.

ResponseText: TypeError: Drupal\group\Access\GroupAccessResult::
allowedIfHasGroupPermissions(): Argument #1 ($group) must be of type 
Drupal\group\Entity\GroupInterface, null given, called in 
modules/contrib/group/src/Plugin/GroupContentAccessControlHandler.php on line 145 in 
Drupal\group\Access\GroupAccessResult::allowedIfHasGroupPermissions() 
(line 57 of modules/contrib/group/src/Access/GroupAccessResult.php).

Proposed resolution

Avoid calling allowedIfHasGroupPermissions() when the $group_content->getGroup() returns NULL.

Remaining tasks

User interface changes

API changes

Data model changes

πŸ› Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡ͺπŸ‡¨Ecuador jwilson3

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @jwilson3
  • πŸ‡ͺπŸ‡¨Ecuador jwilson3
    +++ b/src/Plugin/GroupContentAccessControlHandler.php
    @@ -142,9 +142,11 @@ class GroupContentAccessControlHandler extends GroupContentHandlerBase implement
    -      $result = GroupAccessResult::allowedIfHasGroupPermissions($group_content->getGroup(), $account, $permissions, 'OR');
    -      if ($result->isAllowed()) {
    -        break;
    +      if ($group = $group_content->getGroup()) {
    +        $result = GroupAccessResult::allowedIfHasGroupPermissions($group, $account, $permissions, 'OR');
    +        if ($result->isAllowed()) {
    

    This needed additional work because moving the $result assignment inside a conditional can cause the variable to never be created as an AccessResult object, which is expected later on in the code.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 5.7
    last update over 1 year ago
    5,149 pass
  • First commit to issue fork.
  • πŸ‡³πŸ‡±Netherlands megachriz

    I created a MR for the 3.2.x version. I do am worried by skipping an access check here, we may open a security hole.
    As an admin user, I did check if I could access group content that no longer belongs to a group, and that's not the case (view or edit gives access denied on them).

  • πŸ‡³πŸ‡±Netherlands megachriz

    I moving this to 3.2.x in an attempt to let tests run on the MR.

  • πŸ‡¨πŸ‡¦Canada joseph.olstad

    The merge request patch fixes the issue and applies cleanly to group version 3.2.2.

  • πŸ‡ΊπŸ‡ΈUnited States scotwith1t Birmingham, AL

    I am running into this just upgrading from 3.2 to 3.3

  • πŸ‡§πŸ‡ͺBelgium kristiaanvandeneynde Antwerp, Belgium

    I could never accept a patch that adds if ($group = $group_relationship->getGroup()) { to the code base. Group relationships coming from the DB must have a group and target entity. If they don't, your site is seriously broken.

  • πŸ‡³πŸ‡±Netherlands megachriz

    @kristiaanvandeneynde
    This error occurs when the group is deleted, but the group content still exists. Perhaps it would be better to deny access to the content if the group can no longer be found?

  • πŸ‡³πŸ‡±Netherlands megachriz

    An other possibility is to throw an exception in GroupRelationship::getGroup() in case the group entity could not be loaded. That would be more descriptive than the TypeError that we get now.

    Something like this:

    /**
     * {@inheritdoc}
     */
    public function getGroup(): GroupInterface {
      $group = $this->get('gid')->entity;
      if (!$group instanceof GroupInterface) {
        throw new \RuntimeException(sprintf('Group %d could not be loaded.', $this->get('gid')));
      }
      return $group;
    }
    
  • πŸ‡§πŸ‡ͺBelgium kristiaanvandeneynde Antwerp, Belgium

    This error occurs when the group is deleted, but the group content still exists.

    But that should never happen. See Group::preDelete().

    Something is wrong with your website if deleting a group leaves orphaned relationships. It could be a bug in Group, but judging by the method I just mentioned above, it probably isn't.

  • πŸ‡§πŸ‡ͺBelgium kristiaanvandeneynde Antwerp, Belgium

    I'm going to err on the side of this being a local issue. Without steps to reproduce on a clean install there is nothing I can do here. Please feel free to reopen if you have steps to reproduce.

  • πŸ‡³πŸ‡±Netherlands megachriz

    I see: group relationships should get cleaned up upon group removal. Perhaps it is caused by a local issue then. Or maybe I accidentally patched the wrong file? Because I see that my MR is different from the patch in #3.
    Good call on requesting the steps to reproduce the issue first.

  • πŸ‡©πŸ‡ͺGermany mxh Offenburg

    Experiencing the same that the database table still contains group relationship items while the according group does not exist anymore.

    We're having lots of concurrent requests going on, and one thing I could imagine is that the deletion process may take some time, especially when having many group relationship items. And within that time frame another request might have just added a new relationship to the group that is about to be deleted. But this is just a first guess on finding out what could have gone wrong.

Production build 0.71.5 2024