fix: never emit a wildcard Access-Control-Allow-Origin alongside credentials - #693
Open
sridhar-3009 wants to merge 1 commit into
Open
Conversation
…entials When a CORS policy allows all origins and also allows credentials, the middleware set Access-Control-Allow-Origin to a literal "*" while also setting Access-Control-Allow-Credentials to "true". Per the Fetch spec this combination is invalid and browsers reject the response outright, so credentialed requests against a wildcard-origin, credentials-enabled policy would fail in the browser even though the server responded 200. Echo back the specific request origin instead of "*" whenever credentials are allowed, matching the same escape hatch already used for non-wildcard origins.
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
When a CORS policy has
allow_origins="*"andallow_credentials=True, the CORS middleware sendsAccess-Control-Allow-Origin: *together withAccess-Control-Allow-Credentials: true. Per the Fetch spec, a literal*forAccess-Control-Allow-Originis invalid when credentials are involved, and browsers reject the response outright for anyfetch/XMLHttpRequestcall made withcredentials: 'include'. In practice this means a server configured this way returns a 200 with what looks like valid CORS headers, but every credentialed browser request against it silently fails at the network layer.The middleware already has the correct fallback for named origins (echoing the specific origin instead of
*when it's on the allow-list), it just wasn't applied to the wildcard case when credentials are also allowed.Fix
In
get_cors_middleware, only use the literal"*"forAccess-Control-Allow-Originwhen credentials are not allowed; otherwise echo back the request's ownOrigin, same as the non-wildcard path already does. This applies to both the preflight response and the actual (non-OPTIONS) response.Test plan
test_cors_preflight_request_allow_any_origin_with_credentials, covering both the preflight and the actual GET response for a wildcard-origin + credentials-enabled policymainfirst (confirmedAccess-Control-Allow-Origin: *+Access-Control-Allow-Credentials: truewere both sent) before applying the fixpytest tests/→ 1923 passed, 1 skippedblack --check,isort --check-only,flake8all clean on changed files