Skip to content
Closed
20 changes: 17 additions & 3 deletions litellm/proxy/auth/auth_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -1644,19 +1644,33 @@ async def _cache_team_object(
user_api_key_cache: UserApiKeyCache,
proxy_logging_obj: Optional[ProxyLogging],
):
key = "team_id:{}".format(team_id)

## CACHE REFRESH TIME!
team_table.last_refreshed_at = time.time()

await _cache_management_object(
key=key,
key="team_id:{}".format(team_id),
value=team_table,
user_api_key_cache=user_api_key_cache,
proxy_logging_obj=proxy_logging_obj,
model_type=LiteLLM_TeamTableCachedObj,
)

# Mirror the write under the alias key too. `get_team_object_by_alias`
# (used by the JWT auth path with `team_alias_jwt_field`) reads from
# `team_alias:<alias>`, and `_cache_team_object` is the canonical
# "refresh this team" primitive — every caller (cache-refreshing team
# writes, DB-fetch repopulate, access-group endpoints) needs the
# alias-keyed entry to stay in sync, otherwise the alias path keeps
# serving stale `team.models` / `object_permission` until cache TTL.
if team_table.team_alias:
await _cache_management_object(
key="team_alias:{}".format(team_table.team_alias),
Comment thread
veria-ai[bot] marked this conversation as resolved.
Resolved
value=team_table,
user_api_key_cache=user_api_key_cache,
proxy_logging_obj=proxy_logging_obj,
model_type=LiteLLM_TeamTableCachedObj,
)


async def _cache_key_object(
hashed_token: str,
Expand Down
38 changes: 35 additions & 3 deletions litellm/proxy/auth/model_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

import litellm
from litellm._logging import verbose_proxy_logger
from litellm.litellm_core_utils.credential_accessor import CredentialAccessor
from litellm.proxy._types import SpecialModelNames, UserAPIKeyAuth
from litellm.router import Router
from litellm.router_utils.fallback_event_handlers import get_fallback_model_group
from litellm.types.router import LiteLLM_Params
from litellm.types.router import CredentialLiteLLMParams, LiteLLM_Params
from litellm.utils import get_valid_models


_CREDENTIAL_LITELLM_PARAM_FIELDS = set(CredentialLiteLLMParams.model_fields)


def _check_wildcard_routing(model: str) -> bool:
"""
Returns True if a model is a provider wildcard.
Expand Down Expand Up @@ -178,6 +182,7 @@ def get_complete_model_list(
model_access_groups: Dict[str, List[str]] = {},
include_model_access_groups: Optional[bool] = False,
only_model_access_groups: Optional[bool] = False,
team_id: Optional[str] = None,
) -> List[str]:
"""Logic for returning complete model list for a given key + team pair"""

Expand Down Expand Up @@ -222,13 +227,37 @@ def append_unique(models):
unique_models=unique_models,
return_wildcard_routes=return_wildcard_routes,
llm_router=llm_router,
team_id=team_id,
)

complete_model_list = unique_models + all_wildcard_models

return complete_model_list


def _hydrate_litellm_credential_name(
litellm_params: Optional[LiteLLM_Params],
) -> Optional[LiteLLM_Params]:
if litellm_params is None or litellm_params.litellm_credential_name is None:
return litellm_params

credential_values = CredentialAccessor.get_credential_values(
litellm_params.litellm_credential_name
)
if not credential_values:
return litellm_params

litellm_params = litellm_params.model_copy()
for key, value in credential_values.items():
if (
key in _CREDENTIAL_LITELLM_PARAM_FIELDS
and getattr(litellm_params, key, None) is None
):
setattr(litellm_params, key, value)
litellm_params.litellm_credential_name = None
return litellm_params


def get_known_models_from_wildcard(
wildcard_model: str, litellm_params: Optional[LiteLLM_Params] = None
) -> List[str]:
Expand All @@ -247,7 +276,7 @@ def get_known_models_from_wildcard(
else:
provider = wildcard_provider_prefix

# get all known provider models
litellm_params = _hydrate_litellm_credential_name(litellm_params)

wildcard_models = get_provider_models(
provider=provider, litellm_params=litellm_params
Expand Down Expand Up @@ -285,6 +314,7 @@ def _get_wildcard_models(
unique_models: List[str],
return_wildcard_routes: Optional[bool] = False,
llm_router: Optional[Router] = None,
team_id: Optional[str] = None,
) -> List[str]:
models_to_remove = set()
all_wildcard_models = []
Expand All @@ -297,7 +327,9 @@ def _get_wildcard_models(

## get litellm params from model
if llm_router is not None:
model_list = llm_router.get_model_list(model_name=model)
model_list = llm_router.get_model_list(
model_name=model, team_id=team_id
)
if model_list:
for router_model in model_list:
wildcard_models = get_known_models_from_wildcard(
Expand Down
82 changes: 71 additions & 11 deletions litellm/proxy/management_endpoints/team_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
UserAPIKeyAuth,
)
from litellm.proxy.auth.auth_checks import (
_cache_team_object,
allowed_route_check_inside_route,
can_org_access_model,
get_org_object,
Expand Down Expand Up @@ -128,6 +129,33 @@ def _sanitize_for_log(value: Any) -> str:
return text.replace("\r", "").replace("\n", "")


async def _refresh_cached_team(
team_row: Any,
user_api_key_cache: Any,
proxy_logging_obj: Any,
) -> None:
"""
Refresh the in-memory cached team object after a DB write.

Every endpoint that mutates `litellm_teamtable` must call this so the
cached `LiteLLM_TeamTableCachedObj` used by `common_checks` stays in
sync. Without this, subsequent auth checks read a stale team and can
403 on permissions the DB has already granted (or, symmetrically,
keep granting permissions the DB has already revoked).

`team_row` is the Prisma row returned by `update`/`find_unique` on
`litellm_teamtable`. It is converted to `LiteLLM_TeamTableCachedObj`
via `model_dump()` to match the cache shape `_cache_team_object`
expects.
"""
await _cache_team_object(
team_id=team_row.team_id,
team_table=LiteLLM_TeamTableCachedObj(**team_row.model_dump()),
user_api_key_cache=user_api_key_cache,
proxy_logging_obj=proxy_logging_obj,
)


async def _verify_team_access(
team_obj: LiteLLM_TeamTable,
user_api_key_dict: UserAPIKeyAuth,
Expand Down Expand Up @@ -1580,7 +1608,6 @@ async def update_team( # noqa: PLR0915
```
"""
try:
from litellm.proxy.auth.auth_checks import _cache_team_object
from litellm.proxy.proxy_server import (
litellm_proxy_admin_name,
llm_router,
Expand Down Expand Up @@ -1843,7 +1870,13 @@ async def update_team( # noqa: PLR0915
await prisma_client.db.litellm_teamtable.update(
where={"team_id": data.team_id},
data=updated_kv,
include={"litellm_model_table": True}, # type: ignore
# `object_permission` is included so `_refresh_cached_team`
# doesn't write a cached team with the relation nulled out —
# see team_model_add for the full rationale.
include={
"litellm_model_table": True,
"object_permission": True,
}, # type: ignore
)
)

Expand All @@ -1856,9 +1889,8 @@ async def update_team( # noqa: PLR0915
verbose_proxy_logger.info(
"Successfully updated team - %s, info", team_row.team_id
)
await _cache_team_object(
team_id=team_row.team_id,
team_table=LiteLLM_TeamTableCachedObj(**team_row.model_dump()),
await _refresh_cached_team(
team_row=team_row,
user_api_key_cache=user_api_key_cache,
proxy_logging_obj=proxy_logging_obj,
)
Expand Down Expand Up @@ -4553,7 +4585,11 @@ async def team_model_add(
}'
```
"""
from litellm.proxy.proxy_server import prisma_client
from litellm.proxy.proxy_server import (
prisma_client,
proxy_logging_obj,
user_api_key_cache,
)

if prisma_client is None:
raise HTTPException(status_code=500, detail={"error": "No db connected"})
Expand Down Expand Up @@ -4587,9 +4623,21 @@ async def team_model_add(
)

updated_models = add_new_models_to_team(team_obj=team_obj, new_models=data.models)
# Update team
# Update team. `include` mirrors the relations the auth path consumes
# off the cached team object so that `_refresh_cached_team` doesn't
# null them out — see object_permission_utils.validate_key_search_tools_against_team
# and the MCP/agent authz paths, which treat a missing object_permission
# as "no team-level restriction".
updated_team = await prisma_client.db.litellm_teamtable.update(
where={"team_id": data.team_id}, data={"models": updated_models}
where={"team_id": data.team_id},
data={"models": updated_models},
include={"object_permission": True}, # type: ignore
)

await _refresh_cached_team(
team_row=updated_team,
user_api_key_cache=user_api_key_cache,
proxy_logging_obj=proxy_logging_obj,
)

return updated_team
Expand Down Expand Up @@ -4624,7 +4672,11 @@ async def team_model_delete(
}'
```
"""
from litellm.proxy.proxy_server import prisma_client
from litellm.proxy.proxy_server import (
prisma_client,
proxy_logging_obj,
user_api_key_cache,
)

if prisma_client is None:
raise HTTPException(status_code=500, detail={"error": "No db connected"})
Expand Down Expand Up @@ -4663,9 +4715,17 @@ async def team_model_delete(
# Remove specified models
updated_models = [m for m in current_models if m not in data.models]

# Update team
# Update team. See team_model_add for the rationale on `include`.
updated_team = await prisma_client.db.litellm_teamtable.update(
where={"team_id": data.team_id}, data={"models": updated_models}
where={"team_id": data.team_id},
data={"models": updated_models},
include={"object_permission": True}, # type: ignore
)

await _refresh_cached_team(
team_row=updated_team,
user_api_key_cache=user_api_key_cache,
proxy_logging_obj=proxy_logging_obj,
)

return updated_team
Expand Down
3 changes: 3 additions & 0 deletions litellm/proxy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6068,6 +6068,8 @@ async def get_available_models_for_user(
include_model_access_groups=include_model_access_groups,
)

effective_team_id = team_id or user_api_key_dict.team_id

# Get complete model list
all_models = get_complete_model_list(
key_models=key_models,
Expand All @@ -6080,6 +6082,7 @@ async def get_available_models_for_user(
model_access_groups=model_access_groups,
include_model_access_groups=include_model_access_groups,
only_model_access_groups=only_model_access_groups,
team_id=effective_team_id,
)

return all_models
Expand Down
90 changes: 90 additions & 0 deletions tests/test_litellm/proxy/auth/test_auth_checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3016,3 +3016,93 @@ async def mock_get_current_spend(counter_key, fallback_spend):
proxy_logging_obj=proxy_logging_obj,
)
assert exc_info.value.max_budget == 0.0


@pytest.mark.asyncio
async def test_cache_team_object_writes_both_team_id_and_team_alias_keys():
"""
Regression pin for LIT-3244 patch/1.86.0 follow-up.

`_cache_team_object` is the canonical "refresh this team" primitive.
It must populate BOTH cache keys readers can hit:
- "team_id:<id>" — used by `get_team_object(team_id=...)`,
i.e. API-key auth and JWT-with-team_id_jwt_field
- "team_alias:<alias>" — used by `get_team_object_by_alias(team_alias=...)`,
i.e. JWT-with-team_alias_jwt_field

Without the alias-keyed write, JWT callers using `team_alias_jwt_field`
see a stale team after every team-mutation endpoint (team_model_add,
team_model_delete, update_team, access-group writes), even though
the team_id-keyed cache has been refreshed.

Symmetric absence test: when team_alias is empty/None, the helper
must NOT write to "team_alias:" (an empty alias would collide across
every alias-less team and serve the wrong row).
"""
from unittest.mock import AsyncMock, MagicMock

from litellm.proxy._types import LiteLLM_TeamTableCachedObj
from litellm.proxy.auth.auth_checks import _cache_team_object

base_team_row = {
"team_id": "team-1234",
"team_alias": "H-Capacity",
"models": ["openai/*", "bedrock-claude-sonnet-4"],
}

# ===== team_alias is set: both keys must be written =====
team_table = LiteLLM_TeamTableCachedObj(**base_team_row)
cache = MagicMock()
cache.async_set_cache = AsyncMock()

await _cache_team_object(
team_id="team-1234",
team_table=team_table,
user_api_key_cache=cache,
proxy_logging_obj=MagicMock(),
)

written_keys = []
for call in cache.async_set_cache.await_args_list:
key = call.kwargs.get("key") or call.args[0]
written_keys.append(key)
assert "team_id:team-1234" in written_keys, (
"_cache_team_object must write the team_id-keyed cache "
f"(saw: {written_keys})"
)
assert "team_alias:H-Capacity" in written_keys, (
"_cache_team_object must ALSO write the team_alias-keyed cache "
f"so JWT-with-team_alias_jwt_field readers stay in sync "
f"(saw: {written_keys})"
)

# The cached value under both keys should be the same team object —
# otherwise readers via team_id vs. team_alias diverge.
written_values = []
for call in cache.async_set_cache.await_args_list:
value = call.kwargs.get("value") or call.args[1]
written_values.append(value)
assert all(v is team_table for v in written_values), (
"All cache writes from _cache_team_object must reference the same "
"LiteLLM_TeamTableCachedObj instance (no divergence between keys)."
)

# ===== team_alias is None: alias key must NOT be written =====
aliasless = LiteLLM_TeamTableCachedObj(**{**base_team_row, "team_alias": None})
cache2 = MagicMock()
cache2.async_set_cache = AsyncMock()
await _cache_team_object(
team_id="team-no-alias",
team_table=aliasless,
user_api_key_cache=cache2,
proxy_logging_obj=MagicMock(),
)
written_keys_aliasless = [
(c.kwargs.get("key") or c.args[0])
for c in cache2.async_set_cache.await_args_list
]
assert written_keys_aliasless == ["team_id:team-no-alias"], (
"When team has no alias, only the team_id-keyed write should fire "
"(empty alias would collide across teams). "
f"Got: {written_keys_aliasless}"
)
Loading
Loading