Spaces:
Sleeping
Sleeping
fix(auth): trust reverse proxy headers and force HTTPS scheme in production
Browse files- app/main.py +10 -1
app/main.py
CHANGED
|
@@ -9,7 +9,7 @@ print("PROMETHEUS_MULTIPROC_DIR set to:", os.environ.get("PROMETHEUS_MULTIPROC_D
|
|
| 9 |
print("Does directory exist?:", os.path.isdir(metrics_path))
|
| 10 |
print("----------------------------")
|
| 11 |
|
| 12 |
-
from fastapi import FastAPI, Response # type: ignore
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware # type: ignore
|
| 14 |
from prometheus_client import CollectorRegistry, multiprocess, generate_latest, CONTENT_TYPE_LATEST
|
| 15 |
from starlette.middleware.sessions import SessionMiddleware # type: ignore
|
|
@@ -36,6 +36,15 @@ app.add_middleware(
|
|
| 36 |
)
|
| 37 |
|
| 38 |
# Register routes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
app.include_router(cases_router)
|
| 40 |
app.include_router(auth_router)
|
| 41 |
|
|
|
|
| 9 |
print("Does directory exist?:", os.path.isdir(metrics_path))
|
| 10 |
print("----------------------------")
|
| 11 |
|
| 12 |
+
from fastapi import FastAPI, Response, Request # type: ignore
|
| 13 |
from fastapi.middleware.cors import CORSMiddleware # type: ignore
|
| 14 |
from prometheus_client import CollectorRegistry, multiprocess, generate_latest, CONTENT_TYPE_LATEST
|
| 15 |
from starlette.middleware.sessions import SessionMiddleware # type: ignore
|
|
|
|
| 36 |
)
|
| 37 |
|
| 38 |
# Register routes
|
| 39 |
+
@app.middleware("http")
|
| 40 |
+
async def forward_proto_middleware(request: Request, call_next):
|
| 41 |
+
# Trust Hugging Face reverse proxy protocol header
|
| 42 |
+
proto = request.headers.get("x-forwarded-proto")
|
| 43 |
+
if proto:
|
| 44 |
+
request.scope["scheme"] = proto
|
| 45 |
+
elif "localhost" not in request.url.host and "127.0.0.1" not in request.url.host:
|
| 46 |
+
request.scope["scheme"] = "https"
|
| 47 |
+
return await call_next(request)
|
| 48 |
app.include_router(cases_router)
|
| 49 |
app.include_router(auth_router)
|
| 50 |
|