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

Use allowed methods from view function #303

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

amCap1712
Copy link

@amCap1712 amCap1712 commented Jan 1, 2022

Allowed methods are used in the following hierarchy:

  1. methods parameter of cross_origin decorator
  2. methods parameter of view function route
  3. resource or app level configuration

I tried to implement this in set_cors_headers but couldn't because it is called from both decorator and extension. In the case of the decorator, we do not want to use the methods from view function if the decorator had those specified but in case of extension we want to use it always.

def set_cors_headers(resp, options):

This change is not backward compatible, see PR comments for details.

I am not sure this change is always desirable. With this patch, the view level methods always override resource level configuration. If we have a bunch of views supporting POST and GET methods; and have used resource level configuration to restrict the access control method to GET. With this PR, the view methods will expose those views to POST as well.

Input on how to improve this is appreciated.

Fixes #228

Allowed methods are used in the following hierarchy:
1) methods parameter of cross_origin decorator
2) methods parameter of view function route
3) resource or app level configuration
@@ -38,8 +48,7 @@ def test_defaults(self):
self.assertFalse(ACL_METHODS in self.get('/defaults', origin='www.example.com').headers)
self.assertFalse(ACL_METHODS in self.head('/defaults', origin='www.example.com').headers)
res = self.preflight('/defaults', 'POST', origin='www.example.com')
for method in ALL_METHODS:
self.assertTrue(method in res.headers.get(ACL_METHODS))
self.assertIsNone(res.headers.get(ACL_METHODS))
Copy link
Author

Choose a reason for hiding this comment

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

This is backward incompatible. Earlier if the cross_origin decorator had no methods specified all the methods from the default or app level configuration would be used but now the methods will be restricted to what the view supports.

def index2():
return 'Welcome 2'

res = self.preflight('/', 'GET', origin='www.example.com')
Copy link
Author

Choose a reason for hiding this comment

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

This change is also backward incompatible in a way that instead of default or level configuration, methods from view function are used.

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.

Is it possible to get Allowed methods from Flask?
1 participant