Skip to content

fix(cors): reflect request origin when credentials is true with wildcard#4813

Merged
yusukebe merged 3 commits intomainfrom
unknown repository
Mar 23, 2026
Merged

fix(cors): reflect request origin when credentials is true with wildcard#4813
yusukebe merged 3 commits intomainfrom
unknown repository

Conversation

@ghost
Copy link
Copy Markdown

@ghost ghost commented Mar 19, 2026

Summary

Fixes the CORS middleware sending Access-Control-Allow-Origin: * together with Access-Control-Allow-Credentials: true, which browsers reject per the Fetch spec.

Problem

When configured with origin: '*' and credentials: true:

cors({ origin: '*', credentials: true })

The middleware sets both:

  • Access-Control-Allow-Origin: *
  • Access-Control-Allow-Credentials: true

The Fetch spec requires that Access-Control-Allow-Origin must not be * when credentials mode is include. Browsers reject these responses.

Fix

When credentials is true and origin is '*', reflect the actual request Origin header instead of the wildcard. Also set Vary: Origin since the response now varies by request.

This matches the behavior of other CORS libraries (e.g., Express cors middleware).

Fixes #4811

Per the Fetch spec, Access-Control-Allow-Origin must not be '*' when
credentials mode is 'include'. Browsers reject these responses.

When origin is '*' and credentials is true, reflect the actual request
origin instead of the wildcard. Also set Vary: Origin in this case
since the response now varies by request origin.

Fixes #4811
Copy link
Copy Markdown
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ctonneslan Can you add proper tests?

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 20, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.84%. Comparing base (fe689ec) to head (674372f).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4813   +/-   ##
=======================================
  Coverage   92.83%   92.84%           
=======================================
  Files         177      177           
  Lines       11642    11643    +1     
  Branches     3466     3468    +2     
=======================================
+ Hits        10808    10810    +2     
+ Misses        833      832    -1     
  Partials        1        1           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Cover the case where origin: '*' and credentials: true should reflect
the request Origin header instead of sending *.
@ghost
Copy link
Copy Markdown
Author

ghost commented Mar 20, 2026

Added tests. Three cases covering the credentials + wildcard origin behavior:

  1. GET request reflects the request Origin header instead of *, sets Vary: Origin
  2. Preflight (OPTIONS) does the same
  3. When no Origin header is present, Access-Control-Allow-Origin is omitted (nothing to reflect)

Copy link
Copy Markdown
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Copy Markdown
Member

@ctonneslan Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant