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(proxy): strip the JWT that authenticated on credential-less Vertex passthrough

mateo-berria3f654719e74
diff --git a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
index 6116c719be7..9a3bc82c6fa 100644
--- a/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
+++ b/litellm/proxy/pass_through_endpoints/llm_passthrough_endpoints.py
@@ -29,6 +29,7 @@ from litellm.constants import (
from litellm.llms.anthropic.common_utils import AnthropicModelInfo
from litellm.llms.vertex_ai.vertex_llm_base import VertexBase
from litellm.proxy._types import *
+from litellm.proxy.auth.handle_jwt import JWTHandler
from litellm.proxy.auth.route_checks import RouteChecks
from litellm.proxy.auth.user_api_key_auth import (
_get_bearer_token,
@@ -1815,13 +1816,28 @@ def _operator_configured_caller_key_header_names() -> tuple[str, ...]:
return override + pass_through
+def _is_authenticated_caller_jwt(value: str, jwt_claims: Mapping[str, object]) -> bool:
+ """Whether a header value is the JWT whose claims ``user_api_key_auth`` stored as ``jwt_claims``."""
+ presented_claims: Final = JWTHandler.get_unverified_claims(value)
+ if presented_claims is None:
+ return False
+ return all(
+ presented_claims.get(name) == claim
+ for name, claim in jwt_claims.items()
+ if name not in JWTHandler.LITELLM_INTERNAL_CLAIMS
+ )
+
+
def _is_authenticated_caller_secret(value: str, user_api_key_dict: UserAPIKeyAuth) -> bool:
- """Whether a header value is the master key or the key ``user_api_key_auth`` stored as ``api_key``."""
+ """Whether a header value is the master key, the JWT that authenticated, or the key stored as ``api_key``."""
from litellm.proxy.proxy_server import master_key
normalized: Final = _normalize_credential_value(value)
if master_key is not None and hmac.compare_digest(normalized.encode(), master_key.encode()):
return True
+ jwt_claims: Final = user_api_key_dict.jwt_claims
+ if jwt_claims and _is_authenticated_caller_jwt(normalized, jwt_claims):
+ return True
authenticated_key: Final = user_api_key_dict.api_key
if authenticated_key is None:
return False
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 3844ff603a4..3b506324ad7 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
@@ -1,3 +1,4 @@
+import base64
import contextlib
import json
import os
@@ -38,6 +39,7 @@ from litellm.proxy.pass_through_endpoints.llm_passthrough_endpoints import (
vllm_proxy_route,
)
from litellm.proxy._types import LitellmUserRoles, SpecialHeaders, UserAPIKeyAuth
+from litellm.proxy.auth.handle_jwt import JWTHandler
from litellm.types.passthrough_endpoints.vertex_ai import VertexPassThroughCredentials
@@ -3448,6 +3450,13 @@ def test_is_passthrough_request_streaming_tolerates_non_object_bodies(request_bo
assert is_passthrough_request_streaming(request_body) is expected
+def _unsigned_jwt(claims: Mapping[str, str]) -> str:
+ def segment(payload: Mapping[str, str]) -> str:
+ return base64.urlsafe_b64encode(json.dumps(dict(payload)).encode()).rstrip(b"=").decode()
+
+ return ".".join((segment({"alg": "RS256", "typ": "JWT"}), segment(claims), "c2lnbmF0dXJl"))
+
+
class TestVertexCredentiallessPassthroughVirtualKeyLeak:
"""Regression coverage for LIT-5997.
@@ -3782,10 +3791,14 @@ class TestVertexCredentiallessPassthroughVirtualKeyLeak:
GOOGLE_OAUTH_TOKEN = "ya29.byo-google-oauth-token"
- LITELLM_JWT = (
- "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9."
- "eyJzdWIiOiJqd3Qtc3ViamVjdCIsImlzcyI6Imh0dHBzOi8vaWRwLmV4YW1wbGUuY29tIn0."
- "c2lnbmF0dXJl"
+ LITELLM_JWT_CLAIMS = MappingProxyType({"sub": "jwt-subject", "iss": "https://idp.example.com"})
+ LITELLM_JWT = _unsigned_jwt(LITELLM_JWT_CLAIMS)
+ GOOGLE_SERVICE_ACCOUNT_JWT = _unsigned_jwt(
+ {
+ "sub": "[email protected]",
+ "iss": "[email protected]",
+ "aud": "https://aiplatform.googleapis.com/",
+ }
)
@pytest.mark.asyncio
@@ -3797,7 +3810,11 @@ class TestVertexCredentiallessPassthroughVirtualKeyLeak:
UserAPIKeyAuth(api_key="best-api-key-ever", user_role=LitellmUserRoles.PROXY_ADMIN),
id="custom-auth-returning-its-own-identifier",
),
- pytest.param("sk-master-1234", UserAPIKeyAuth(api_key=None, user_id="jwt-subject"), id="jwt-auth"),
+ pytest.param(
+ "sk-master-1234",
+ UserAPIKeyAuth(api_key=None, user_id="jwt-subject", jwt_claims=dict(LITELLM_JWT_CLAIMS)),
+ id="jwt-auth",
+ ),
pytest.param(None, UserAPIKeyAuth(api_key=GOOGLE_OAUTH_TOKEN), id="no-master-key-echoes-raw-header"),
],
)
@@ -3822,7 +3839,29 @@ class TestVertexCredentiallessPassthroughVirtualKeyLeak:
("credential", "authenticated"),
[
pytest.param("modified_key", UserAPIKeyAuth(api_key="modified_key"), id="custom-auth-echoing-opaque-credential"),
- pytest.param(LITELLM_JWT, UserAPIKeyAuth(api_key=LITELLM_JWT, user_id="jwt-subject"), id="jwt-auth-consuming-header"),
+ pytest.param(
+ LITELLM_JWT,
+ UserAPIKeyAuth(api_key=LITELLM_JWT, user_id="jwt-subject"),
+ id="custom-auth-echoing-jwt",
+ ),
+ pytest.param(
+ LITELLM_JWT,
+ UserAPIKeyAuth(api_key=None, user_id="jwt-subject", jwt_claims=dict(LITELLM_JWT_CLAIMS)),
+ id="jwt-auth",
+ ),
+ pytest.param(
+ LITELLM_JWT,
+ UserAPIKeyAuth(
+ api_key=None,
+ user_id="jwt-subject",
+ jwt_claims={
+ **LITELLM_JWT_CLAIMS,
+ JWTHandler.LITELLM_JWT_ISSUER_CLAIM: "https://idp.example.com",
+ JWTHandler.LITELLM_USER_ID_CLAIM: "jwt-subject",
+ },
+ ),
+ id="multi-issuer-jwt-auth-normalized-claims",
+ ),
],
)
async def test_non_sk_litellm_credential_that_authenticated_is_rejected_not_forwarded(
@@ -3836,6 +3875,22 @@ class TestVertexCredentiallessPassthroughVirtualKeyLeak:
assert forwarded is None, "the credential that authenticated the caller must never reach the upstream forwarder"
assert raised is not None and raised.status_code == 401
+ @pytest.mark.asyncio
+ async def test_jwt_authenticated_caller_keeps_a_different_byo_google_jwt(self, monkeypatch):
+ raised, forwarded = await self._run(
+ monkeypatch,
+ [
+ (b"x-litellm-api-key", self.LITELLM_JWT.encode()),
+ (b"authorization", f"Bearer {self.GOOGLE_SERVICE_ACCOUNT_JWT}".encode()),
+ (b"content-type", b"application/json"),
+ ],
+ authenticated=UserAPIKeyAuth(api_key=None, user_id="jwt-subject", jwt_claims=dict(self.LITELLM_JWT_CLAIMS)),
+ )
+ assert raised is None, f"a Google JWT that is not the one that authenticated must keep flowing: {raised}"
+ assert forwarded is not None
+ assert forwarded.get("authorization") == f"Bearer {self.GOOGLE_SERVICE_ACCOUNT_JWT}"
+ assert "x-litellm-api-key" not in forwarded
+
@pytest.mark.asyncio
async def test_master_key_in_authorization_alone_is_rejected(self, monkeypatch):
raised, forwarded = await self._run(