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 @crossdomain decorator #3113

Open
wants to merge 2 commits into
base: catch-all-path-fix
Choose a base branch
from
Open

Conversation

amCap1712
Copy link
Member

When handling OPTIONS requests, Flask can either generate an automatic response internally or route the request to the view function for handling. The request is routed to the view function if the view function has a provides_automatic_options attribute set to False or if the methods list passed to route definition includes OPTIONS. OPTIONS requests handled by Flask internally don't have CORS headers. Hence, API endpoints that need to support CORS should ensure that the OPTIONS request is routed to the view function.

The https://github.com/crossdomain decorator sets the provides_automatic_options attribute on the view function which should have ensured this. However, due to the way decorators work the attribute should have instead been set on the decorator function itself for Flask to pick it up. However, we often list OPTIONS in the methods list of the route anyway when applying https://github.com/crossdomain. As a result, Flask would still route the request as expected. The errors would only happen when the OPTIONS method was not mentioned in the route's methods list.

https://github.com/crossdomain decorator has now been fixed to set the attribute correctly and also add OPTIONS to the methods list automatically. Further, a test has been added to ensure that all API endpoints support CORS.

While at it, also refactor the views to use newer .get/.post decorators insteads of methods list.

When handling OPTIONS requests, Flask can either generate an automatic response internally
or route the request to the view function for handling. The request is routed to the view
function if the view function has a provides_automatic_options attribute set to False or
if the methods list passed to route definition includes OPTIONS. OPTIONS requests
handled by Flask internally don't have CORS headers. Hence, API endpoints that need to
support CORS should ensure that the OPTIONS request is routed to the view function.

The @crossdomain decorator sets the provides_automatic_options attribute on the view
function which should have ensured this. However, due to the way decorators work the
attribute should have instead been set on the decorator function itself for Flask to
pick it up. However, we often list OPTIONS in the methods list of the route anyway
when applying @crossdomain. As a result, Flask would still route the request as expected.
The errors would only happen when the OPTIONS method was not mentioned in the route's
methods list.

@crossdomain decorator has now been fixed to set the attribute correctly and also add
OPTIONS to the methods list automatically. Further, a test has been added to ensure that
all API endpoints support CORS.
@pep8speaks
Copy link

Hello @amCap1712! Thanks for opening this PR. We checked the lines you've touched for PEP 8 issues, and found:

Line 24:5: E303 too many blank lines (2)

Line 129:1: E303 too many blank lines (3)

Line 301:1: E303 too many blank lines (3)

Line 337:1: E302 expected 2 blank lines, found 1

@amCap1712
Copy link
Member Author

As this touches almost all views, deployed on beta for testing for a while.

@amCap1712 amCap1712 requested a review from mayhem January 8, 2025 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants