Skip to content

The agent that grows with you

fix(security): ignore spoofed XFF for dashboard login limits

hinotoi-agentfb5bba80dce1
diff --git a/hermes_cli/dashboard_auth/request_utils.py b/hermes_cli/dashboard_auth/request_utils.py
index fa161474668..2eca3d145c8 100644
--- a/hermes_cli/dashboard_auth/request_utils.py
+++ b/hermes_cli/dashboard_auth/request_utils.py
@@ -17,9 +17,13 @@ _NEXT_DENY_PREFIXES = ("/login", "/auth/", "/api/auth/")
def client_ip(request: Request) -> str:
- """First ``X-Forwarded-For`` hop, else the peer address."""
- fwd = request.headers.get("x-forwarded-for", "")
- return fwd.split(",")[0].strip() if fwd else (request.client.host if request.client else "")
+ """ASGI peer address for rate limits, native pending caps, and auth audit.
+
+ Never parse client-supplied ``X-Forwarded-For`` here: direct clients can
+ spoof it. Trusted proxy normalization belongs upstream, where the server
+ may rewrite ``request.client`` only for operator-configured trusted peers.
+ """
+ return request.client.host if request.client else ""
def extract_bearer(request: Request) -> str:
diff --git a/hermes_cli/dashboard_auth/routes.py b/hermes_cli/dashboard_auth/routes.py
index 4115a21c2a5..5778f5bd27e 100644
--- a/hermes_cli/dashboard_auth/routes.py
+++ b/hermes_cli/dashboard_auth/routes.py
@@ -360,8 +360,8 @@ async def auth_callback(
# --- Public: password (non-redirect) login ---------------------------------
# Brute-force throttle: a process-local sliding window per client IP. Best-effort
-# defence-in-depth on top of the provider's constant-time verify (resets on restart; behind a
-# proxy the IP is the proxy's unless X-Forwarded-For).
+# defence-in-depth on top of the provider's constant-time verify (resets on restart).
+# Uses the ASGI peer; trusted proxy normalization must happen upstream.
_PW_RATE_MAX_ATTEMPTS = 10
_PW_RATE_WINDOW_SEC = 60.0
_pw_attempts: Dict[str, Deque[float]] = defaultdict(deque)
diff --git a/tests/hermes_cli/test_dashboard_auth_password_login.py b/tests/hermes_cli/test_dashboard_auth_password_login.py
index 640e137db13..e3c2c6958b9 100644
--- a/tests/hermes_cli/test_dashboard_auth_password_login.py
+++ b/tests/hermes_cli/test_dashboard_auth_password_login.py
@@ -331,6 +331,40 @@ class TestRateLimit:
)
assert good.status_code == 429
+ def test_spoofed_x_forwarded_for_does_not_reset_rate_limit(self, gated_app):
+ # X-Forwarded-For is attacker-controlled unless it came from a trusted
+ # reverse proxy, so direct dashboard requests must not be able to pick
+ # fresh rate-limit buckets by rotating the header value.
+ for i in range(10):
+ resp = gated_app.post(
+ "/auth/password-login",
+ headers={"X-Forwarded-For": f"198.51.100.{i}"},
+ json={"provider": "testpw", "username": "admin", "password": "WRONG"},
+ )
+ assert resp.status_code == 401
+
+ blocked = gated_app.post(
+ "/auth/password-login",
+ headers={"X-Forwarded-For": "198.51.100.250"},
+ json={"provider": "testpw", "username": "admin", "password": "hunter2"},
+ )
+ assert blocked.status_code == 429
+
+
[email protected]("peer", [("203.0.113.7", 12345), None])
+def test_client_ip_uses_asgi_peer_not_forwarded_header(peer):
+ from fastapi import Request
+
+ from hermes_cli.dashboard_auth.request_utils import client_ip
+
+ # Preserve the ASGI address, including one normalized by trusted upstream
+ # middleware; a missing peer must not fall back to an untrusted header.
+ request = Request({
+ "type": "http", "client": peer,
+ "headers": [(b"x-forwarded-for", b"198.51.100.1, 192.0.2.1")],
+ })
+ assert client_ip(request) == (peer[0] if peer else "")
+
# ---------------------------------------------------------------------------
# Login page rendering