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.
This commit is contained in:
Victor Kyriazakos
2026-07-30 19:03:33 +00:00
committed by Teknium
parent 173a73e191
commit 008f1efe08
2 changed files with 9 additions and 2 deletions
+4 -2
View File
@@ -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]:
@@ -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",
"",
],
)