From 008f1efe087feebd4ec76677c2f8f78908ae5f0f Mon Sep 17 00:00:00 2001 From: Victor Kyriazakos Date: Thu, 30 Jul 2026 19:03:33 +0000 Subject: [PATCH] refactor(providers): delegate is_official_openai_host to base_url_host_matches Independent review pass: utils.base_url_host_matches already owns the exact-or-dot-suffix hostname contract (userinfo/port stripped, lowercased, trailing dot removed), so the predicate delegates instead of hand-rolling a second suffix match to keep in sync. Also locks in the normalization behavior the review verified empirically: uppercase+port, trailing-dot, userinfo-stripped, and IPv6-literal cases added to the contract tests. --- hermes_cli/providers.py | 6 ++++-- tests/hermes_cli/test_official_openai_host.py | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/hermes_cli/providers.py b/hermes_cli/providers.py index 57483e9982..241b51d700 100644 --- a/hermes_cli/providers.py +++ b/hermes_cli/providers.py @@ -592,9 +592,11 @@ def is_official_openai_host(base_url: str) -> bool: (``proxy.test/api.openai.com/v1``) are rejected. A genuine ``*.api.openai.com`` subdomain requires control of openai.com DNS, so the dot-suffix match does not reopen the #32243 spoofing hole. + Delegates to ``utils.base_url_host_matches``, which owns the + exact-or-dot-suffix hostname contract (userinfo/port stripped, + lowercased, trailing dot removed) — one implementation, not two. """ - hostname = base_url_hostname(base_url) - return hostname == "api.openai.com" or hostname.endswith(".api.openai.com") + return base_url_host_matches(base_url, "api.openai.com") def host_mandated_api_mode(base_url: str = "") -> Optional[str]: diff --git a/tests/hermes_cli/test_official_openai_host.py b/tests/hermes_cli/test_official_openai_host.py index da0c92b556..8d91adfe5f 100644 --- a/tests/hermes_cli/test_official_openai_host.py +++ b/tests/hermes_cli/test_official_openai_host.py @@ -26,6 +26,9 @@ class TestOfficialHosts: "https://eu.api.openai.com/v1", "https://US.api.OpenAI.com/v1", # case-insensitive hostname "https://in.api.openai.com/v1", # future regional variants + "https://api.openai.com:443/v1", # port stripped by hostname parse + "https://api.openai.com./v1", # trailing dot normalized + "https://attacker.test@us.api.openai.com/v1", # userinfo stripped; real host wins ], ) def test_official_hosts_match(self, url): @@ -50,6 +53,8 @@ class TestSpoofRejection: # Unrelated hosts. "https://openrouter.ai/api/v1", "https://api.anthropic.com/v1", + # IPv6 literal and empty input. + "https://[::1]:8080/v1", "", ], )