diff --git a/.claude/settings.json b/.claude/settings.json new file mode 100644 index 00000000..7d7563d2 --- /dev/null +++ b/.claude/settings.json @@ -0,0 +1,24 @@ +{ + "permissions": { + "allow": [ + "Bash(ruff check *)", + "Bash(ruff format *)", + "Bash(pytest *)", + "Bash(git status *)", + "Bash(git diff *)", + "Bash(git log *)", + "Bash(npm test *)" + ], + "ask": [ + "Bash(git push *)" + ], + "deny": [ + "Read(**/.env)", + "Read(**/.env.local)", + "Read(**/.env.*.local)", + "Edit(**/.env)", + "Edit(**/.env.local)", + "Edit(**/.env.*.local)" + ] + } +} diff --git a/App/main.py b/App/main.py index 6d3dab25..130b6cd3 100644 --- a/App/main.py +++ b/App/main.py @@ -26,6 +26,10 @@ # Register static files (images, videos) app.mount("/static", StaticFiles(directory="static"), name="static") +# CSRF defense: reject state-changing requests without the custom header +# (registered before CORS so CORS stays outside it and still handles preflight) +app.add_middleware(middleware.CSRFHeaderMiddleware) + # CORS Setup for cross-origin requests # FrontEnd: React, BackEnd: FastAPI origins_str = os.getenv("CORS_ALLOWED_ORIGINS") diff --git a/App/routes/auth.py b/App/routes/auth.py index 314f3bb3..12557273 100644 --- a/App/routes/auth.py +++ b/App/routes/auth.py @@ -73,7 +73,7 @@ async def login_user(request: Request, "status": "success" } -@router.get("/logout", status_code=status.HTTP_200_OK, response_class=JSONResponse, summary="로그아웃") +@router.post("/logout", status_code=status.HTTP_200_OK, response_class=JSONResponse, summary="로그아웃") async def logout_user(request: Request): request.state.session.clear() return { diff --git a/App/services/auth_svc.py b/App/services/auth_svc.py index 8aaeaacd..2bfd6762 100644 --- a/App/services/auth_svc.py +++ b/App/services/auth_svc.py @@ -46,7 +46,7 @@ async def get_user_by_email(conn: Connection, email: str) -> UserData | None: HTTPException 500: When an unexpected exception occurs. """ try: - query = f""" + query = """ SELECT id, name, email from user where email = :email """ @@ -86,7 +86,7 @@ async def register_user(conn: Connection, name: str, email:str, hashed_password: HTTPException 400: When the DB INSERT fails or the request data is invalid. """ try: - query = f""" + query = """ INSERT INTO user(name, email, hashed_password) values (:name, :email, :hashed_password) """ diff --git a/App/utils/middleware.py b/App/utils/middleware.py index 12b2bc68..6aac673c 100644 --- a/App/utils/middleware.py +++ b/App/utils/middleware.py @@ -1,4 +1,5 @@ -from fastapi import Request, FastAPI +from fastapi import Request, FastAPI, status +from fastapi.responses import JSONResponse from starlette.middleware.base import BaseHTTPMiddleware import redis.asyncio as aioredis import uuid @@ -51,7 +52,7 @@ async def dispatch(self, request: Request, call_next): or request.headers.get('x-forwarded-proto') == 'https') response.set_cookie(self.session_cookie, session_id, max_age=self.max_age, httponly=True, - secure=is_https, samesite='None' if is_https else 'Lax') + secure=is_https, samesite='Lax') await redis_client.setex(session_id, self.max_age, json.dumps(request.state.session)) else: @@ -63,4 +64,24 @@ async def dispatch(self, request: Request, call_next): return response - \ No newline at end of file + +# CSRF defense: state-changing requests must carry a custom header. +# A cross-origin page can't add it without passing a CORS preflight, and requests +# without it (e.g. plain