Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: invoking a protected endpoint that internally uses rest_do_request fails with 502 bad gateway #114

Open
pablobarvo opened this issue Aug 28, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@pablobarvo
Copy link

pablobarvo commented Aug 28, 2024

Bug Report

Plugin Version

3.5.6

PHP Version

8.1.29

WordPress Version

6.6.1

Bug description

  1. I have an custom endpoint registered via register_rest_route and protected via "Protected endpoints", configured via "Apply only on specific REST endpoints.
  2. Internally, this API makes an invocation to a separate API using the WP rest_do_request to request some rest API results, and it is invoking a REST endpoint not protected with "Protected endpoints"

Expected results:

The invocation works well: the JWT is validated, the API runs and the internal rest_do_request succeeds

Current results:

Invoking the API results in a 502 bad gateway response

What I've tried to confirm this is a bug:

  1. If I remove the rest_do_request call from the API implementation, the API request starts working well
  2. If I disable protected endpoints and do requests unauthenticated, the API request starts working well

What I've done to confirm why this happens:

Debugging this a bit, it seems like the code in routes/api.php is being invoked twice: first for the API call, which validates the JWT token successfully, but the invocation of rest_do_request internally calls this again, and the invocation of hasAccess fails.

Potential fix:

If I edit ProtectEndpointService.php and make sure that hasAccess checks if the user is already logged in and returns true in line 40, the API starts to work well while authenticated, so it is clear that this is the code path causing the issue. Basically, move the code in line 61 up.

Please let me know your thoughts both on this issue and the potential fix (proposed or other) to make this scenario work well.

Thanks in advance!

@pablobarvo pablobarvo added the bug Something isn't working label Aug 28, 2024
@vins13pattar
Copy link

This issue makes this plugin useless. Is it being addressed? I am also facing the same issue.

@github-staff github-staff deleted a comment from Superstar-IT Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants