The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(auth): validate native loopback redirect URIs
FroRaut37fea25c2b8f
diff --git a/hermes_cli/dashboard_auth/routes.py b/hermes_cli/dashboard_auth/routes.pyindex 6239777bd93..300dec2a397 100644--- a/hermes_cli/dashboard_auth/routes.py+++ b/hermes_cli/dashboard_auth/routes.py@@ -216,17 +216,49 @@ async def auth_login(request: Request, provider: str, next: str = ""):# --- Public: RFC 8252 native-app authorization (system browser + loopback + PKCE)def _validate_loopback_redirect_uri(raw: str) -> str:- """Accept only ``http://127.0.0.1[:port]/…`` / ``http://[::1][:port]/…``. Security boundary:- the route is public, so a non-loopback host would make the callback an open redirect leaking- a live code. ``localhost`` is rejected (RFC 8252 §8.3)."""+ """Return a canonical RFC 8252 loopback URI or reject it.++ Both the server parser and the system browser must see the same authority: URL userinfo,+ backslashes, fragments, controls, and non-canonical host/port spellings are rejected before+ the URI is persisted. ``localhost`` is rejected in favour of IP literals (RFC 8252 §8.3).+ """if not raw:raise _http(400, "redirect_uri required")- parsed = urlparse(raw)+ if (+ not raw.isascii()+ or "\\" in raw+ or any(ord(ch) <= 0x20 or ord(ch) == 0x7F for ch in raw)+ ):+ raise _http(400, "native redirect_uri must use a canonical loopback URL")+ try:+ parsed = urlparse(raw)+ hostname = parsed.hostname+ port = parsed.port+ except ValueError:+ raise _http(400, "native redirect_uri must use a canonical loopback URL")if parsed.scheme != "http":raise _http(400, "native redirect_uri must be http:// on the loopback interface")- if (parsed.hostname or "").lower() not in ("127.0.0.1", "::1"):+ if "#" in raw:+ raise _http(400, "native redirect_uri must not contain a fragment")+ if parsed.username is not None or parsed.password is not None:+ raise _http(400, "native redirect_uri must not contain userinfo")+ if hostname == "127.0.0.1":+ canonical_host = "127.0.0.1"+ elif hostname == "::1":+ canonical_host = "[::1]"+ else:raise _http(400, "native redirect_uri host must be a loopback IP literal (127.0.0.1 / ::1)")- return raw+ canonical_netloc = canonical_host + (f":{port}" if port is not None else "")+ if parsed.netloc != canonical_netloc:+ raise _http(400, "native redirect_uri must use a canonical loopback authority")+ return urlunparse(+ parsed._replace(+ scheme="http",+ netloc=canonical_netloc,+ path=parsed.path or "/",+ fragment="",+ )+ )def _select_native_provider(provider: str):@@ -251,7 +283,7 @@ async def auth_native_authorize(raise _http(400, "code_challenge_method must be S256")if not code_challenge:raise _http(400, "code_challenge required")- _validate_loopback_redirect_uri(redirect_uri)+ redirect_uri = _validate_loopback_redirect_uri(redirect_uri)p = _select_native_provider(provider)if p is None and not provider:candidates = list_session_providers()diff --git a/tests/hermes_cli/test_dashboard_auth_native_flow.py b/tests/hermes_cli/test_dashboard_auth_native_flow.pyindex 8ce8c5e4153..4bf0ab40020 100644--- a/tests/hermes_cli/test_dashboard_auth_native_flow.py+++ b/tests/hermes_cli/test_dashboard_auth_native_flow.py@@ -452,6 +452,104 @@ def _start_native_password_login(client, *, challenge, state="desk-state"):return r.cookies+def test_native_password_login_rejects_browser_parser_differential(pw_gated_client):+ """A redirect accepted as loopback must not resolve to a remote browser authority."""+ _verifier, challenge = _make_pkce()+ r = pw_gated_client.get(+ "/auth/native/authorize",+ params={+ "provider": "testpw",+ "code_challenge": challenge,+ "code_challenge_method": "S256",+ "redirect_uri": "http://attacker.example\\@127.0.0.1/callback",+ "state": "desk-state",+ },+ )+ assert r.status_code == 400, r.text+ assert "set-cookie" not in r.headers+++def test_native_redirect_uri_boundary_matches_browser_authority(gated_client):+ """Only canonical loopback authorities survive the upstream OAuth callback."""+ verifier, challenge = _make_pkce()+ rejected = (+ "http://[email protected]/callback",+ "http://@127.0.0.1/callback",+ "http://%31%32%37%2e%30%2e%30%2e%31/callback",+ "http://127.0.0.1%2fattacker.example/callback",+ "http://127.1/callback",+ "http://2130706433/callback",+ "http://0x7f000001/callback",+ "http://127.0.0.1/callback#fragment",+ "http://127.0.0.1/callback#",+ "http://127.0.0.1:/callback",+ "http://127.0.0.1:053999/callback",+ "http://127.0.0.1:65536/callback",+ "http://127.0.0.1:not-a-port/callback",+ "http://[0:0:0:0:0:0:0:1]/callback",+ "\thttp://127.0.0.1:53999/callback",+ "http://127.0.0.1:53999/\tcallback",+ )+ for redirect_uri in rejected:+ r = gated_client.get(+ "/auth/native/authorize",+ params={+ "provider": "stub",+ "code_challenge": challenge,+ "code_challenge_method": "S256",+ "redirect_uri": redirect_uri,+ "state": "desk-state",+ },+ )+ assert r.status_code == 400, (redirect_uri, r.status_code, r.text)+ assert "set-cookie" not in r.headers++ accepted = (+ (+ "HTTP://127.0.0.1:53999/callback/path?existing=one",+ "http://127.0.0.1:53999/callback/path?existing=one",+ ),+ (+ "http://[::1]:54000/callback/path?existing=two",+ "http://[::1]:54000/callback/path?existing=two",+ ),+ )+ for redirect_uri, canonical in accepted:+ started = gated_client.get(+ "/auth/native/authorize",+ params={+ "provider": "stub",+ "code_challenge": challenge,+ "code_challenge_method": "S256",+ "redirect_uri": redirect_uri,+ "state": "desk-state",+ },+ )+ assert started.status_code == 302, (redirect_uri, started.status_code, started.text)+ upstream = urlparse(started.headers["location"])+ upstream_query = parse_qs(upstream.query)+ completed = gated_client.get(+ "/auth/callback",+ params={+ "code": upstream_query["code"][0],+ "state": upstream_query["state"][0],+ },+ cookies=started.cookies,+ )+ assert completed.status_code == 302, completed.text+ target = completed.headers["location"]+ assert target.startswith(f"{canonical}&code="), target+ query = parse_qs(urlparse(target).query)+ expected_query = parse_qs(urlparse(canonical).query)+ assert query["existing"] == expected_query["existing"]+ assert query["state"] == ["desk-state"]+ redeemed = gated_client.post(+ "/auth/native/token",+ json={"code": query["code"][0], "code_verifier": verifier},+ )+ assert redeemed.status_code == 200, redeemed.text++def test_native_password_login_full_roundtrip(pw_gated_client):"""authorize → /login → password-login → loopback code → bearer tokens."""verifier, challenge = _make_pkce()