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]

refactor(auth): resolve PATCH team org-context from the route template

Yuneng Jiang368342cdde89
diff --git a/litellm/proxy/auth/auth_checks.py b/litellm/proxy/auth/auth_checks.py
index 1db5e2f5724..230b9b70ff0 100644
--- a/litellm/proxy/auth/auth_checks.py
+++ b/litellm/proxy/auth/auth_checks.py
@@ -102,7 +102,7 @@ from .auth_checks_organization import (
add_team_org_context_to_request_body,
organization_role_based_access_check,
)
-from .auth_utils import get_model_from_request
+from .auth_utils import get_model_from_request, get_request_route_template
if TYPE_CHECKING:
from opentelemetry.trace import Span as _Span
@@ -726,7 +726,7 @@ async def common_checks(
route=route,
request_body=request_body,
fetch_team_org_id=_fetch_team_org_id,
- path_team_id=(getattr(request, "path_params", None) or {}).get("team_id"),
+ route_template=get_request_route_template(request),
)
_is_route_allowed = _is_api_route_allowed(
diff --git a/litellm/proxy/auth/auth_checks_organization.py b/litellm/proxy/auth/auth_checks_organization.py
index bb82b3a142d..9b9e0661084 100644
--- a/litellm/proxy/auth/auth_checks_organization.py
+++ b/litellm/proxy/auth/auth_checks_organization.py
@@ -2,7 +2,6 @@
Auth Checks for Organizations
"""
-import re
from typing import Awaitable, Callable, Dict, List, Optional, Tuple
from fastapi import status
@@ -174,14 +173,17 @@ def _user_is_org_admin(
TEAM_ORG_CONTEXT_ROUTES = frozenset({"/team/update"})
-_TEAM_ID_PATH_ROUTE = re.compile(r"^/team/[^/]+$")
+# The RESTful update route carries the team id in the path. Match on the route
+# template so the sibling /team/<verb> routes (which share the single-segment
+# shape) are not mistaken for it and don't trigger a team lookup.
+PATCH_TEAM_ROUTE_TEMPLATE = "/team/{team_id}"
async def add_team_org_context_to_request_body(
route: str,
request_body: dict,
fetch_team_org_id: Callable[[str], Awaitable[Optional[str]]],
- path_team_id: Optional[str] = None,
+ route_template: Optional[str] = None,
) -> dict:
"""
Return a copy of request_body with organization_id resolved from the target
@@ -193,15 +195,15 @@ async def add_team_org_context_to_request_body(
are untouched.
The team_id is taken from the body for TEAM_ORG_CONTEXT_ROUTES, or from the
- path (``path_team_id``) for the bare ``/team/{team_id}`` route.
+ last path segment when ``route_template`` is the ``/team/{team_id}`` route.
"""
if request_body.get("organization_id"):
return request_body
if route in TEAM_ORG_CONTEXT_ROUTES:
team_id: Optional[str] = request_body.get("team_id")
- elif path_team_id and _TEAM_ID_PATH_ROUTE.match(route):
- team_id = path_team_id
+ elif route_template == PATCH_TEAM_ROUTE_TEMPLATE:
+ team_id = route.rsplit("/", 1)[-1]
else:
return request_body
diff --git a/tests/test_litellm/proxy/auth/test_route_checks.py b/tests/test_litellm/proxy/auth/test_route_checks.py
index 404701bb8ac..a6d4dc63697 100644
--- a/tests/test_litellm/proxy/auth/test_route_checks.py
+++ b/tests/test_litellm/proxy/auth/test_route_checks.py
@@ -2730,8 +2730,9 @@ def test_team_update_gate_rejects_cross_org_admin_with_resolved_org():
@pytest.mark.asyncio
async def test_add_team_org_context_resolves_org_from_path_for_patch_route():
"""PATCH /team/{team_id} carries team_id in the PATH, not the body. The target
- team's org is resolved from path_team_id and injected, so an org admin of that
- team's org clears the same gate they clear for POST /team/update."""
+ team's org is resolved from the last path segment (identified by the route
+ template) and injected, so an org admin of that team's org clears the same gate
+ they clear for POST /team/update."""
async def fetch(team_id: str):
assert team_id == "team-1"
@@ -2741,15 +2742,15 @@ async def test_add_team_org_context_resolves_org_from_path_for_patch_route():
route="/team/team-1",
request_body={"metadata": {"cost_center": "x"}},
fetch_team_org_id=fetch,
- path_team_id="team-1",
+ route_template="/team/{team_id}",
)
assert out == {"metadata": {"cost_center": "x"}, "organization_id": "org-1"}
@pytest.mark.asyncio
async def test_add_team_org_context_path_noop_for_team_subresource():
- """A sub-resource like /team/{team_id}/members/me is NOT the bare team route, so
- no org is injected even though a team_id path param is present."""
+ """A sub-resource like /team/{team_id}/members/me has a different route template,
+ so it is not mistaken for the bare team route and no org is injected."""
async def fetch(team_id: str):
raise AssertionError("must not resolve for a team sub-resource route")
@@ -2759,25 +2760,26 @@ async def test_add_team_org_context_path_noop_for_team_subresource():
route="/team/team-1/members/me",
request_body=body,
fetch_team_org_id=fetch,
- path_team_id="team-1",
+ route_template="/team/{team_id}/members/me",
)
assert out == body
@pytest.mark.asyncio
-async def test_add_team_org_context_path_noop_without_path_team_id():
- """Routes with no team_id path param (e.g. POST /team/new, whose path also
- matches the bare shape) resolve nothing."""
+async def test_add_team_org_context_noop_for_static_team_route():
+ """A static sibling route (e.g. POST /team/new) whose resolved path also has the
+ single-segment shape has its own template, not /team/{team_id}, so no team lookup
+ is attempted — the guard against a spurious DB hit on every /team/<verb> call."""
async def fetch(team_id: str):
- raise AssertionError("must not resolve when there is no path team_id")
+ raise AssertionError("must not resolve for a static /team/<verb> route")
body = {"team_alias": "new team"}
out = await add_team_org_context_to_request_body(
route="/team/new",
request_body=body,
fetch_team_org_id=fetch,
- path_team_id=None,
+ route_template="/team/new",
)
assert out == body