Fix: secure cookie only over HTTPS, not plain HTTP

This commit is contained in:
Georges Haddad
2026-04-10 16:15:37 +03:00
parent 7305e54f66
commit 61b9c23803
+3 -1
View File
@@ -42,12 +42,14 @@ async def login_post(request: Request, username: str = Form(...), password: str
sessions[session_id] = dict(user) sessions[session_id] = dict(user)
from config import ENVIRONMENT from config import ENVIRONMENT
# secure=True only when accessed via HTTPS (check X-Forwarded-Proto from nginx)
is_https = ENVIRONMENT == "production" and request.headers.get("x-forwarded-proto") == "https"
response = RedirectResponse(url="/", status_code=status.HTTP_303_SEE_OTHER) response = RedirectResponse(url="/", status_code=status.HTTP_303_SEE_OTHER)
response.set_cookie( response.set_cookie(
key="session_id", key="session_id",
value=session_id, value=session_id,
httponly=True, httponly=True,
secure=ENVIRONMENT == "production", secure=is_https,
samesite="lax", samesite="lax",
max_age=86400, # 24 hours max_age=86400, # 24 hours
) )