Blocked rerouting of URL for jsonapi

Created on 12 December 2023, 12 months ago
Updated 12 January 2024, 11 months ago

Problem/Motivation

Originally posted this issue in JSON:API here: https://www.drupal.org/project/drupal/issues/3407746 💬 500 error when accessing /jsonapi/ Closed: works as designed

Our development site this is on hasn't updated to 10.x yet so we are using the 1.0.2 version of the module on 9.5.11. It is a multilingual site.

The issue:
With the module installed, when navigating to a URL like https://sitenme.org/jsonapi/ we expect the path to redirect to https://sitename.org/en/jsonapi. It doesn't and instead gives a 500 error in the browser.

{"jsonapi":{"version":"1.0","meta":{"links":{"self":{"href":"http:\/\/jsonapi.org\/format\/1.0\/"}}}},"errors":[{"title":"Internal Server Error","status":"500","detail":"Syntax error","links":{"via":{"href":"https:\/\/branch-sitename.pantheonsite.io\/jsonapi\/"},"info":{"href":"http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html#sec10.5.1"}}}]}

and in the logs it gives:

JsonException: Syntax error in json_decode() (line 120 of /code/web/modules/contrib/json_api_book/src/EventSubscriber/ResponseSubscriber.php).

I believe the 1.1.x and 1.0.2 are using the same code, but I haven't tested this on a D10 site yet.

NOTE: The same issue happens when navigating to https://sitenme.org/en/jsonapi/, it doesn't remove the trailing "/", so it may not be just with multilingual sites.

Steps to reproduce

  1. Drupal 9.5.11 site with JSON:API enabled and multilingual enabled
  2. JSON:API Book module enabled
  3. Navigate in a browser to https://sitenme.org/jsonapi/
🐛 Bug report
Status

Fixed

Version

1.1

Component

Code

Created by

🇨🇦Canada ridefree

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

Merge Requests

Comments & Activities

  • Issue created by @ridefree
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Hmm, never seen this before, but looking at the code, I wonder if $response->getContent() isn't returning any valid content which then leads to the JsonException.

  • Status changed to Needs review 12 months ago
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Please give the MR/patch a try and let us know if that fixes your issue.

  • 🇮🇳India saurabh-2k17

    Hi @jurgenhaas, thanks for your response and MR. I updated the Drupal core to 10.1.7 and tried to visit https://sitenme.org/jsonapi/ and faced the similar issue. Later, I used your MR and patched the module but the same problem persists.

  • 🇮🇳India saurabh-2k17

    I found that the problem was not with the content, the content was already there, just that it was expecting it to be in a readable format. I made a slight change and it works now.

  • 🇩🇪Germany jurgenhaas Gottmadingen

    Would you mind collaborating in the MR instead of a new patch? The latter will not be supported for much longer.

    WRT to your proposed change, I'm not sure that this is reasonable. The patch replaces the third argument 512 to JSON_UNESCAPED_SLASHES which is about the depth of the JSON decoding, where the new argument is a constant which should be used as a constant for the fourth argument.

    Looking at my proposed change in the MR, I just realized that we're currently using the option JSON_THROW_ON_ERROR. That might be the issue, as this will throw the exception and prevent any further handling of other middlewares being in place.

    I've updated the MR to catch exceptions from JSON decoding and encoding to make sure, that this subscriber will not do anything if the JSON caused any issues.

    Please give that another try and if you need to propose further changes, please do that in the issue fork.

  • Status changed to RTBC 11 months ago
  • 🇮🇳India saurabh-2k17

    Apologies @jurgenhaas, I will keep MR thing in mind. I see what you did and tested it too, it works! I am changing the state to RTBC.

    • jurgenhaas committed d3d996e7 on 1.1.x
      Issue #3408255 by jurgenhaas, saurabh-2k17: Blocked rerouting of URL for...
  • Status changed to Fixed 11 months ago
  • 🇩🇪Germany jurgenhaas Gottmadingen
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024