Problem/Motivation

Error messages about access checks from core rest requests are not returned with JSON API enabled. After #2808233: REST 403 responses don't tell the user *why* access is not granted: requires deep Drupal understanding to figure out was committed, error messages should be returned, instead an empty {} is returned, despite the correct message being generated.

  curl -X "GET" "http://drupal.localhost/node/251?_format=json" -u user:password
  // ...
  { }
CommentFileSizeAuthor
#6 2852691-6.patch1.3 KBwim leers

Comments

drpal created an issue. See original summary.

tedbow’s picture

I can confirm this error.

\Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::onException handles the exception

Then in \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::setEventResponse
$encoded_content = $this->serializer->serialize($exception, $format, ['data_wrapper' => 'errors']);
The exception sent to serialize() here does have the message "The 'access content' permission is required."

$encoded_content though then equals {}

I think the error gets taken out \Drupal\jsonapi\Normalizer\HttpExceptionNormalizer::normalize

The above could try to be fixed but would there be a way to check if the error is from a jsonapi route and then only handle errors from those routes?

e0ipso’s picture

would there be a way to check if the error is from a jsonapi route and then only handle errors from those routes?

The short answer is that in the current state of core. We can't. See #2831137: Remove the need for ?_format=api_json: assume the use of the 'api_json' format for routes managed by JSON API.

wim leers’s picture

No, the bug is in \Drupal\jsonapi\EventSubscriber\DefaultExceptionSubscriber::onException().

It is not checking the getHandledFormats() return value: it's not comparing that to the format of the current request. Instead, it is handling every format that is supported by the serializer. This is wrong.

wim leers’s picture

Status: Active » Needs review
StatusFileSize
new1.3 KB

Basically this.

Status: Needs review » Needs work

The last submitted patch, 6: 2852691-6.patch, failed testing.

hampercm’s picture

Status: Needs work » Needs review

Retesting...

  • e0ipso committed bc7c3b1 on 8.x-1.x authored by Wim Leers
    fix(Error Handling) REST 403 responses are nullified when JSON API is...
e0ipso’s picture

Status: Needs review » Fixed

Thanks everyone!

vasyl.kletsko’s picture

Status: Fixed » Closed (fixed)