mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(proxy): deny agent requests when any echoed caller id cannot be resolved
A known team next to an unknown user is still an unresolvable caller: both echoed ids must load before the delegated cap applies. MCP gateway tests now pass a real UserAPIKeyAuth through extract_mcp_auth_context so the manager level caller ceiling sees a typed auth object Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
parent
e7f1f0197b
commit
f6821b83f1
8 changed files with 132 additions and 69 deletions
|
|
@ -96,17 +96,15 @@ async def agent_caller_resolves(
|
|||
load_team: CallerTeamLoader = load_agent_caller_team,
|
||||
load_user: CallerUserLoader = load_agent_caller_user,
|
||||
) -> bool:
|
||||
"""Whether the echoed ids name a team (when one was echoed, else a user) the proxy can load. The
|
||||
grant resolvers answer "no grants" for a missing row just as for an unrestricted one, so callers
|
||||
ask this first and deny outright when the invoking identity cannot be resolved."""
|
||||
"""Whether every echoed id names a row the proxy can load. The grant resolvers answer "no
|
||||
grants" for a missing row just as for an unrestricted one, so callers ask this first and deny
|
||||
outright when the invoking identity cannot be resolved."""
|
||||
caller: Final = user_api_key_auth.agent_caller
|
||||
if caller is None:
|
||||
return True
|
||||
try:
|
||||
if caller.team_id is not None:
|
||||
await load_team(user_api_key_auth)
|
||||
else:
|
||||
await load_user(user_api_key_auth)
|
||||
await load_team(user_api_key_auth)
|
||||
await load_user(user_api_key_auth)
|
||||
except Exception as error: # noqa: BLE001 # any failure to resolve the caller must deny, never widen
|
||||
verbose_proxy_logger.warning("agent caller %s could not be resolved, denying: %s", caller, error)
|
||||
return False
|
||||
|
|
|
|||
|
|
@ -4387,6 +4387,9 @@ async def _check_agent_caller_model_access(
|
|||
caller_team: Final = await _load_agent_caller_row_or_deny(
|
||||
load_team, valid_token, model, ProxyErrorTypes.team_model_access_denied
|
||||
)
|
||||
caller_user: Final = await _load_agent_caller_row_or_deny(
|
||||
load_user, valid_token, model, ProxyErrorTypes.user_model_access_denied
|
||||
)
|
||||
if caller_team is not None:
|
||||
await can_team_access_model(
|
||||
model=model,
|
||||
|
|
@ -4404,9 +4407,6 @@ async def _check_agent_caller_model_access(
|
|||
proxy_logging_obj=proxy_logging_obj,
|
||||
)
|
||||
return
|
||||
caller_user: Final = await _load_agent_caller_row_or_deny(
|
||||
load_user, valid_token, model, ProxyErrorTypes.user_model_access_denied
|
||||
)
|
||||
if caller_user is None:
|
||||
return
|
||||
await can_user_call_model(model=model, llm_router=llm_router, user_object=caller_user)
|
||||
|
|
|
|||
|
|
@ -2332,7 +2332,7 @@ async def test_mcp_routing_chunked_initialize_to_stateful():
|
|||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.extract_mcp_auth_context",
|
||||
new_callable=AsyncMock,
|
||||
return_value=(MagicMock(), None, ["progress_test"], None, None, None),
|
||||
return_value=(UserAPIKeyAuth(), None, ["progress_test"], None, None, None),
|
||||
),
|
||||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.set_auth_context",
|
||||
|
|
@ -2444,7 +2444,7 @@ async def test_mcp_routing_caps_body_peek_for_oversized_chunked_body():
|
|||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.extract_mcp_auth_context",
|
||||
new_callable=AsyncMock,
|
||||
return_value=(MagicMock(), None, ["progress_test"], None, None, None),
|
||||
return_value=(UserAPIKeyAuth(), None, ["progress_test"], None, None, None),
|
||||
),
|
||||
patch("litellm.proxy._experimental.mcp_server.server.set_auth_context"),
|
||||
patch(
|
||||
|
|
@ -2655,7 +2655,7 @@ async def test_mcp_routing_initialize_rejected_when_owner_at_session_cap():
|
|||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.extract_mcp_auth_context",
|
||||
new_callable=AsyncMock,
|
||||
return_value=(MagicMock(), None, ["progress_test"], None, None, None),
|
||||
return_value=(UserAPIKeyAuth(), None, ["progress_test"], None, None, None),
|
||||
),
|
||||
patch("litellm.proxy._experimental.mcp_server.server.set_auth_context"),
|
||||
patch(
|
||||
|
|
|
|||
|
|
@ -11937,9 +11937,9 @@ class TestAgentCallerServerCeiling:
|
|||
caller: Final = user_api_key_auth.agent_caller
|
||||
if caller is None:
|
||||
return True
|
||||
if caller.team_id is not None:
|
||||
return caller.team_id in team_ids
|
||||
return caller.user_id in user_ids
|
||||
team_known: Final = caller.team_id is None or caller.team_id in team_ids
|
||||
user_known: Final = caller.user_id is None or caller.user_id in user_ids
|
||||
return team_known and user_known
|
||||
|
||||
return AsyncMock(side_effect=resolves)
|
||||
|
||||
|
|
@ -11974,7 +11974,9 @@ class TestAgentCallerServerCeiling:
|
|||
async def test_agent_key_acting_for_a_user_is_capped_at_the_invoking_teams_servers(self):
|
||||
agent_key: Final = self._agent_key_acting_for(user_id="alice", team_id="callers")
|
||||
with self._resolvers(
|
||||
{"agent": ("server_1", "server_2"), "callers": ("server_2", "server_3")}, known_teams=frozenset({"callers"})
|
||||
{"agent": ("server_1", "server_2"), "callers": ("server_2", "server_3")},
|
||||
known_teams=frozenset({"callers"}),
|
||||
known_users=frozenset({"alice"}),
|
||||
) as manager:
|
||||
assert set(await manager.get_allowed_mcp_servers(agent_key)) == {"server_2", "public"}
|
||||
|
||||
|
|
@ -11993,7 +11995,11 @@ class TestAgentCallerServerCeiling:
|
|||
by the caller resolving to no public server) stays out of the agent's reach on their behalf."""
|
||||
agent_key: Final = self._agent_key_acting_for(user_id="alice", team_id="callers")
|
||||
with (
|
||||
self._resolvers({"agent": ("server_1",), "callers": ("server_1",)}, known_teams=frozenset({"callers"})),
|
||||
self._resolvers(
|
||||
{"agent": ("server_1",), "callers": ("server_1",)},
|
||||
known_teams=frozenset({"callers"}),
|
||||
known_users=frozenset({"alice"}),
|
||||
),
|
||||
patch.object(
|
||||
MCPServerManager,
|
||||
"operator_open_server_ids",
|
||||
|
|
@ -12007,15 +12013,17 @@ class TestAgentCallerServerCeiling:
|
|||
async def test_agent_key_acting_for_a_team_with_no_grants_reaches_only_what_that_team_can(self):
|
||||
agent_key: Final = self._agent_key_acting_for(user_id="alice", team_id="callers")
|
||||
with self._resolvers(
|
||||
{"agent": ("server_1", "server_2"), "callers": ()}, known_teams=frozenset({"callers"})
|
||||
{"agent": ("server_1", "server_2"), "callers": ()},
|
||||
known_teams=frozenset({"callers"}),
|
||||
known_users=frozenset({"alice"}),
|
||||
) as manager:
|
||||
assert await manager.get_allowed_mcp_servers(agent_key) == ["public"]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("user_id", "team_id"),
|
||||
[("alice", "ghost-team"), ("ghost", None)],
|
||||
ids=["unknown_team", "unknown_teamless_user"],
|
||||
[("alice", "ghost-team"), ("ghost", None), ("ghost", "callers")],
|
||||
ids=["unknown_team", "unknown_teamless_user", "unknown_user_on_known_team"],
|
||||
)
|
||||
async def test_agent_key_acting_for_an_unresolvable_caller_reaches_nothing(self, user_id: str, team_id: str | None):
|
||||
"""An echoed identity the proxy cannot load is an empty ceiling: not even public servers."""
|
||||
|
|
@ -12041,7 +12049,7 @@ class TestAgentCallerServerCeiling:
|
|||
return MCPServerAccess(server_ids=("server_1",))
|
||||
|
||||
with (
|
||||
self._resolvers({}, known_teams=frozenset({"callers"})),
|
||||
self._resolvers({}, known_teams=frozenset({"callers"}), known_users=frozenset({"alice"})),
|
||||
patch.object(MCPRequestHandler, "get_mcp_server_access", AsyncMock(side_effect=faulting_for_agent)),
|
||||
patch.object(
|
||||
MCPServerManager,
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ they may send a stale `mcp-session-id` header. This test verifies that:
|
|||
|
||||
import asyncio
|
||||
from unittest.mock import AsyncMock, MagicMock, patch
|
||||
from litellm.proxy._types import UserAPIKeyAuth
|
||||
from litellm.types.mcp import MCPAuth
|
||||
import pytest
|
||||
|
||||
|
|
@ -260,7 +261,7 @@ async def test_stale_mcp_session_id_is_stripped():
|
|||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.extract_mcp_auth_context",
|
||||
new_callable=AsyncMock,
|
||||
return_value=(MagicMock(), None, None, None, None, None),
|
||||
return_value=(UserAPIKeyAuth(), None, None, None, None, None),
|
||||
),
|
||||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.set_auth_context",
|
||||
|
|
@ -337,7 +338,7 @@ async def test_delete_stale_mcp_session_returns_success():
|
|||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.extract_mcp_auth_context",
|
||||
new_callable=AsyncMock,
|
||||
return_value=(MagicMock(), None, None, None, None, None),
|
||||
return_value=(UserAPIKeyAuth(), None, None, None, None, None),
|
||||
),
|
||||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.set_auth_context",
|
||||
|
|
@ -386,7 +387,7 @@ async def test_failed_delete_preserves_stateful_session_tracking():
|
|||
pytest.skip("MCP server not available")
|
||||
|
||||
session_id = "delete-failure-session"
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.api_key = "sk-test"
|
||||
user_auth.user_id = "test-user"
|
||||
auth_context = MagicMock()
|
||||
|
|
@ -491,7 +492,7 @@ async def test_valid_mcp_session_id_is_preserved():
|
|||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.extract_mcp_auth_context",
|
||||
new_callable=AsyncMock,
|
||||
return_value=(MagicMock(), None, None, None, None, None),
|
||||
return_value=(UserAPIKeyAuth(), None, None, None, None, None),
|
||||
),
|
||||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.set_auth_context",
|
||||
|
|
@ -554,7 +555,7 @@ async def test_no_mcp_session_id_header_works_normally():
|
|||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.extract_mcp_auth_context",
|
||||
new_callable=AsyncMock,
|
||||
return_value=(MagicMock(), None, None, None, None, None),
|
||||
return_value=(UserAPIKeyAuth(), None, None, None, None, None),
|
||||
),
|
||||
patch(
|
||||
"litellm.proxy._experimental.mcp_server.server.set_auth_context",
|
||||
|
|
@ -613,7 +614,7 @@ async def test_per_user_oauth_missing_stored_token_returns_preemptive_401():
|
|||
}
|
||||
receive = AsyncMock()
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = "test-user-id"
|
||||
oauth_server = MagicMock()
|
||||
oauth_server.auth_type = MCPAuth.oauth2
|
||||
|
|
@ -700,7 +701,7 @@ async def test_admitted_subject_missing_stored_token_challenged_with_resource_me
|
|||
}
|
||||
receive = AsyncMock()
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = "sso-user-42"
|
||||
user_auth.mcp_admitted_user_subject = True
|
||||
oauth_server = MagicMock()
|
||||
|
|
@ -806,7 +807,7 @@ async def test_client_credentials_server_is_not_preemptively_challenged(m2m_fiel
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = "test-user-id"
|
||||
m2m_server = MCPServer(
|
||||
server_id="m2m-server-id",
|
||||
|
|
@ -892,7 +893,7 @@ async def test_handle_streamable_http_mcp_delegated_server_surfaces_upstream_cha
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = None
|
||||
delegated_server = MagicMock()
|
||||
delegated_server.auth_type = MCPAuth.oauth2
|
||||
|
|
@ -996,7 +997,7 @@ async def test_per_user_oauth_with_stored_token_skips_preemptive_401():
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = "test-user-id"
|
||||
oauth_server = MagicMock()
|
||||
oauth_server.auth_type = MCPAuth.oauth2
|
||||
|
|
@ -1092,7 +1093,7 @@ async def test_handle_streamable_http_mcp_delegated_server_without_token_returns
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = None
|
||||
delegated_server = MagicMock()
|
||||
delegated_server.auth_type = MCPAuth.oauth2
|
||||
|
|
@ -1192,7 +1193,7 @@ async def test_handle_streamable_http_mcp_token_exchange_without_subject_returns
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = None
|
||||
obo_server = MagicMock()
|
||||
obo_server.auth_type = MCPAuth.oauth2_token_exchange
|
||||
|
|
@ -1301,7 +1302,7 @@ async def test_handle_streamable_http_mcp_oauth_delegate_without_token_returns_g
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = "u1"
|
||||
od_server = _build_passthrough_mode_server("od_server", MCPAuth.oauth_delegate)
|
||||
|
||||
|
|
@ -1366,7 +1367,7 @@ async def test_handle_streamable_http_mcp_oauth_delegate_with_forwarded_token_sk
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = "u1"
|
||||
od_server = _build_passthrough_mode_server("od_server", MCPAuth.oauth_delegate)
|
||||
|
||||
|
|
@ -1431,7 +1432,7 @@ async def _run_passthrough_connect(
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = "u1"
|
||||
server = _build_passthrough_mode_server(server_names[0], auth_type)
|
||||
|
||||
|
|
@ -1554,7 +1555,7 @@ async def test_handle_streamable_http_mcp_true_passthrough_without_token_surface
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = None
|
||||
tp_server = _build_passthrough_mode_server("tp_server", MCPAuth.true_passthrough)
|
||||
|
||||
|
|
@ -1620,7 +1621,7 @@ async def test_handle_streamable_http_mcp_true_passthrough_dcr_bridge_challenges
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = None
|
||||
bridge_server = _build_passthrough_mode_server("tp_bridge_server", MCPAuth.true_passthrough).model_copy(
|
||||
update={"dcr_bridge": True}
|
||||
|
|
@ -1691,7 +1692,7 @@ async def test_handle_streamable_http_mcp_true_passthrough_with_token_skips_prob
|
|||
}
|
||||
)
|
||||
send = AsyncMock()
|
||||
user_auth = MagicMock()
|
||||
user_auth = UserAPIKeyAuth()
|
||||
user_auth.user_id = None
|
||||
tp_server = _build_passthrough_mode_server("tp_server", MCPAuth.true_passthrough)
|
||||
|
||||
|
|
|
|||
|
|
@ -62,33 +62,51 @@ def test_agent_caller_cannot_be_set_from_a_request_payload() -> None:
|
|||
|
||||
|
||||
async def _team_row(user_api_key_auth: UserAPIKeyAuth) -> LiteLLM_TeamTable | None:
|
||||
"""Behaves like the production loader: no row when no team was echoed, the row for ``callers``,
|
||||
an error for any other id."""
|
||||
caller: Final = user_api_key_auth.agent_caller
|
||||
if caller is None or caller.team_id is None:
|
||||
return None
|
||||
if caller.team_id != "callers":
|
||||
raise ValueError(f"Team doesn't exist in db. Team={caller.team_id}")
|
||||
return LiteLLM_TeamTable(team_id="callers")
|
||||
|
||||
|
||||
async def _user_row(user_api_key_auth: UserAPIKeyAuth) -> LiteLLM_UserTable | None:
|
||||
caller: Final = user_api_key_auth.agent_caller
|
||||
if caller is None or caller.user_id is None:
|
||||
return None
|
||||
if caller.user_id != "alice":
|
||||
raise ValueError(f"User doesn't exist in db. User={caller.user_id}")
|
||||
return LiteLLM_UserTable(user_id="alice", max_budget=None, user_email=None)
|
||||
|
||||
|
||||
async def _no_row(user_api_key_auth: UserAPIKeyAuth) -> None:
|
||||
raise ValueError("doesn't exist in db")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize(
|
||||
("caller", "load_team", "load_user", "expected"),
|
||||
("caller", "expected"),
|
||||
[
|
||||
(None, _no_row, _no_row, True),
|
||||
(AgentCaller(user_id="alice", team_id="callers"), _team_row, _no_row, True),
|
||||
(AgentCaller(user_id="alice", team_id="ghost-team"), _no_row, _user_row, False),
|
||||
(AgentCaller(user_id="alice", team_id=None), _no_row, _user_row, True),
|
||||
(AgentCaller(user_id="ghost", team_id=None), _team_row, _no_row, False),
|
||||
(None, True),
|
||||
(AgentCaller(user_id="alice", team_id="callers"), True),
|
||||
(AgentCaller(user_id=None, team_id="callers"), True),
|
||||
(AgentCaller(user_id="alice", team_id=None), True),
|
||||
(AgentCaller(user_id="alice", team_id="ghost-team"), False),
|
||||
(AgentCaller(user_id="ghost", team_id="callers"), False),
|
||||
(AgentCaller(user_id="ghost", team_id=None), False),
|
||||
],
|
||||
ids=[
|
||||
"no_caller",
|
||||
"known_team_and_user",
|
||||
"known_team_alone",
|
||||
"known_user_alone",
|
||||
"unknown_team_despite_known_user",
|
||||
"unknown_user_despite_known_team",
|
||||
"unknown_user_alone",
|
||||
],
|
||||
ids=["no_caller", "known_team", "unknown_team_despite_known_user", "known_user", "unknown_user"],
|
||||
)
|
||||
async def test_caller_resolves_only_when_the_echoed_team_or_else_user_names_a_row(
|
||||
caller: AgentCaller | None, load_team, load_user, expected: bool
|
||||
async def test_caller_resolves_only_when_every_echoed_id_names_a_row(
|
||||
caller: AgentCaller | None, expected: bool
|
||||
) -> None:
|
||||
agent_key: Final = UserAPIKeyAuth(agent_id="agent-1")
|
||||
agent_key.agent_caller = caller
|
||||
|
||||
assert await agent_caller_resolves(agent_key, load_team=load_team, load_user=load_user) is expected
|
||||
assert await agent_caller_resolves(agent_key, load_team=_team_row, load_user=_user_row) is expected
|
||||
|
|
|
|||
|
|
@ -10,7 +10,13 @@ from unittest.mock import AsyncMock, patch
|
|||
import pytest
|
||||
|
||||
from litellm.constants import UI_SESSION_TOKEN_TEAM_ID
|
||||
from litellm.proxy._types import LiteLLM_ObjectPermissionTable, LiteLLM_TeamTable, LitellmUserRoles, UserAPIKeyAuth
|
||||
from litellm.proxy._types import (
|
||||
LiteLLM_ObjectPermissionTable,
|
||||
LiteLLM_TeamTable,
|
||||
LiteLLM_UserTable,
|
||||
LitellmUserRoles,
|
||||
UserAPIKeyAuth,
|
||||
)
|
||||
from litellm.proxy.agent_endpoints.agent_registry import AgentRegistry
|
||||
from litellm.proxy.agent_endpoints.auth.agent_access_groups import AgentAccessGroupCeiling, CeilingResolver
|
||||
from litellm.proxy.agent_endpoints.auth.agent_caller import CallerResolver, agent_caller_resolves
|
||||
|
|
@ -206,11 +212,11 @@ class TestAgentRequestHandler:
|
|||
return AsyncMock(side_effect=by_team)
|
||||
|
||||
@staticmethod
|
||||
def _known_teams(team_ids: frozenset[str]) -> CallerResolver:
|
||||
"""The real caller resolver over a team loader that behaves like ``get_team_object``: a row for
|
||||
a known id, an error for an unknown one."""
|
||||
def _known_teams(team_ids: frozenset[str], user_ids: frozenset[str] = frozenset({"alice"})) -> CallerResolver:
|
||||
"""The real caller resolver over loaders that behave like ``get_team_object`` and
|
||||
``get_user_object``: a row for a known id, an error for an unknown one."""
|
||||
|
||||
async def load(user_api_key_auth: UserAPIKeyAuth) -> LiteLLM_TeamTable | None:
|
||||
async def load_team(user_api_key_auth: UserAPIKeyAuth) -> LiteLLM_TeamTable | None:
|
||||
caller: Final = user_api_key_auth.agent_caller
|
||||
if caller is None or caller.team_id is None:
|
||||
return None
|
||||
|
|
@ -218,8 +224,16 @@ class TestAgentRequestHandler:
|
|||
raise ValueError(f"Team doesn't exist in db. Team={caller.team_id}")
|
||||
return LiteLLM_TeamTable(team_id=caller.team_id)
|
||||
|
||||
async def load_user(user_api_key_auth: UserAPIKeyAuth) -> LiteLLM_UserTable | None:
|
||||
caller: Final = user_api_key_auth.agent_caller
|
||||
if caller is None or caller.user_id is None:
|
||||
return None
|
||||
if caller.user_id not in user_ids:
|
||||
raise ValueError(f"User doesn't exist in db. User={caller.user_id}")
|
||||
return LiteLLM_UserTable(user_id=caller.user_id, max_budget=None, user_email=None)
|
||||
|
||||
async def resolves(user_api_key_auth: UserAPIKeyAuth) -> bool:
|
||||
return await agent_caller_resolves(user_api_key_auth, load_team=load)
|
||||
return await agent_caller_resolves(user_api_key_auth, load_team=load_team, load_user=load_user)
|
||||
|
||||
return resolves
|
||||
|
||||
|
|
@ -269,21 +283,29 @@ class TestAgentRequestHandler:
|
|||
|
||||
assert all(call.args[0].team_id is None for call in mock_team.call_args_list), "unknown team was resolved"
|
||||
|
||||
async def test_agent_key_acting_for_an_unknown_teamless_user_reaches_no_agent(self):
|
||||
@pytest.mark.parametrize(
|
||||
"caller",
|
||||
[AgentCaller(user_id="nobody", team_id=None), AgentCaller(user_id="nobody", team_id="callers")],
|
||||
ids=["teamless", "on_a_known_team"],
|
||||
)
|
||||
async def test_agent_key_acting_for_an_unknown_user_reaches_no_agent(self, caller: AgentCaller):
|
||||
"""A user id that names nobody denies even next to a real team: that team's grants must not
|
||||
stand in for the missing user."""
|
||||
agent_key: Final = self._key_granting(["agent-alpha"], agent_id="caller-agent")
|
||||
agent_key.agent_caller = AgentCaller(user_id="nobody", team_id=None)
|
||||
agent_key.agent_caller = caller
|
||||
resolve, _ = self._ceiling_resolver(None)
|
||||
|
||||
async def nobody_resolves(user_api_key_auth: UserAPIKeyAuth) -> bool:
|
||||
return False
|
||||
|
||||
with patch.object( # test-quality-ok: the team resolver reads proxy_server globals with no injection seam
|
||||
AgentRequestHandler, "_get_allowed_agents_for_team", self._team_grants({})
|
||||
):
|
||||
AgentRequestHandler,
|
||||
"_get_allowed_agents_for_team",
|
||||
self._team_grants({"callers": RestrictedAgentAccess(frozenset({"agent-alpha"}))}),
|
||||
) as mock_team:
|
||||
assert await AgentRequestHandler.resolve_agent_access(
|
||||
agent_key, resolve, nobody_resolves
|
||||
agent_key, resolve, self._known_teams(frozenset({"callers"}))
|
||||
) == RestrictedAgentAccess(frozenset())
|
||||
|
||||
assert all(call.args[0].team_id is None for call in mock_team.call_args_list), "unknown user was resolved"
|
||||
|
||||
async def test_agent_key_acting_for_a_user_whose_team_grants_no_agent_reaches_none(self):
|
||||
agent_key: Final = UserAPIKeyAuth(api_key="test-key", user_id="test-user", agent_id="caller-agent")
|
||||
agent_key.agent_caller = AgentCaller(user_id="alice", team_id="callers")
|
||||
|
|
|
|||
|
|
@ -9230,7 +9230,7 @@ async def test_agent_key_acting_for_a_team_is_capped_at_that_teams_models():
|
|||
|
||||
assert exc_info.value.type == ProxyErrorTypes.team_model_access_denied
|
||||
assert exc_info.value.code == str(status.HTTP_403_FORBIDDEN)
|
||||
assert asked == ["team:team-a", "team:team-a"]
|
||||
assert asked == ["team:team-a", "user:alice", "team:team-a", "user:alice"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
|
|
@ -9301,6 +9301,22 @@ async def test_agent_key_acting_for_an_unknown_teamless_user_is_denied_with_403(
|
|||
assert "user_id='nobody'" in exc_info.value.internal_message
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_agent_key_acting_for_an_unknown_user_on_a_known_team_is_denied_with_403():
|
||||
"""A real team id next to a user id that names nobody is still an unresolvable caller: the team's
|
||||
own grant must not stand in for the missing user."""
|
||||
agent_key: Final = _agent_key_acting_for(user_id="nobody", team_id="team-a")
|
||||
load_team, _, asked = _caller_loaders(LiteLLM_TeamTable(team_id="team-a", models=["gpt-5"]), None)
|
||||
|
||||
with pytest.raises(ModelAccessDeniedProxyException) as exc_info:
|
||||
await _check_caller_models(agent_key, "gpt-5", load_team, _no_such_user)
|
||||
|
||||
assert exc_info.value.type == ProxyErrorTypes.user_model_access_denied
|
||||
assert exc_info.value.code == str(status.HTTP_403_FORBIDDEN)
|
||||
assert "user_id='nobody'" in exc_info.value.internal_message
|
||||
assert asked == ["team:team-a"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_agent_key_without_an_echoed_caller_keeps_its_own_models():
|
||||
agent_key: Final = UserAPIKeyAuth(token="agent-token", agent_id="agent-1", models=["gpt-5", "claude-sonnet"])
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue