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: allow non-admin access to /guardrails/submissions and fix register membership check
Ryan Crabbe79064a68e416
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.pyindex 8faf36df4c6..c19e4c9f39f 100644--- a/litellm/proxy/_types.py+++ b/litellm/proxy/_types.py@@ -665,6 +665,9 @@ class LiteLLMRoutes(enum.Enum):"/invitation/delete",# Team guardrail submission - requires team-scoped key; endpoint enforces team_id"/guardrails/register",+ # Team guardrail submissions - endpoint scopes results to caller's teams (non-admin)+ "/guardrails/submissions",+ "/guardrails/submissions/{guardrail_id}",] # routes that manage their own allowed/disallowed logic## Org Admin Routes ##diff --git a/litellm/proxy/guardrails/guardrail_endpoints.py b/litellm/proxy/guardrails/guardrail_endpoints.pyindex 4aa552a631d..422bdc13780 100644--- a/litellm/proxy/guardrails/guardrail_endpoints.py+++ b/litellm/proxy/guardrails/guardrail_endpoints.py@@ -615,16 +615,8 @@ async def register_guardrail(# Validate team membership for non-admin users when team differs from keyis_admin = user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMINif not is_admin and team_id != user_api_key_dict.team_id:- from litellm.proxy.auth.auth_checks import get_team_membership- from litellm.proxy.proxy_server import user_api_key_cache-- membership = await get_team_membership(- user_id=user_api_key_dict.user_id or "",- team_id=team_id,- prisma_client=prisma_client,- user_api_key_cache=user_api_key_cache,- )- if membership is None:+ user_team_ids = await _get_user_team_ids(user_api_key_dict)+ if team_id not in user_team_ids:raise HTTPException(status_code=403,detail=f"You are not a member of team {team_id!r}",diff --git a/tests/test_litellm/proxy/guardrails/test_guardrail_endpoints.py b/tests/test_litellm/proxy/guardrails/test_guardrail_endpoints.pyindex 244150f8554..defea08594f 100644--- a/tests/test_litellm/proxy/guardrails/test_guardrail_endpoints.py+++ b/tests/test_litellm/proxy/guardrails/test_guardrail_endpoints.py@@ -1220,6 +1220,59 @@ async def test_register_guardrail_requires_team_id(mocker):assert "team" in exc_info.value.detail.lower()+async def test_register_guardrail_non_admin_cross_team_allowed(mocker):+ """Non-admin may register for a team in their user.teams list even if the key's team_id differs."""+ mock_prisma = mocker.Mock()+ mock_prisma.db.litellm_guardrailstable.find_unique = AsyncMock(return_value=None)+ created = mocker.Mock(+ guardrail_id="g1",+ guardrail_name=MOCK_REGISTER_REQUEST.guardrail_name,+ status="pending_review",+ submitted_at=datetime.now(),+ )+ mock_prisma.db.litellm_guardrailstable.create = AsyncMock(return_value=created)+ mocker.patch("litellm.proxy.proxy_server.prisma_client", mock_prisma)+ mocker.patch(+ "litellm.proxy.guardrails.guardrail_endpoints._get_user_team_ids",+ AsyncMock(return_value=["team-alpha", "team-beta"]),+ )+ req = RegisterGuardrailRequest(+ guardrail_name=MOCK_REGISTER_REQUEST.guardrail_name,+ team_id="team-beta",+ litellm_params=MOCK_REGISTER_REQUEST.litellm_params,+ )+ user = UserAPIKeyAuth(+ user_id="u1", user_role=LitellmUserRoles.INTERNAL_USER, team_id="team-alpha"+ )++ result = await register_guardrail(req, user)++ assert result.guardrail_id == "g1"+++async def test_register_guardrail_non_admin_cross_team_forbidden(mocker):+ """Non-admin gets 403 when registering for a team they are not a member of."""+ mocker.patch("litellm.proxy.proxy_server.prisma_client", mocker.Mock())+ mocker.patch(+ "litellm.proxy.guardrails.guardrail_endpoints._get_user_team_ids",+ AsyncMock(return_value=["team-alpha"]),+ )+ req = RegisterGuardrailRequest(+ guardrail_name=MOCK_REGISTER_REQUEST.guardrail_name,+ team_id="team-other",+ litellm_params=MOCK_REGISTER_REQUEST.litellm_params,+ )+ user = UserAPIKeyAuth(+ user_id="u1", user_role=LitellmUserRoles.INTERNAL_USER, team_id="team-alpha"+ )++ with pytest.raises(HTTPException) as exc_info:+ await register_guardrail(req, user)+ assert exc_info.value.status_code == 403++@pytest.mark.asyncioasync def test_register_guardrail_duplicate_name(mocker):"""Register returns 400 when guardrail_name already exists."""diff --git a/ui/litellm-dashboard/src/components/guardrails.tsx b/ui/litellm-dashboard/src/components/guardrails.tsxindex a4383e1648f..fee9d02d3ae 100644--- a/ui/litellm-dashboard/src/components/guardrails.tsx+++ b/ui/litellm-dashboard/src/components/guardrails.tsx@@ -234,21 +234,21 @@ const GuardrailsPanel: React.FC<GuardrailsPanelProps> = ({ accessToken, userRole</>),},+ {+ key: "playground",+ label: "Test Playground",+ disabled: !accessToken,+ children: (+ <GuardrailTestPlayground+ guardrailsList={guardrailsList}+ isLoading={isLoading}+ accessToken={accessToken}+ onClose={() => {}}+ />+ ),+ },]: []),- {- key: "playground",- label: "Test Playground",- disabled: !accessToken,- children: (- <GuardrailTestPlayground- guardrailsList={guardrailsList}- isLoading={isLoading}- accessToken={accessToken}- onClose={() => {}}- />- ),- },{key: "submitted",label: "Submitted Guardrails",