-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
fix(proxy): preserve and forward OAuth Authorization headers through proxy layer #19912
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -237,15 +237,22 @@ def clean_headers( | |
| """ | ||
| Removes litellm api key from headers | ||
| """ | ||
| from litellm.llms.anthropic.common_utils import is_anthropic_oauth_key | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Provider-specific import in proxy layer This introduces Anthropic-specific logic ( That said, I see that Context Used: Rule from Why: This practice ensur... (source) Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
|
|
||
| clean_headers = {} | ||
| litellm_key_lower = ( | ||
| litellm_key_header_name.lower() if litellm_key_header_name is not None else None | ||
| ) | ||
|
|
||
| for header, value in headers.items(): | ||
| header_lower = header.lower() | ||
| # Preserve Authorization header if it contains Anthropic OAuth token (sk-ant-oat*) | ||
| # This allows OAuth tokens to be forwarded to Anthropic-compatible providers | ||
| # via add_provider_specific_headers_to_request() | ||
| if header_lower == "authorization" and is_anthropic_oauth_key(value): | ||
| clean_headers[header] = value | ||
| # Check if header should be excluded: either in special headers cache or matches custom litellm key | ||
| if header_lower not in _SPECIAL_HEADERS_CACHE and ( | ||
| elif header_lower not in _SPECIAL_HEADERS_CACHE and ( | ||
| litellm_key_lower is None or header_lower != litellm_key_lower | ||
| ): | ||
| clean_headers[header] = value | ||
|
|
@@ -1687,6 +1694,8 @@ def add_provider_specific_headers_to_request( | |
| data: dict, | ||
| headers: dict, | ||
| ): | ||
| from litellm.llms.anthropic.common_utils import is_anthropic_oauth_key | ||
|
|
||
| anthropic_headers = {} | ||
| # boolean to indicate if a header was added | ||
| added_header = False | ||
|
|
@@ -1696,6 +1705,14 @@ def add_provider_specific_headers_to_request( | |
| anthropic_headers[header] = header_value | ||
| added_header = True | ||
|
|
||
| # Check for Authorization header with Anthropic OAuth token (sk-ant-oat*) | ||
| # This needs to be handled via provider-specific headers to ensure it only | ||
| # goes to Anthropic-compatible providers, not all providers in the router | ||
| for header, value in headers.items(): | ||
| if header.lower() == "authorization" and is_anthropic_oauth_key(value): | ||
| anthropic_headers[header] = value | ||
| added_header = True | ||
| break | ||
| if added_header is True: | ||
| # Anthropic headers work across multiple providers | ||
| # Store as comma-separated list so retrieval can match any of them | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing blank line between functions
PEP 8 requires two blank lines between top-level function definitions. There's only one blank line separating
is_anthropic_oauth_keyfromoptionally_handle_anthropic_oauth.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!