fix(cors): reflect request origin when credentials is true with wildcard#4813
Conversation
Per the Fetch spec, Access-Control-Allow-Origin must not be '*' when credentials mode is 'include'. Browsers reject these responses. When origin is '*' and credentials is true, reflect the actual request origin instead of the wildcard. Also set Vary: Origin in this case since the response now varies by request origin. Fixes #4811
yusukebe
reviewed
Mar 20, 2026
Member
yusukebe
left a comment
There was a problem hiding this comment.
@ctonneslan Can you add proper tests?
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4813 +/- ##
=======================================
Coverage 92.83% 92.84%
=======================================
Files 177 177
Lines 11642 11643 +1
Branches 3466 3468 +2
=======================================
+ Hits 10808 10810 +2
+ Misses 833 832 -1
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Cover the case where origin: '*' and credentials: true should reflect the request Origin header instead of sending *.
Author
|
Added tests. Three cases covering the credentials + wildcard origin behavior:
|
Member
|
@ctonneslan Thanks! |
This was referenced Mar 23, 2026
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
Fixes the CORS middleware sending
Access-Control-Allow-Origin: *together withAccess-Control-Allow-Credentials: true, which browsers reject per the Fetch spec.Problem
When configured with
origin: '*'andcredentials: true:The middleware sets both:
Access-Control-Allow-Origin: *Access-Control-Allow-Credentials: trueThe Fetch spec requires that
Access-Control-Allow-Originmust not be*when credentials mode isinclude. Browsers reject these responses.Fix
When
credentialsis true and origin is'*', reflect the actual requestOriginheader instead of the wildcard. Also setVary: Originsince the response now varies by request.This matches the behavior of other CORS libraries (e.g., Express cors middleware).
Fixes #4811