The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(anthropic): bearer-only clients omit x-api-key the same copy-safe way
kshitijk4poor17b3cc202342
diff --git a/agent/anthropic_adapter.py b/agent/anthropic_adapter.pyindex 44684b58646..2246ab74faf 100644--- a/agent/anthropic_adapter.py+++ b/agent/anthropic_adapter.py@@ -343,24 +343,22 @@ def _build_anthropic_client_with_bearer_hook(def _new_sdk_client(sdk, kwargs: Dict[str, Any], headers: Dict[str, str]):- """``sdk.Anthropic(**kwargs)`` with ``headers`` attached. Bearer-only construction leaves- ``api_key`` unset, so the SDK fills it from ANTHROPIC_API_KEY (loaded from ~/.hermes/.env) and- sends dual auth — X-Api-Key *and* Authorization: Bearer *** on every Portal/MiniMax/OAuth/Entra- request; clear it whenever we intentionally authenticated via auth_token. Api-key-only- construction has the mirror problem: the SDK fills ``auth_token`` from ANTHROPIC_AUTH_TOKEN in- the environment and ships that Bearer credential to third-party Anthropic-compatible endpoints- alongside x-api-key — suppress it whenever we intentionally authenticated via api_key."""- if headers:- kwargs["default_headers"] = headers+ """``sdk.Anthropic(**kwargs)`` with ``headers`` attached, sending exactly ONE credential.++ The SDK fills whichever of ``api_key`` / ``auth_token`` we left unset from ANTHROPIC_API_KEY /+ ANTHROPIC_AUTH_TOKEN in the environment (both loaded from ~/.hermes/.env) and then sends dual+ auth — x-api-key *and* Authorization: Bearer — shipping a foreign credential to Portal / MiniMax+ / OAuth / Entra / third-party endpoints (#26970, #105774). An ``Omit()`` default header is the+ SDK-sanctioned way to drop the other header, and unlike an attribute clear it survives+ ``with_options()``, which re-runs the constructor and re-reads the environment."""+ merged = dict(headers or {})if "api_key" in kwargs and "auth_token" not in kwargs:- # A `client.auth_token = None` clear does not survive with_options(): the copy re-runs- # the constructor and re-reads ANTHROPIC_AUTH_TOKEN. An Omit() default header rides- # along on every copy, so the ambient Bearer never reaches the wire.- kwargs["default_headers"] = {**(kwargs.get("default_headers") or {}), "Authorization": sdk.Omit()}- client = sdk.Anthropic(**kwargs)- if "auth_token" in kwargs and "api_key" not in kwargs:- client.api_key = None- return client+ merged["Authorization"] = sdk.Omit()+ elif "auth_token" in kwargs and "api_key" not in kwargs:+ merged["X-Api-Key"] = sdk.Omit()+ if merged:+ kwargs["default_headers"] = merged+ return sdk.Anthropic(**kwargs)def _auth_style(api_key, base_url, normalized_base_url) -> str:diff --git a/tests/agent/test_anthropic_adapter.py b/tests/agent/test_anthropic_adapter.pyindex 7ed804baa7f..2bddea3bb1d 100644--- a/tests/agent/test_anthropic_adapter.py+++ b/tests/agent/test_anthropic_adapter.py@@ -84,9 +84,9 @@ class TestBuildAnthropicClient:kwargs = mock_sdk.Anthropic.call_args[1]assert kwargs["auth_token"] == "minimax-secret-123"assert "api_key" not in kwargs- assert kwargs["default_headers"] == {- "anthropic-beta": "interleaved-thinking-2025-05-14"- }+ assert kwargs["default_headers"]["anthropic-beta"] == "interleaved-thinking-2025-05-14"+ # bearer-only construction omits x-api-key so an env ANTHROPIC_API_KEY never rides along+ assert kwargs["default_headers"]["X-Api-Key"] is mock_sdk.Omit.return_valuedef test_azure_foundry_anthropic_endpoint_uses_bearer_auth(self):diff --git a/tests/agent/test_anthropic_client_auth.py b/tests/agent/test_anthropic_client_auth.pyindex f922dcd4333..a7502844009 100644--- a/tests/agent/test_anthropic_client_auth.py+++ b/tests/agent/test_anthropic_client_auth.py@@ -30,11 +30,14 @@ def test_api_key_client_and_its_copies_never_carry_the_env_bearer(monkeypatch):assert headers.get("x-api-key") == "provider-key"assert "authorization" not in headers- # The mirror case is untouched: bearer-style construction still clears the env api key.+ # Mirror case: bearer-style construction must not ship an env ANTHROPIC_API_KEY, on copies either.monkeypatch.setenv("ANTHROPIC_API_KEY", "sentinel-env-key-DO-NOT-SEND")bearer = _new_sdk_client(anthropic_sdk, {"auth_token": "bearer-secret", "base_url": "http://127.0.0.1:1"}, {})- assert bearer.api_key is None- assert bearer.auth_headers == {"Authorization": "Bearer bearer-secret"}+ for wire_client in (bearer, bearer.with_options(timeout=30)):+ headers = dict(wire_client._build_headers(+ FinalRequestOptions(method="post", url="/v1/messages", json_data={})))+ assert headers.get("authorization") == "Bearer bearer-secret"+ assert "x-api-key" not in headersdef test_third_party_request_on_the_wire_carries_no_foreign_bearer(monkeypatch):@@ -75,4 +78,4 @@ def test_third_party_request_on_the_wire_carries_no_foreign_bearer(monkeypatch):server.shutdown()assert captured["headers"].get("x-api-key") == "third-party-provider-key"- assert SENTINEL not in captured["headers"].get("authorization", "")+ assert "authorization" not in captured["headers"]diff --git a/tests/agent/test_auxiliary_client_azure_foundry.py b/tests/agent/test_auxiliary_client_azure_foundry.pyindex b0bce44af40..79ced884f76 100644--- a/tests/agent/test_auxiliary_client_azure_foundry.py+++ b/tests/agent/test_auxiliary_client_azure_foundry.py@@ -226,6 +226,8 @@ class TestAuxAzureFoundryEntra:self.base_url = kwargs.get("base_url", "")class _FakeAnthropicSDK:+ Omit = object # the adapter omits the unused credential header via sdk.Omit()+class Anthropic:def __init__(self, **kwargs):received["anthropic"] = kwargs