The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(config-remote): a configured URL's user info is never echoed in messages
Ben Barclay748b71cef764
diff --git a/plugins/config_backends/remote/backend.py b/plugins/config_backends/remote/backend.pyindex a7fdaefae8e..61e6eacbf2d 100644--- a/plugins/config_backends/remote/backend.py+++ b/plugins/config_backends/remote/backend.py@@ -232,7 +232,7 @@ class RemoteBackend:continueraise ConfigBackendUnavailable(f"Remote Config: cannot load the config for profile {profile!r} from "- f"{client.base_url()}: {exc}. Hermes does not start without its remote config "+ f"{client.shown_url()}: {exc}. Hermes does not start without its remote config ""(HERMES_CONFIG_BACKEND=remote has no local fallback).") from excdef _fetch_into(self, st: _ProfileState, *, conditional: bool) -> bool:@@ -431,7 +431,7 @@ class RemoteBackend:doc, version, locks = self._snapshot(st, take)return UserLayer(doc=doc, version=version, locks=locks,- provenance=f"remote:{client.base_url()} profile={st.profile}")+ provenance=f"remote:{client.shown_url()} profile={st.profile}")def read_user_doc(self, home: Path) -> Any:return self.read_user_layer(home).doc@@ -770,7 +770,7 @@ class RemoteBackend:def describe(self, home: Path) -> str:st = self._states.get(self._key(home))- head = f"Remote Config {client.base_url()} (instance {client.instance_id() or '<unset>'})"+ head = f"Remote Config {client.shown_url()} (instance {client.instance_id() or '<unset>'})"if st is None:return f"{head}: profile not fetched yet"age = int(time.time() - st.fetched_at) if st.fetched_at else -1diff --git a/plugins/config_backends/remote/client.py b/plugins/config_backends/remote/client.pyindex 0990625034c..1fcbc81057d 100644--- a/plugins/config_backends/remote/client.py+++ b/plugins/config_backends/remote/client.py@@ -15,7 +15,7 @@ from pathlib import Pathfrom typing import Any, Dict, Optionalfrom .credentials import plane_token-from .destination import checked_url, open_credentialed+from .destination import checked_url, display_url, open_credentialedDEFAULT_URL = "https://config-config.nousresearch.com"URL_ENV = "HERMES_CONFIG_REMOTE_URL"@@ -44,11 +44,16 @@ class TransportError(RuntimeError):def base_url() -> str:- """The configured plane origin (+ path prefix). Display only: :func:`request` re-checks it- against the credential destination policy before anything is sent."""+ """The configured plane origin (+ path prefix), as configured: :func:`request` checks it+ against the credential destination policy before anything is sent. Messages use :func:`shown_url`."""return (os.environ.get(URL_ENV, "").strip() or DEFAULT_URL).rstrip("/")+def shown_url() -> str:+ """:func:`base_url` without any user info, for messages, ``describe()`` and provenance."""+ return display_url(base_url())++def instance_id() -> str:return os.environ.get(INSTANCE_ENV, "").strip()diff --git a/plugins/config_backends/remote/credentials.py b/plugins/config_backends/remote/credentials.pyindex 692f63fa456..4fb4e413d59 100644--- a/plugins/config_backends/remote/credentials.py+++ b/plugins/config_backends/remote/credentials.py@@ -85,7 +85,7 @@ def _idp_token() -> str:f"{IDP_ENV_PREFIX}TOKEN_URL is set but {IDP_ENV_PREFIX}CLIENT_ID / {IDP_ENV_PREFIX}CLIENT_SECRET ""are not: Remote Config needs the OAuth2 client-credentials grant (both values in .env).",retryable=False)- from .destination import CredentialDestinationError, checked_url, open_credentialed+ from .destination import CredentialDestinationError, checked_url, display_url, open_credentialedchecked_url(token_url, env_name=f"{IDP_ENV_PREFIX}TOKEN_URL") # before the secret leaves the processkey = (token_url, client_id, scope)now = time.monotonic()@@ -105,7 +105,7 @@ def _idp_token() -> str:except CredentialDestinationError:raiseexcept Exception as exc: # HTTP and network errors alike; the message has no secret- raise PlaneCredentialError(f"IdP token request to {token_url} failed: {exc}", retryable=True) from exc+ raise PlaneCredentialError(f"IdP token request to {display_url(token_url)} failed: {exc}", retryable=True) from excaccess_token = payload.get("access_token") if isinstance(payload, dict) else Noneif not isinstance(access_token, str) or not access_token:raise PlaneCredentialError("IdP client-credentials response had no access_token", retryable=True)diff --git a/plugins/config_backends/remote/destination.py b/plugins/config_backends/remote/destination.pyindex a098b8d37f4..922d7ee6b83 100644--- a/plugins/config_backends/remote/destination.py+++ b/plugins/config_backends/remote/destination.py@@ -36,6 +36,23 @@ def _is_loopback(host: str) -> bool:return False+def display_url(url: str) -> str:+ """*url* for messages: any user info (``user:password@``) dropped, so a misconfigured URL+ never echoes a credential back (an unparsable one is cut at the last ``@``)."""+ try:+ parts = urllib.parse.urlsplit(url)+ if parts.username is None and parts.password is None:+ return url+ host = parts.hostname or ""+ if ":" in host:+ host = f"[{host}]"+ port = parts.port+ except ValueError:+ head, sep, tail = url.partition("//")+ return f"{head}{sep}{tail.rpartition('@')[2]}" if sep else url.rpartition("@")[2]+ return urllib.parse.urlunsplit(parts._replace(netloc=f"{host}:{port}" if port is not None else host))++def checked_url(url: str, *, env_name: str) -> str:"""``url`` when a credential may be sent to it, else :class:`CredentialDestinationError`."""try:@@ -43,9 +60,9 @@ def checked_url(url: str, *, env_name: str) -> str:host = (parts.hostname or "").lower()_ = parts.port # raises ValueError on a malformed portexcept ValueError as exc:- raise CredentialDestinationError(f"{env_name}={url!r} is not a valid URL: {exc}") from exc+ raise CredentialDestinationError(f"{env_name}={display_url(url)!r} is not a valid URL: {exc}") from excif not host:- raise CredentialDestinationError(f"{env_name}={url!r} has no host.")+ raise CredentialDestinationError(f"{env_name}={display_url(url)!r} has no host.")if parts.username is not None or parts.password is not None or parts.query or parts.fragment:raise CredentialDestinationError(f"{env_name} must be a plain origin and path (no user info, query or fragment); "@@ -64,7 +81,7 @@ class _RefuseRedirect(urllib.request.BaseHandler):def _refuse(self, req, fp, code, msg, headers):fp.close()raise CredentialDestinationError(- f"{req.get_full_url()} answered HTTP {code} redirecting to {headers.get('Location')!r}; "+ f"{req.get_full_url()} answered HTTP {code} redirecting to {display_url(headers.get('Location') or '')!r}; ""Remote Config never follows a redirect with credentials. Point the URL at the final ""destination.")diff --git a/tests/plugins/config_backends/test_remote_credential_destination.py b/tests/plugins/config_backends/test_remote_credential_destination.pyindex 7eedee8b091..bf6b44e3067 100644--- a/tests/plugins/config_backends/test_remote_credential_destination.py+++ b/tests/plugins/config_backends/test_remote_credential_destination.py@@ -231,3 +231,73 @@ def test_idp_redirect_is_not_followed_with_the_secret(monkeypatch, listeners, coassert exc.value.retryable is Falseassert len(idp.seen) == 1 and CLIENT_SECRET in idp.seen[0]["body"]assert target.seen == []+++# --- user info is never echoed (teknium1 P3) ----------------------------------------------------++USERINFO_URL = "https://u:[email protected]"++[email protected]("url, shown", [+ ("https://u:[email protected]", "https://example.com"),+ ("https://u:[email protected]:8443/plane", "https://example.com:8443/plane"),+ ("http://u@[::1]:9/x", "http://[::1]:9/x"),+ ("https://u:[email protected]:bad", "https://example.com:bad"), # unparsable port: cut at the @+ ("https://example.com/plane", "https://example.com/plane"),+])+def test_display_url_drops_user_info(url, shown):+ assert destination.display_url(url) == shown+++def test_refused_url_never_echoes_user_info(monkeypatch, tmp_path):+ """The boot error, describe() and the destination errors show the URL without its user info."""+ from hermes_cli.config_backend import ConfigBackendUnavailable+ from plugins.config_backends import remote as remote_pkg+ from plugins.config_backends.remote import backend as backend_mod++ home = tmp_path / "home"+ home.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(home))+ monkeypatch.setenv(client.URL_ENV, USERINFO_URL)+ monkeypatch.setenv(client.INSTANCE_ENV, INSTANCE)+ monkeypatch.setattr(backend_mod.time, "sleep", lambda s: None)+ remote_pkg._reset_for_tests()+ try:+ backend = remote_pkg.get_remote_backend()+ with pytest.raises(ConfigBackendUnavailable) as exc:+ backend.boot(home)+ out = [str(exc.value), backend.describe(home), client.shown_url()]+ for bad in (USERINFO_URL + ":bad", "https://u:p@"):+ monkeypatch.setenv(client.URL_ENV, bad)+ with pytest.raises(destination.CredentialDestinationError) as derr:+ client.request("GET", home, "default")+ out.append(str(derr.value))+ finally:+ remote_pkg._reset_for_tests()+ assert "example.com" in out[0] and "example.com" in out[1]+ for text in out:+ assert "u:p" not in text and "u:p@" not in text, text+++def test_provenance_never_echoes_user_info(monkeypatch, tmp_path):+ from plugins.config_backends import remote as remote_pkg++ home = tmp_path / "home"+ home.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(home))+ with StubPlane() as plane:+ for k, v in remote_env(plane).items():+ monkeypatch.setenv(k, v)+ remote_pkg._reset_for_tests()+ cred_mod._IDP_CACHE.clear()+ try:+ backend = remote_pkg.get_remote_backend()+ backend.boot(home)+ monkeypatch.setenv(client.URL_ENV, plane.url.replace("http://", "http://u:p@"))+ layer = backend.read_user_layer(home)+ described = backend.describe(home)+ finally:+ remote_pkg._reset_for_tests()+ cred_mod._IDP_CACHE.clear()+ assert layer.provenance.startswith("remote:http://127.0.0.1:")+ assert "u:p" not in layer.provenance and "u:p" not in described