The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(auth): make prefixed cookie deletions valid per cookie-prefix rules
Ben Barclay52629a5de656
diff --git a/hermes_cli/dashboard_auth/cookies.py b/hermes_cli/dashboard_auth/cookies.pyindex f212a99250b..5d41bc31d23 100644--- a/hermes_cli/dashboard_auth/cookies.py+++ b/hermes_cli/dashboard_auth/cookies.py@@ -56,7 +56,7 @@ Refresh-token handling:"""from __future__ import annotations-from typing import Optional, Tuple+from typing import Literal, Optional, Tuplefrom fastapi import Requestfrom fastapi.responses import Response@@ -212,6 +212,46 @@ def set_session_cookies()+def _clear_cookie_variants(+ response: Response,+ bare_name: str,+ *,+ prefix: str,+ https_samesite: Literal["lax", "strict", "none"],+ bare_attrs: dict,+) -> None:+ """Emit Max-Age=0 deletions for every plausible name variant of a cookie.++ Cookie-prefix rules make the deletion shape load-bearing: a Set-Cookie+ for a ``__Host-``/``__Secure-`` name is rejected outright by the+ browser unless it carries ``Secure`` (and ``__Host-`` additionally+ requires ``Path=/``), so those deletions always carry the attributes+ their name demands. The bare-name deletion mirrors the shape the+ setter uses (``bare_attrs``) — under RFC 6265bis a deletion sent from+ a secure origin may omit ``Secure`` and still delete a Secure cookie,+ while a ``Secure`` deletion on a plain-HTTP origin can be ignored, so+ matching the setter is the shape that works on both origins.+ """+ for variant in _NAME_VARIANTS:+ if variant == "__Host-":+ # __Host- demands Secure AND Path=/ or the header is invalid.+ response.set_cookie(+ f"{variant}{bare_name}", "", max_age=0,+ path="/", httponly=True, samesite=https_samesite,+ secure=True,+ )+ elif variant == "__Secure-":+ response.set_cookie(+ f"{variant}{bare_name}", "", max_age=0,+ path=_cookie_path(prefix), httponly=True,+ samesite=https_samesite, secure=True,+ )+ else:+ response.set_cookie(+ bare_name, "", max_age=0, **bare_attrs,+ )++def clear_session_cookies(response: Response, *, prefix: str = "") -> None:"""Emit Max-Age=0 deletions for both session cookies.@@ -221,19 +261,13 @@ def clear_session_cookies(response: Response, *, prefix: str = "") -> None:depends on the request that set it), so we emit deletions for everyplausible variant under the active path."""- path = _cookie_path(prefix)- for variant in _NAME_VARIANTS:- response.set_cookie(- f"{variant}{SESSION_AT_COOKIE}", "", max_age=0,- path=path, httponly=True, samesite="lax",- )- response.set_cookie(- f"{variant}{SESSION_RT_COOKIE}", "", max_age=0,- path=path, httponly=True, samesite="lax",- )- response.set_cookie(- f"{variant}{SESSION_PROVIDER_COOKIE}", "", max_age=0,- path=path, httponly=True, samesite="lax",+ bare_attrs = {+ "path": _cookie_path(prefix), "httponly": True, "samesite": "lax",+ }+ for name in (SESSION_AT_COOKIE, SESSION_RT_COOKIE, SESSION_PROVIDER_COOKIE):+ _clear_cookie_variants(+ response, name,+ prefix=prefix, https_samesite="lax", bare_attrs=bare_attrs,)@@ -285,22 +319,11 @@ def clear_pkce_cookie(can only ever have been set on an HTTPS origin — always carry``Secure; SameSite=None``."""- path = _cookie_path(prefix)- for variant in _NAME_VARIANTS:- if variant:- # __Host-/__Secure- cookies only exist over HTTPS, and a- # deletion for a prefixed name must itself be Secure or the- # browser rejects the header outright.- response.set_cookie(- f"{variant}{PKCE_COOKIE}", "", max_age=0,- path=path, httponly=True, samesite="none",- secure=True,- )- else:- response.set_cookie(- f"{variant}{PKCE_COOKIE}", "", max_age=0,- **_pkce_attrs(use_https=use_https, prefix=prefix),- )+ _clear_cookie_variants(+ response, PKCE_COOKIE,+ prefix=prefix, https_samesite="none",+ bare_attrs=_pkce_attrs(use_https=use_https, prefix=prefix),+ )def _read_with_fallback(@@ -365,12 +388,13 @@ def clear_sso_attempt_cookie(response: Response, *, prefix: str = "") -> None:Called on a successful callback and whenever the gate falls back to/login, so the marker never lingers to suppress a later silent attempt."""- path = _cookie_path(prefix)- for variant in _NAME_VARIANTS:- response.set_cookie(- f"{variant}{SSO_ATTEMPT_COOKIE}", "", max_age=0,- path=path, httponly=True, samesite="lax",- )+ _clear_cookie_variants(+ response, SSO_ATTEMPT_COOKIE,+ prefix=prefix, https_samesite="lax",+ bare_attrs={+ "path": _cookie_path(prefix), "httponly": True, "samesite": "lax",+ },+ )def detect_https(request: Request) -> bool:diff --git a/tests/hermes_cli/test_dashboard_auth_cookies.py b/tests/hermes_cli/test_dashboard_auth_cookies.pyindex 04f91e7b25a..0d362b76c34 100644--- a/tests/hermes_cli/test_dashboard_auth_cookies.py+++ b/tests/hermes_cli/test_dashboard_auth_cookies.py@@ -206,3 +206,26 @@ def test_clear_pkce_cookie_http_bare_deletion_is_insecure_lax():deletion = next(c for c in cookies if c.startswith(f'{name}="'))assert "Max-Age=0" in deletionassert "Secure" in deletion+++def test_clear_session_cookies_prefixed_deletions_carry_secure():+ """__Host-/__Secure- deletions must carry Secure (and __Host- Path=/):+ browsers reject a prefixed Set-Cookie that violates its prefix rules,+ so an insecure deletion for __Host-hermes_session_at is silently+ ignored and the session cookie survives logout on HTTPS origins."""+ client = TestClient(_build_app(use_https=True, prefix=""))+ cookies = client.get("/clear").headers.get_list("set-cookie")+ for name in (SESSION_AT_COOKIE, SESSION_RT_COOKIE, SESSION_PROVIDER_COOKIE):+ host = next(c for c in cookies if c.startswith(f'__Host-{name}="'))+ assert "Secure" in host+ assert "Path=/;" in host or host.rstrip().endswith("Path=/")+ secure = next(c for c in cookies if c.startswith(f'__Secure-{name}="'))+ assert "Secure" in secure+ bare = next(+ c for c in cookies+ if c.startswith(f'{name}="') and not c.startswith("__")+ )+ # Bare-name deletion mirrors the bare setter (Lax, no Secure) so+ # it still works on plain-HTTP origins.+ assert "Secure" not in bare+ assert "Max-Age=0" in bare