Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
11 changes: 11 additions & 0 deletions litellm/proxy/auth/user_api_key_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
populate_request_with_path_params,
)
from litellm.proxy.common_utils.realtime_utils import _realtime_request_body
from litellm.proxy.litellm_pre_call_utils import LiteLLMProxyRequestSetup
from litellm.proxy.utils import (
PrismaClient,
ProxyLogging,
Expand Down Expand Up @@ -1977,6 +1978,16 @@ async def _run_centralized_common_checks(
llm_router=llm_router,
)

# Merge x-litellm-tags into request_data BEFORE common_checks runs.
# _tag_max_budget_check inside common_checks only inspects request_data;
# without this pre-merge, header-supplied tags bypass tag-budget
# enforcement.
LiteLLMProxyRequestSetup.apply_client_tag_policy_pre_auth(
request=request,
request_data=request_data,
user_api_key_dict=user_api_key_auth_obj,
)

_ = await common_checks(
request=request,
request_body=request_data,
Expand Down
60 changes: 60 additions & 0 deletions litellm/proxy/litellm_pre_call_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
TeamCallbackMetadata,
UserAPIKeyAuth,
)
from litellm.proxy.common_utils.callback_utils import (
get_metadata_variable_name_from_kwargs,
)
from litellm.proxy.common_utils.http_parsing_utils import _safe_get_request_headers

# Cache special headers as a frozenset for O(1) lookup performance
Expand Down Expand Up @@ -1187,6 +1190,63 @@ def add_request_tag_to_metadata(

return tags

@staticmethod
def apply_client_tag_policy_pre_auth(
request: Request,
request_data: dict,
user_api_key_dict: UserAPIKeyAuth,
) -> None:
"""
Merge ``x-litellm-tags`` header tags into ``request_data`` BEFORE
auth budget gates run, so ``_tag_max_budget_check`` (which only
inspects ``request_data``) sees them. Without this, header-tagged
requests silently bypass per-tag budget enforcement.

Why: ``add_litellm_data_to_request`` runs the equivalent merge
post-auth, after ``_tag_max_budget_check`` has already executed.
Header-supplied tags merged there are invisible to that check.
Running the merge here closes that gap; the post-auth merge in
``add_litellm_data_to_request`` remains as defense-in-depth.

How to apply: invoked from the auth chain just before
``common_checks``. Mutates ``request_data`` in place; idempotent
when followed by ``add_litellm_data_to_request``.
"""
# No allow_client_tags opt-in: caller-supplied tags always flow
# into metadata.tags (see add_litellm_data_to_request). The pre-auth
# merge mirrors that so _tag_max_budget_check sees the same tags.
headers = _safe_get_request_headers(request=request)
raw_header_tags = headers.get("x-litellm-tags")
if not raw_header_tags:
return

if isinstance(raw_header_tags, str):
header_tags: List[str] = [
t.strip() for t in raw_header_tags.split(",") if t.strip()
]
elif isinstance(raw_header_tags, list):
header_tags = [t for t in raw_header_tags if isinstance(t, str) and t]
else:
return

if not header_tags:
return

# Match the metadata key that get_tags_from_request_body will read
# from (litellm_metadata vs metadata) so the merged tags are visible
# to _tag_max_budget_check.
_metadata_variable_name = get_metadata_variable_name_from_kwargs(request_data)
metadata = request_data.get(_metadata_variable_name)
if not isinstance(metadata, dict):
metadata = {}

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: Tag-budget bypass with string metadata

get_tags_from_request_body() parses string metadata for budget checks, but this new pre-auth merge replaces a string metadata or litellm_metadata value with {} before that parser runs. An authenticated caller whose key allows client tags can send metadata='{"tags":["over-budget-tag"]}' plus any x-litellm-tags header, causing _tag_max_budget_check to see only the header tags and skip the over-budget metadata tag. Parse string metadata here, or preserve the existing value and merge into the parsed dict instead of overwriting it.

request_data[_metadata_variable_name] = metadata

existing_tags = metadata.get("tags")
metadata["tags"] = LiteLLMProxyRequestSetup._merge_tags(
request_tags=existing_tags if isinstance(existing_tags, list) else None,
tags_to_add=header_tags,
)


async def add_litellm_data_to_request( # noqa: PLR0915
data: dict,
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
"""
Tests for cascading credential renames into model rows.

When a credential is renamed via PATCH /credentials/{old_name}, every model
row whose `litellm_params.litellm_credential_name` references the old name
must be updated in lockstep — otherwise those models will fail at request
time when the router tries to resolve a credential that no longer exists.
"""

import json
import types
from unittest.mock import AsyncMock, MagicMock

import pytest

import litellm.proxy.proxy_server as ps
from litellm.proxy.common_utils.encrypt_decrypt_utils import encrypt_value_helper
from litellm.proxy.credential_endpoints.endpoints import (
_cascade_rename_credential_in_models,
)
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved


@pytest.fixture
def salt_key(monkeypatch):
"""Encrypt/decrypt helpers require a signing key — set one for the test."""
monkeypatch.setenv("LITELLM_SALT_KEY", "test-salt-key-cascade-rename")
yield


def _model_row(model_id: str, credential_name_plain: str | None):
"""
Build a fake LiteLLM_ProxyModelTable row whose `litellm_params` mirrors the
on-disk shape (encrypted values, dict-typed JSON column).
"""
params: dict = {"model": "gpt-4o"}
if credential_name_plain is not None:
params["litellm_credential_name"] = encrypt_value_helper(
value=credential_name_plain
)
row = MagicMock()
row.model_id = model_id
row.litellm_params = params
return row


@pytest.mark.asyncio
async def test_cascade_renames_only_matching_models(salt_key, monkeypatch):
"""Only models referencing the old name are updated; others are left alone."""
monkeypatch.setattr(ps, "llm_router", None)

matching = _model_row("model-1", "old-cred")
other = _model_row("model-2", "different-cred")
no_credential = _model_row("model-3", None)

tx = types.SimpleNamespace(
litellm_proxymodeltable=types.SimpleNamespace(
find_many=AsyncMock(return_value=[matching, other, no_credential]),
update=AsyncMock(),
)
)

updated = await _cascade_rename_credential_in_models(
tx=tx,
old_credential_name="old-cred",
new_credential_name="new-cred",
)

assert updated == 1
tx.litellm_proxymodeltable.update.assert_awaited_once()
call = tx.litellm_proxymodeltable.update.await_args
assert call.kwargs["where"] == {"model_id": "model-1"}

# Prisma's JSON column expects a serialized string, not a dict.
raw_params = call.kwargs["data"]["litellm_params"]
assert isinstance(raw_params, str)
written_params = json.loads(raw_params)
assert written_params["model"] == "gpt-4o"
# The new credential name is stored encrypted, not plain text.
assert written_params["litellm_credential_name"] != "new-cred"
# And it must round-trip back to the new name.
from litellm.proxy.common_utils.encrypt_decrypt_utils import (
decrypt_value_helper,
)

assert (
decrypt_value_helper(
value=written_params["litellm_credential_name"],
key="litellm_credential_name",
return_original_value=True,
)
== "new-cred"
)


@pytest.mark.asyncio
async def test_cascade_updates_in_memory_router(salt_key, monkeypatch):
"""
The router's in-memory model_list holds decrypted credential names. The
cascade must rewrite them so live traffic doesn't try to resolve a name
that no longer exists in litellm.credential_list.
"""
fake_router = MagicMock()
fake_router.model_list = [
{"litellm_params": {"litellm_credential_name": "old-cred", "model": "gpt-4o"}},
{
"litellm_params": {
"litellm_credential_name": "other-cred",
"model": "claude",
}
},
{"litellm_params": {"model": "no-creds"}},
]
monkeypatch.setattr(ps, "llm_router", fake_router)

matching = _model_row("model-1", "old-cred")
tx = types.SimpleNamespace(
litellm_proxymodeltable=types.SimpleNamespace(
find_many=AsyncMock(return_value=[matching]),
update=AsyncMock(),
)
)

await _cascade_rename_credential_in_models(
tx=tx,
old_credential_name="old-cred",
new_credential_name="new-cred",
)

assert (
fake_router.model_list[0]["litellm_params"]["litellm_credential_name"]
== "new-cred"
)
assert (
fake_router.model_list[1]["litellm_params"]["litellm_credential_name"]
== "other-cred"
)
assert "litellm_credential_name" not in fake_router.model_list[2]["litellm_params"]


@pytest.mark.asyncio
async def test_cascade_noop_when_no_models_match(salt_key, monkeypatch):
"""No matching rows → no updates issued, no in-memory mutation."""
fake_router = MagicMock()
untouched = {
"litellm_params": {"litellm_credential_name": "other-cred", "model": "gpt-4o"}
}
fake_router.model_list = [untouched]
monkeypatch.setattr(ps, "llm_router", fake_router)

tx = types.SimpleNamespace(
litellm_proxymodeltable=types.SimpleNamespace(
find_many=AsyncMock(return_value=[_model_row("model-2", "other-cred")]),
update=AsyncMock(),
)
)

updated = await _cascade_rename_credential_in_models(
tx=tx,
old_credential_name="old-cred",
new_credential_name="new-cred",
)

assert updated == 0
tx.litellm_proxymodeltable.update.assert_not_awaited()
assert untouched["litellm_params"]["litellm_credential_name"] == "other-cred"
Loading
Loading