Fix: Always set Vary: Origin header according to CORS spec to avoid caching issues #348
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.