fix(cors): preserve existing Vary header from response#4479
Closed
Higangssh wants to merge 1 commit intohonojs:mainfrom
Closed
fix(cors): preserve existing Vary header from response#4479Higangssh wants to merge 1 commit intohonojs:mainfrom
Higangssh wants to merge 1 commit intohonojs:mainfrom
Conversation
The CORS middleware was incorrectly reading the Vary header from the request instead of the response, causing it to overwrite any Vary headers set by the application. Changes: - Move Vary header handling to after await next() for non-OPTIONS requests - Read existing Vary header from response instead of request - Preserve and append 'Origin' to existing Vary values - Add test case to verify Vary header preservation Fixes honojs#4471
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4479 +/- ##
=======================================
Coverage 91.30% 91.30%
=======================================
Files 173 173
Lines 11190 11195 +5
Branches 3228 3230 +2
=======================================
+ Hits 10217 10222 +5
Misses 972 972
Partials 1 1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
Summary
Fixes #4471
The CORS middleware was incorrectly reading the Vary header from the request instead of the response, causing it to overwrite any Vary headers set by the application.
Problem
When a controller sets a Vary header (e.g.,
Vary: Accept), the CORS middleware would overwrite it because:c.req.header('Vary')(request header)Vary: Originwithout preserving existing valuesSolution
await next()for non-OPTIONS requestsc.res.headersinstead ofc.req.headerChanges
src/middleware/cors/index.ts: Fixed Vary header logicsrc/middleware/cors/index.test.ts: Added test case to verify preservationTesting
Should preserve existing Vary header from responseadded