Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions litellm/proxy/auth/handle_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,13 +874,15 @@ def can_rbac_role_call_model(
if role_based_models is None or model is None:
return True

if model not in role_based_models:
raise HTTPException(
status_code=403,
detail=f"Role={rbac_role} not allowed to call model={model}. Allowed models={role_based_models}",
)
if model in role_based_models or any(
fnmatch.fnmatch(model, pattern) for pattern in role_based_models

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Medium: Model allowlist expands fnmatch metacharacters

fnmatch treats ? and character classes like [eu] as wildcards, even when a role_permissions.models entry was intended to be an exact model alias. A user whose role is allowed to call a custom alias like prod-[eu] can now call prod-e or prod-u because this authz check interprets the alias as a pattern.

Suggested change
fnmatch.fnmatch(model, pattern) for pattern in role_based_models
re.fullmatch(re.escape(pattern).replace(r"\*", ".*"), model) is not None for pattern in role_based_models if "*" in pattern

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch -- fixed in bcc3472. Wildcard matching now only applies when the pattern contains *. Entries with ? or [...] are treated as exact model names via the model in role_based_models check. This matches the guard pattern used in guardrail_hooks. Added a test verifying that prod-[eu] rejects prod-e and model-v2? rejects model-v2x.

):
return True

return True
raise HTTPException(
status_code=403,
detail=f"Role={rbac_role} not allowed to call model={model}. Allowed models={role_based_models}",
)

@staticmethod
def check_scope_based_access(
Expand Down
36 changes: 36 additions & 0 deletions tests/proxy_unit_tests/test_user_api_key_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,42 @@ def test_can_rbac_role_call_model_no_role_permissions():
)


def test_can_rbac_role_call_model_wildcard():
"""Wildcard patterns in role_permissions.models should match model names."""
from litellm.proxy.auth.handle_jwt import JWTAuthManager
from litellm.proxy._types import RoleBasedPermissions

roles_based_permissions = [
RoleBasedPermissions(
role=LitellmUserRoles.INTERNAL_USER,
models=["bedrock-claude-*"],
),
RoleBasedPermissions(
role=LitellmUserRoles.PROXY_ADMIN,
models=["*"],
),
]

assert JWTAuthManager.can_rbac_role_call_model(
rbac_role=LitellmUserRoles.INTERNAL_USER,
general_settings={"role_permissions": roles_based_permissions},
model="bedrock-claude-draft-rep-sonnet",
)

assert JWTAuthManager.can_rbac_role_call_model(
rbac_role=LitellmUserRoles.PROXY_ADMIN,
general_settings={"role_permissions": roles_based_permissions},
model="any-model-name",
)

with pytest.raises(HTTPException):
JWTAuthManager.can_rbac_role_call_model(
rbac_role=LitellmUserRoles.INTERNAL_USER,
general_settings={"role_permissions": roles_based_permissions},
model="openai-gpt-4",
)


@pytest.mark.parametrize(
"route, request_data, expected_model",
[
Expand Down
51 changes: 51 additions & 0 deletions tests/test_litellm/proxy/auth/test_handle_jwt.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pytest

from fastapi import HTTPException

from litellm.proxy._types import (
JWTLiteLLMRoleMap,
LiteLLM_JWTAuth,
Expand All @@ -13,6 +15,7 @@
Member,
ProxyErrorTypes,
ProxyException,
RoleBasedPermissions,
)
from litellm.proxy.auth.handle_jwt import JWTAuthManager, JWTHandler

Expand Down Expand Up @@ -2680,3 +2683,51 @@ def test_build_decode_kwargs_no_warning_when_scoped(
if "neither JWT_AUDIENCE nor JWT_ISSUER" in r.getMessage()
]
assert matching == []


class TestCanRbacRoleCallModelWildcard:
"""Wildcard patterns in role_permissions.models should match model names."""

def _settings(self, permissions):
return {"role_permissions": permissions}

def test_wildcard_prefix_matches(self):
perms = [
RoleBasedPermissions(
role=LitellmUserRoles.INTERNAL_USER,
models=["bedrock-claude-*"],
),
]
assert JWTAuthManager.can_rbac_role_call_model(
rbac_role=LitellmUserRoles.INTERNAL_USER,
general_settings=self._settings(perms),
model="bedrock-claude-draft-rep-sonnet",
)

def test_star_matches_any_model(self):
perms = [
RoleBasedPermissions(
role=LitellmUserRoles.PROXY_ADMIN,
models=["*"],
),
]
assert JWTAuthManager.can_rbac_role_call_model(
rbac_role=LitellmUserRoles.PROXY_ADMIN,
general_settings=self._settings(perms),
model="any-model-name",
)

def test_non_matching_wildcard_raises(self):
perms = [
RoleBasedPermissions(
role=LitellmUserRoles.INTERNAL_USER,
models=["bedrock-claude-*"],
),
]
with pytest.raises(HTTPException) as exc_info:
JWTAuthManager.can_rbac_role_call_model(
rbac_role=LitellmUserRoles.INTERNAL_USER,
general_settings=self._settings(perms),
model="openai-gpt-4",
)
assert exc_info.value.status_code == 403
Loading