Fix: secure cookie only over HTTPS, not plain HTTP
This commit is contained in:
+3
-1
@@ -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
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user