Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(mcp): reject duplicate Authorization headers at MCP ingress

Tin4a25cce11492
diff --git a/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py b/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py
index 9f28da19292..e7ffef0e4e3 100644
--- a/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py
+++ b/litellm/proxy/_experimental/mcp_server/auth/user_api_key_auth_mcp.py
@@ -591,10 +591,19 @@ class MCPRequestHandler:
ASGI headers are in format: List[List[bytes, bytes]]
We need to convert them to the format Headers expects.
+
+ Collapsing the ASGI list into a dict keeps the last value for a duplicated
+ header name, so a request carrying more than one ``Authorization`` is
+ rejected first: for the client-forwarded token modes the gateway relays the
+ caller's ``Authorization`` upstream, so a duplicate would make which token is
+ forwarded ambiguous (and diverge from what admission inspected). Multiple
+ ``Authorization`` headers is malformed for bearer auth anyway (RFC 9110: not
+ a comma-combinable field), so fail closed with a 400.
"""
+ raw_headers = scope.get("headers", [])
+ MCPRequestHandler._reject_duplicate_authorization(raw_headers)
try:
# ASGI headers are list of [name: bytes, value: bytes] pairs
- raw_headers = scope.get("headers", [])
# Convert bytes to strings and create dict for Headers constructor
headers_dict = {name.decode("latin-1"): value.decode("latin-1") for name, value in raw_headers}
return Headers(headers_dict)
@@ -603,6 +612,26 @@ class MCPRequestHandler:
# Return empty Headers object with empty dict
return Headers({})
+ @staticmethod
+ def _reject_duplicate_authorization(raw_headers: object) -> None:
+ """Raise 400 when the raw ASGI headers carry more than one ``Authorization`` header."""
+ if not isinstance(raw_headers, (list, tuple)):
+ return
+ count = 0
+ for entry in raw_headers:
+ if not isinstance(entry, (list, tuple)) or len(entry) < 1:
+ continue
+ name = entry[0]
+ if isinstance(name, (bytes, bytearray)) and bytes(name).lower() == b"authorization":
+ count += 1
+ elif isinstance(name, str) and name.lower() == "authorization":
+ count += 1
+ if count > 1:
+ raise HTTPException(
+ status_code=400,
+ detail="Multiple Authorization headers are not allowed",
+ )
+
@staticmethod
async def get_allowed_mcp_servers(
user_api_key_auth: Optional[UserAPIKeyAuth] = None,
diff --git a/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py b/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py
index c984ccb783e..ebaa6bc7cc0 100644
--- a/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py
+++ b/tests/test_litellm/proxy/_experimental/mcp_server/auth/test_user_api_key_auth_mcp.py
@@ -4,6 +4,7 @@ import sys
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
+from fastapi import HTTPException
from fastapi.testclient import TestClient
sys.path.insert(0, os.path.abspath("../../../..")) # Adds the parent directory to the system path
@@ -750,6 +751,40 @@ class TestMCPRequestHandler:
# For these tests, mcp_server_auth_headers should be empty
assert mcp_server_auth_headers == {}
+ def test_duplicate_authorization_header_is_rejected(self):
+ """A request carrying more than one Authorization header is malformed for bearer auth and,
+ for the client-forwarded token modes, would make which upstream token is forwarded ambiguous.
+ The ingress header converter must reject it with a 400 rather than silently keeping one."""
+ scope = {
+ "type": "http",
+ "method": "POST",
+ "path": "/mcp/tp_server",
+ "headers": [
+ (b"authorization", b"Bearer upstream-token-a"),
+ (b"authorization", b"Bearer upstream-token-b"),
+ (b"content-type", b"application/json"),
+ ],
+ }
+ with pytest.raises(HTTPException) as exc_info:
+ MCPRequestHandler._safe_get_headers_from_scope(scope)
+ assert exc_info.value.status_code == 400
+ assert "Authorization" in str(exc_info.value.detail)
+
+ def test_single_authorization_header_is_forwarded_verbatim(self):
+ """The rejection must not disturb the normal single-Authorization case: the value passes
+ through unchanged (guards against the duplicate check over-matching)."""
+ scope = {
+ "type": "http",
+ "method": "POST",
+ "path": "/mcp/tp_server",
+ "headers": [
+ (b"authorization", b"Bearer upstream-token"),
+ (b"content-type", b"application/json"),
+ ],
+ }
+ headers = MCPRequestHandler._safe_get_headers_from_scope(scope)
+ assert headers.get("authorization") == "Bearer upstream-token"
+
@pytest.mark.asyncio
class TestMCPOAuth2AuthFlow: