Skip to content

The agent that grows with you

test(dashboard-auth): cover proxy trust and native admission limits

BearHuddlestona0d0279ae805
diff --git a/tests/hermes_cli/test_dashboard_auth_native_flow.py b/tests/hermes_cli/test_dashboard_auth_native_flow.py
index b6c4257282f..63d4e0b6f62 100644
--- a/tests/hermes_cli/test_dashboard_auth_native_flow.py
+++ b/tests/hermes_cli/test_dashboard_auth_native_flow.py
@@ -226,6 +226,37 @@ def _native_authorize_params(challenge, **overrides):
return params
[email protected]("forwarded_template", ["198.51.100.{i}", ", 198.51.100.{i}"],
+ ids=["rotated-address", "empty-first-hop"])
+def test_native_authorize_spoofed_forwarded_headers_cannot_bypass_pending_cap(
+ gated_client, forwarded_template,
+):
+ # The public OAuth entry point must limit one peer before allocating the
+ # global pending store, even when XFF rotates or has an empty first hop.
+ _verifier, challenge = _make_pkce()
+ params = _native_authorize_params(challenge, provider="stub")
+ for i in range(native_flow._MAX_PENDING_PER_IP):
+ response = gated_client.get(
+ "/auth/native/authorize", params=params,
+ headers={"X-Forwarded-For": forwarded_template.format(i=i)},
+ )
+ assert response.status_code == 302
+
+ blocked = gated_client.get(
+ "/auth/native/authorize", params=params,
+ headers={"X-Forwarded-For": forwarded_template.format(i=native_flow._MAX_PENDING_PER_IP)},
+ )
+ assert blocked.status_code == 503
+ assert "too many pending" in blocked.json()["detail"]
+
+ # Exhausting one peer's allowance must leave room for another real peer.
+ other_client = TestClient(
+ web_server.app, base_url=gated_client.base_url,
+ client=("203.0.113.10", 50000), follow_redirects=False,
+ )
+ assert other_client.get("/auth/native/authorize", params=params).status_code == 302
+
+
def test_native_authorize_mixed_providers_offers_both_choices(gated_client):
"""SSO-with-password-fallback (one OAuth + the bundled password provider): the desktop
sends no ``provider``, so BOTH configured methods must stay reachable. #78906's symptom
diff --git a/tests/hermes_cli/test_dashboard_auth_password_login.py b/tests/hermes_cli/test_dashboard_auth_password_login.py
index e3c2c6958b9..695850808f2 100644
--- a/tests/hermes_cli/test_dashboard_auth_password_login.py
+++ b/tests/hermes_cli/test_dashboard_auth_password_login.py
@@ -13,10 +13,12 @@ provider, flip ``app.state.auth_required = True``, drive a ``TestClient``.
from __future__ import annotations
import time
+from typing import Any, cast
import pytest
from fastapi.testclient import TestClient
+from uvicorn.middleware.proxy_headers import ProxyHeadersMiddleware
from hermes_cli import web_server
from hermes_cli.dashboard_auth import (
@@ -29,7 +31,8 @@ from hermes_cli.dashboard_auth import (
)
from hermes_cli.dashboard_auth.cookies import SESSION_AT_COOKIE, SESSION_RT_COOKIE
from hermes_cli.dashboard_auth.login_page import render_login_html
-from hermes_cli.dashboard_auth.routes import _reset_password_rate_limit
+from hermes_cli.dashboard_auth.routes import _PW_RATE_MAX_ATTEMPTS, _reset_password_rate_limit
+from hermes_cli.web_server_lifecycle import _dashboard_forwarded_allow_ips
from tests.hermes_cli.conftest_dashboard_auth import StubAuthProvider
@@ -331,25 +334,48 @@ class TestRateLimit:
)
assert good.status_code == 429
- def test_spoofed_x_forwarded_for_does_not_reset_rate_limit(self, gated_app):
+ @pytest.mark.parametrize(
+ "peer, forwarded_suffix",
+ [("203.0.113.9", ""), ("127.0.0.1", ", 203.0.113.9"),
+ ("172.18.0.9", ", 203.0.113.9")],
+ ids=["direct", "loopback-proxy", "configured-proxy"],
+ )
+ def test_spoofed_x_forwarded_for_does_not_reset_rate_limit(
+ self, gated_app, peer, forwarded_suffix,
+ ):
# 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(
+ # reverse proxy. Even behind an appending proxy, the client-supplied
+ # first hop must not override Uvicorn's resolved client address.
+ trusted = _dashboard_forwarded_allow_ips({"trusted_proxies": ["172.18.0.0/16"]})
+ # Uvicorn and Starlette expose incompatible static ASGI type aliases.
+ app = cast(Any, ProxyHeadersMiddleware(cast(Any, web_server.app), trusted_hosts=trusted))
+ client = TestClient(app, base_url=gated_app.base_url, client=(peer, 50000))
+ for i in range(_PW_RATE_MAX_ATTEMPTS):
+ resp = client.post(
"/auth/password-login",
- headers={"X-Forwarded-For": f"198.51.100.{i}"},
+ headers={"X-Forwarded-For": f"198.51.100.{i}{forwarded_suffix}"},
json={"provider": "testpw", "username": "admin", "password": "WRONG"},
)
assert resp.status_code == 401
- blocked = gated_app.post(
+ blocked = client.post(
"/auth/password-login",
- headers={"X-Forwarded-For": "198.51.100.250"},
+ headers={"X-Forwarded-For": f"198.51.100.250{forwarded_suffix}"},
json={"provider": "testpw", "username": "admin", "password": "hunter2"},
)
assert blocked.status_code == 429
+ # Another real client must retain its own budget, including when both
+ # clients reach the dashboard through the same trusted proxy.
+ other_peer = peer if forwarded_suffix else "203.0.113.10"
+ other_client = TestClient(app, base_url=gated_app.base_url, client=(other_peer, 50001))
+ allowed = other_client.post(
+ "/auth/password-login",
+ headers={"X-Forwarded-For": "203.0.113.10"},
+ json={"provider": "testpw", "username": "admin", "password": "hunter2"},
+ )
+ assert allowed.status_code == 200
+
@pytest.mark.parametrize("peer", [("203.0.113.7", 12345), None])
def test_client_ip_uses_asgi_peer_not_forwarded_header(peer):