Skip to content

The agent that grows with you

fix(anthropic): never refresh Anthropic credentials onto a foreign host

teknium1a4d8f173a9c4
diff --git a/agent/client_lifecycle.py b/agent/client_lifecycle.py
index 3af08919374..96527aa6969 100644
--- a/agent/client_lifecycle.py
+++ b/agent/client_lifecycle.py
@@ -872,13 +872,14 @@ class ClientLifecycleMixin:
def _try_refresh_anthropic_client_credentials(self) -> bool:
# Only native Anthropic rotates OAuth tokens; other anthropic_messages providers (MiniMax, Alibaba, ...)
# and Azure use static keys — a refresh would pick up the ~/.claude OAuth token and break auth.
- # Third-party Anthropic-compatible endpoints under provider 'anthropic' use static keys too;
- # skip them with the same detection build_anthropic_client uses.
- from agent.anthropic_endpoints import _is_third_party_anthropic_endpoint
+ # Any other host under provider 'anthropic' (a URL-bearing alias, #28660) holds no Anthropic
+ # credential either: refreshing would ship ANTHROPIC_API_KEY / the OAuth token to it. Match
+ # the hostname, not a substring, so ``proxy.example/anthropic.com`` stays foreign.
+ anthropic_base_url = getattr(self, "_anthropic_base_url", "") or ""
if (
self.api_mode != "anthropic_messages" or not hasattr(self, "_anthropic_api_key")
or self.provider != "anthropic"
- or _is_third_party_anthropic_endpoint(getattr(self, "_anthropic_base_url", "") or "")
+ or (anthropic_base_url and not base_url_host_matches(anthropic_base_url, "anthropic.com"))
):
return False
try:
diff --git a/tests/agent/test_anthropic_third_party_oauth_guard.py b/tests/agent/test_anthropic_third_party_oauth_guard.py
index ad92893e8b5..29251d02d44 100644
--- a/tests/agent/test_anthropic_third_party_oauth_guard.py
+++ b/tests/agent/test_anthropic_third_party_oauth_guard.py
@@ -75,13 +75,17 @@ class TestOAuthFlagOnRefresh:
# And the flag is untouched regardless.
assert agent._is_anthropic_oauth is False
- def test_third_party_endpoint_skips_refresh(self, agent):
+ @pytest.mark.parametrize("base_url", [
+ "https://llmbox.bytedance.net",
+ "http://127.0.0.1:8080/anthropic.com", # substring spoof: the host is still foreign
+ ])
+ def test_third_party_endpoint_skips_refresh(self, agent, base_url):
"""provider == 'anthropic' on a third-party endpoint must not refresh: the refresh
would swap in native Anthropic credentials the endpoint was never given."""
agent.api_mode = "anthropic_messages"
agent.provider = "anthropic"
agent._anthropic_api_key = "custom-api-key"
- agent._anthropic_base_url = "https://llmbox.bytedance.net"
+ agent._anthropic_base_url = base_url
agent._anthropic_client = MagicMock()
agent._is_anthropic_oauth = False