fix: don't allow credentials for the wildcard CORS fallback#1
Merged
Conversation
app.add_middleware() was configured with allow_credentials=True regardless of which origins were resolved, including the "*" fallback used when ANTHROPIC_API_KEY isn't set at import time (e.g. a misconfigured deployment). Wildcard origin + credentials is an unsafe combination: browsers refuse a literal "*" on a credentialed response, so CORS middlewares (including Starlette's) work around that by reflecting back whatever Origin header the request sent — meaning any site could get its origin accepted and make credentialed requests, defeating CORS entirely for that fallback path. Extracts the credentials decision into _allow_credentials_for(), which only returns True for an explicit, non-wildcard origin list, and adds direct unit tests for both branches. No behavior change for the documented/normal setup (CORS_ORIGINS set to real origins).
devthedevil
added a commit
that referenced
this pull request
Jul 22, 2026
fix: don't allow credentials for the wildcard CORS fallback
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.
Problem
server.pyregistersCORSMiddlewarewithallow_credentials=Trueunconditionally:The
"*"fallback fires wheneverANTHROPIC_API_KEYisn't set at import time — realistically, a misconfigured deployment rather than just tests (the comment says "e.g. during tests" but nothing stops this from being the live prod config if the env var is missing).Wildcard origin +
allow_credentials=Trueis an unsafe combination: browsers refuse a literal*on a credentialed response, so CORS middlewares — including Starlette's, which this app uses — work around that by reflecting back whateverOriginheader the request sent instead of a literal*. That means when this fallback is active, any site can have its origin accepted and make credentialed cross-origin requests, which defeats CORS entirely for that path.Fix
Extracted the credentials decision into a small, directly-testable function:
Credentials are now only enabled when explicit, non-wildcard origins are configured. No behavior change for the documented/normal setup (
CORS_ORIGINSset to real origins in.env) — this only tightens the unsafe fallback path.Testing
test_wildcard_origin_disallows_credentialsandtest_explicit_origins_allow_credentials, unit-testing the new function directly (avoids needing to re-import the module under different env configs just to exercise the fallback branch throughTestClient).ruff checkclean,pytest25/25 passing locally.