Skip to content

The agent that grows with you

fix(credentials): scrub the keyed `providers` schema on rotate/remove

Drexuxux22f9caf84ed2
diff --git a/hermes_cli/credential_lifecycle.py b/hermes_cli/credential_lifecycle.py
index 3448add360a..9741fc30e89 100644
--- a/hermes_cli/credential_lifecycle.py
+++ b/hermes_cli/credential_lifecycle.py
@@ -141,11 +141,18 @@ def _scrub_config_yaml_mirrors(old_value: str, new_value: str | None) -> List[st
touched: List[str] = []
- def _fix(section: Any, key_path: str) -> None:
+ def _fix(
+ section: Any,
+ key_path: str,
+ fields: tuple[str, ...] = ("api_key", "api"),
+ ) -> None:
if not isinstance(section, dict):
return
# "api" is the legacy alias for model.api_key kept by older configs.
- for field in ("api_key", "api"):
+ # NOTE: in the keyed ``providers`` schema ``api`` means the base_url,
+ # not a credential (see ``get_compatible_custom_providers``), so that
+ # section passes ``fields=("api_key",)`` to avoid touching a base_url.
+ for field in fields:
current = section.get(field)
if isinstance(current, str) and current == old_value:
if new_value:
@@ -169,6 +176,18 @@ def _scrub_config_yaml_mirrors(old_value: str, new_value: str | None) -> List[st
for name, entry in custom.items():
_fix(entry, f"custom_providers.{name}")
+ # The keyed ``providers`` schema (v12+) is where the dashboard/desktop
+ # write custom-endpoint credentials — ``providers.<id>.api_key``. It is a
+ # real inline secret and higher-precedence than the env var, so a stale
+ # copy left here shadows a rotation (persistent 401 with a key the UI no
+ # longer shows, #62269) and survives a removal that promised to clear the
+ # credential from EVERY store. Scrub only ``api_key``; ``api`` here is the
+ # base_url alias, not a credential.
+ keyed_providers = user_config.get("providers")
+ if isinstance(keyed_providers, dict):
+ for provider_id, entry in keyed_providers.items():
+ _fix(entry, f"providers.{provider_id}", fields=("api_key",))
+
if touched:
require_readable_config_before_write(config_path)
atomic_yaml_write(config_path, user_config, sort_keys=False)
diff --git a/tests/hermes_cli/test_credential_lifecycle.py b/tests/hermes_cli/test_credential_lifecycle.py
index 7b2f5078816..5ea5a9d9521 100644
--- a/tests/hermes_cli/test_credential_lifecycle.py
+++ b/tests/hermes_cli/test_credential_lifecycle.py
@@ -257,6 +257,80 @@ def test_put_api_env_writes_auth_json_for_provider(hermes_home):
)
+# ---------------------------------------------------------------------------
+# Keyed `providers` schema (v12+) — where the dashboard writes custom
+# endpoints. Its inline api_key is a real credential and higher-precedence
+# than the env var, so a stale copy left here shadows a rotation (#62269) and
+# survives a "remove from EVERY store" delete.
+# ---------------------------------------------------------------------------
+
+
+def test_update_rotates_keyed_providers_mirror(hermes_home):
+ old = "sk-kp-" + "n" * 24
+ new = "sk-kp-" + "o" * 24
+ _write_env(hermes_home, OPENAI_API_KEY=old)
+ _write_config(
+ hermes_home,
+ "providers:\n"
+ " myendpoint:\n"
+ " base_url: https://llm.example.test/v1\n"
+ f" api_key: {old}\n",
+ )
+
+ resp = client.put(
+ "/api/env", json={"key": "OPENAI_API_KEY", "value": new}, headers=HEADERS
+ )
+ assert resp.status_code == 200
+ assert "providers.myendpoint.api_key" in resp.json().get("config_updates", [])
+
+ cfg_text = hermes_home.joinpath("config.yaml").read_text(encoding="utf-8")
+ assert old not in cfg_text, "stale key in providers.<id> shadows the rotation (#62269)"
+ assert new in cfg_text, "keyed providers mirror not rotated to the new key"
+
+
+def test_delete_scrubs_keyed_providers_mirror(hermes_home):
+ old = "sk-kp-" + "p" * 24
+ _write_env(hermes_home, OPENAI_API_KEY=old)
+ _write_config(
+ hermes_home,
+ "providers:\n"
+ " myendpoint:\n"
+ " base_url: https://llm.example.test/v1\n"
+ f" api_key: {old}\n",
+ )
+
+ resp = client.request(
+ "DELETE", "/api/env", json={"key": "OPENAI_API_KEY"}, headers=HEADERS
+ )
+ assert resp.status_code == 200
+ assert "providers.myendpoint.api_key" in resp.json()["config_scrubbed"]
+ cfg_text = hermes_home.joinpath("config.yaml").read_text(encoding="utf-8")
+ assert old not in cfg_text, "delete must clear the credential from EVERY store"
+
+
+def test_scrub_never_touches_providers_base_url_alias(hermes_home):
+ """In the keyed ``providers`` schema ``api`` is the base_url alias, NOT a
+ credential. Even if the .env value coincided with a base_url, the scrub
+ must not rewrite a provider's endpoint URL."""
+ old = "https://llm.example.test/v1" # a URL that also happens to be the key value
+ _write_env(hermes_home, OPENAI_API_KEY=old)
+ _write_config(
+ hermes_home,
+ "providers:\n"
+ " myendpoint:\n"
+ f" api: {old}\n" # base_url alias — must be preserved
+ " model: my-model\n",
+ )
+
+ resp = client.request(
+ "DELETE", "/api/env", json={"key": "OPENAI_API_KEY"}, headers=HEADERS
+ )
+ assert resp.status_code == 200
+ cfg_text = hermes_home.joinpath("config.yaml").read_text(encoding="utf-8")
+ assert old in cfg_text, "providers.<id>.api is a base_url and must survive"
+ assert "providers.myendpoint.api" not in resp.json().get("config_scrubbed", [])
+
+
# ---------------------------------------------------------------------------
# Suppression round-trip: delete sticks, re-add lifts it
# ---------------------------------------------------------------------------