Skip to content

Commit 13b59d3

Browse files
committed
feat(backend): Make session cookie secure in production
- Add IS_PRODUCTION flag from ENVIRONMENT env var - Set secure=True for cookie only in production (requires HTTPS) - Development uses secure=False for localhost testing Part of #93
1 parent fc1690e commit 13b59d3

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

backend/routes/playground.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
- IP fallback: 100 searches/day for shared networks
77
- Global circuit breaker: 10k searches/hour (cost protection)
88
"""
9+
import os
910
from typing import Optional
1011
from fastapi import APIRouter, HTTPException, Request, Response
1112
from pydantic import BaseModel
@@ -24,6 +25,7 @@
2425
# Session cookie config
2526
SESSION_COOKIE_NAME = "pg_session"
2627
SESSION_COOKIE_MAX_AGE = 86400 # 24 hours
28+
IS_PRODUCTION = os.getenv("ENVIRONMENT", "development").lower() == "production"
2729

2830

2931
class PlaygroundSearchRequest(BaseModel):
@@ -72,9 +74,9 @@ def _set_session_cookie(response: Response, token: str):
7274
key=SESSION_COOKIE_NAME,
7375
value=token,
7476
max_age=SESSION_COOKIE_MAX_AGE,
75-
httponly=True, # Can't be accessed by JavaScript
76-
samesite="lax", # CSRF protection
77-
secure=False, # Set True in production with HTTPS
77+
httponly=True, # Can't be accessed by JavaScript
78+
samesite="lax", # CSRF protection
79+
secure=IS_PRODUCTION, # HTTPS only in production
7880
)
7981

8082

0 commit comments

Comments
 (0)