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(vertex-passthrough): never forward proxy auth headers to Google

mateo-berrie7c2ede159c9
diff --git a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
index eaed5185fa8..75e3baf2c4a 100644
--- a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
+++ b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
@@ -1742,19 +1742,27 @@ def _bearer_stripped(value: str) -> str:
return value
+_HEADERS_NEVER_FORWARDED_TO_VERTEX: Final = frozenset(
+ {"content-length", "host", "x-litellm-api-key", "api-key", "x-api-key"}
+)
+
+
def _forwarded_headers_for_credentialless_vertex_passthrough(request: Request) -> Mapping[str, str]:
"""
Header set to forward on the bring-your-own-credentials Vertex passthrough
branch, used when the proxy has no Vertex credential configured.
- The LiteLLM virtual key that authenticated the caller is never forwarded to
- Google. LiteLLM accepts that key from several headers (``Authorization``,
- ``x-litellm-api-key``, ``x-goog-api-key``, ``api-key``, ``x-api-key``), and
- ``x-goog-api-key`` doubles as a genuine Google credential, so the key is dropped
- by value across every header rather than by name. A caller may still bring their
- own Google credential in the ``Authorization`` (OAuth token) or ``x-goog-api-key``
- header; when neither survives the request is rejected so the virtual key cannot
- leak upstream.
+ No credential the proxy accepts for caller authentication is forwarded to
+ Google. LiteLLM reads the caller's virtual key from ``x-litellm-api-key``,
+ ``api-key``, ``x-api-key``, ``Authorization``, and ``x-goog-api-key``. Vertex
+ only ever authenticates with an OAuth token in ``Authorization`` or an API key
+ in ``x-goog-api-key``, so ``x-litellm-api-key`` / ``api-key`` / ``x-api-key``
+ can only carry caller auth material and are dropped by name. ``Authorization``
+ and ``x-goog-api-key`` may instead carry a genuine bring-your-own Google
+ credential, so they are kept unless their value is the caller's virtual key,
+ which is dropped by value (normalizing any ``Bearer`` prefix). When neither a
+ surviving ``Authorization`` nor ``x-goog-api-key`` remains the request is
+ rejected so the virtual key cannot leak upstream.
"""
incoming: Final = _safe_get_request_headers(request)
caller_virtual_key: Final = _bearer_stripped(get_litellm_virtual_key(request))
@@ -1762,7 +1770,7 @@ def _forwarded_headers_for_credentialless_vertex_passthrough(request: Request) -
{
name: value
for name, value in incoming.items()
- if name not in ("content-length", "host", "x-litellm-api-key")
+ if name not in _HEADERS_NEVER_FORWARDED_TO_VERTEX
and not (caller_virtual_key and _bearer_stripped(value) == caller_virtual_key)
}
)
diff --git a/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py b/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py
index 52ff6e709c5..e268c8cd2b9 100644
--- a/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py
+++ b/tests/test_litellm/proxy/pass_through_endpoints/test_llm_pass_through_endpoints.py
@@ -3457,9 +3457,11 @@ class TestVertexCredentiallessPassthroughVirtualKeyLeak:
leak the proxy's own secret to an upstream provider.
A credential-less request that carries no upstream Google credential must now
- fail with a clean 401 and never reach ``create_pass_through_route``; a genuine
- bring-your-own Google credential must still pass through, with the virtual key
- stripped by value from every forwarded header.
+ fail with a clean 401 and never reach ``create_pass_through_route``. The
+ proxy-only auth headers Google never consumes (``x-litellm-api-key``,
+ ``api-key``, ``x-api-key``) are dropped by name, and the virtual key is dropped
+ by value from ``Authorization`` / ``x-goog-api-key``, which may instead carry a
+ genuine bring-your-own Google credential that must still pass through.
"""
VKEY = "sk-litellm-victim-key"
@@ -3582,14 +3584,14 @@ class TestVertexCredentiallessPassthroughVirtualKeyLeak:
assert self.VKEY not in " ".join(f"{name}:{value}" for name, value in forwarded.items())
@pytest.mark.asyncio
- async def test_virtual_key_echoed_in_alternate_auth_headers_is_stripped_by_value(self, monkeypatch):
+ async def test_alternate_proxy_auth_headers_are_never_forwarded_to_google(self, monkeypatch):
raised, forwarded = await self._run(
monkeypatch,
[
(b"x-litellm-api-key", self.VKEY.encode()),
(b"authorization", b"Bearer ya29.google-oauth-token"),
- (b"api-key", self.VKEY.encode()),
- (b"x-api-key", self.VKEY.encode()),
+ (b"api-key", b"azure-style-caller-secret"),
+ (b"x-api-key", b"anthropic-style-caller-secret"),
(b"content-type", b"application/json"),
],
)
@@ -3599,7 +3601,10 @@ class TestVertexCredentiallessPassthroughVirtualKeyLeak:
assert "api-key" not in forwarded
assert "x-api-key" not in forwarded
assert "x-litellm-api-key" not in forwarded
- assert self.VKEY not in " ".join(f"{name}:{value}" for name, value in forwarded.items())
+ forwarded_blob = " ".join(f"{name}:{value}" for name, value in forwarded.items())
+ assert self.VKEY not in forwarded_blob
+ assert "azure-style-caller-secret" not in forwarded_blob
+ assert "anthropic-style-caller-secret" not in forwarded_blob
class TestGetAzureAISearchIndexFromEndpoint: