Spaces:
Running
Running
refactor: remove ProxyHeadersMiddleware and reorder CORS middleware configuration to ensure proper request processing
Browse files
main.py
CHANGED
|
@@ -100,17 +100,6 @@ app = FastAPI(
|
|
| 100 |
lifespan=lifespan,
|
| 101 |
)
|
| 102 |
|
| 103 |
-
app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*")
|
| 104 |
-
|
| 105 |
-
@app.middleware("http")
|
| 106 |
-
async def normalize_path(request, call_next):
|
| 107 |
-
# Fix double slashes in paths (e.g., //api/usage -> /api/usage)
|
| 108 |
-
path = request.scope.get("path")
|
| 109 |
-
if path and "//" in path:
|
| 110 |
-
request.scope["path"] = path.replace("//", "/")
|
| 111 |
-
return await call_next(request)
|
| 112 |
-
|
| 113 |
-
|
| 114 |
# When allow_credentials=True, browsers REJECT responses with "Access-Control-Allow-Origin: *"
|
| 115 |
# and refuse to store or send cookies. We must always use explicit origins.
|
| 116 |
_DEFAULT_ORIGINS = [
|
|
@@ -120,8 +109,21 @@ _DEFAULT_ORIGINS = [
|
|
| 120 |
"http://localhost:3000",
|
| 121 |
"http://localhost:3001",
|
| 122 |
]
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
|
|
|
| 125 |
app.add_middleware(
|
| 126 |
CORSMiddleware,
|
| 127 |
allow_origins=_cors_origins,
|
|
|
|
| 100 |
lifespan=lifespan,
|
| 101 |
)
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
# When allow_credentials=True, browsers REJECT responses with "Access-Control-Allow-Origin: *"
|
| 104 |
# and refuse to store or send cookies. We must always use explicit origins.
|
| 105 |
_DEFAULT_ORIGINS = [
|
|
|
|
| 109 |
"http://localhost:3000",
|
| 110 |
"http://localhost:3001",
|
| 111 |
]
|
| 112 |
+
_raw_origins = settings.cors_allowed_origins if settings.cors_allowed_origins else _DEFAULT_ORIGINS
|
| 113 |
+
# Remove '*' if present to avoid browser credential rejection
|
| 114 |
+
_cors_origins = [o.strip() for o in _raw_origins if o.strip() and o.strip() != "*"] or _DEFAULT_ORIGINS
|
| 115 |
+
|
| 116 |
+
@app.middleware("http")
|
| 117 |
+
async def normalize_path(request, call_next):
|
| 118 |
+
# Fix double slashes in paths (e.g., //api/usage -> /api/usage)
|
| 119 |
+
path = request.scope.get("path")
|
| 120 |
+
if path and "//" in path:
|
| 121 |
+
request.scope["path"] = path.replace("//", "/")
|
| 122 |
+
return await call_next(request)
|
| 123 |
+
|
| 124 |
+
app.add_middleware(ProxyHeadersMiddleware, trusted_hosts="*")
|
| 125 |
|
| 126 |
+
# CORSMiddleware MUST be added LAST so it becomes the outermost layer in Starlette's middleware stack.
|
| 127 |
app.add_middleware(
|
| 128 |
CORSMiddleware,
|
| 129 |
allow_origins=_cors_origins,
|