Access denied when adding field value via Media Library dialog

Created on 9 November 2023, about 1 year ago
Updated 28 August 2024, 3 months ago

Problem/Motivation

On our site, we've added media reference fields to some of our menus with the Menu Item Extras module. When we add a value to these fields, the media library widget shows a dialog containing the media library. When we select a media entity and click "Insert Selected", the following error appears in the browser console/dblog:

Path: /media-library?ajax_form=1&_wrapper_format=drupal_ajax&media_library_opener_id=media_library.opener.field_widget&media_library_allowed_types%5Bimage%5D=image&media_library_selected_type=image&media_library_remaining=1&media_library_opener_parameters%5Bfield_widget_id%5D=field_section_icon&media_library_opener_parameters%5Bentity_type_id%5D=menu_link_content&media_library_opener_parameters%5Bbundle%5D=main-navigation&media_library_opener_parameters%5Bfield_name%5D=field_section_icon&hash=__DcczjYUEwR573DEScmLx3iOdgkCLw-uz23c11_chw&views_display_id=widget&_wrapper_format=drupal_ajax. Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException: in Drupal\Core\Routing\AccessAwareRouter->checkAccess() (line 118 of /var/www/html/docroot/core/lib/Drupal/Core/Routing/AccessAwareRouter.php).

This appears to have worked in Drupal 9, but we recently updated to Drupal 10, which seems to be stricter about this check.

Steps to reproduce

  1. Install the Menu Item Extras module.
  2. Add a media reference field to a menu. For simplicity's sake, have it reference an image media type.
  3. Go to the Add menu link page.
  4. Find the media field you added in the second step.
  5. Click the Add media button.
  6. Upload an image or select an existing image.
  7. Click Insert selected.
  8. Check the browser console and/or dblog. You should see the access denied error in both places.

Proposed resolution

This issue occurs because an access check is performed any time hook_menu_link_content_create_access is invoked. In this case, we're assigning a value to a field, so I would suggest returning a neutral access result, and letting the full access check run when the menu item is saved.

One thing that may be useful is to expand the path check in this case so other field checks can be accounted for. However, the original use case that led me to this proposed resolution was for media, so I'll leave it to others to expand on it as needed.

Remaining tasks

I'll post a patch to implement my proposed resolution.

User interface changes

None.

API changes

Return a neutral access result if the originating URL is a media library.

Data model changes

None.

πŸ› Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States jsutta United States

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

Comments & Activities

  • Issue created by @jsutta
  • πŸ‡ΊπŸ‡ΈUnited States jsutta United States

    Attaching patch.

  • πŸ‡ΊπŸ‡ΈUnited States jsutta United States

    Attaching updated patch for the 2.1.0 version.

  • πŸ‡ΊπŸ‡ΈUnited States jsutta United States

    Regenerated the patch after updating PHP to version 8.2 and finding a couple issues I introduced unintentionally.

  • πŸ‡³πŸ‡±Netherlands Ewout Goosmann

    I just released a new version (3.0.0-alpha1) of this module, which refactored almost all of the code in the .module. I'm curious if anyone is still experiencing the above issues when using the new version.

  • πŸ‡ΊπŸ‡ΈUnited States jsutta United States

    Just tried it out with Drupal 10.3.1 and version 3.0.0-alpha1 of this module, without the patch from this issue, and it works! Thank you for fixing it, and for following up here.

  • Status changed to Fixed 3 months ago
  • πŸ‡³πŸ‡±Netherlands Ewout Goosmann

    Thank you for testing and the quick response! Great to hear that everything is working without the patch.

    PS. I just released another version (alpha2) with a small fix for a bug that popped up when saving permissions after deleting a menu.

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

Production build 0.71.5 2024