Skip to content

Return explicit origin for CORS requests with Authorization header#3134

Closed
veeceey wants to merge 2 commits intoKludex:mainfrom
veeceey:fix/issue-1832
Closed

Return explicit origin for CORS requests with Authorization header#3134
veeceey wants to merge 2 commits intoKludex:mainfrom
veeceey:fix/issue-1832

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 8, 2026

Summary

Problem

Browsers reject CORS responses that return Access-Control-Allow-Origin: * when the request includes credentials. The middleware already handled this for Cookie headers, but not for Authorization headers:

# Before (only checked cookies):
if self.allow_all_origins and has_cookie:
    self.allow_explicit_origin(headers, origin)

# After (also checks authorization):
if self.allow_all_origins and (has_cookie or has_authorization):
    self.allow_explicit_origin(headers, origin)

Changes

  • starlette/middleware/cors.py: Added has_authorization check alongside the existing has_cookie check in the send method.
  • tests/middleware/test_cors.py: Added 4 new test functions covering:
    • Authorization header returns explicit origin (with and without allow_credentials)
    • Origin does not leak between authorization and non-authorization requests
    • Vary: Origin header is properly set for authorization requests

Test plan

  • All 42 CORS tests pass (21 asyncio + 21 trio), including 8 new test runs
  • Existing tests remain unmodified and passing
  • Manual verification with a browser making credentialed fetch() calls with Authorization header

veeceey and others added 2 commits February 8, 2026 15:00
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>
@Kludex
Copy link
Owner

Kludex commented Feb 14, 2026

I've merged #3137 instead, but thank you for the PR! :)

@Kludex Kludex closed this Feb 14, 2026
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.

CORSMiddleware does not provide explicit origin although Authorization header is present

2 participants