From 0351c0e10da235f601e0430cda3f2cc60ea8479d Mon Sep 17 00:00:00 2001 From: jesus Date: Tue, 3 Nov 2026 18:16:56 +0000 Subject: [PATCH] fix(proxy): reject client-supplied model_id in request bodies --- litellm/proxy/auth/auth_utils.py | 57 ++++++++++++++++-- tests/unit/proxy/auth/test_auth_utils.py | 73 ++++++++++++++++++++++++ 2 files changed, 126 insertions(+), 4 deletions(-) diff --git a/litellm/proxy/auth/auth_utils.py b/litellm/proxy/auth/auth_utils.py index 161ff2fdfcd..a7b48b6e67c 100644 --- a/litellm/proxy/auth/auth_utils.py +++ b/litellm/proxy/auth/auth_utils.py @@ -318,6 +318,18 @@ def route_manages_mcp_servers(route: str | None) -> bool: return route is not None and _MCP_SERVER_MANAGEMENT_ROUTE.fullmatch(route) is not None +_LLM_CALL_ONLY_BANNED_PARAMS: Final[frozenset[str]] = frozenset(("model_id",)) + + +def route_calls_an_llm(route: str | None) -> bool: + """Whether a body on ``route`` becomes a provider call, where ``model_id`` steers Bedrock. + + Admin routes such as ``/health?model_id=`` and ``/spend/logs/ui?model_id=`` take it as a deployment + id instead. ``None`` is a body checked with no route, which callers only do before a provider call. + """ + return route is None or RouteChecks.is_llm_api_route(route) + + _NESTED_CONFIG_KEYS: Final[tuple[str, ...]] = ("litellm_embedding_config", "extra_body") # Metadata containers that carry per-request configuration consumed by the @@ -434,6 +446,11 @@ _BANNED_REQUEST_BODY_PARAMS: Final[tuple[str, ...]] = ( "anthropic_workspace_id", "anthropic-workspace-id", "bedrock_tags", + # Bedrock deployment-pinned inference profile or model ARN. The handlers + # take it over the configured model, so a caller-supplied value swaps the + # upstream model while the proxy authorizes, logs and bills the ``model`` + # the key may use. Only enforced on LLM routes, see ``route_calls_an_llm``. + "model_id", # Deployment-pinned AgentCore session: every new caller-chosen value provisions another microVM. "agentcore_runtime_session_id", # Provider-specific endpoint overrides that flow into the outbound @@ -487,6 +504,7 @@ def _check_banned_params( *, manages_deployments: bool = False, mcp_server_fields_allowed: bool = False, + calls_an_llm: bool = True, ) -> None: """Raise ``ValueError`` if ``body`` carries a banned param without admin opt-in. @@ -504,6 +522,8 @@ def _check_banned_params( for param in _BANNED_REQUEST_BODY_PARAMS: if param not in body: continue + if not calls_an_llm and param in _LLM_CALL_ONLY_BANNED_PARAMS: + continue if general_settings.get("allow_client_side_credentials") is True: # Proxy-wide opt-in: every banned param is permitted, exit # entirely so the rest of the loop doesn't waste work. @@ -691,6 +711,7 @@ def is_request_body_safe( if "model_list" in request_body: raise ValueError("Rejected Request: model_list is not allowed in the request body.") manages_deployments: Final = route_manages_deployments(route) + calls_an_llm: Final = route_calls_an_llm(route) _check_banned_params( request_body, general_settings, @@ -698,15 +719,30 @@ def is_request_body_safe( model, manages_deployments=manages_deployments, mcp_server_fields_allowed=route_manages_mcp_servers(route), + calls_an_llm=calls_an_llm, ) for nested_key in _NESTED_CONFIG_KEYS: nested = _coerce_metadata_to_dict(request_body.get(nested_key)) if nested is not None: - _check_banned_params(nested, general_settings, llm_router, model, manages_deployments=manages_deployments) + _check_banned_params( + nested, + general_settings, + llm_router, + model, + manages_deployments=manages_deployments, + calls_an_llm=calls_an_llm, + ) for metadata_key in _NESTED_METADATA_KEYS: metadata = _coerce_metadata_to_dict(request_body.get(metadata_key)) if metadata is not None: - _check_banned_params(metadata, general_settings, llm_router, model, manages_deployments=manages_deployments) + _check_banned_params( + metadata, + general_settings, + llm_router, + model, + manages_deployments=manages_deployments, + calls_an_llm=calls_an_llm, + ) if any(isinstance(key, str) and key.startswith(f"{metadata_key}[") for key in request_body): _check_banned_params( extract_nested_form_metadata(form_data=request_body, prefix=f"{metadata_key}["), @@ -714,10 +750,18 @@ def is_request_body_safe( llm_router, model, manages_deployments=manages_deployments, + calls_an_llm=calls_an_llm, ) for target in iter_request_fallback_targets(request_body): if isinstance(target, dict): - _check_banned_params(target, general_settings, llm_router, model, manages_deployments=manages_deployments) + _check_banned_params( + target, + general_settings, + llm_router, + model, + manages_deployments=manages_deployments, + calls_an_llm=calls_an_llm, + ) target_model = target.get("model") if isinstance(target_model, str): _reject_url_valued_fallback_target(target_model) @@ -731,7 +775,12 @@ def is_request_body_safe( litellm_params_metadata: Final = _coerce_metadata_to_dict(litellm_params.get("metadata")) if litellm_params_metadata is not None: _check_banned_params( - litellm_params_metadata, general_settings, llm_router, model, manages_deployments=manages_deployments + litellm_params_metadata, + general_settings, + llm_router, + model, + manages_deployments=manages_deployments, + calls_an_llm=calls_an_llm, ) for user_config_params in _user_config_litellm_params(request_body): # pyright: ignore[reportUnknownArgumentType] # request_body is an untyped dict reject_api_key_secret_reference(user_config_params) diff --git a/tests/unit/proxy/auth/test_auth_utils.py b/tests/unit/proxy/auth/test_auth_utils.py index 928c73d4b04..5a7eb32700f 100644 --- a/tests/unit/proxy/auth/test_auth_utils.py +++ b/tests/unit/proxy/auth/test_auth_utils.py @@ -4978,6 +4978,79 @@ class TestIsRequestBodySafeBlocksAwsIdentitySelectors: ) +@pytest.mark.parametrize( + "request_body", + [ + { + "model": "claude-sonnet-4-6", + "model_id": "us.amazon.nova-micro-v1:0", + "messages": [{"role": "user", "content": "hi"}], + }, + {"model": "claude-sonnet-4-6", "extra_body": {"model_id": "us.amazon.nova-micro-v1:0"}}, + ], + ids=["root", "extra_body"], +) +@pytest.mark.parametrize("route", [None, "/v1/chat/completions", "/v1/messages"]) +def test_client_model_id_cannot_swap_the_bedrock_model_behind_an_allowed_model( + request_body: dict[str, object], route: str | None +) -> None: + with pytest.raises(ValueError, match="model_id"): + is_request_body_safe( + request_body=request_body, + general_settings={}, + llm_router=None, + model="claude-sonnet-4-6", + route=route, + ) + + +def test_client_model_id_is_allowed_under_proxy_wide_opt_in() -> None: + assert ( + is_request_body_safe( + request_body={"model": "claude-sonnet-4-6", "model_id": "us.amazon.nova-micro-v1:0"}, + general_settings={"allow_client_side_credentials": True}, + llm_router=None, + model="claude-sonnet-4-6", + ) + is True + ) + + +def test_client_model_id_is_allowed_when_the_deployment_opts_in() -> None: + router: Final = Router( + model_list=[ + { + "model_name": "claude-sonnet-4-6", + "litellm_params": {"model": "bedrock/us.anthropic.claude-sonnet-4-6"}, + "model_info": {"configurable_clientside_auth_params": ["model_id"]}, + } + ] + ) + assert ( + is_request_body_safe( + request_body={"model": "claude-sonnet-4-6", "model_id": "us.amazon.nova-micro-v1:0"}, + general_settings={}, + llm_router=router, + model="claude-sonnet-4-6", + ) + is True + ) + + +@pytest.mark.parametrize("route", ["/health", "/spend/logs/ui", "/model/block", "/model/unblock"]) +def test_admin_routes_keep_model_id_as_a_deployment_id(route: str) -> None: + assert ( + is_request_body_safe( + request_body={"model_id": "6a5a6b1e-deployment-id"}, + general_settings={}, + llm_router=None, + model="", + route=route, + ) + is True + ) + + @pytest.mark.parametrize( "allowed_param, input_value, should_return_true", [