The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(web): stop mirroring env-backed provider keys into model.api_key (#88990)
Tekniuma90be562f4d3
diff --git a/hermes_cli/web_server.py b/hermes_cli/web_server.pyindex 9dfc5ef6436..ab620719005 100644--- a/hermes_cli/web_server.py+++ b/hermes_cli/web_server.py@@ -7747,7 +7747,27 @@ def _apply_model_assignment_sync(cfg.get("model", {}), provider, model, base_url, api_key)if isinstance(provider_entry, dict) and provider_entry.get("api_key"):- model_cfg["api_key"] = provider_entry["api_key"]+ # #88990: provider_entry comes from load_config(), which expands+ # ${VAR} env refs to plaintext. Copying that resolved value into+ # model.api_key writes the SECRET into config.yaml (and recreates+ # it on every re-apply, even after the user deletes it by hand).+ # Only mirror the key when the on-disk entry holds a literal+ # value; env-ref and key_env entries already resolve at runtime+ # via the provider entry itself.+ _raw_entry = None+ try:+ _stored, _raw_entry = find_provider_entry(+ read_raw_config().get("providers"), provider+ )+ except Exception:+ _raw_entry = None+ _raw_key = _raw_entry.get("api_key") if isinstance(_raw_entry, dict) else None+ _entry_uses_env = bool(+ (isinstance(_raw_entry, dict) and str(_raw_entry.get("key_env") or "").strip())+ or (isinstance(_raw_key, str) and re.search(r"\$\{[^}]+\}", _raw_key))+ )+ if not _entry_uses_env:+ model_cfg["api_key"] = provider_entry["api_key"]cfg["model"] = model_cfg# When switching the main provider to Nous, mirror the CLI'sdiff --git a/tests/hermes_cli/test_model_assignment_env_key_mirror.py b/tests/hermes_cli/test_model_assignment_env_key_mirror.pynew file mode 100644index 00000000000..5b31c7fd133--- /dev/null+++ b/tests/hermes_cli/test_model_assignment_env_key_mirror.py@@ -0,0 +1,77 @@+"""#88990: Desktop model assignment must not copy env-backed keys into model.api_key.++``POST /api/model/set`` (``_apply_model_assignment_sync``) mirrors a custom+provider entry's ``api_key`` into ``model.api_key``. ``load_config()`` expands+``${VAR}`` env refs to plaintext, so before the fix the RESOLVED secret was+written into config.yaml — and re-written on every re-apply even after the+user deleted it by hand. The mirror must be skipped when the on-disk provider+entry references the environment (``${VAR}`` template or ``key_env``), and+preserved when the entry holds a literal key.+"""++import importlib+import os++import pytest+import yaml+++def _hermes_home(tmp_path, monkeypatch):+ home = tmp_path / ".hermes"+ home.mkdir()+ monkeypatch.setenv("HERMES_HOME", str(home))+ import hermes_cli.config as config_mod++ importlib.reload(config_mod)+ yield home+ importlib.reload(config_mod)+++def _write_config(home, providers):+ cfg = {+ "model": {"provider": "openrouter", "default": "some/model"},+ "providers": providers,+ }+ (home / "config.yaml").write_text(yaml.safe_dump(cfg))+++def _apply(provider, model="local/model"):+ import hermes_cli.web_server as ws++ return ws._apply_model_assignment_sync("main", provider, model, "", "")+++def _raw_model_cfg(home):+ return (yaml.safe_load((home / "config.yaml").read_text()) or {}).get("model", {})+++def test_env_ref_key_not_copied_into_model_api_key(_hermes_home, monkeypatch):+ monkeypatch.setenv("MY_LOCAL_KEY", "sk-super-secret-value")+ _write_config(+ _hermes_home,+ {"mylocal": {"base_url": "http://localhost:1234/v1", "api_key": "${MY_LOCAL_KEY}"}},+ )+ _apply("mylocal")+ model_cfg = _raw_model_cfg(_hermes_home)+ assert "sk-super-secret-value" not in yaml.safe_dump(model_cfg)+ assert not model_cfg.get("api_key")+++def test_key_env_entry_not_copied(_hermes_home, monkeypatch):+ monkeypatch.setenv("MYLOCAL_API_KEY", "sk-keyenv-secret")+ _write_config(+ _hermes_home,+ {"mylocal": {"base_url": "http://localhost:1234/v1", "key_env": "MYLOCAL_API_KEY"}},+ )+ _apply("mylocal")+ assert "sk-keyenv-secret" not in (_hermes_home / "config.yaml").read_text()+++def test_literal_key_still_mirrored(_hermes_home):+ _write_config(+ _hermes_home,+ {"mylocal": {"base_url": "http://localhost:1234/v1", "api_key": "literal-key-abc"}},+ )+ _apply("mylocal")+ assert _raw_model_cfg(_hermes_home).get("api_key") == "literal-key-abc"