Skip to content

The agent that grows with you

fix(runtime_provider): don't send an OPENAI_BASE_URL-bound OPENAI_API_KEY to OpenRouter

teknium1da031c31c12b
diff --git a/hermes_cli/runtime_provider_backends.py b/hermes_cli/runtime_provider_backends.py
index 9e94ad6f243..01e70240798 100644
--- a/hermes_cli/runtime_provider_backends.py
+++ b/hermes_cli/runtime_provider_backends.py
@@ -13,7 +13,7 @@ from typing import Any, Dict, Optional
from agent.azure_identity_adapter import is_token_provider
from agent.secret_scope import get_secret_str
from hermes_constants import OPENROUTER_BASE_URL
-from utils import base_url_host_matches
+from utils import base_url_host_matches, base_url_hostname
def _rp():
@@ -156,7 +156,12 @@ def _resolve_openrouter_runtime(
)
)
if is_openrouter_context:
- candidates = [explicit_api_key, get_secret_str("OPENROUTER_API_KEY"), get_secret_str("OPENAI_API_KEY")]
+ # OPENAI_API_KEY is a legacy home for an OpenRouter key -- unless OPENAI_BASE_URL binds it
+ # to another host, where sending it to OpenRouter leaks that host's credential.
+ openai_base_host = base_url_hostname(get_secret_str("OPENAI_BASE_URL", "").strip())
+ openai_key_ok = not openai_base_host or openai_base_host == base_url_hostname(base_url)
+ candidates = [explicit_api_key, get_secret_str("OPENROUTER_API_KEY"),
+ get_secret_str("OPENAI_API_KEY") if openai_key_ok else ""]
else:
# ``model.api_key`` and ``model.key_env`` back a trusted config base_url only; the key_env
# rung is what a bare ``provider: custom`` block relies on (#67453).
diff --git a/tests/hermes_cli/test_runtime_provider_resolution.py b/tests/hermes_cli/test_runtime_provider_resolution.py
index 36e278fa706..caae54a521b 100644
--- a/tests/hermes_cli/test_runtime_provider_resolution.py
+++ b/tests/hermes_cli/test_runtime_provider_resolution.py
@@ -623,6 +623,26 @@ def test_openai_key_used_when_no_openrouter_key(monkeypatch):
assert resolved["api_key"] == "sk-openai-fallback"
[email protected]("openai_base_url, expected_key", [
+ ("https://proxy.corp.example/v1", ""),
+ ("proxy.corp.example:8080/v1", ""), # scheme-less still names a foreign host
+ ("https://openrouter.ai/api/v1", "sk-openai-fallback"),
+])
+def test_openai_key_bound_to_another_host_never_reaches_openrouter(monkeypatch, openai_base_url, expected_key):
+ """OPENAI_API_KEY is an OpenRouter fallback only while OPENAI_BASE_URL doesn't bind it elsewhere."""
+ from hermes_cli.runtime_provider_backends import _resolve_openrouter_runtime
+ monkeypatch.setattr(rp, "_get_model_config", lambda: {})
+ monkeypatch.setenv("OPENAI_BASE_URL", openai_base_url)
+ monkeypatch.delenv("OPENROUTER_BASE_URL", raising=False)
+ monkeypatch.delenv("OPENROUTER_API_KEY", raising=False)
+ monkeypatch.setenv("OPENAI_API_KEY", "sk-openai-fallback")
+
+ resolved = _resolve_openrouter_runtime(requested_provider="openrouter")
+
+ assert resolved["base_url"] == "https://openrouter.ai/api/v1"
+ assert resolved["api_key"] == expected_key
+
+
def test_custom_endpoint_uses_saved_config_base_url_when_env_missing(monkeypatch):
"""Persisted custom endpoints in config.yaml must still resolve when
OPENAI_BASE_URL is absent from the current environment.