fix: add fallback CORS middleware to guarantee Access-Control-Allow-Origin

CORSMiddleware only fires when Origin header reaches the app. Cloudflare or
proxy layers can strip it before the middleware sees it. The fallback http
middleware ensures the header is always present when Origin is in the request.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
vee1e 2026-06-02 13:27:37 +05:30
parent fc74d0b539
commit afc9f6b06d
No known key found for this signature in database
GPG key ID: EB498AFC60A7A01A

View file

@ -36,6 +36,16 @@ app.add_middleware(
allow_headers=["*"],
)
@app.middleware("http")
async def add_cors_header(request: Request, call_next):
response = await call_next(request)
origin = request.headers.get("origin", "")
if origin and "access-control-allow-origin" not in response.headers:
response.headers["access-control-allow-origin"] = "*"
response.headers["access-control-allow-methods"] = "GET, POST, PUT, DELETE, OPTIONS"
response.headers["access-control-allow-headers"] = "*"
return response
db_service = DatabaseService()
METRICS_FILE = os.path.join(os.path.dirname(__file__), 'metrics.txt')