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
Merge remote-tracking branch 'origin/litellm_internal_staging' into codex/resolve-team-callback-conflicts
usera03d24076b04
diff --cc litellm/proxy/management_endpoints/team_callback_endpoints.pyindex 934824dd89f,a6c0c7dcc0e..17d86410a0f--- a/litellm/proxy/management_endpoints/team_callback_endpoints.py+++ b/litellm/proxy/management_endpoints/team_callback_endpoints.py@@@ -10,10 -13,13 +13,14 @@@ from typing import Any, List, Optionafrom fastapi import APIRouter, Depends, Header, HTTPException, Request, status+ import litellmfrom litellm._logging import verbose_proxy_logger+ from litellm._uuid import uuidfrom litellm.proxy._types import (AddTeamCallback,+ LiteLLM_AuditLogs,+ LiteLLM_TeamTable,+ LitellmTableNames,ProxyErrorTypes,ProxyException,TeamCallbackMetadata,@@@ -207,16 -316,9 +327,17 @@@ async def disable_team_loggingdetail={"error": f"Team id = {team_id} does not exist."},)+ # IDOR guard: only proxy admins / org admins / team admins of THIS+ # team may disable its logging — otherwise any authenticated key+ # holder can silence audit logging for any team.+ await _verify_team_access(+ team_obj=LiteLLM_TeamTable(**_existing_team.model_dump()),+ user_api_key_dict=user_api_key_dict,+ )+# Update team metadata to disable loggingteam_metadata = _existing_team.metadata+ before_metadata = copy.deepcopy(team_metadata)team_callback_settings = team_metadata.get("callback_settings", {})team_callback_settings_obj = TeamCallbackMetadata(**team_callback_settings)diff --cc tests/test_litellm/proxy/management_endpoints/test_team_callback_endpoints.pyindex e26820ea9cd,6c4d1be7831..55afd4061d8--- a/tests/test_litellm/proxy/management_endpoints/test_team_callback_endpoints.py+++ b/tests/test_litellm/proxy/management_endpoints/test_team_callback_endpoints.py@@@ -1,31 -1,25 +1,22 @@@"""- Regression tests for the IDOR fix on team callback endpoints- (GHSA-xxv2-fprq-9x93).-- The three endpoints below previously authenticated the caller but never- checked whether the caller could manage the target team — any- authenticated key holder could write callback credentials to any team,- disable any team's logging, or read back another team's stored- third-party API credentials (Langfuse / Langsmith / GCS).-Audit-log emission for the team-callback admin endpoints.--- The fix routes each handler through ``_verify_team_access``, which- enforces the proxy-admin / org-admin / team-admin hierarchy used by- sibling endpoints in ``team_endpoints.py``.-The endpoints in ``team_callback_endpoints.py`` mutate a team's logging-callbacks (``add_team_callbacks``) or zero them out entirely-(``disable_team_logging``). Both are admin-only mutations, and the-disable variant is itself a logging-control action, so when the operator-has Enterprise audit logging enabled (``litellm.store_audit_logs = True``)-each call must emit a row that captures who did it and what the metadata-looked like before/after.++Regression tests for team callback endpoint access control and audit logging.++++The team callback endpoints mutate or expose callback credentials. They must++enforce target-team management access and, when audit logging is enabled, emit++redacted audit rows for callback mutations."""- import os- import sys+ import json-from unittest.mock import AsyncMock, MagicMock, patch+from unittest.mock import AsyncMock, MagicMock, Mock, patchimport pytest-from fastapi import Request+from fastapi import HTTPException, Request- sys.path.insert(0, os.path.abspath("../../../.."))-+ import litellmfrom litellm.proxy._types import (AddTeamCallback,+ LitellmTableNames,+ LitellmUserRoles,- Member,UserAPIKeyAuth,)from litellm.proxy.management_endpoints.team_callback_endpoints import (@@@ -35,168 -28,267 +26,397 @@@)- def _other_team_existing_row():- """Return a mock team row owned by someone other than the test caller."""++def _team_row(++ *,++ team_id: str = "team-victim",++ metadata: dict | None = None,++ admin_user_id: str = "victim_admin",++ organization_id: str = "org-victim",++) -> MagicMock:+ row = MagicMock()++ row.team_id = team_id++ row.metadata = metadata or {}+ row.model_dump.return_value = {- "team_id": "team-victim",++ "team_id": team_id,+ "team_alias": "victim-team",+ "members_with_roles": [- {"role": "admin", "user_id": "victim_admin"},++ {"role": "admin", "user_id": admin_user_id},+ ],- "organization_id": "org-victim",++ "organization_id": organization_id,++ "metadata": row.metadata,+ }- row.metadata = {}+ return row++++def _patch_prisma(existing_team: MagicMock):++ mock_prisma = MagicMock()++ mock_prisma.get_data = AsyncMock(return_value=existing_team)++++ updated_row = MagicMock()++ updated_row.team_id = existing_team.team_id++ mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=updated_row)++ return mock_prisma+++++ def _admin_auth() -> UserAPIKeyAuth:+ return UserAPIKeyAuth(+ api_key="hashed",+ user_id="admin-user",- user_role="proxy_admin",++ user_role=LitellmUserRoles.PROXY_ADMIN,+ )++-def _existing_team_row(metadata: dict) -> MagicMock:- row = MagicMock()- row.team_id = "team-1"- row.metadata = metadata- return row+def unauthorized_caller():+ return UserAPIKeyAuth(+ user_role=LitellmUserRoles.INTERNAL_USER,+ user_id="random_authenticated_user",+ api_key="sk-random",+ )-def _patch_prisma(existing_metadata: dict):- """Build a context-manager that patches the proxy's ``prisma_client``- to return ``existing_metadata`` from ``get_data`` and a stub team row- from ``litellm_teamtable.update``."""- mock_prisma = MagicMock()- mock_prisma.get_data = AsyncMock(return_value=_existing_team_row(existing_metadata))+def patched_prisma():- """- Patch the proxy_server.prisma_client used inside each handler with a- mock that returns a victim-team row from get_data().- """+ with (- patch(- "litellm.proxy.proxy_server.prisma_client",- ) as mock_client,++ patch("litellm.proxy.proxy_server.prisma_client") as mock_client,+ patch(+ "litellm.proxy.management_endpoints.team_endpoints._is_user_org_admin_for_team",+ new_callable=AsyncMock,+ return_value=False,+ ),+ ):- mock_client.get_data = AsyncMock(return_value=_other_team_existing_row())++ mock_client.get_data = AsyncMock(return_value=_team_row())+ mock_client.db.litellm_teamtable.update = AsyncMock()+ yield mock_client- updated_row = MagicMock()- updated_row.team_id = "team-1"- mock_prisma.db.litellm_teamtable.update = AsyncMock(return_value=updated_row)- return mock_prisma++async def test_add_team_callbacks_rejects_unauthorized_caller(+ patched_prisma, unauthorized_caller+):+ data = AddTeamCallback(+ callback_name="langfuse",+ callback_type="success",+ callback_vars={+ "langfuse_public_key": "pk-attacker",+ "langfuse_secret_key": "sk-attacker",+ },+ )+ with pytest.raises(HTTPException) as exc:+ await add_team_callbacks(+ data=data,+ http_request=Mock(spec=Request),+ team_id="team-victim",+ user_api_key_dict=unauthorized_caller,+ )+ assert exc.value.status_code == 403- # The unauthorized caller must NOT have written to the victim team.+ patched_prisma.db.litellm_teamtable.update.assert_not_called()+++async def test_disable_team_logging_rejects_unauthorized_caller(+ patched_prisma, unauthorized_caller+):- # The HTTPException from the access guard now propagates directly- # — the catch-all that previously re-wrapped (and logged at error- # level) was narrowed so legitimate 4xx don't pollute alerting.+ with pytest.raises(HTTPException) as exc:+ await disable_team_logging(+ http_request=Mock(spec=Request),+ team_id="team-victim",+ user_api_key_dict=unauthorized_caller,+ )+ assert exc.value.status_code == 403+ patched_prisma.db.litellm_teamtable.update.assert_not_called()+++async def test_get_team_callbacks_rejects_unauthorized_caller(+ patched_prisma, unauthorized_caller+):+ with pytest.raises(HTTPException) as exc:+ await get_team_callbacks(+ http_request=Mock(spec=Request),+ team_id="team-victim",+ user_api_key_dict=unauthorized_caller,+ )+ assert exc.value.status_code == 403+++async def test_proxy_admin_can_add_team_callbacks(patched_prisma):- """- A proxy admin should pass the access guard and reach the DB write.- Sanity check that the guard didn't over-rotate.- """- proxy_admin = UserAPIKeyAuth(- user_role=LitellmUserRoles.PROXY_ADMIN,- user_id="admin",- api_key="sk-admin",- )+ data = AddTeamCallback(+ callback_name="langfuse",+ callback_type="success",+ callback_vars={+ "langfuse_public_key": "pk-admin",+ "langfuse_secret_key": "sk-admin",+ },+ )+ await add_team_callbacks(+ data=data,+ http_request=Mock(spec=Request),+ team_id="team-victim",- user_api_key_dict=proxy_admin,++ user_api_key_dict=_admin_auth(),+ )+ patched_prisma.db.litellm_teamtable.update.assert_awaited_once()+++async def test_team_admin_of_target_team_can_add_callbacks(patched_prisma):- """- A team admin OF THE TARGET team should pass the access guard.- """- # Override the victim row so the caller IS the team admin.- row = MagicMock()- row.model_dump.return_value = {- "team_id": "team-victim",- "team_alias": "victim-team",- "members_with_roles": [- {"role": "admin", "user_id": "team_admin_user"},- ],- "organization_id": "org-victim",- }- row.metadata = {}- patched_prisma.get_data = AsyncMock(return_value=row)++ patched_prisma.get_data = AsyncMock(++ return_value=_team_row(admin_user_id="team_admin_user")++ )++ team_admin = UserAPIKeyAuth(+ user_role=LitellmUserRoles.INTERNAL_USER,+ user_id="team_admin_user",+ api_key="sk-team-admin",+ )+ data = AddTeamCallback(+ callback_name="langfuse",+ callback_type="success",+ callback_vars={+ "langfuse_public_key": "pk-team",+ "langfuse_secret_key": "sk-team",+ },+ )+ await add_team_callbacks(+ data=data,+ http_request=Mock(spec=Request),+ team_id="team-victim",+ user_api_key_dict=team_admin,+ )+ patched_prisma.db.litellm_teamtable.update.assert_awaited_once()+++ @pytest.mark.asyncio+ async def test_disable_team_logging_emits_audit_log_when_enabled(monkeypatch):+ monkeypatch.setattr(litellm, "store_audit_logs", True)+ mock_prisma = _patch_prisma(- {- "callback_settings": {- "success_callback": ["langfuse"],- "failure_callback": [],- }- }++ _team_row(++ team_id="team-1",++ metadata={++ "callback_settings": {++ "success_callback": ["langfuse"],++ "failure_callback": [],++ }++ },++ )+ )++ audit_calls = []++ async def capture(request_data):+ audit_calls.append(request_data)++ with (+ patch("litellm.proxy.proxy_server.prisma_client", mock_prisma),+ patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"),+ patch(+ "litellm.proxy.management_helpers.audit_logs.create_audit_log_for_update",+ new=capture,+ ),+ ):+ await disable_team_logging(+ http_request=MagicMock(spec=Request),+ team_id="team-1",+ user_api_key_dict=_admin_auth(),+ litellm_changed_by=None,+ )- # asyncio.create_task fires the coroutine eagerly; await one tick to let- # the audit-log emit run before the test exits.+ import asyncio++ for _ in range(3):+ await asyncio.sleep(0)++ assert len(audit_calls) == 1+ log = audit_calls[0]+ assert log.table_name == LitellmTableNames.TEAM_TABLE_NAME+ assert log.object_id == "team-1"+ assert log.action == "updated"+ assert log.changed_by == "admin-user"++ before = json.loads(log.before_value)+ after = json.loads(log.updated_values)- # Before: the team's pre-existing success_callback survives in the snapshot.+ assert before["metadata"]["callback_settings"]["success_callback"] == ["langfuse"]- # After: callbacks zeroed out by the endpoint.+ assert after["metadata"]["callback_settings"]["success_callback"] == []+ assert after["metadata"]["callback_settings"]["failure_callback"] == []+++ @pytest.mark.asyncio+ async def test_disable_team_logging_no_audit_when_disabled(monkeypatch):+ monkeypatch.setattr(litellm, "store_audit_logs", False)+ mock_prisma = _patch_prisma(- {- "callback_settings": {- "success_callback": ["langfuse"],- "failure_callback": [],- }- }++ _team_row(++ team_id="team-1",++ metadata={++ "callback_settings": {++ "success_callback": ["langfuse"],++ "failure_callback": [],++ }++ },++ )+ )++ audit_calls = []++ async def capture(request_data):+ audit_calls.append(request_data)++ with (+ patch("litellm.proxy.proxy_server.prisma_client", mock_prisma),+ patch(+ "litellm.proxy.management_helpers.audit_logs.create_audit_log_for_update",+ new=capture,+ ),+ ):+ await disable_team_logging(+ http_request=MagicMock(spec=Request),+ team_id="team-1",+ user_api_key_dict=_admin_auth(),+ litellm_changed_by=None,+ )++ assert audit_calls == []+++ @pytest.mark.asyncio+ async def test_add_team_callbacks_emits_audit_log_when_enabled(monkeypatch):+ monkeypatch.setattr(litellm, "store_audit_logs", True)- mock_prisma = _patch_prisma({"logging": []})++ mock_prisma = _patch_prisma(_team_row(team_id="team-1", metadata={"logging": []}))++ audit_calls = []++ async def capture(request_data):+ audit_calls.append(request_data)++ with (+ patch("litellm.proxy.proxy_server.prisma_client", mock_prisma),+ patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"),+ patch(+ "litellm.proxy.management_helpers.audit_logs.create_audit_log_for_update",+ new=capture,+ ),+ ):+ await add_team_callbacks(+ data=AddTeamCallback(+ callback_name="langfuse",+ callback_type="success",+ callback_vars={+ "langfuse_public_key": "pk",+ "langfuse_secret_key": "sk",+ },+ ),+ http_request=MagicMock(spec=Request),+ team_id="team-1",+ user_api_key_dict=_admin_auth(),+ litellm_changed_by="ops-on-call",+ )+ import asyncio++ for _ in range(3):+ await asyncio.sleep(0)++ assert len(audit_calls) == 1+ log = audit_calls[0]+ assert log.table_name == LitellmTableNames.TEAM_TABLE_NAME+ assert log.object_id == "team-1"+ assert log.action == "updated"- # ``litellm_changed_by`` header takes precedence over the auth user_id.+ assert log.changed_by == "ops-on-call"++ before = json.loads(log.before_value)+ after = json.loads(log.updated_values)+ assert before["metadata"]["logging"] == []+ assert len(after["metadata"]["logging"]) == 1+ assert after["metadata"]["logging"][0]["callback_name"] == "langfuse"+- # Callback secrets MUST NOT leak into the audit log payload.+ callback_vars = after["metadata"]["logging"][0]["callback_vars"]+ assert callback_vars["langfuse_public_key"] != "pk"+ assert callback_vars["langfuse_secret_key"] != "sk"- # Key names are preserved so the auditor can see which fields changed.+ assert "langfuse_public_key" in callback_vars+ assert "langfuse_secret_key" in callback_vars- # And no plaintext secret should appear anywhere in the serialized row.- assert "sk" not in log.updated_values.replace("sk-", "") # crude leak check++ assert "sk" not in log.updated_values.replace("sk-", "")+ assert "pk" not in (log.updated_values.replace("pk-", "").replace("public_key", ""))+++ @pytest.mark.asyncio+ async def test_disable_team_logging_redacts_existing_callback_secrets(monkeypatch):+ monkeypatch.setattr(litellm, "store_audit_logs", True)- # Existing team has populated callback_vars containing secrets — redaction- # must apply to the BEFORE snapshot too.+ mock_prisma = _patch_prisma(- {- "callback_settings": {- "success_callback": ["langfuse"],- "failure_callback": [],- "callback_vars": {- "langfuse_public_key": "pk-real",- "langfuse_secret_key": "sk-real-secret",- },- }- }++ _team_row(++ team_id="team-1",++ metadata={++ "callback_settings": {++ "success_callback": ["langfuse"],++ "failure_callback": [],++ "callback_vars": {++ "langfuse_public_key": "pk-real",++ "langfuse_secret_key": "sk-real-secret",++ },++ }++ },++ )+ )++ audit_calls = []++ async def capture(request_data):+ audit_calls.append(request_data)++ with (+ patch("litellm.proxy.proxy_server.prisma_client", mock_prisma),+ patch("litellm.proxy.proxy_server.litellm_proxy_admin_name", "admin"),+ patch(+ "litellm.proxy.management_helpers.audit_logs.create_audit_log_for_update",+ new=capture,+ ),+ ):+ await disable_team_logging(+ http_request=MagicMock(spec=Request),+ team_id="team-1",+ user_api_key_dict=_admin_auth(),+ litellm_changed_by=None,+ )+ import asyncio++ for _ in range(3):+ await asyncio.sleep(0)++ assert len(audit_calls) == 1+ log = audit_calls[0]- # The pre-existing secret_key value must NOT appear in the serialized- # before_value or updated_values.+ assert "sk-real-secret" not in log.before_value+ assert "sk-real-secret" not in log.updated_values+ assert "pk-real" not in log.before_value+ assert "pk-real" not in log.updated_values+++ @pytest.mark.asyncio+ async def test_add_team_callbacks_no_audit_when_disabled(monkeypatch):+ monkeypatch.setattr(litellm, "store_audit_logs", False)- mock_prisma = _patch_prisma({"logging": []})++ mock_prisma = _patch_prisma(_team_row(team_id="team-1", metadata={"logging": []}))++ audit_calls = []++ async def capture(request_data):+ audit_calls.append(request_data)++ with (+ patch("litellm.proxy.proxy_server.prisma_client", mock_prisma),+ patch(+ "litellm.proxy.management_helpers.audit_logs.create_audit_log_for_update",+ new=capture,+ ),+ ):+ await add_team_callbacks(+ data=AddTeamCallback(+ callback_name="langfuse",+ callback_type="success",+ callback_vars={+ "langfuse_public_key": "pk",+ "langfuse_secret_key": "sk",+ },+ ),+ http_request=MagicMock(spec=Request),+ team_id="team-1",+ user_api_key_dict=_admin_auth(),+ litellm_changed_by=None,+ )++ assert audit_calls == []