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 explicit origin when with credentials #1824

Closed
wants to merge 2 commits into from
Closed
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: 2 additions & 1 deletion starlette/middleware/cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ async def send(
headers.update(self.simple_headers)
origin = request_headers["Origin"]
has_cookie = "cookie" in request_headers
has_authorization_header = "authorization" in request_headers

# If request includes any cookie headers, then we must respond
# with the specific origin instead of '*'.
if self.allow_all_origins and has_cookie:
if self.allow_all_origins and (has_cookie or has_authorization_header):
self.allow_explicit_origin(headers, origin)

# If we only allow specific origins, then we have to mirror back
Expand Down
9 changes: 9 additions & 0 deletions tests/middleware/test_cors.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ def homepage(request):
assert response.headers["access-control-expose-headers"] == "X-Status"
assert response.headers["access-control-allow-credentials"] == "true"

# Test Authorization header credentialed response
headers = {"Origin": "https://example.org", "Authorization": "Bearer some_token"}
response = client.get("/", headers=headers)
assert response.status_code == 200
assert response.text == "Homepage"
assert response.headers["access-control-allow-origin"] == "https://example.org"
assert response.headers["access-control-expose-headers"] == "X-Status"
assert response.headers["access-control-allow-credentials"] == "true"

# Test non-CORS response
response = client.get("/")
assert response.status_code == 200
Expand Down