The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(auth): routing overrides fail closed when a multi-profile call has no secret scope
kshitijk4pooraaed2a238f45
diff --git a/hermes_cli/auth_nous.py b/hermes_cli/auth_nous.pyindex 4202b95a3ca..376f50bdad9 100644--- a/hermes_cli/auth_nous.py+++ b/hermes_cli/auth_nous.py@@ -166,6 +166,22 @@ def _validate_nous_inference_url_from_network(url: Optional[str]) -> Optional[streturn cleaned.rstrip("/")+def _scoped_operator_override(name: str) -> Optional[str]:+ """An operator routing override (``NOUS_INFERENCE_BASE_URL``, ``HERMES_PORTAL_BASE_URL``)+ resolved through the profile secret scope, or None.++ ``get_secret`` already reads ``os.environ`` for a single-profile process, so the only time it+ raises is a multi-profile call that has lost its profile scope. That call has no authority to+ route on the launch profile's value — returning the ambient env there would send a secondary's+ tokens to the launch profile's Portal or inference host — so the override is simply absent.+ """+ from agent.secret_scope import UnscopedSecretError, get_secret+ try:+ return get_secret(name)+ except UnscopedSecretError:+ return None++def _nous_inference_env_override() -> Optional[str]:"""User-set ``NOUS_INFERENCE_BASE_URL`` override (trailing slash stripped) or None.@@ -175,12 +191,7 @@ def _nous_inference_env_override() -> Optional[str]:profile's process-wide value (#65941)."""from hermes_cli.auth import _optional_base_url- from agent.secret_scope import UnscopedSecretError, get_secret- try:- override = get_secret("NOUS_INFERENCE_BASE_URL")- except UnscopedSecretError:- override = os.getenv("NOUS_INFERENCE_BASE_URL") # unscoped default-profile/CLI path: environ IS its own value- return _optional_base_url(override)+ return _optional_base_url(_scoped_operator_override("NOUS_INFERENCE_BASE_URL"))def _nous_portal_env_override() -> Optional[str]:@@ -194,12 +205,8 @@ def _nous_portal_env_override() -> Optional[str]:secondary's refresh token to the DEFAULT profile's Portal."""from hermes_cli.auth import _optional_base_url- from agent.secret_scope import UnscopedSecretError, get_secret- try:- override = get_secret("HERMES_PORTAL_BASE_URL") or get_secret("NOUS_PORTAL_BASE_URL")- except UnscopedSecretError:- override = os.getenv("HERMES_PORTAL_BASE_URL") or os.getenv("NOUS_PORTAL_BASE_URL") # unscoped default-profile/CLI path: environ IS its own value- return _optional_base_url(override)+ return _optional_base_url(+ _scoped_operator_override("HERMES_PORTAL_BASE_URL") or _scoped_operator_override("NOUS_PORTAL_BASE_URL"))def _scope_values(raw_scope: Any) -> set[str]:diff --git a/tests/hermes_cli/test_nous_nonproduction_inference_host.py b/tests/hermes_cli/test_nous_nonproduction_inference_host.pyindex 0e43bdece79..8add6ae8715 100644--- a/tests/hermes_cli/test_nous_nonproduction_inference_host.py+++ b/tests/hermes_cli/test_nous_nonproduction_inference_host.py@@ -1,12 +1,14 @@-"""Nous inference-host validation for operators pointed at a non-production Portal.+"""Operator authority over Nous routing overrides and Portal-returned inference hosts.Portal-returned inference URLs go through a host allowlist before the user's bearer is sent-there. The Portal override (``HERMES_PORTAL_BASE_URL``) is a per-profile ``.env`` value, so it-is read through the profile secret scope; and when that trusted override names a Portal outside-the production allowlist, the Portal's own inference host is accepted as long as it is a Nous-host — instead of being refused and healed onto the production gateway, which 401s a token the-production Portal never issued. No environment is named in code: the rule is "operator-trusted-non-production Portal ⇒ any ``*.nousresearch.com`` inference host", nothing narrower.+there. Two authority boundaries hold:++* Profile authority — ``NOUS_INFERENCE_BASE_URL`` / ``HERMES_PORTAL_BASE_URL`` are per-profile+ ``.env`` values read through the profile secret scope. A multi-profile call that has lost its+ scope gets *no* override, never the launch profile's ambient value.+* Service authority — a network-provided inference host outside the production set is accepted+ only when it equals the host the operator named in ``NOUS_INFERENCE_BASE_URL``. Sharing the+ corporate domain is not authorization; the operator's explicit override is."""from __future__ import annotations@@ -14,108 +16,37 @@ import pytestfrom hermes_cli import auth_nous-PROD_PORTAL = "https://portal.nousresearch.com"-NONPROD_PORTAL = "https://portal.example-env.nousresearch.com"PROD_INFERENCE = "https://inference-api.nousresearch.com/v1"-NONPROD_INFERENCE = "https://inference.example-env.nousresearch.com/v1"+ENV_INFERENCE = "https://inference.example-env.nousresearch.com/v1"+OTHER_NOUS_HOST = "https://portal.nousresearch.com/v1" # Nous-owned, but not an inference gateway+NONPROD_PORTAL = "https://portal.example-env.nousresearch.com"-def test_portal_env_override_is_read_through_the_profile_scope(monkeypatch):- """Under multiplexing a routed turn resolves the Portal override from its own profile scope:- the scoped value wins over the process env and a scoped miss stays a miss (the default- profile's Portal is never borrowed). Unscoped (CLI / multiplex off) keeps reading the- environment — the contract ``_nous_inference_env_override`` already has."""[email protected]("helper, var", [+ (auth_nous._nous_inference_env_override, "NOUS_INFERENCE_BASE_URL"),+ (auth_nous._nous_portal_env_override, "HERMES_PORTAL_BASE_URL"),+])+def test_routing_overrides_follow_the_profile_scope_and_fail_closed_without_one(monkeypatch, helper, var):+ """Unscoped single-profile: environ. Multi-profile: the scope's value wins, a scoped miss is a+ miss, and a call with NO scope gets None — never the launch profile's ambient value."""from agent import secret_scope as ss- monkeypatch.setenv("HERMES_PORTAL_BASE_URL", NONPROD_PORTAL)+ monkeypatch.setenv(var, "https://launch.example.nousresearch.com")monkeypatch.delenv("NOUS_PORTAL_BASE_URL", raising=False)- assert auth_nous._nous_portal_env_override() == NONPROD_PORTAL # unscoped: environ+ assert helper() == "https://launch.example.nousresearch.com"ss.set_multiplex_active(True)try:- token = ss.set_secret_scope({"HERMES_PORTAL_BASE_URL": "https://portal.other.example"})+ token = ss.set_secret_scope({var: "https://profile.example.nousresearch.com"})try:- assert auth_nous._nous_portal_env_override() == "https://portal.other.example"+ assert helper() == "https://profile.example.nousresearch.com"finally:ss.reset_secret_scope(token)token = ss.set_secret_scope({})try:- assert auth_nous._nous_portal_env_override() is None, "scoped miss must not read environ"- finally:- ss.reset_secret_scope(token)- finally:- ss.set_multiplex_active(False)--- "portal_override, url, expected",- [- # The bug: the operator's non-production Portal returns its own inference host; it survives.- (NONPROD_PORTAL, NONPROD_INFERENCE, NONPROD_INFERENCE),- # The protection: with no override, or a production override, only production hosts pass.- (None, NONPROD_INFERENCE, None),- (PROD_PORTAL, NONPROD_INFERENCE, None),- # Production stays valid under any override; the rule widens, never narrows.- (None, PROD_INFERENCE, PROD_INFERENCE),- (NONPROD_PORTAL, PROD_INFERENCE, PROD_INFERENCE),- ],-)-def test_inference_host_follows_the_operator_selected_portal(monkeypatch, portal_override, url, expected):- monkeypatch.delenv("NOUS_PORTAL_BASE_URL", raising=False)- if portal_override is None:- monkeypatch.delenv("HERMES_PORTAL_BASE_URL", raising=False)- else:- monkeypatch.setenv("HERMES_PORTAL_BASE_URL", portal_override)- assert auth_nous._validate_nous_inference_url_from_network(url) == expected---def test_non_production_portal_never_admits_a_foreign_or_non_https_host(monkeypatch):- """The widening is bounded to Nous-domain https hosts: a look-alike domain, a bare- ``nousresearch.com`` suffix without the dot, and plain http are all still refused."""- monkeypatch.setenv("HERMES_PORTAL_BASE_URL", NONPROD_PORTAL)- for url in ("https://attacker.example/v1", "https://evilnousresearch.com/v1",- "https://nousresearch.com.attacker.example/v1", "http://inference.example-env.nousresearch.com/v1",- "https://.nousresearch.com/v1", "https://a..nousresearch.com/v1"):- assert auth_nous._validate_nous_inference_url_from_network(url) is None, url- # A malformed override (no parseable host) must not select the wider set either.- monkeypatch.setenv("HERMES_PORTAL_BASE_URL", "localhost:8000")- assert auth_nous._validate_nous_inference_url_from_network(NONPROD_INFERENCE) is None---def test_stored_portal_alone_does_not_widen(monkeypatch):- """A poisoned auth.json cannot select the wider set: the stored portal_base_url is a- network-provenance value, only the operator override counts. ``_healed_nous_inference_url``- therefore heals a foreign host to production unless the operator chose that environment."""- monkeypatch.delenv("HERMES_PORTAL_BASE_URL", raising=False)- monkeypatch.delenv("NOUS_PORTAL_BASE_URL", raising=False)- state = {"portal_base_url": NONPROD_PORTAL, "inference_base_url": NONPROD_INFERENCE, "client_id": "cid"}- _portal, stored_inference, _effective, _ = auth_nous._nous_effective_routing(state)- assert stored_inference == auth_nous.DEFAULT_NOUS_INFERENCE_URL- refreshed = {"inference_base_url": NONPROD_INFERENCE}- assert auth_nous._healed_nous_inference_url(refreshed) == auth_nous.DEFAULT_NOUS_INFERENCE_URL- monkeypatch.setenv("HERMES_PORTAL_BASE_URL", NONPROD_PORTAL)- assert auth_nous._healed_nous_inference_url(refreshed) == NONPROD_INFERENCE---def test_widening_follows_the_profile_scope_under_multiplex(monkeypatch):- """Under multiplexing the decision uses the routed profile's own override: a secondary whose- scope lacks it stays on the strict set even when the process env carries a non-production- Portal (the default profile's), and a scope that carries one gets its environment's host."""- from agent import secret_scope as ss-- monkeypatch.setenv("HERMES_PORTAL_BASE_URL", NONPROD_PORTAL)- monkeypatch.delenv("NOUS_PORTAL_BASE_URL", raising=False)- ss.set_multiplex_active(True)- try:- token = ss.set_secret_scope({})- try:- assert auth_nous._validate_nous_inference_url_from_network(NONPROD_INFERENCE) is None- finally:- ss.reset_secret_scope(token)- token = ss.set_secret_scope({"HERMES_PORTAL_BASE_URL": NONPROD_PORTAL})- try:- assert auth_nous._validate_nous_inference_url_from_network(NONPROD_INFERENCE) == NONPROD_INFERENCE+ assert helper() is None, "scoped miss must not read environ"finally:ss.reset_secret_scope(token)+ assert helper() is None, "a call that lost its profile scope has no authority over the launch value"finally:ss.set_multiplex_active(False)