Skip to content

Fix CORS response when using default wildcard origin#398

Merged
pubkey merged 2 commits intomasterfrom
claude/fix-cors-bug-ydzX3
Apr 15, 2026
Merged

Fix CORS response when using default wildcard origin#398
pubkey merged 2 commits intomasterfrom
claude/fix-cors-bug-ydzX3

Conversation

@pubkey
Copy link
Copy Markdown
Owner

@pubkey pubkey commented Apr 14, 2026

This PR contains:

  • A BUGFIX
  • IMPROVED TESTS

Describe the problem you have without this PR

When the RxDB server is configured with the default cors: '*' setting, it sends both Access-Control-Allow-Origin: * and Access-Control-Allow-Credentials: true in CORS responses. According to the CORS specification, these two headers cannot be combined—browsers reject such responses for credentialed requests (those with cookies or authorization headers). This causes cross-origin requests with credentials to fail.

Solution

Modified the Express adapter's setCors function to reflect the request Origin header back to the client when cors is set to '*'. This maintains the "allow from anywhere" semantics while remaining compatible with the credentials: true setting that the server always sends.

The fix:

  • Changes origin: cors to origin: cors === '*' ? true : cors in the CORS configuration
  • When cors is '*', the express-cors middleware's origin: true option automatically reflects the request's Origin header back
  • For explicit origin configurations, the behavior remains unchanged

Todos

  • Tests - Added comprehensive unit test verifying the fix
  • Changelog - Updated with bug fix description

Test Plan

Added a new unit test should not combine Access-Control-Allow-Origin: * with Access-Control-Allow-Credentials: true that:

  1. Creates a server with default CORS settings (no explicit cors option)
  2. Sends a preflight OPTIONS request from a cross-origin client
  3. Verifies that Access-Control-Allow-Credentials: true is present
  4. Verifies that Access-Control-Allow-Origin is NOT the wildcard *

The test confirms the fix resolves the invalid CORS response issue.

@pubkey

https://claude.ai/code/session_019bXsHspdrrMQGtwnuE1cqG

claude and others added 2 commits April 14, 2026 13:36
…entials

Per the CORS spec, the wildcard Access-Control-Allow-Origin: * cannot be
combined with Access-Control-Allow-Credentials: true - browsers reject
such responses on credentialed requests. The express adapter always sends
credentials: true, so the default cors: '*' produced an invalid combination
that broke every cross-origin credentialed request in browsers.

When cors === '*', pass origin: true to the cors middleware so the request
origin is reflected back, preserving the "allow from anywhere" intent while
staying spec-compliant with credentials: true.

Adds a public-API-only regression test that hits an endpoint with an Origin
header and asserts the response does not combine '*' with credentials.
@pubkey pubkey merged commit 1605f35 into master Apr 15, 2026
2 checks passed
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.

2 participants