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: Origin
response header in the CORS middleware (cors/lib/index.js
).Currently, the
Vary: Origin
header is not set when theOrigin
request 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: Origin
header must always be set whenever the server’s response varies based on theOrigin
header—even if theOrigin
header is absent in the request.If
Vary: Origin
is missing, browsers may cache a non-CORS response without theAccess-Control-Allow-Origin
header, and then incorrectly reuse that cached response for subsequent CORS requests, leading to failed cross-origin requests.This PR ensures that
Vary: Origin
is consistently set in all cases where CORS logic depends on theOrigin
header, preventing this caching issue.This behavior is aligned with discussions and recommendations in the official CORS specification and issues like #332.
How to test?
Origin
header) to the server.Vary: Origin
header.Origin
header).Access-Control-Allow-Origin
header 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.