mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(proxy): enforce key model allowlist on ?model= / {model} for chat, completions and embeddings
The chat-completions, completions and embeddings handlers bind a `model`
query or path parameter (`?model=`, `/openai/deployments/{model}/...`,
`/engines/{model}/...`) and route from it in preference to the body model.
The allowlist check only validated the body model on these routes, so a key
scoped to one model could name an allowed model in the body and a denied one
in the query string, and be served the denied model.
Add the query-string and deployment-path model as allowlist candidates on
those routes so every model the handler can dispatch to is validated. The
`x-litellm-model` header is deliberately not read: these handlers do not
route from it. /v1/messages and /v1/responses are unchanged; they do not
route from the query string.
Fixes #41810
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hs4zF96YHUSUHnCjAVM1xD
This commit is contained in:
parent
a9ee15372f
commit
9eaf2eb3c9
2 changed files with 128 additions and 0 deletions
|
|
@ -1697,6 +1697,23 @@ _MODEL_ROUTING_BODY_TARGET_MODEL_ROUTE_MARKERS: Final = (
|
|||
"/vector_stores",
|
||||
)
|
||||
_MODEL_ROUTING_COMPLETION_MODEL_ROUTE_MARKERS: Final = ("/evals",)
|
||||
# Routes served by the chat-completions, completions and embeddings handlers.
|
||||
# These handlers bind a ``model`` path or query parameter
|
||||
# (``/openai/deployments/{model}/...``, ``/engines/{model}/...``, ``?model=``)
|
||||
# and route from it in preference to the body ``model``. The allowlist check
|
||||
# therefore has to validate that value too: with only the body validated, a
|
||||
# key scoped to one model could name an allowed model in the body and a
|
||||
# denied one in the query string, and be served the denied model.
|
||||
_MODEL_ROUTING_LLM_HANDLER_ROUTE_MARKERS: Final = (
|
||||
"/chat/completions",
|
||||
"/completions",
|
||||
"/embeddings",
|
||||
)
|
||||
# Anchored at the path start on purpose: ``/azure/openai/deployments/...`` is the
|
||||
# Azure relay route, whose model group is resolved separately below.
|
||||
_LLM_HANDLER_PATH_MODEL_PATTERN: Final = re.compile(
|
||||
r"^/(?:openai/deployments|engines)/(.+?)/(?:chat/completions|completions|embeddings)$"
|
||||
)
|
||||
# Realtime WebRTC routes carry the effective model inside the nested
|
||||
# ``session.model`` field (see realtime_endpoints.endpoints), so the model the
|
||||
# request will actually use is not present at the top level. Extract it here so
|
||||
|
|
@ -1760,6 +1777,17 @@ def _route_matches_any_marker(route: str, markers: tuple[str, ...]) -> bool:
|
|||
return any(marker in normalized_route for marker in markers)
|
||||
|
||||
|
||||
def _llm_handler_path_model(route: str) -> str | None:
|
||||
"""Model named in the path of a deployment-style LLM route.
|
||||
|
||||
``/openai/deployments/{model}/chat/completions`` and ``/engines/{model}/...``
|
||||
bind ``{model}`` (which may itself contain ``/``) to the handler's ``model``
|
||||
argument, which then takes precedence over the body model when routing.
|
||||
"""
|
||||
match: Final = _LLM_HANDLER_PATH_MODEL_PATTERN.match(route)
|
||||
return match.group(1) if match else None
|
||||
|
||||
|
||||
def _route_uses_model_routing_sources(route: str) -> bool:
|
||||
return _route_matches_any_marker(route=route, markers=_MODEL_ROUTING_ROUTE_MARKERS)
|
||||
|
||||
|
|
@ -1905,6 +1933,13 @@ def _extract_model_candidates_from_request(
|
|||
_append_model_candidates(candidates, session.get("model"))
|
||||
if uses_completion_model_sources and isinstance(request_data.get("completion"), dict):
|
||||
_append_model_candidates(candidates, request_data["completion"].get("model"))
|
||||
if _route_matches_any_marker(route=route, markers=_MODEL_ROUTING_LLM_HANDLER_ROUTE_MARKERS):
|
||||
# The handler routes from ``?model=`` / ``{model}`` ahead of the body, so
|
||||
# every model it can end up dispatching to must pass the allowlist. The
|
||||
# ``x-litellm-model`` header is deliberately not read here: these handlers
|
||||
# do not route from it.
|
||||
_append_model_candidates(candidates, _get_case_insensitive_mapping_value(request_query_params, "model"))
|
||||
_append_model_candidates(candidates, _llm_handler_path_model(route))
|
||||
|
||||
if uses_model_routing_sources:
|
||||
if uses_header_or_query_model_sources:
|
||||
|
|
|
|||
|
|
@ -3918,3 +3918,96 @@ class TestIsRequestBodySafeBlocksAwsIdentitySelectors:
|
|||
)
|
||||
is True
|
||||
)
|
||||
|
||||
# --- query-string / path model must be allowlisted on LLM handler routes ------------
|
||||
# The chat-completions, completions and embeddings handlers route from a ``model``
|
||||
# query or path parameter in preference to the body model. Only the body used to
|
||||
# be validated, so a key scoped to one model could be served another by putting
|
||||
# it in ``?model=`` (BerriAI/litellm#41810).
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"route",
|
||||
[
|
||||
"/v1/chat/completions",
|
||||
"/chat/completions",
|
||||
"/v1/completions",
|
||||
"/completions",
|
||||
"/v1/embeddings",
|
||||
"/embeddings",
|
||||
],
|
||||
)
|
||||
def test_get_model_from_request_includes_query_model_on_llm_handler_routes(route):
|
||||
"""Body says allowed-model, query string says denied-model: both are candidates,
|
||||
so the allowlist check rejects the request if either is outside the key's models."""
|
||||
assert get_model_from_request(
|
||||
request_data={"model": "allowed-model"},
|
||||
route=route,
|
||||
request_query_params={"model": "denied-model"},
|
||||
) == ["allowed-model", "denied-model"]
|
||||
|
||||
|
||||
def test_get_model_from_request_query_model_alone_on_chat_completions():
|
||||
assert (
|
||||
get_model_from_request(
|
||||
request_data={"messages": []},
|
||||
route="/v1/chat/completions",
|
||||
request_query_params={"model": "denied-model"},
|
||||
)
|
||||
== "denied-model"
|
||||
)
|
||||
|
||||
|
||||
def test_get_model_from_request_query_model_equal_to_body_dedupes():
|
||||
assert (
|
||||
get_model_from_request(
|
||||
request_data={"model": "allowed-model"},
|
||||
route="/v1/chat/completions",
|
||||
request_query_params={"model": "allowed-model"},
|
||||
)
|
||||
== "allowed-model"
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"route, expected",
|
||||
[
|
||||
("/openai/deployments/denied-model/chat/completions", ["allowed-model", "denied-model"]),
|
||||
("/openai/deployments/denied-model/completions", ["allowed-model", "denied-model"]),
|
||||
("/openai/deployments/denied-model/embeddings", ["allowed-model", "denied-model"]),
|
||||
("/engines/denied-model/chat/completions", ["allowed-model", "denied-model"]),
|
||||
# {model:path} may contain slashes
|
||||
("/openai/deployments/org/denied-model/chat/completions", ["allowed-model", "org/denied-model"]),
|
||||
],
|
||||
)
|
||||
def test_get_model_from_request_includes_deployment_path_model(route, expected):
|
||||
"""The ``{model}`` path segment is bound to the handler and routed from even when
|
||||
the body carries a different model, so it is validated alongside the body."""
|
||||
assert get_model_from_request(request_data={"model": "allowed-model"}, route=route) == expected
|
||||
|
||||
|
||||
@pytest.mark.parametrize("route", ["/v1/messages", "/v1/responses"])
|
||||
def test_get_model_from_request_ignores_query_model_on_routes_that_do_not_route_from_it(route):
|
||||
"""/v1/messages and /v1/responses serve the body model regardless of ``?model=``,
|
||||
so the query value is not a candidate there (unchanged behaviour)."""
|
||||
assert (
|
||||
get_model_from_request(
|
||||
request_data={"model": "allowed-model"},
|
||||
route=route,
|
||||
request_query_params={"model": "denied-model"},
|
||||
)
|
||||
== "allowed-model"
|
||||
)
|
||||
|
||||
|
||||
def test_get_model_from_request_ignores_routing_header_on_chat_completions():
|
||||
"""``x-litellm-model`` is only honoured on the managed-resource routes; the chat
|
||||
handler never routes from it, so it must not become a candidate (control)."""
|
||||
assert (
|
||||
get_model_from_request(
|
||||
request_data={"model": "allowed-model"},
|
||||
route="/v1/chat/completions",
|
||||
request_headers={"x-litellm-model": "denied-model"},
|
||||
)
|
||||
== "allowed-model"
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue