File tree Expand file tree Collapse file tree 1 file changed +30
-11
lines changed
Expand file tree Collapse file tree 1 file changed +30
-11
lines changed Original file line number Diff line number Diff line change @@ -599,19 +599,38 @@ def _register_routers(app: FastAPI):
599599
600600
601601def _setup_cors_middleware (app : FastAPI , config : GSRestConfig ):
602- """Setup CORS middleware on the app"""
602+ """Setup CORS middleware on the app.
603+
604+ When ALLOWED_ORIGINS contains "*", we use allow_origin_regex instead of
605+ allow_origins=["*"]. This makes the middleware echo back the requesting
606+ Origin header instead of sending literal "*", which allows credentials
607+ to work (browsers reject Access-Control-Allow-Origin: * with credentials).
608+ """
603609 origins = config .ALLOWED_ORIGINS
604610 if isinstance (origins , str ):
605- origins = [origins ] if origins != "*" else ["*" ]
606-
607- app .add_middleware (
608- CORSMiddleware ,
609- allow_origins = origins ,
610- allow_credentials = True ,
611- allow_methods = ["*" ],
612- allow_headers = ["*" ],
613- expose_headers = ["*" ],
614- )
611+ origins = [origins ]
612+
613+ # allow_origins=["*"] sends literal "*" which is incompatible with credentials.
614+ # Using allow_origin_regex=".*" echoes the Origin header, allowing credentials.
615+ # Check if "*" is anywhere in the list (not just exactly ["*"])
616+ if "*" in origins :
617+ app .add_middleware (
618+ CORSMiddleware ,
619+ allow_origin_regex = ".*" ,
620+ allow_credentials = True ,
621+ allow_methods = ["*" ],
622+ allow_headers = ["*" ],
623+ expose_headers = ["*" ],
624+ )
625+ else :
626+ app .add_middleware (
627+ CORSMiddleware ,
628+ allow_origins = origins ,
629+ allow_credentials = True ,
630+ allow_methods = ["*" ],
631+ allow_headers = ["*" ],
632+ expose_headers = ["*" ],
633+ )
615634
616635
617636def create_app (
You can’t perform that action at this time.
0 commit comments