Skip to content

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]

Fix access-group bypass via litellm-model fallback path

mateo-berri9d945fe23b23
diff --git a/litellm/router.py b/litellm/router.py
index 878d65fa8ff..8e29f8cfc11 100644
--- a/litellm/router.py
+++ b/litellm/router.py
@@ -9475,6 +9475,16 @@ class Router:
request_kwargs=request_kwargs,
request_team_id=request_team_id,
)
+ # If the litellm-model lookup produced candidates that access-group
+ # filtering then removed, treat this the same as the by-name path
+ # being emptied: prevent default-model fallback from bypassing the
+ # restriction (the fallback model may have no access_groups and
+ # would short-circuit the filter).
+ if (
+ len(_litellm_model_deployments) > 0
+ and len(healthy_deployments) == 0
+ ):
+ _access_group_filter_emptied_candidates = True
if verbose_router_logger.isEnabledFor(logging.DEBUG):
verbose_router_logger.debug(
diff --git a/tests/test_litellm/test_router.py b/tests/test_litellm/test_router.py
index e759619b5f6..b6ea6b374e0 100644
--- a/tests/test_litellm/test_router.py
+++ b/tests/test_litellm/test_router.py
@@ -3379,7 +3379,9 @@ def test_explicit_model_access_does_not_force_access_group_filtering():
},
)
- deployment_groups = [d.get("model_info", {}).get("access_groups") for d in deployments]
+ deployment_groups = [
+ d.get("model_info", {}).get("access_groups") for d in deployments
+ ]
assert ["AG1"] in deployment_groups
assert ["AG2"] in deployment_groups
@@ -3531,3 +3533,72 @@ def test_access_group_block_does_not_silently_use_default_fallback_model(
}
},
)
+
+
+def test_access_group_block_via_litellm_model_branch_does_not_use_default_fallback(
+ monkeypatch: pytest.MonkeyPatch,
+):
+ """
+ When the by-name lookup returns no deployments and the litellm-model fallback
+ branch finds candidates that access-group filtering then empties, the router
+ must not fall through to default ``fallbacks`` routing — the default fallback
+ model may have no ``access_groups`` and would short-circuit the filter,
+ silently serving a caller blocked by access-group restrictions.
+ """
+ from litellm.proxy._types import UserAPIKeyAuth
+
+ router = litellm.Router(
+ model_list=[
+ {
+ "model_name": "gpt-5-alias",
+ "litellm_params": {
+ "model": "gpt-5",
+ "api_key": "key1",
+ "mock_response": "blocked-dep-1",
+ },
+ "model_info": {"access_groups": ["AG2"]},
+ },
+ {
+ "model_name": "gpt-4-fallback",
+ "litellm_params": {
+ "model": "gpt-4",
+ "api_key": "fallback-key",
+ "mock_response": "should-not-reach",
+ },
+ },
+ ],
+ fallbacks=[{"*": ["gpt-4-fallback"]}],
+ )
+
+ orig_groups = router.get_model_access_groups
+
+ def fake_get_model_access_groups(
+ model_name=None, model_access_group=None, team_id=None
+ ):
+ if model_name == "gpt-5" and model_access_group is None:
+ return {"AG1": ["gpt-5"], "AG2": ["gpt-5"]}
+ return orig_groups(
+ model_name=model_name,
+ model_access_group=model_access_group,
+ team_id=team_id,
+ )
+
+ monkeypatch.setattr(router, "get_model_access_groups", fake_get_model_access_groups)
+
+ scoped_key = UserAPIKeyAuth(
+ api_key="hashed-key",
+ team_id="team2",
+ models=["AG1"],
+ team_models=["AG1"],
+ )
+
+ with pytest.raises(litellm.BadRequestError):
+ router._common_checks_available_deployment(
+ model="gpt-5",
+ request_kwargs={
+ "metadata": {
+ "user_api_key_team_id": "team2",
+ "user_api_key_auth": scoped_key,
+ }
+ },
+ )