Problem/Motivation

The autocomplete widget does not filter by permission, on save of entity group content created even if user does not have permission to create.

Steps to reproduce

Add the Group Autocomplete widget to a Group Content Enable entity. For a user with permission to create entity, add new and enter name of group they don't have permission to add content to in the auto complete. Save the entity.

They will probably no longer have access to the entity because it now belongs to a group they can't add/edit group content for.

Proposed resolution

Can this be done with GroupQueryAccessHandler; if "create $entity_plugin_id entity" permission was also considered an operation?

Remaining tasks

  1. Stop bypassing group access in all our tests.
  2. Add explicit test coverage of the widgets for users that only have access to a subset of groups.
  3. Fix the actual access bypass bug.
  4. Reviews / refinements.
  5. RTBC.
  6. Commit.

User interface changes

API changes

Data model changes

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

dwkitchen created an issue. See original summary.

dww’s picture

Priority: Normal » Critical
Issue summary: View changes

Thanks for opening this!

If this were a 1.0.0 release, this would be a security bug, so bumping to critical.

The existing test coverage is all bypassing group access, which was a good way to get basic tests going quickly, but allows bugs like this to exist. So that's definitely going to be part of fixing this -- adding test coverage that's dealing with group access, not bypassing it. ;)

Fleshed out remaining tasks.

Thanks again,
-Derek

dwkitchen’s picture

I have created #3163943: Allow GroupQueryAccessHandler to accept any permission as an operation, with a change in GroupQueryAccessHandler that will allow Entity Field Group to limit the query results.

This works for Autocomplete (tested local) but can also be used for the select list as well.

dwkitchen’s picture

Here is stage 1 patch; this will limit the groups in the autocomplete to those the user has permission to add the entity to.

It depends on the patch in #3163943: Allow GroupQueryAccessHandler to accept any permission as an operation

Still needed is a change in EntityGroupFieldWidgetBase::addMoreSubmit() to check the user has permission before creating the group content entity. This will work for both the Autocomplete and Select widgets.

dww’s picture

Status: Active » Needs work
Issue tags: +Needs tests

Excellent, thanks for getting this started!

Tagging for tests (per the remaining tasks).

dww’s picture

Note: it's possible to configure a drupalci.yml to apply a patch to group before running tests in here. So we could be testing both this and your patch upstream. This is an example from datetime_extras that needed to patch duration_field:

 build:
   assessment:
     validate_codebase:
      container_command:
        commands:
          - "cd ${SOURCE_DIR} && cd modules/contrib/duration_field && sudo -u www-data curl https://www.drupal.org/files/issues/2019-11-08/3093354-2.patch | patch -p1"
...

See https://www.drupal.org/drupalorg/docs/drupal-ci/customizing-drupalci-tes... for more.

dww’s picture

Issue summary: View changes
Status: Needs work » Postponed (maintainer needs more info)
Issue tags: -Needs tests
StatusFileSize
new1.79 KB

Hrm, I can't seem to reproduce this. Not sure if the latest group releases have automatically fixed this, or what. Here's a test that according to the summary should fail, but it passes. A test user not in group-A3 is unable to use group-A3 in the autocomplete widget.

Maybe I misunderstand these directions:

Add the Group Autocomplete widget to a Group Content Enable entity. For a user with permission to create entity, add new and enter name of group they don't have permission to add content to in the auto complete. Save the entity.

In this test, groupTypeA has a member roll with the following group perms:

      'view group',
      'create group_node:article content',
      'create group_node:article entity',
      'create group_node:page content',
      'create group_node:page entity',
      'delete own group_node:article content',
      'delete own group_node:article entity',
      'delete own group_node:page content',
      'delete own group_node:page entity',
      'update own group_node:article content',
      'update own group_node:article entity',
      'update own group_node:page content',
      'update own group_node:page entity',
      'view group_node:article content',
      'view group_node:article entity',
      'view group_node:page content',
      'view group_node:page entity',

The testUser belongs to group-A1 and group-A2, but not group-A3.

The test is logged in as testUser, trying to access group-A3, and everything checks out fine. The user cannot see group-A3, so the form submit fails (as expected).

Can you provide more clarity on what the actual bug is here?

Thanks/sorry,
-Derek

scotwith1t’s picture

Not sure if I'm addressing your question directly, but we stumbled across this issue with a build we're working on. Essentially, it seems like every group is available via the autocomplete widget, regardless of the user's membership. The Select List widget works as expected, only providing the groups the users is in for selection, so we just switched everything to select list and moved on but would love to get this working for autocomplete as the size of the dropdown grows the further we're into the build (more groups).

dww’s picture

I know I tried and failed to reproduce this locally months ago. I believe you, but I'm curious: were you testing as a user with some kind of 'bypass access checks' perm or UID 1? It's extra weird that select works right but autocomplete does not -- that would suggest a code bug, not human error during testing.

Some day (inshallah!), my life will settle down a little bit and I'll have more time to work on this module again. ;)

Thanks/sorry,
-Derek

dww’s picture

p.s. Re-reading this issue -- right, how can the test in #7 be passing if this is a bug? Doesn't that replicate the steps that are supposed to trigger the bug? That patch doesn't "fix" anything, it just adds more test coverage.

azinck’s picture

-- Comment deleted because it was inaccurate. --

floydm’s picture

I have not tried running the test, but I ran into this issue on a project today.

  • Create a Group type and, within it, a Group Role that is allowed to add content to it as group content.
  • Create One User and Two Groups, Group A and Group B.
  • Add the User to Group A and Group B but with different access levels. In Group A, they have that privileged role and so can add group content, in Group B they do not.
  • As that user, go to /node/add/{my_content_type} and, using the Autocomplete Group widget, type "Group." I can select both A and B. If I add both groups and save, the Group Content gets added to both groups even though I do not have permission to add group content to Group B.

With the patch from comment 4 and the Groups patch mentioned in comment 3, I can only select and add the content to Group A, which is the expected behaviour.

dww’s picture

Version: 1.0.x-dev » 2.0.x-dev
Assigned: Unassigned » dww
Status: Postponed (maintainer needs more info) » Needs work
Issue tags: +Needs tests

I'd like to tag stable 2.0.0 and 1.0.0 releases. But this bug is giving me pause. I'm going to try again to add automated test coverage for the autocomplete widget that either reveals the bug or proves it's no longer there (presumably, fixed upstream in Group somehow?).

dww’s picture

Opened MRs for both 1.0.x and 2.0.x (so I can see all the possible group / core versions from both pipelines) with my patch from #7. As I understand it, that should fail if this bug exists.

However, upon further review, maybe #12 is pointing to the difference. In that scenario, the user is a member of the "forbidden" group, but their group role isn't supposed to let you add stuff to the group. I'll expand the test coverage in the MRs to try that case, too.

dww’s picture

Assigned: dww » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs tests

Phew! That was fun.

Indeed, I still can't reproduce the thing where someone without permission to view groups they don't belong to can do anything bad with the autocomplete widget.

However, I definitely was able to get the test to fail in the case where an "outsider" role has 'view group' permission. In this case, someone can view groups, but isn't supposed to be able to add content to those groups. The autocomplete widget shows them all the groups they have access to view, so they can select whatever groups they want. Then the widget (wrongly) lets them add content to those groups.

I pushed a fix to both branches. I'm not sure it's the best fix. 😅 Ideally, we'd alter the query for what results the autocomplete is returning to filter by groups the current user has permission to add stuff to. But frankly, I haven't been following upstream Group changes enough, all the flexible_permissions stuff, etc. I don't know The Right Way(tm) to tie into that situation anymore. So for right now, simply to stop the bleeding, I added a EntityGroupFieldSelection::validateReferenceableEntities() method (which is plumbing from the upstream EntityReferenceSelection/DefaultSelection world. The autocomplete widget sets the right things to trigger this to further validate the selection that not only does the group exist, but the given user has permission to add relationships to it.

The tests, which were all failing in this can-view-but-can't-add-stuff case (failing because the test user was able to add the content), are now all passing (because the test user is no longer able to add content to groups they can see but don't have the right permission to add group relationship entities).

dww’s picture

#3163943: Allow GroupQueryAccessHandler to accept any permission as an operation would indeed make this better. 😅 I'll ping @kristiaanvde about it and try to get some input on that approach.

But probably for the purposes of a short-term fix that doesn't require any upstream changes, what I've got is Good Enough(tm) to actually close the bug. We can open a follow-up and add an @todo about making it more slick, once something like #3163943 is working in Group core.

dww’s picture

Status: Needs review » Needs work

Just realized the "fix" would be broken in the case of group memberships (users). We need to special-case the permission checks if ($entity_plugin_id == 'group_membership')...

But I'm done for tonight, I'll pick this up again tomorrow.

dww’s picture

Status: Needs work » Needs review

  • dww committed 55f4c5cb on 2.0.x
    fix: [#3163814] User without permission can add entity to group through...

  • dww committed 061b66a4 on 1.0.x
    fix: [#3163814] User without permission can add entity to group through...

dww’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

dww’s picture

FYI: https://www.drupal.org/project/entitygroupfield/releases/2.0.0-rc1 and https://www.drupal.org/project/entitygroupfield/releases/1.0.0-rc1 are both now out. Testing welcome. I'd like to put out the corresponding stable releases before the end of 2025, ideally before the end of November. 😅 Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

monkk’s picture

However, when I try to crate blank node/report and link it via group field and click button Add to Group I get silent error. Not sure where to find more details, while drupal error log doesn't contain any.
When saving node I get: The referenced entity (group: gid) does not exist.
Seems this all started after this fix has been implemented.

Just to prove that Anonymous user can create node/add to group I successfully can do it via direct link group/gid/content/create/group_node:report.