Return explicit origin for CORS requests with Authorization header#3134
Closed
veeceey wants to merge 2 commits intoKludex:mainfrom
Closed
Return explicit origin for CORS requests with Authorization header#3134veeceey wants to merge 2 commits intoKludex:mainfrom
veeceey wants to merge 2 commits intoKludex:mainfrom
Conversation
When `allow_origins=["*"]` and a request includes an `Authorization` header, the middleware now returns the explicit request origin in `Access-Control-Allow-Origin` instead of `*`. This matches the existing behavior for `Cookie` headers and complies with the CORS spec, which requires an explicit origin when credentials are involved. Browsers consider authentication headers (e.g. `Authorization`) as credentials per the Fetch specification. Responding with `*` causes browsers to reject the response for credentialed requests. Fixes Kludex#1832 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Owner
|
I've merged #3137 instead, but thank you for the PR! :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
CORSMiddlewarenow treats theAuthorizationheader as a credential, matching the existing behavior forCookieheaders.allow_origins=["*"]and a request includes anAuthorizationheader, the response returns the explicit request origin inAccess-Control-Allow-Origininstead of*.Problem
Browsers reject CORS responses that return
Access-Control-Allow-Origin: *when the request includes credentials. The middleware already handled this forCookieheaders, but not forAuthorizationheaders:Changes
starlette/middleware/cors.py: Addedhas_authorizationcheck alongside the existinghas_cookiecheck in thesendmethod.tests/middleware/test_cors.py: Added 4 new test functions covering:allow_credentials)Vary: Originheader is properly set for authorization requestsTest plan
fetch()calls withAuthorizationheader