Skip to content

Fix: throw error when credentials=true with origin='*'#413

Open
jiafeimao0p wants to merge 1 commit intoexpressjs:masterfrom
jiafeimao0p:fix/credentials-origin-validation
Open

Fix: throw error when credentials=true with origin='*'#413
jiafeimao0p wants to merge 1 commit intoexpressjs:masterfrom
jiafeimao0p:fix/credentials-origin-validation

Conversation

@jiafeimao0p
Copy link
Copy Markdown

Summary

According to the CORS specification, using wildcard * in Access-Control-Allow-Origin header is forbidden when Access-Control-Allow-Credentials is set to true.

This fix adds validation that throws an error when both options are set, following the fetch spec: https://fetch.spec.whatwg.org/#cors-protocol-and-credentials

Problem

Currently, the cors middleware allows setting credentials: true with origin: '*', which violates the CORS standard and causes browsers to reject the request.

Solution

Added a validateCredentialsAndOrigin() function that throws an error when both credentials is true and origin is '*'.

Changes

  • lib/index.js: Added validateCredentialsAndOrigin() function and call it in the cors middleware
  • test/test.js: Added test cases for the new validation

Testing

Added test cases:

  • Verifies error is thrown when credentials=true and origin='*'
  • Verifies origin='*' works normally when credentials=false

Fixes #333

According to CORS specification, using wildcard '*' in
Access-Control-Allow-Origin header is forbidden when
Access-Control-Allow-Credentials is set to true.

This fix adds validation that throws an error when both
options are set, following the fetch spec:
https://fetch.spec.whatwg.org/#cors-protocol-and-credentials

Fixes expressjs#333
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.

CORS requests with credentials should forbid *

2 participants