Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions backend/common/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,19 @@ async def dispatch(self, request: Request, call_next):
status_code=500,
content={"detail": "Internal Server Error"}
)

class CORSLoggingMiddleware(BaseHTTPMiddleware):
"""
Middleware to log CORS related information.
Checks if Origin header is present but Access-Control-Allow-Origin is missing in response.
"""
async def dispatch(self, request: Request, call_next):
origin = request.headers.get("origin")
response = await call_next(request)

# Log if origin is present but no CORS headers in response
if origin and "access-control-allow-origin" not in response.headers:
logger = get_logger("backend.middleware.cors")
logger.warning(f"CORS Missing Header for origin: {origin} - Path: {request.url.path}")

return response
6 changes: 5 additions & 1 deletion backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from backend.common.middleware import PathRewriteMiddleware, ExceptionHandlingMiddleware
from backend.common.middleware import PathRewriteMiddleware, ExceptionHandlingMiddleware, CORSLoggingMiddleware
from backend.api.problems import router as problems_router
from backend.api.sql import router as sql_router
from backend.api.stats import router as stats_router
Expand Down Expand Up @@ -133,6 +133,10 @@ def start_scheduler_background():
allow_methods=["*"],
allow_headers=["*"],
)

# CORS Logging Middleware 등록
# CORS 미들웨어보다 나중에 등록하여 바깥쪽에 위치하게 함 (응답 헤더 확인용)
app.add_middleware(CORSLoggingMiddleware)

# 404 및 기타 에러 로깅 미들웨어
@app.middleware("http")
Expand Down
Loading