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

Fix MRO to return 405 for unsupported methods #3175

Merged
merged 5 commits into from
Oct 13, 2023
Merged

Fix MRO to return 405 for unsupported methods #3175

merged 5 commits into from
Oct 13, 2023

Conversation

dhruvkb
Copy link
Member

@dhruvkb dhruvkb commented Oct 10, 2023

Fixes

Fixes #2374 by @krysal

Description

This PR fixes the method resolution order of the token APIView so that the exception handler will be invoked and a 405 Method Not Allowed response will be sent. When the MRO is incorrect the exception is not handled and results in a 500 Internal Server Error.

Testing Instructions

  1. Check out the PR and run the dev server.
  2. Make a GET request to the /v1/auth_tokens/token/ endpoint.
  3. Receive a 405 response.

Checklist

  • My pull request has a descriptive title (not a vague title likeUpdate index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.
  • I ran the DAG documentation generator (if applicable).

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@dhruvkb dhruvkb requested a review from a team as a code owner October 10, 2023 16:27
@github-actions github-actions bot added the 🧱 stack: api Related to the Django API label Oct 10, 2023
@openverse-bot openverse-bot added 🟨 priority: medium Not blocking but should be addressed soon 🛠 goal: fix Bug fix 💻 aspect: code Concerns the software code in the repository labels Oct 10, 2023
@dhruvkb dhruvkb marked this pull request as draft October 10, 2023 16:49
@dhruvkb dhruvkb marked this pull request as ready for review October 10, 2023 18:48
Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great find @dhruvkb! Tested manually and it returns the error message correctly. Could you add an automated test for when the HTTP method is different from POST? It may go in tests/test_auth.py.

api/api/views/oauth2_views.py Outdated Show resolved Hide resolved
dhruvkb and others added 2 commits October 11, 2023 19:47
Co-authored-by: Krystle Salazar <krystle.salazar@automattic.com>
api/test/test_auth.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one small request to remove some unnecessary (at least I think, could be wrong) JSON parsing. Krystle's change to the unit test is also critical too 🙂

Otherwise LGTM!

Comment on lines +159 to +160
data = json.loads(res.content)
return Response(data, status=res.status_code)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to json.loads the content? Would something like this work without the need for that?

Suggested change
data = json.loads(res.content)
return Response(data, status=res.status_code)
data = json.loads(res.content)
return HttpResponse(content=res.content, content_type="application/json", status=res.status_code)

HttpResponse is the more low-level version from django.http that allows you to bypass the DRF Response data parsing. Doing json.loads and then passing it directly to Response means we're unnecessary marshalling the JSON string to Python and then immediately back to a string.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although, more general question: why is it necessary to do anything manual here at all, other than just return res like we used to? Does APIView complain about not receiving the right type of response object or something?

Copy link
Member Author

@dhruvkb dhruvkb Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the Response class simply provides a nicer interface for returning content-negotiated Web API responses, that can be rendered to multiple formats.
— DRF docs

We can use HttpResponse for sure, but sending DRF's Response is more consistent and it renders the DRF API UI in the browser like our other endpoints.

I agree that it's one more step of JSON parsing followed by serialization but DRF doesn't allow any other way to set data where the content_type can be negotiated automatically.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use HttpResponse for sure, but sending DRF's Response is more consistent and it renders the DRF API UI in the browser like our other endpoints.

Ahh, interesting, I didn't realise that was a side effect. For this view, it doesn't seem like a tragic loss, though? It's not exactly a "browseable" response 🤔 At least not any less so if your browser just renders a nice JSON explorer (Firefox does this, not sure of others).

I also don't think we want to support anyone using this view over anything than JSON anyway, but I guess Response handles it regardless so 🤷 I would personally use HttpResponse but if you prefer to keep it, all good with me as well 👍

Co-authored-by: Krystle Salazar <krystle.salazar@automattic.com>
Copy link
Member

@krysal krysal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent! Thank you, @dhruvkb.

@dhruvkb dhruvkb merged commit ac08b3b into main Oct 13, 2023
44 checks passed
@dhruvkb dhruvkb deleted the drf_405 branch October 13, 2023 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code Concerns the software code in the repository 🛠 goal: fix Bug fix 🟨 priority: medium Not blocking but should be addressed soon 🧱 stack: api Related to the Django API
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

Prevent failing at /v1/auth_tokens/token/ due to MethodNotAllowed
4 participants