mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
Merge f92a137b4b into 715e9bb08d
This commit is contained in:
commit
606e347edb
3 changed files with 80 additions and 16 deletions
|
|
@ -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: object = None
|
||||
) -> tuple[dict, str | None]:
|
||||
"""
|
||||
Handle Anthropic OAuth token detection and header setup.
|
||||
|
||||
|
|
@ -368,33 +370,53 @@ 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
|
||||
try:
|
||||
return urlparse(url).hostname == "api.anthropic.com"
|
||||
except ValueError:
|
||||
return False
|
||||
|
||||
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")
|
||||
):
|
||||
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:
|
||||
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"
|
||||
else:
|
||||
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 +1353,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
|
||||
|
|
|
|||
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -213,6 +213,28 @@ 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
|
||||
|
||||
|
||||
class TestGetAnthropicHeaders:
|
||||
"""Tests for get_anthropic_headers method with OAuth support."""
|
||||
|
|
@ -4522,3 +4544,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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue