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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(auth): harden JWT routing wildcard iss and merge list team_id claims
Sameer Kankute055bdc35077c
diff --git a/litellm/proxy/auth/handle_jwt.py b/litellm/proxy/auth/handle_jwt.pyindex a270031c33c..5b116c142b2 100644--- a/litellm/proxy/auth/handle_jwt.py+++ b/litellm/proxy/auth/handle_jwt.py@@ -227,8 +227,8 @@ class JWTHandler:def get_all_jwt_team_ids(self, token: dict) -> List[str]:"""Return team IDs from both the plural ``team_ids_jwt_field`` and the- singular ``team_id_jwt_field`` claim, as a deduplicated list preserving- plural-first order.+ singular ``team_id_jwt_field`` claim (string or list of strings), as a+ deduplicated list preserving plural-first order.Membership-reconciliation paths (SSO callback, JWT-bearer sync) needto consider both claim shapes. Reading only the plural field — as@@ -249,9 +249,14 @@ class JWTHandler:default=None,)if isinstance(singular, list):- singular = singular[0] if singular else None- if singular and singular not in team_ids:- team_ids.append(singular)+ for item in singular:+ if item is None:+ continue+ sid = str(item)+ if sid and sid not in team_ids:+ team_ids.append(sid)+ elif singular and str(singular) not in team_ids:+ team_ids.append(str(singular))return team_idsdef get_end_user_id(diff --git a/litellm/proxy/auth/user_api_key_auth.py b/litellm/proxy/auth/user_api_key_auth.pyindex 2f61a40bc9a..213c68f1db2 100644--- a/litellm/proxy/auth/user_api_key_auth.py+++ b/litellm/proxy/auth/user_api_key_auth.py@@ -219,6 +219,11 @@ def _routing_selector_matches_claim(def _selector_matches_claim(selector: str, claim: str) -> bool:# NOTE: wildcard matching is case-sensitive (fnmatch.fnmatchcase).if "*" in selector or "?" in selector:+ # Without scope splitting, do not let `*` span whitespace: a malformed+ # iss like "trusted.example.com evil.com" must not match "trusted.*".+ # Scope uses split_space_delimited so each claim token is checked separately.+ if not split_space_delimited and any(ch.isspace() for ch in claim):+ return Falsereturn fnmatch.fnmatchcase(claim, selector)return selector == claimdiff --git a/tests/test_litellm/proxy/auth/test_handle_jwt.py b/tests/test_litellm/proxy/auth/test_handle_jwt.pyindex c09c303deee..90c5d4f4fc5 100644--- a/tests/test_litellm/proxy/auth/test_handle_jwt.py+++ b/tests/test_litellm/proxy/auth/test_handle_jwt.py@@ -524,6 +524,11 @@ def test_get_all_jwt_team_ids_unions_singular_and_plural():"b",]+ # singular field as multi-element list (some IdPs) — merge all, preserve plural-first order+ assert jwt_handler.get_all_jwt_team_ids(+ {"team_id": ["primary", "secondary"], "teams": ["a"]}+ ) == ["a", "primary", "secondary"]+# neither populatedassert jwt_handler.get_all_jwt_team_ids({}) == []diff --git a/tests/test_litellm/proxy/auth/test_user_api_key_auth.py b/tests/test_litellm/proxy/auth/test_user_api_key_auth.pyindex 4b0f6c0ea2f..a6483403c27 100644--- a/tests/test_litellm/proxy/auth/test_user_api_key_auth.py+++ b/tests/test_litellm/proxy/auth/test_user_api_key_auth.py@@ -587,6 +587,14 @@ def _assert_get_api_key_with_custom_litellm_key_header(False,False,),+ # Wildcard iss must not match space-containing claim strings (fnmatch * spans spaces)+ (+ "trusted.*",+ "trusted.example.com attacker.example.com",+ False,+ False,+ ),+ ("trusted.*", "trusted.example.com", True, False),(["issuer-a.example.com", "issuer-b.example.com"],"issuer-b.example.com",@@ -667,7 +675,10 @@ def test_routing_selector_matches_claim_parametrized(aud=["api://litellm", "api://fallback"],path="oauth2",),- {"iss": "oauth-issuer.example.com", "aud": ["api://other", "api://litellm"]},+ {+ "iss": "oauth-issuer.example.com",+ "aud": ["api://other", "api://litellm"],+ },True,),# All provided selectors are AND-ed.@@ -1738,20 +1749,21 @@ class TestJWTOAuth2Coexistence:mock_request.headers = {"authorization": f"Bearer {jwt_token}"}mock_request.query_params = {}- with patch(- "litellm.proxy.proxy_server.general_settings", general_settings- ), patch("litellm.proxy.proxy_server.premium_user", True), patch(- "litellm.proxy.proxy_server.master_key", "sk-master"- ), patch(- "litellm.proxy.proxy_server.prisma_client", None- ), patch(- "litellm.proxy.auth.user_api_key_auth.Oauth2Handler.check_oauth2_token",- new_callable=AsyncMock,- return_value=mock_oauth2_response,- ) as mock_oauth2, patch(- "litellm.proxy.auth.user_api_key_auth.JWTAuthManager.auth_builder",- new_callable=AsyncMock,- ) as mock_jwt_auth:+ with (+ patch("litellm.proxy.proxy_server.general_settings", general_settings),+ patch("litellm.proxy.proxy_server.premium_user", True),+ patch("litellm.proxy.proxy_server.master_key", "sk-master"),+ patch("litellm.proxy.proxy_server.prisma_client", None),+ patch(+ "litellm.proxy.auth.user_api_key_auth.Oauth2Handler.check_oauth2_token",+ new_callable=AsyncMock,+ return_value=mock_oauth2_response,+ ) as mock_oauth2,+ patch(+ "litellm.proxy.auth.user_api_key_auth.JWTAuthManager.auth_builder",+ new_callable=AsyncMock,+ ) as mock_jwt_auth,+ ):litellm.proxy.proxy_server.jwt_handler.update_environment(prisma_client=None,user_api_key_cache=DualCache(),@@ -1809,20 +1821,21 @@ class TestJWTOAuth2Coexistence:mock_request.headers = {"authorization": f"Bearer {jwt_token}"}mock_request.query_params = {}- with patch(- "litellm.proxy.proxy_server.general_settings", general_settings- ), patch("litellm.proxy.proxy_server.premium_user", True), patch(- "litellm.proxy.proxy_server.master_key", "sk-master"- ), patch(- "litellm.proxy.proxy_server.prisma_client", None- ), patch(- "litellm.proxy.auth.user_api_key_auth.Oauth2Handler.check_oauth2_token",- new_callable=AsyncMock,- ) as mock_oauth2, patch(- "litellm.proxy.auth.user_api_key_auth.JWTAuthManager.auth_builder",- new_callable=AsyncMock,- return_value=mock_jwt_result,- ) as mock_jwt_auth:+ with (+ patch("litellm.proxy.proxy_server.general_settings", general_settings),+ patch("litellm.proxy.proxy_server.premium_user", True),+ patch("litellm.proxy.proxy_server.master_key", "sk-master"),+ patch("litellm.proxy.proxy_server.prisma_client", None),+ patch(+ "litellm.proxy.auth.user_api_key_auth.Oauth2Handler.check_oauth2_token",+ new_callable=AsyncMock,+ ) as mock_oauth2,+ patch(+ "litellm.proxy.auth.user_api_key_auth.JWTAuthManager.auth_builder",+ new_callable=AsyncMock,+ return_value=mock_jwt_result,+ ) as mock_jwt_auth,+ ):litellm.proxy.proxy_server.jwt_handler.update_environment(prisma_client=None,user_api_key_cache=DualCache(),@@ -1873,20 +1886,21 @@ class TestJWTOAuth2Coexistence:mock_request.headers = {"authorization": f"Bearer {jwt_token}"}mock_request.query_params = {}- with patch(- "litellm.proxy.proxy_server.general_settings", general_settings- ), patch("litellm.proxy.proxy_server.premium_user", True), patch(- "litellm.proxy.proxy_server.master_key", "sk-master"- ), patch(- "litellm.proxy.proxy_server.prisma_client", None- ), patch(- "litellm.proxy.auth.user_api_key_auth.Oauth2Handler.check_oauth2_token",- new_callable=AsyncMock,- return_value=mock_oauth2_response,- ) as mock_oauth2, patch(- "litellm.proxy.auth.user_api_key_auth.JWTAuthManager.auth_builder",- new_callable=AsyncMock,- ) as mock_jwt_auth:+ with (+ patch("litellm.proxy.proxy_server.general_settings", general_settings),+ patch("litellm.proxy.proxy_server.premium_user", True),+ patch("litellm.proxy.proxy_server.master_key", "sk-master"),+ patch("litellm.proxy.proxy_server.prisma_client", None),+ patch(+ "litellm.proxy.auth.user_api_key_auth.Oauth2Handler.check_oauth2_token",+ new_callable=AsyncMock,+ return_value=mock_oauth2_response,+ ) as mock_oauth2,+ patch(+ "litellm.proxy.auth.user_api_key_auth.JWTAuthManager.auth_builder",+ new_callable=AsyncMock,+ ) as mock_jwt_auth,+ ):litellm.proxy.proxy_server.jwt_handler.update_environment(prisma_client=None,user_api_key_cache=DualCache(),