Related issue
https://www.drupal.org/project/drupal/issues/2915792

Patch #3 in https://www.drupal.org/project/drupal/issues/2915792 helps partially.

After applying patch #3 - JSON for menu_link_content/menu_link_content starts showing up fine for anonymous users.

But the problem occurs when we have fields referring to menus and we add includes to those fields in our JSON query. The include in such a scenario fails with a 403 error, as anonymous users do not have access to menu.

Comments

gargsuchi created an issue. See original summary.

gargsuchi’s picture

StatusFileSize
new1.53 KB

Patch attached for fix.

gabesullice’s picture

Status: Active » Closed (works as designed)
  1. +++ b/src/Controller/EntityResource.php
    @@ -124,7 +124,7 @@ class EntityResource {
    +      throw new EntityAccessDeniedHttpException($entity, $entity_access, '/data', 'The current user is not allowed to GET 1 the selected resource.');
    

    I'm not sure what the "1" here is supposed to mean, can you explain?

  2. +++ b/src/Controller/EntityResource.php
    @@ -937,7 +937,12 @@ class EntityResource {
    -    $access = $entity->access('view', NULL, TRUE);
    +    if (($entity->entityTypeId == 'menu') OR ($entity->entityTypeId == 'menu_link_content')) {
    

    I don't think JSON API module should start making special exceptions for entity types. Unfortunately, I can't take this patch as is.

    I would recommend implementing hook_entity_access in a custom module for the menu and menu_link_content entity types and/or giving anonymous users the appropriate permissions to view them. Then, those access rules will be picked up automatically and honored by JSON API.

    Thanks for the report and patch though!

alex.skrypnyk’s picture

StatusFileSize
new1.59 KB

Attached is an updated version of the patch in #2 for those who needs this functionality.

wim leers’s picture

#2 + #4: Note that view label support is present in JSON API 2.x: #2843922: Show label of inaccessible entities ('view' access denied) when 'view label' access is allowed. The patches in #2 and #4 introduce a security vulnerability though!