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
POSTs) are rejected here. +CSRF_HEADER = "X-CSRF-Guard" +CSRF_PROTECTED_METHODS = {"POST", "PUT", "PATCH", "DELETE"} + + +class CSRFHeaderMiddleware(BaseHTTPMiddleware): + async def dispatch(self, request: Request, call_next): + if request.method in CSRF_PROTECTED_METHODS and CSRF_HEADER not in request.headers: + return JSONResponse( + content={ + "error_type": "http", + "status_code": status.HTTP_403_FORBIDDEN, + "title_message": "불편을 드려 죄송합니다", + "detail": "허용되지 않은 요청입니다. 페이지를 새로고침한 뒤 다시 시도해주세요.", + }, + status_code=status.HTTP_403_FORBIDDEN, + ) + return await call_next(request) diff --git a/client/public/samples/images/asian_fake_2.JPG b/client/public/samples/images/asian_fake_2.JPG deleted file mode 100644 index 2e5cb743..00000000 Binary files a/client/public/samples/images/asian_fake_2.JPG and /dev/null differ diff --git a/client/src/components/Navbar.js b/client/src/components/Navbar.js index ae6cdbaf..8b47f52b 100644 --- a/client/src/components/Navbar.js +++ b/client/src/components/Navbar.js @@ -31,7 +31,7 @@ const Navbar = ({ variant = 'dark', sessionUser, onLogout, links = [], logoutRed const handleLogoutClick = async () => { if (!window.confirm("로그아웃 하시겠습니까?")) return; try { - await axios.get('/auth/logout'); + await axios.post('/auth/logout'); if (onLogout) onLogout(); alert("성공적으로 로그아웃되었습니다."); navigate(logoutRedirect); diff --git a/client/src/index.js b/client/src/index.js index e86f3e12..b7a86666 100644 --- a/client/src/index.js +++ b/client/src/index.js @@ -9,6 +9,7 @@ import axios from 'axios'; axios.defaults.withCredentials = true; axios.defaults.baseURL = '/api'; axios.defaults.headers.common['ngrok-skip-browser-warning'] = 'true'; +axios.defaults.headers.common['X-CSRF-Guard'] = '1'; const root = ReactDOM.createRoot(document.getElementById('root')); root.render( diff --git a/client/src/pages/AnalysisDetailPage.js b/client/src/pages/AnalysisDetailPage.js index e4db7e2c..d2adfc31 100644 --- a/client/src/pages/AnalysisDetailPage.js +++ b/client/src/pages/AnalysisDetailPage.js @@ -72,8 +72,8 @@ const VERDICT_COPY = { }, real: { badge: '실제 얼굴로 보여요', - image: '뚜렷한 위조 흔적이 발견되지 않았어요. 히트맵에서 모델이 본 부위를 확인할 수 있어요.', - video: '뚜렷한 위조 흔적이 발견되지 않았어요. 타임라인에서 구간별 결과를 확인할 수 있어요.', + image: '뚜렷한 위조 흔적이 발견되지 않았어요.', + video: '영상에서 뚜렷한 위조 흔적이 발견되지 않았어요.', }, unknown: { badge: '판정할 수 없어요', @@ -277,7 +277,7 @@ const AnalysisDetailPage = ({ sessionUser }) => { {VERDICT_COPY[verdictTone].badge}

{VERDICT_COPY[verdictTone][isVideo ? 'video' : 'image']}

- {mediaLoc && ( + {mediaLoc && verdictTone !== 'real' && (