fix: set Vary: Origin header when origin callback rejects#403
Open
mahmoodhamdi wants to merge 1 commit intoexpressjs:masterfrom
Open
fix: set Vary: Origin header when origin callback rejects#403mahmoodhamdi wants to merge 1 commit intoexpressjs:masterfrom
mahmoodhamdi wants to merge 1 commit intoexpressjs:masterfrom
Conversation
When the origin callback returns false (rejecting the request origin), the middleware now sets the Vary: Origin response header before passing control to next(). This prevents HTTP caches from incorrectly reusing a response generated for one origin when handling requests from a different origin. Per the Fetch specification (https://fetch.spec.whatwg.org/#cors-protocol-and-http-caches): if a server's response depends on the Origin request header, Vary: Origin must be set — even when the origin is not allowed — to ensure correct cache behavior. Previously, when a dynamic origin function rejected a request, no headers were set at all. A cache could then store this response and serve it to a subsequent CORS request from an allowed origin, which would incorrectly lack the Access-Control-Allow-Origin header. Fixes expressjs#330
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.
What
Sets the
Vary: Originresponse header when a dynamic origin callback returnsfalse(rejecting the request origin).Why
When using a dynamic
originfunction, the server's response depends on theOriginrequest header. Per the Fetch specification, theVary: Originheader must be set whenever the response varies based onOrigin— even when the origin is not allowed.Before this fix: When the origin callback returned
false, no headers were set. An HTTP cache could store this response and serve it to a later request from an allowed origin — that response would incorrectly lackAccess-Control-Allow-Origin, breaking the CORS request.After this fix:
Vary: Originis set even when the origin is rejected, so caches correctly treat responses as origin-dependent.The fix is a single line in
middlewareWrapper— callingvary(res, 'Origin')beforenext()in the rejection branch.Testing
Updated two existing tests to verify
Vary: Originis present when origin callback rejects:Vary: OriginVary: OriginFixes #330