Fix: Always set Vary: Origin header according to CORS spec to avoid caching issues#348
Open
arashkyadegar wants to merge 3 commits intoexpressjs:masterfrom
Open
Fix: Always set Vary: Origin header according to CORS spec to avoid caching issues#348arashkyadegar wants to merge 3 commits intoexpressjs:masterfrom
arashkyadegar wants to merge 3 commits intoexpressjs:masterfrom
Conversation
Member
|
Thanks for the proposal @arashkyadegar! I was checking the CI and as we are compatible with Node@0.x we need to use and older syntax that does not allows to use |
Author
|
Thanks for the feedback! I’ve updated the code to avoid using Array.prototype.includes for Node 0.x compatibility. Please let me know if there’s anything else that needs adjustment. |
compatibility The previous use of Array.prototype.includes is not supported in Node 0.x. Replaced it with a compatible alternative to ensure CI passes and maintain backward compatibility.
Author
|
Hello, just wanted to check in on this PR. Let me know if there's anything you'd like me to update or clarify. Thanks! |
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 does this PR do?
This PR fixes a subtle issue with the handling of the
Vary: Originresponse header in the CORS middleware (cors/lib/index.js).Currently, the
Vary: Originheader is not set when theOriginrequest header is missing (i.e., non-CORS requests), which can cause incorrect caching behavior in user agents.Why is this needed?
According to the CORS specification and best practices, the
Vary: Originheader must always be set whenever the server’s response varies based on theOriginheader—even if theOriginheader is absent in the request.If
Vary: Originis missing, browsers may cache a non-CORS response without theAccess-Control-Allow-Originheader, and then incorrectly reuse that cached response for subsequent CORS requests, leading to failed cross-origin requests.This PR ensures that
Vary: Originis consistently set in all cases where CORS logic depends on theOriginheader, preventing this caching issue.This behavior is aligned with discussions and recommendations in the official CORS specification and issues like #332.
How to test?
Originheader) to the server.Vary: Originheader.Originheader).Access-Control-Allow-Originheader is present and the response is not cached incorrectly.Checklist
Thanks for reviewing! Please let me know if you’d like me to adjust or expand anything.