Skip to content

Commit f87f319

Browse files
committed
feat(backend): configure secure CORS
1 parent ef7d4a6 commit f87f319

1 file changed

Lines changed: 12 additions & 7 deletions

File tree

backend-api/app/main.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
from fastapi import FastAPI
22
from fastapi.middleware.cors import CORSMiddleware
3-
from app.core.logging import setup_logging
3+
44
from app.api.v1.router import api_router
55
from app.core.config import get_settings
6+
from app.core.errors import NotFound, not_found_handler
7+
from app.core.logging import setup_logging
68
from app.core.middleware import RequestLoggingMiddleware
7-
from app.core.errors import not_found_handler, NotFound
9+
810
settings = get_settings()
911

1012

@@ -16,15 +18,17 @@ def create_app() -> FastAPI:
1618
# (middleware executes in reverse order - last added runs first)
1719
app.add_middleware(RequestLoggingMiddleware)
1820

19-
# Allow frontend (localhost:3000 and others) to call the API during development.
20-
# CORS must be added last so it runs first and wraps all responses including errors.
21+
# Allow the configured frontend to make credentialed API requests.
22+
# Expose X-Request-ID so the frontend can use it when reporting errors.
2123
app.add_middleware(
2224
CORSMiddleware,
23-
allow_origins=["*"], # permissive for dev; adjust in prod
24-
allow_credentials=False, # must be False when using wildcard origins
25+
allow_origins=[settings.FRONTEND_URL.rstrip("/")],
26+
allow_credentials=True,
2527
allow_methods=["*"],
2628
allow_headers=["*"],
29+
expose_headers=["X-Request-ID"],
2730
)
31+
2832
app.include_router(api_router, prefix=settings.API_PREFIX)
2933

3034
# error handler
@@ -39,7 +43,8 @@ def health_check():
3943
return {
4044
"status": "healthy",
4145
}
42-
46+
4347
return app
4448

49+
4550
app = create_app()

0 commit comments

Comments
 (0)