FastAPI Response Set-Cookie와 HttpOnly 보안 구현

2025. 2. 27. 10:26·파이썬/Fast API
반응형

FastAPI에서 HttpOnly 쿠키를 설정하여 보안을 강화하는 방법을 알아보겠습니다.

기본 쿠키 설정

from fastapi import FastAPI, Response
from fastapi.responses import JSONResponse

app = FastAPI()

@app.post("/login")
async def login(response: Response):
    # 기본적인 쿠키 설정
    response.set_cookie(
        key="session",
        value="abc123",
        httponly=True,
        secure=True,
        samesite='lax',
        max_age=1800  # 30분
    )
    return {"message": "로그인 성공"}

보안 옵션 설정 비교

옵션 설명 권장 설정
httponly JavaScript 접근 차단 True
secure HTTPS에서만 전송 True
samesite CSRF 보호 'lax' 또는 'strict'
max_age 쿠키 유효 기간 세션에 따라 적절히 설정
domain 쿠키 도메인 필요한 경우만 설정
path 쿠키 경로 기본값 '/' 권장

실제 구현 예시

from fastapi import FastAPI, Response, HTTPException
from datetime import datetime
import jwt

app = FastAPI()

SECRET_KEY = "your-secret-key"

@app.post("/login")
async def login(username: str, password: str, response: Response):
    # 인증 로직
    if not authenticate_user(username, password):
        raise HTTPException(status_code=401, detail="인증 실패")

    # JWT 토큰 생성
    token = jwt.encode(
        {
            "sub": username,
            "exp": datetime.utcnow() + timedelta(minutes=30)
        },
        SECRET_KEY,
        algorithm="HS256"
    )

    # 보안 쿠키 설정
    response.set_cookie(
        key="session_token",
        value=token,
        httponly=True,
        secure=True,
        samesite='lax',
        max_age=1800,
        path="/"
    )

    return {"message": "로그인 성공"}

쿠키 관리 유틸리티

class CookieManager:
    def __init__(self, response: Response):
        self.response = response

    def set_secure_cookie(
        self,
        key: str,
        value: str,
        max_age: int = 1800,
        secure: bool = True
    ):
        self.response.set_cookie(
            key=key,
            value=value,
            httponly=True,
            secure=secure,
            samesite='lax',
            max_age=max_age,
            path="/"
        )

    def delete_cookie(self, key: str):
        self.response.delete_cookie(
            key=key,
            path="/",
            httponly=True
        )

@app.post("/auth")
async def auth_handler(response: Response):
    cookie_manager = CookieManager(response)
    cookie_manager.set_secure_cookie("auth_token", "user_token")
    return {"status": "success"}

전역 미들웨어 설정

from fastapi.middleware.base import BaseHTTPMiddleware
from fastapi import Request

class SecurityHeadersMiddleware(BaseHTTPMiddleware):
    async def dispatch(self, request: Request, call_next):
        response = await call_next(request)
        response.headers['X-Content-Type-Options'] = 'nosniff'
        response.headers['X-Frame-Options'] = 'DENY'
        response.headers['X-XSS-Protection'] = '1; mode=block'
        return response

app.add_middleware(SecurityHeadersMiddleware)

쿠키 검증과 처리

from fastapi import Cookie, Depends

async def verify_token(
    session_token: str = Cookie(None)
) -> dict:
    if not session_token:
        raise HTTPException(
            status_code=401,
            detail="인증되지 않은 요청"
        )

    try:
        payload = jwt.decode(
            session_token,
            SECRET_KEY,
            algorithms=["HS256"]
        )
        return payload
    except jwt.ExpiredSignatureError:
        raise HTTPException(
            status_code=401,
            detail="토큰 만료"
        )
    except jwt.JWTError:
        raise HTTPException(
            status_code=401,
            detail="유효하지 않은 토큰"
        )

@app.get("/protected")
async def protected_route(payload: dict = Depends(verify_token)):
    return {"user": payload["sub"]}

 

HttpOnly 쿠키를 사용할 때 주의할 점

  1. 보안 설정
    • 항상 httponly=True 설정
    • 가능한 경우 secure=True 사용
    • 적절한 samesite 정책 적용
  2. 에러 처리
    • 쿠키 누락 상황 처리
    • 만료된 쿠키 처리
    • 잘못된 형식의 쿠키 처리
  3. 세션 관리
    • 적절한 만료 시간 설정
    • 세션 갱신 메커니즘 구현
    • 로그아웃 시 쿠키 삭제

이러한 보안 설정을 통해 XSS와 CSRF 같은 일반적인 웹 취약점으로부터 애플리케이션을 보호할 수 있습니다.

저작자표시 비영리 변경금지 (새창열림)

'파이썬 > Fast API' 카테고리의 다른 글

FastAPI에서 파일 다운로드 구현 가이드: 방식별 특징과 한글 파일명 문제 해결  (0) 2025.04.16
FastAPI Security: API 보안 구현  (0) 2025.02.24
SlowAPI: FastAPI에서 Rate Limiting 구현  (0) 2025.02.23
FastAPI Permissions: 권한 관리 구현  (0) 2025.02.22
FastAPI-Users with MongoDB: JWT 인증  (0) 2025.02.21
'파이썬/Fast API' 카테고리의 다른 글
  • FastAPI에서 파일 다운로드 구현 가이드: 방식별 특징과 한글 파일명 문제 해결
  • FastAPI Security: API 보안 구현
  • SlowAPI: FastAPI에서 Rate Limiting 구현
  • FastAPI Permissions: 권한 관리 구현
코샵
코샵
나의 코딩 일기장
    반응형
  • 코샵
    끄적끄적 코딩 공방
    코샵
  • 전체
    오늘
    어제
    • 분류 전체보기 (525) N
      • 상품 추천 (41) N
      • MongoDB (4)
      • 하드웨어 (5) N
      • 일기장 (4)
      • Unity (138)
        • Tip (41)
        • Project (1)
        • Design Pattern (8)
        • Firebase (6)
        • Asset (2)
      • 파이썬 (127)
        • Basic (40)
        • OpenCV (8)
        • Pandas (15)
        • PyQT (3)
        • SBC(Single Board Computer) (1)
        • 크롤링 (14)
        • Fast API (29)
        • Package (6)
      • Linux (4)
      • C# (97)
        • Algorithm (11)
        • Window (7)
      • TypeScript (48)
        • CSS (10)
      • Git (11)
      • SQL (5)
      • Flutter (10)
        • Tip (1)
      • System (1)
      • BaekJoon (6)
      • Portfolio (2)
      • MacOS (1)
      • 유틸리티 (1)
      • 서비스 (6)
      • 자동화 (3)
      • Hobby (10)
        • 물생활 (10)
        • 식집사 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 다비즈
  • 공지사항

  • 인기 글

  • 태그

    유니티
    리뷰관리
    상품 리뷰 크롤링
    codingcommunity
    긴유통기한우유
    쇼핑몰리뷰
    파이썬
    rtsp
    C#
    셀레니움
    ipcamera
    cv2
    카페24리뷰
    learntocode
    라떼우유
    스마트스토어리뷰
    리스트
    리뷰이관
    devlife
    list
    programmerlife
    codingtips
    Python
    unity
    programming101
    카페24리뷰이관
    appdevelopment
    믈레코비타멸균우유
    스크립트 실행 순서
    스크립트 실행
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
코샵
FastAPI Response Set-Cookie와 HttpOnly 보안 구현
상단으로

티스토리툴바