Skip to content

The agent that grows with you

fix(runtime_provider): bind an OPENAI_BASE_URL-bound OPENAI_API_KEY to its origin, not its hostname

John Paul Solivaa5368c206b73
diff --git a/hermes_cli/runtime_provider_backends.py b/hermes_cli/runtime_provider_backends.py
index 1893371ea74..a0792943cdc 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, base_url_hostname
+from utils import base_url_host_matches, base_url_hostname, base_url_origin
def _rp():
@@ -158,12 +158,14 @@ def _resolve_openrouter_runtime(
)
if is_openrouter_context:
# OPENAI_API_KEY is a legacy home for an OpenRouter key. When OPENAI_BASE_URL binds it, it
- # goes only to that host. Unbound, openrouter.ai gets it only when it is OpenRouter-shaped
- # (sk-or-), so a real OpenAI key never reaches a third party.
+ # goes only to that origin: another scheme or port on the same host is another endpoint.
+ # Unbound, openrouter.ai gets it only when it is OpenRouter-shaped (sk-or-), so a real OpenAI
+ # key never reaches a third party.
openai_key = get_secret_str("OPENAI_API_KEY")
- openai_base_host = base_url_hostname(get_secret_str("OPENAI_BASE_URL", "").strip())
- if openai_base_host:
- openai_key_ok = openai_base_host == base_url_hostname(base_url)
+ openai_base_url = get_secret_str("OPENAI_BASE_URL", "").strip()
+ if base_url_hostname(openai_base_url):
+ openai_origin = base_url_origin(openai_base_url) # empty on a bad port: bound, matches nothing
+ openai_key_ok = bool(openai_origin[1]) and openai_origin == base_url_origin(base_url)
else:
openai_key_ok = not is_openrouter_url or rp.looks_like_openrouter_key(openai_key)
candidates = [explicit_api_key, get_secret_str("OPENROUTER_API_KEY"),
diff --git a/tests/hermes_cli/test_runtime_provider_resolution.py b/tests/hermes_cli/test_runtime_provider_resolution.py
index 12a21568f2e..dcbfa6dca01 100644
--- a/tests/hermes_cli/test_runtime_provider_resolution.py
+++ b/tests/hermes_cli/test_runtime_provider_resolution.py
@@ -722,6 +722,25 @@ def test_openai_key_bound_to_another_host_never_reaches_openrouter(monkeypatch,
assert resolved["api_key"] == expected_key
[email protected]("openrouter_base_url, inherits", [
+ ("https://gw.example.com:443/v1", True),
+ ("http://gw.example.com/v1", False),
+ ("https://gw.example.com:8443/v1", False),
+])
+def test_openai_key_follows_only_its_bound_origin(monkeypatch, openrouter_base_url, inherits):
+ """An OPENAI_BASE_URL-bound OPENAI_API_KEY reaches an OpenRouter mirror only on the identical origin:
+ the same hostname over http://, or on another port, is a different endpoint."""
+ monkeypatch.setenv("OPENAI_BASE_URL", "https://gw.example.com/v1")
+ monkeypatch.setenv("OPENAI_API_KEY", "sk-proxy-bound-key")
+ monkeypatch.setenv("OPENROUTER_BASE_URL", openrouter_base_url)
+ monkeypatch.delenv("OPENROUTER_API_KEY", raising=False)
+
+ resolved = rp.resolve_runtime_provider(requested="openrouter")
+
+ assert resolved["base_url"] == openrouter_base_url
+ assert (resolved["api_key"] == "sk-proxy-bound-key") is inherits
+
+
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.