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 check for cors allow credentials #320

Merged
merged 3 commits into from
Jul 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cornice/cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ def ensure_origin(service, request, response=None):
for o in service.cors_origins_for(method)]):
request.errors.add('header', 'Origin',
'%s not allowed' % origin)
elif request.headers.get(
'Access-Control-Allow-Credentials', False):
elif service.cors_support_credentials_for(method):
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this is not the correct way of handling the problem: here it prevents the response to contain the correct AC-Allow-Origin response header (as the tests showcase).

This is probably due to the fact this is in an elif where the else tests that the allowed origins contain a wildcard.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe the test is testing for something that in fact should not be true and that's why I eliminated the test. Let me explain there on your other comment.

response.headers['Access-Control-Allow-Origin'] = origin
else:
if any([o == "*" for o in service.cors_origins_for(method)]):
Expand Down
12 changes: 2 additions & 10 deletions cornice/tests/test_cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,26 +228,18 @@ def test_resp_dont_include_allow_origin(self):
self.assertNotIn('Access-Control-Allow-Origin', resp.headers)
self.assertEqual(resp.json, 'squirels')

def test_resp_allow_origin_wildcard(self):
resp = self.app.options(
'/cors_klass',
status=200,
headers={
'Origin': 'lolnet.org',
'Access-Control-Request-Method': 'POST'})
self.assertEqual(resp.headers['Access-Control-Allow-Origin'], '*')

def test_origin_is_not_wildcard_if_allow_credentials(self):
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand why this test should go away.

resp = self.app.options(
'/cors_klass',
status=200,
headers={
'Origin': 'lolnet.org',
'Access-Control-Request-Method': 'POST',
'Access-Control-Allow-Credentials': 'true'
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Two ways to support removing this test case:

  1. it is technically incorrect in CORS to return what this test wants to see i.e. the * should not be returned in AC-Allow-Origin when withCredentials is used. I found this explicitly stated in Monsur Hossain's book CORS in Action and also in a few StackOverflow threads, most notably here

  2. The test right after this eliminated test has the same issue that is resolved in the fix. The AC-Allow-Credentials header is not a request header, but rather a response header, so it shouldn't be set by the client. When you delete it, you then have 2 tests that expect contradictory results, so one needs to be eliminated anyway.

Make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes! I believe this fix then misses a test that exercise exactly what you described (we should check that the header is returned correctly, but not sent).

self.assertEqual(resp.headers['Access-Control-Allow-Origin'],
'lolnet.org')
self.assertEqual(resp.headers['Access-Control-Allow-Credentials'],
'true')

def test_responses_include_an_allow_origin_header(self):
resp = self.app.get('/squirel', headers={'Origin': 'notmyidea.org'})
Expand Down