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(proxy): guard agent activity owned-fallback against null user_id
user38fd1a99c4a9
diff --git a/litellm/proxy/agent_endpoints/endpoints.py b/litellm/proxy/agent_endpoints/endpoints.pyindex 978e1a73be1..32d9a7f2721 100644--- a/litellm/proxy/agent_endpoints/endpoints.py+++ b/litellm/proxy/agent_endpoints/endpoints.py@@ -992,11 +992,17 @@ async def get_agent_daily_activity(# and team carry no agent restrictions. For activity scoping that's# not "see everything" — fall back to the agents the caller# created so they cannot enumerate other tenants' agents.+ # Guard against `user_id is None`: a literal None in Prisma+ # `where={"created_by": None}` resolves to ``created_by IS NULL``+ # and would expose every ownerless agent's rows.if not permitted_agent_ids:- owned_records = await prisma_client.db.litellm_agentstable.find_many(- where={"created_by": user_api_key_dict.user_id}- )- permitted_agent_ids = [a.agent_id for a in owned_records]+ if user_api_key_dict.user_id is None:+ permitted_agent_ids = []+ else:+ owned_records = await prisma_client.db.litellm_agentstable.find_many(+ where={"created_by": user_api_key_dict.user_id}+ )+ permitted_agent_ids = [a.agent_id for a in owned_records]if agent_ids_list:agent_ids_list = [diff --git a/tests/test_litellm/proxy/management_endpoints/test_activity_tenant_scoping.py b/tests/test_litellm/proxy/management_endpoints/test_activity_tenant_scoping.pyindex ebc73f87562..0855c201945 100644--- a/tests/test_litellm/proxy/management_endpoints/test_activity_tenant_scoping.py+++ b/tests/test_litellm/proxy/management_endpoints/test_activity_tenant_scoping.py@@ -294,6 +294,54 @@ async def test_agent_activity_non_admin_intersects_explicit_agent_ids():assert captured["entity_id"] == ["agent-permitted"]+async def test_agent_activity_keyless_caller_does_not_query_created_by_null():+ """Guard against the `WHERE created_by IS NULL` pitfall: a non-admin+ caller without a user_id (e.g. a service-account key with no+ explicit agent allowlist) must NOT trigger a fallback DB query that+ would expose every ownerless agent."""+ from litellm.proxy.agent_endpoints import endpoints++ user = UserAPIKeyAuth(+ api_key="sk-svc",+ # NOTE: no user_id+ user_role=LitellmUserRoles.INTERNAL_USER.value,+ )++ prisma = MagicMock()+ prisma.db.litellm_agentstable.find_many = AsyncMock(return_value=[])++ fake_get_daily = AsyncMock()++ with (+ patch.object(endpoints, "prisma_client", prisma, create=True),+ patch("litellm.proxy.proxy_server.prisma_client", prisma),+ patch(+ "litellm.proxy.agent_endpoints.endpoints.check_feature_access_for_user",+ new=AsyncMock(return_value=None),+ ),+ patch(+ "litellm.proxy.agent_endpoints.auth.agent_permission_handler.AgentRequestHandler.get_allowed_agents",+ new=AsyncMock(return_value=[]),+ ),+ patch(+ "litellm.proxy.agent_endpoints.endpoints.get_daily_activity",+ new=fake_get_daily,+ ),+ ):+ result = await endpoints.get_agent_daily_activity(+ agent_ids=None,+ start_date="2026-01-01",+ end_date="2026-01-02",+ user_api_key_dict=user,+ )++ # Empty page; the owned-agents fallback must NOT have been queried.+ assert result.results == []+ prisma.db.litellm_agentstable.find_many.assert_not_called()+ fake_get_daily.assert_not_awaited()++@pytest.mark.asyncioasync def test_agent_activity_non_admin_no_access_returns_empty_page():"""Non-admin with no permitted agents and no owned agents must get an