Skip to content

The agent that grows with you

fix(dashboard): prefix post-login landing behind reverse proxy

liuhao1024e33cf1b2569a
diff --git a/hermes_cli/dashboard_auth/routes.py b/hermes_cli/dashboard_auth/routes.py
index e047cecd3d4..2f8a5e385cb 100644
--- a/hermes_cli/dashboard_auth/routes.py
+++ b/hermes_cli/dashboard_auth/routes.py
@@ -141,6 +141,23 @@ def _login_success(request: Request, session: Session, provider: str) -> None:
email=session.email, org_id=session.org_id)
+def _externalize_post_login_target(target: str, prefix: str) -> str:
+ """Prepend *prefix* to *target* when behind a reverse proxy.
+
+ When the dashboard is mounted at a sub-path (e.g. ``/hermes``) via
+ ``X-Forwarded-Prefix``, a bare ``/`` landing must become ``/hermes/``
+ so the browser stays inside the mount. Paths that already carry the
+ prefix are returned unchanged; the native loopback redirect (an
+ absolute ``http://127.0.0.1...`` URL) never matches and is untouched.
+ """
+ prefix = prefix.rstrip("/")
+ if not target or not prefix:
+ return target
+ if target == prefix or target.startswith(f"{prefix}/"):
+ return target
+ return f"{prefix}{target}"
+
+
def _complete_login(request: Request, provider: str, session: Session, *, broker_state: str,
next_raw: str) -> tuple:
"""Shared tail of the callback + password routes after credentials verified: audit success,
@@ -150,7 +167,8 @@ def _complete_login(request: Request, provider: str, session: Session, *, broker
if broker_state:
return _finish_native_login(
request, broker_state=broker_state, session=session, provider=provider), True
- return _validate_post_login_target(next_raw) or "/", False
+ landing = _validate_post_login_target(next_raw) or "/"
+ return _externalize_post_login_target(landing, _prefix(request)), False
def _start_upstream_login(request: Request, p, *, audit_failure: bool, extra_pkce: dict[str, str]):
diff --git a/tests/hermes_cli/test_dashboard_auth_prefix.py b/tests/hermes_cli/test_dashboard_auth_prefix.py
index e18e58de267..d3c05110246 100644
--- a/tests/hermes_cli/test_dashboard_auth_prefix.py
+++ b/tests/hermes_cli/test_dashboard_auth_prefix.py
@@ -525,3 +525,118 @@ class TestCookiePathRespectsPrefix:
assert "Path=/hermes" in at_cookies[0]
assert "Secure" in at_cookies[0]
assert "HttpOnly" in at_cookies[0]
+
+
+# ---------------------------------------------------------------------------
+# Post-login landing respects X-Forwarded-Prefix
+# ---------------------------------------------------------------------------
+
+
+class TestExternalizePostLoginTarget:
+ """Unit tests for ``_externalize_post_login_target``."""
+
+ def test_bare_slash_gets_prefix(self):
+ from hermes_cli.dashboard_auth.routes import (
+ _externalize_post_login_target,
+ )
+
+ assert _externalize_post_login_target("/", "/hermes") == "/hermes/"
+
+ def test_prefixed_path_unchanged(self):
+ from hermes_cli.dashboard_auth.routes import (
+ _externalize_post_login_target,
+ )
+
+ assert (
+ _externalize_post_login_target("/hermes/sessions", "/hermes")
+ == "/hermes/sessions"
+ )
+
+ def test_empty_target_returns_empty(self):
+ from hermes_cli.dashboard_auth.routes import (
+ _externalize_post_login_target,
+ )
+
+ assert _externalize_post_login_target("", "/hermes") == ""
+
+ def test_empty_prefix_returns_target_unchanged(self):
+ from hermes_cli.dashboard_auth.routes import (
+ _externalize_post_login_target,
+ )
+
+ assert _externalize_post_login_target("/", "") == "/"
+
+ def test_trailing_slash_on_prefix_stripped(self):
+ from hermes_cli.dashboard_auth.routes import (
+ _externalize_post_login_target,
+ )
+
+ assert _externalize_post_login_target("/", "/hermes/") == "/hermes/"
+
+ def test_exact_prefix_match_returns_target(self):
+ from hermes_cli.dashboard_auth.routes import (
+ _externalize_post_login_target,
+ )
+
+ assert _externalize_post_login_target("/hermes", "/hermes") == "/hermes"
+
+
+class TestPostLoginLandingPrefix:
+ """Integration: callback and password-login landing URLs carry prefix."""
+
+ def test_callback_redirect_location_carries_prefix(
+ self, gated_app_proxied
+ ):
+ """After OAuth callback with prefix, the 302 Location must be
+ ``/hermes/`` not bare ``/``."""
+ r1 = gated_app_proxied.get(
+ "/auth/login?provider=stub",
+ headers={"x-forwarded-prefix": "/hermes"},
+ follow_redirects=False,
+ )
+ pkce_set = next(
+ c for c in r1.headers.get_list("set-cookie")
+ if "hermes_session_pkce" in c
+ )
+ pkce_kv = pkce_set.split(";", 1)[0]
+ state = r1.headers["location"].split("state=")[1]
+
+ r2 = gated_app_proxied.get(
+ f"/auth/callback?code=stub_code&state={state}",
+ headers={
+ "x-forwarded-prefix": "/hermes",
+ "cookie": pkce_kv,
+ },
+ follow_redirects=False,
+ )
+ assert r2.status_code == 302, r2.text
+ location = r2.headers["location"]
+ assert location.startswith("/hermes/"), (
+ f"callback redirect should carry prefix, got {location!r}"
+ )
+
+ def test_callback_redirect_no_prefix_direct_deploy(
+ self, gated_app_direct
+ ):
+ """Without prefix, callback redirects to bare ``/``."""
+ r1 = gated_app_direct.get(
+ "/auth/login?provider=stub",
+ follow_redirects=False,
+ )
+ pkce_set = next(
+ c for c in r1.headers.get_list("set-cookie")
+ if "hermes_session_pkce" in c
+ )
+ pkce_kv = pkce_set.split(";", 1)[0]
+ state = r1.headers["location"].split("state=")[1]
+
+ r2 = gated_app_direct.get(
+ f"/auth/callback?code=stub_code&state={state}",
+ headers={"cookie": pkce_kv},
+ follow_redirects=False,
+ )
+ assert r2.status_code == 302, r2.text
+ location = r2.headers["location"]
+ assert location == "/", (
+ f"direct deploy should redirect to /, got {location!r}"
+ )