fix: do not set Vary: Origin for static string origin option#405
Open
Vansh1811 wants to merge 1 commit intoexpressjs:masterfrom
Open
fix: do not set Vary: Origin for static string origin option#405Vansh1811 wants to merge 1 commit intoexpressjs:masterfrom
Vansh1811 wants to merge 1 commit intoexpressjs:masterfrom
Conversation
When origin option is a static string, the Access-Control-Allow-Origin response is always the same value regardless of the request's Origin header. Per the Fetch spec, Vary: Origin must NOT be set in this case, as it would unnecessarily increase cache size without any benefit. Fixes expressjs#332
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.
Problem
When the
originoption is set to a static string (e.g.origin: 'https://example.com'), the current code incorrectly setsVary: Originin the response:This violates the Fetch spec:
When
originis a static string,Access-Control-Allow-Originis always the same value regardless of the request'sOriginheader. SettingVary: Originin this case unnecessarily inflates cache size — every unique client origin creates a separate cache entry even though the response is identical.Fix
Remove the
Vary: Originheader from theisString(options.origin)branch ofconfigureOrigin().Vary: Originis still correctly set for dynamic origin cases (RegExp, Array, function) where the response varies based on the request'sOriginheader.Testing
The existing test suite covers the static string origin case. A new test can verify
Vary: Originis NOT present in the response whenoriginis set to a static string.Fixes #332