diff --git a/litellm/llms/anthropic/common_utils.py b/litellm/llms/anthropic/common_utils.py index 1f5e4e7de39..9ac04798d5f 100644 --- a/litellm/llms/anthropic/common_utils.py +++ b/litellm/llms/anthropic/common_utils.py @@ -358,7 +358,9 @@ def merge_anthropic_beta_headers(existing: str | Sequence[str] | None, new_beta: return ",".join(sorted(betas)) -def optionally_handle_anthropic_oauth(headers: dict, api_key: str | None) -> tuple[dict, str | None]: +def optionally_handle_anthropic_oauth( + headers: dict, api_key: str | None, api_base: str | None = None +) -> tuple[dict, str | None]: """ Handle Anthropic OAuth token detection and header setup. @@ -368,33 +370,55 @@ def optionally_handle_anthropic_oauth(headers: dict, api_key: str | None) -> tup Args: headers: Request headers dict api_key: Current API key (may be None) + api_base: Resolved api_base (may be None) Returns: Tuple of (updated headers, api_key) """ # Check Authorization header (passthrough / forwarded requests) auth_header: Final = next((value for name, value in headers.items() if name.lower() == "authorization"), "") + + from urllib.parse import urlparse + + def _is_anthropic_host(url: object) -> bool: + if not isinstance(url, str) or not url: + return True + normalized_url: Final = url if "://" in url else f"https://{url}" + try: + parsed: Final = urlparse(normalized_url) + except ValueError: + return False + else: + return parsed.scheme == "https" and parsed.hostname == "api.anthropic.com" + + is_anthropic: Final = _is_anthropic_host(api_base) + if auth_header.startswith(f"Bearer {ANTHROPIC_OAUTH_TOKEN_PREFIX}"): - api_key = auth_header.removeprefix("Bearer ") - for name in tuple( - header_name for header_name in headers if header_name.lower() in ("x-api-key", "authorization") - ): + pop_targets: Final = ("x-api-key", "authorization") if is_anthropic else ("authorization",) + for name in tuple(header_name for header_name in headers if header_name.lower() in pop_targets): headers.pop(name) - headers["authorization"] = auth_header - headers["anthropic-beta"] = merge_anthropic_beta_headers( - headers.get("anthropic-beta"), ANTHROPIC_OAUTH_BETA_HEADER - ) - headers["anthropic-dangerous-direct-browser-access"] = "true" + if is_anthropic: + api_key = auth_header.removeprefix("Bearer ") # rebind-ok: use oauth token as api_key for anthropic host + headers["authorization"] = auth_header + headers["anthropic-beta"] = merge_anthropic_beta_headers( + headers.get("anthropic-beta"), ANTHROPIC_OAUTH_BETA_HEADER + ) + headers["anthropic-dangerous-direct-browser-access"] = "true" + elif api_key and api_key.startswith(ANTHROPIC_OAUTH_TOKEN_PREFIX): + api_key = None # rebind-ok: drop oauth token when routing to non-anthropic host return headers, api_key # Check api_key directly (standard chat/completion flow) if api_key and api_key.startswith(ANTHROPIC_OAUTH_TOKEN_PREFIX): - for name in tuple(header_name for header_name in headers if header_name.lower() == "x-api-key"): - headers.pop(name) - headers["authorization"] = f"Bearer {api_key}" - headers["anthropic-beta"] = merge_anthropic_beta_headers( - headers.get("anthropic-beta"), ANTHROPIC_OAUTH_BETA_HEADER - ) - headers["anthropic-dangerous-direct-browser-access"] = "true" + if is_anthropic: + for name in tuple(header_name for header_name in headers if header_name.lower() == "x-api-key"): + headers.pop(name) + headers["authorization"] = f"Bearer {api_key}" + headers["anthropic-beta"] = merge_anthropic_beta_headers( + headers.get("anthropic-beta"), ANTHROPIC_OAUTH_BETA_HEADER + ) + headers["anthropic-dangerous-direct-browser-access"] = "true" + else: + api_key = None # rebind-ok: drop oauth token when routing to non-anthropic host return headers, api_key @@ -1331,7 +1355,11 @@ class AnthropicModelInfo(BaseLLMModelInfo): params_mapping is not None and params_mapping.get("use_bearer_for_custom_base", False) ) # Check for Anthropic OAuth token in headers - headers, api_key = optionally_handle_anthropic_oauth(headers=headers, api_key=api_key) + headers, api_key = optionally_handle_anthropic_oauth( + headers=headers, + api_key=api_key, + api_base=api_base, # pyright: ignore[reportUnknownArgumentType] # params_mapping is untyped dict + ) api_key = AnthropicModelInfo.get_api_key(api_key) # Resolve auth_token from ANTHROPIC_AUTH_TOKEN if api_key is not set auth_token: str | None = None diff --git a/litellm/llms/anthropic/pass_through/messages/transformation.py b/litellm/llms/anthropic/pass_through/messages/transformation.py index 35db54147c5..07c15f99ef7 100644 --- a/litellm/llms/anthropic/pass_through/messages/transformation.py +++ b/litellm/llms/anthropic/pass_through/messages/transformation.py @@ -307,7 +307,7 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): api_base: str | None = None, ) -> tuple[dict, str | None]: # Check for Anthropic OAuth token in Authorization header - headers, api_key = optionally_handle_anthropic_oauth(headers=headers, api_key=api_key) + headers, api_key = optionally_handle_anthropic_oauth(headers=headers, api_key=api_key, api_base=api_base) if not _carries_caller_credential(headers): self._apply_env_auth_header( @@ -347,7 +347,9 @@ class AnthropicMessagesConfig(BaseAnthropicMessagesConfig): api_key=api_key, api_base=api_base, ) - oauth_headers, oauth_api_key = optionally_handle_anthropic_oauth(headers=headers, api_key=api_key) + oauth_headers, oauth_api_key = optionally_handle_anthropic_oauth( + headers=headers, api_key=api_key, api_base=api_base + ) if not _carries_caller_credential(oauth_headers): self._apply_env_auth_header( diff --git a/tests/unit/llms/anthropic/test_anthropic_common_utils.py b/tests/unit/llms/anthropic/test_anthropic_common_utils.py index 093b4d6cce8..d29267fea67 100644 --- a/tests/unit/llms/anthropic/test_anthropic_common_utils.py +++ b/tests/unit/llms/anthropic/test_anthropic_common_utils.py @@ -213,6 +213,44 @@ class TestOptionallyHandleAnthropicOAuth: assert returned_api_key is None assert "authorization" not in updated_headers + def test_optionally_handle_anthropic_oauth_scopes_to_anthropic_host(self): + """OAuth tokens should only be forwarded if api_base points to api.anthropic.com.""" + from litellm.llms.anthropic.common_utils import ( + optionally_handle_anthropic_oauth, + ) + + headers = {"Authorization": f"Bearer {FAKE_OAUTH_TOKEN}"} + + # Test completely different hostname + updated_headers, extracted_api_key = optionally_handle_anthropic_oauth( + headers, None, api_base="https://custom-gateway.com/v1" + ) + assert extracted_api_key is None + assert "authorization" not in updated_headers + + # Test lookalike hostname + updated_headers, extracted_api_key = optionally_handle_anthropic_oauth( + headers, FAKE_OAUTH_TOKEN, api_base="https://api.anthropic.com.attacker.com/v1" + ) + assert extracted_api_key is None + assert "authorization" not in updated_headers + + # Test configured deployment api_key is preserved for custom gateway + custom_headers = {"Authorization": f"Bearer {FAKE_OAUTH_TOKEN}"} + updated_headers, extracted_api_key = optionally_handle_anthropic_oauth( + custom_headers, "sk-configured-deploy-key", api_base="https://custom-gateway.com/v1" + ) + assert extracted_api_key == "sk-configured-deploy-key" + assert "authorization" not in updated_headers + + # Test plaintext HTTP to api.anthropic.com is rejected for TLS security + http_headers = {"Authorization": f"Bearer {FAKE_OAUTH_TOKEN}"} + updated_headers, extracted_api_key = optionally_handle_anthropic_oauth( + http_headers, "sk-configured-deploy-key", api_base="http://api.anthropic.com" + ) + assert extracted_api_key == "sk-configured-deploy-key" + assert "authorization" not in updated_headers + class TestGetAnthropicHeaders: """Tests for get_anthropic_headers method with OAuth support.""" @@ -4522,3 +4560,17 @@ def test_shared_legacy_thinking_translation_drops_a_non_numeric_budget_under_dro ) assert optional_params == {"thinking": {"type": "adaptive"}} + + +def test_optionally_handle_anthropic_oauth_invalid_url(): + """Test that invalid URLs don't break OAuth validation and default to non-Anthropic.""" + from litellm.llms.anthropic.common_utils import optionally_handle_anthropic_oauth + + headers = {"Authorization": "Bearer sk-ant-oat01-token"} + api_key = "sk-ant-oat01-token" + + # Test with a URL that will trigger ValueError in urllib.parse.urlparse + updated_headers, updated_api_key = optionally_handle_anthropic_oauth(headers, api_key, api_base="http://[::1") + + assert "Authorization" not in {k.lower(): v for k, v in updated_headers.items()} + assert updated_api_key is None