Skip to content

Commit afae2c5

Browse files
committed
feat: update anonymous chat cookie settings for cross-site compatibility
- Implemented dynamic SameSite and Secure cookie settings based on the backend URL context. - Enhanced cookie handling to ensure proper functionality in cross-domain scenarios.
1 parent 2cb30c6 commit afae2c5

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

surfsense_backend/app/routes/anonymous_chat_routes.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@
2626
ANON_COOKIE_NAME = "surfsense_anon_session"
2727
ANON_COOKIE_MAX_AGE = config.ANON_TOKEN_QUOTA_TTL_DAYS * 86400
2828

29+
# Cross-site cookie settings: when the backend runs on a different domain
30+
# than the frontend (e.g. api.x.com vs www.y.com), browsers reject
31+
# SameSite=Lax cookies on fetch() calls. Use SameSite=None + Secure
32+
# in production (HTTPS) so the cookie is sent cross-site.
33+
_IS_SECURE_CONTEXT = bool(
34+
config.BACKEND_URL and config.BACKEND_URL.startswith("https://")
35+
)
36+
_COOKIE_SAMESITE: str = "none" if _IS_SECURE_CONTEXT else "lax"
37+
_COOKIE_SECURE: bool = _IS_SECURE_CONTEXT
38+
2939

3040
# ---------------------------------------------------------------------------
3141
# Helpers
@@ -43,8 +53,8 @@ def _get_or_create_session_id(request: Request, response: Response) -> str:
4353
value=session_id,
4454
max_age=ANON_COOKIE_MAX_AGE,
4555
httponly=True,
46-
samesite="lax",
47-
secure=request.url.scheme == "https",
56+
samesite=_COOKIE_SAMESITE,
57+
secure=_COOKIE_SECURE,
4858
path="/",
4959
)
5060
return session_id

0 commit comments

Comments
 (0)