From 1c86f39adfa1672b527089c52653daa68d262aac Mon Sep 17 00:00:00 2001 From: Juan Meeske Date: Sat, 19 Sep 2026 08:21:32 +0200 Subject: [PATCH] fix(proxy): keep comments descriptive per review Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_01Hs4zF96YHUSUHnCjAVM1xD --- litellm/proxy/auth/auth_utils.py | 30 ++++++++----------- .../proxy/auth/test_auth_utils.py | 14 ++++----- 2 files changed, 18 insertions(+), 26 deletions(-) diff --git a/litellm/proxy/auth/auth_utils.py b/litellm/proxy/auth/auth_utils.py index d5d7f39af15..65691fc1ef0 100644 --- a/litellm/proxy/auth/auth_utils.py +++ b/litellm/proxy/auth/auth_utils.py @@ -1709,18 +1709,13 @@ _MODEL_ROUTING_BODY_TARGET_MODEL_ROUTE_MARKERS: Final = ( "/vector_stores", ) _MODEL_ROUTING_COMPLETION_MODEL_ROUTE_MARKERS: Final = ("/evals",) -# The chat-completions, completions and embeddings handlers (and the queue -# variant) declare a ``model`` parameter that FastAPI binds from the URL — -# from ``?model=`` on the plain routes, from ``{model:path}`` on the -# deployment-style routes — and request processing prefers that value over the -# body ``model``. Every model a request can resolve to is a candidate for the -# access checks, so the URL-bound value is collected here alongside the body -# value. The two sets below are the exact route templates those handlers are -# mounted on (proxy_server.py); classification is by matched template, never -# by substring, so unrelated routes that merely contain ``/completions`` do not -# get a stray ``?model=`` promoted to an authorization input. On a +# Route templates on which the handler binds ``model`` from the URL +# (``?model=`` on the plain routes, ``{model:path}`` on the deployment-style +# routes). The URL-bound value is collected as a model candidate alongside the +# body value. Classification is by matched template, never by substring, so a +# route that merely contains ``/completions`` is not affected. On a # ``{model:path}`` route FastAPI binds the path value and ignores a same-named -# query parameter, so only the path value is a candidate there. +# query parameter, so only the path value is read there. _LLM_HANDLER_QUERY_MODEL_ROUTES: Final = frozenset( { "/v1/chat/completions", @@ -1823,10 +1818,10 @@ def _llm_handler_url_model( Mirrors the handlers' ``model`` parameter binding: the ``{model:path}`` segment on the deployment-style templates (a same-named query parameter is ignored there, as FastAPI does), the exact-case ``model`` query - parameter on the plain routes (``?MODEL=`` is not bound, so it is not a - candidate; duplicates are last-wins in both FastAPI and the parsed - mapping). ``route_template`` comes from the matched Starlette route when a - request is available; without one, the literal path is classified instead. + parameter on the plain routes (``?MODEL=`` is not bound; duplicates are + last-wins in both FastAPI and the parsed mapping). ``route_template`` + comes from the matched Starlette route when a request is available; + without one, the literal path is classified instead. """ if route_template is None: if route in _LLM_HANDLER_QUERY_MODEL_ROUTES: @@ -1993,9 +1988,8 @@ 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")) - # URL-bound model on the LLM handler routes (request processing prefers it - # over the body model). The ``x-litellm-model`` header is deliberately not - # read: these handlers do not route from it. + # URL-bound model on the LLM handler routes. The ``x-litellm-model`` header + # is deliberately not read: these handlers do not bind it. _append_model_candidates( candidates, _llm_handler_url_model( diff --git a/tests/test_litellm/proxy/auth/test_auth_utils.py b/tests/test_litellm/proxy/auth/test_auth_utils.py index a6d61a5b148..6326a4e6d60 100644 --- a/tests/test_litellm/proxy/auth/test_auth_utils.py +++ b/tests/test_litellm/proxy/auth/test_auth_utils.py @@ -3922,8 +3922,8 @@ class TestIsRequestBodySafeBlocksAwsIdentitySelectors: # --- URL-bound model on the LLM handler routes --------------------------------------- # chat-completions, completions and embeddings bind ``model`` from the URL # (``?model=`` on the plain routes, ``{model:path}`` on the deployment-style -# routes) and request processing prefers it over the body model, so it is part -# of the model candidates the access checks see. +# routes); that value is part of the model candidates get_model_from_request +# returns. def _matched_request(template: str, path_params: dict[str, object] | None = None) -> Request: @@ -3949,8 +3949,7 @@ def _matched_request(template: str, path_params: dict[str, object] | None = None ], ) 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.""" + """Body model and query-string model are both returned as candidates.""" assert get_model_from_request( request_data={"model": "allowed-model"}, route=route, @@ -3994,7 +3993,7 @@ def test_get_model_from_request_query_model_equal_to_body_dedupes(): def test_get_model_from_request_query_model_lookup_is_exact_case(): """FastAPI binds ``model`` case-sensitively: ``?MODEL=`` is not bound by the - handler, so it must not become a candidate either (it would only false-403).""" + handler, so it is not a candidate either.""" assert ( get_model_from_request( request_data={"model": "allowed-model"}, @@ -4020,8 +4019,7 @@ def test_get_model_from_request_query_model_lookup_is_exact_case(): ], ) def test_get_model_from_request_includes_deployment_path_model(template, path, path_model): - """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.""" + """The resolved ``{model:path}`` segment is returned alongside the body model.""" assert get_model_from_request( request_data={"model": "allowed-model"}, route=path, @@ -4039,7 +4037,7 @@ def test_get_model_from_request_deployment_path_model_without_request_object(): def test_get_model_from_request_deployment_route_ignores_query_model(): """On a ``{model:path}`` route FastAPI binds the path value and ignores a - same-named query parameter, so auth validates exactly what will be routed.""" + same-named query parameter, so only the path value is read.""" assert ( get_model_from_request( request_data={"model": "allowed-model"},