The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(dashboard): prepend X-Forwarded-Prefix in _safe_next_target() for post-login redirect
liuhao102453e5965b24b3
diff --git a/hermes_cli/dashboard_auth/middleware.py b/hermes_cli/dashboard_auth/middleware.pyindex a5ffc5a9605..ea9b03801e4 100644--- a/hermes_cli/dashboard_auth/middleware.py+++ b/hermes_cli/dashboard_auth/middleware.py@@ -54,10 +54,17 @@ def _path_is_public(path: str) -> bool:def _safe_next_target(request: Request) -> str:"""URL-encoded ``next`` value for the login redirect, or ``""``. Only same-origin paths outsidethe auth flow and ``/api`` are kept (query preserved); dropped deep links fall back to the- SPA's ``sessionStorage["hermes.lastLocation"]``."""+ SPA's ``sessionStorage["hermes.lastLocation"]``.++ Behind a reverse proxy at a sub-path (``X-Forwarded-Prefix``, e.g. ``/hermes``), the prefix+ is prepended AFTER validation so the post-login redirect lands within the mount+ (``/hermes/sessions`` rather than bare ``/sessions``)."""path = request.url.pathif not path or not is_safe_next_path(path):return ""+ prefix = prefix_from_request(request)+ if prefix:+ path = prefix + pathquery = request.url.queryreturn quote(f"{path}?{query}" if query else path, safe="")diff --git a/tests/hermes_cli/test_dashboard_auth_401_reauth.py b/tests/hermes_cli/test_dashboard_auth_401_reauth.pyindex 40476efdb0e..e89de395489 100644--- a/tests/hermes_cli/test_dashboard_auth_401_reauth.py+++ b/tests/hermes_cli/test_dashboard_auth_401_reauth.py@@ -396,6 +396,7 @@ class TestNextSameOriginValidation:class FakeRequest:def __init__(self, path, query=""):self.url = type("URL", (), {"path": path, "query": query})()+ self.headers = {}assert _safe_next_target(FakeRequest("/sessions")) == "%2Fsessions"assert (@@ -403,6 +404,55 @@ class TestNextSameOriginValidation:== "%2Fsessions%3Fpage%3D2")+ def test_safe_next_validator_rejects_protocol_relative(self):+ from hermes_cli.dashboard_auth.middleware import _safe_next_target++ class FakeRequest:+ def __init__(self, path):+ self.url = type("URL", (), {"path": path, "query": ""})()+ self.headers = {}++ assert _safe_next_target(FakeRequest("//evil.com")) == ""++ def test_safe_next_validator_rejects_login_loop(self):+ from hermes_cli.dashboard_auth.middleware import _safe_next_target++ class FakeRequest:+ def __init__(self, path):+ self.url = type("URL", (), {"path": path, "query": ""})()+ self.headers = {}++ assert _safe_next_target(FakeRequest("/login")) == ""+ assert _safe_next_target(FakeRequest("/auth/login")) == ""+ assert _safe_next_target(FakeRequest("/api/auth/me")) == ""++ def test_safe_next_validator_rejects_api_paths(self):+ """``/api/*`` paths must not round-trip through ``next=``.++ Any API URL is a JSON endpoint; landing the browser there after+ OAuth shows raw JSON instead of the dashboard. This is the bug+ fix that closes the analytics-page redirect mishap.+ """+ from hermes_cli.dashboard_auth.middleware import _safe_next_target++ class FakeRequest:+ def __init__(self, path, query=""):+ self.url = type("URL", (), {"path": path, "query": query})()+ self.headers = {}++ assert _safe_next_target(FakeRequest("/api/analytics/models")) == ""+ assert (+ _safe_next_target(FakeRequest("/api/analytics/models", "days=30"))+ == ""+ )+ assert _safe_next_target(FakeRequest("/api/sessions")) == ""+ assert _safe_next_target(FakeRequest("/api/config")) == ""+ assert _safe_next_target(FakeRequest("/api/status")) == ""+ # Exact ``/api`` (no trailing slash) also rejected — the dashboard+ # has no such SPA route, but pinning the boundary keeps the rule+ # crisp.+ assert _safe_next_target(FakeRequest("/api")) == ""+def test_safe_next_validator_does_not_reject_api_prefix_lookalikes(self):"""Negative guard: ``/api-docs`` or ``/apis`` aren't ``/api/*``@@ -412,6 +462,7 @@ class TestNextSameOriginValidation:class FakeRequest:def __init__(self, path):self.url = type("URL", (), {"path": path, "query": ""})()+ self.headers = {}# ``/apidocs`` or ``/api-keys`` lookalike SPA routes — we must# only match the ``/api/`` prefix or exact ``/api``.@@ -419,6 +470,61 @@ class TestNextSameOriginValidation:assert _safe_next_target(FakeRequest("/api-keys")) == "%2Fapi-keys"++ def test_safe_next_validator_respects_prefix(self):+ """When ``X-Forwarded-Prefix`` is set, the ``next`` target must+ include the prefix so the post-login redirect lands within the+ reverse-proxy sub-path."""+ from hermes_cli.dashboard_auth.middleware import _safe_next_target++ class FakeRequest:+ def __init__(self, path, query="", prefix=""):+ self.url = type("URL", (), {"path": path, "query": query})()+ self.headers = {"x-forwarded-prefix": prefix} if prefix else {}++ # Without prefix: behaves as before+ assert _safe_next_target(FakeRequest("/sessions")) == "%2Fsessions"+ assert (+ _safe_next_target(FakeRequest("/sessions", "page=2"))+ == "%2Fsessions%3Fpage%3D2"+ )++ # With prefix: path is prefixed+ assert (+ _safe_next_target(FakeRequest("/sessions", prefix="/hermes"))+ == "%2Fhermes%2Fsessions"+ )+ assert (+ _safe_next_target(FakeRequest("/sessions", "page=2", prefix="/hermes"))+ == "%2Fhermes%2Fsessions%3Fpage%3D2"+ )++ # With multi-level prefix like /profile/nati+ assert (+ _safe_next_target(FakeRequest("/sessions", prefix="/profile/nati"))+ == "%2Fprofile%2Fnati%2Fsessions"+ )++ # Auth rejection paths still work with prefix+ assert (+ _safe_next_target(FakeRequest("/login", prefix="/hermes"))+ == ""+ )+ assert (+ _safe_next_target(FakeRequest("/auth/login", prefix="/hermes"))+ == ""+ )+ assert (+ _safe_next_target(FakeRequest("/api/auth/me", prefix="/hermes"))+ == ""+ )++ # API paths rejected even with prefix+ assert (+ _safe_next_target(FakeRequest("/api/analytics/models", prefix="/hermes"))+ == ""+ )+# ---------------------------------------------------------------------------# /auth/callback honours next= and validates it# ---------------------------------------------------------------------------