mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(mistral): stop extra_body from overriding sanitized Conversations fields
The shared HTTP handler merges extra_body into the request after
transform_request, so a caller allowlisted only for standard web_search could
send web_search_options plus extra_body={"tools":[{"type":"web_search_premium"}]}
and replace the sanitized tools list on its way to /v1/conversations, reaching
the premium connector past the tool allowlist. Provider configs can now declare
reserved_request_body_keys that extra_body may not override (empty by default,
so the escape hatch is unchanged for every other provider), and the Mistral
Conversations config reserves tools, store, inputs and model; other extra_body
keys such as completion_args still pass through
This commit is contained in:
parent
6a9beed596
commit
e86f74be96
4 changed files with 84 additions and 1 deletions
|
|
@ -420,6 +420,16 @@ class BaseConfig(ABC):
|
|||
"""
|
||||
return True
|
||||
|
||||
@property
|
||||
def reserved_request_body_keys(self) -> frozenset[str]:
|
||||
"""
|
||||
Request-body keys that ``extra_body`` may not override after
|
||||
``transform_request`` has sanitized them (e.g. an allowlisted ``tools``
|
||||
list). Empty by default so ``extra_body`` stays a full escape hatch for
|
||||
providers that do not sanitize their body.
|
||||
"""
|
||||
return frozenset()
|
||||
|
||||
def post_stream_processing(self, stream: Any) -> Any:
|
||||
"""Hook for providers to post-process streaming responses. Default: pass-through."""
|
||||
return stream
|
||||
|
|
|
|||
|
|
@ -546,7 +546,8 @@ class BaseLLMHTTPHandler:
|
|||
)
|
||||
|
||||
if extra_body is not None:
|
||||
data = {**data, **extra_body}
|
||||
reserved_keys = provider_config.reserved_request_body_keys
|
||||
data = {**data, **{k: v for k, v in extra_body.items() if k not in reserved_keys}}
|
||||
|
||||
headers, signed_json_body = provider_config.sign_request(
|
||||
headers=headers,
|
||||
|
|
|
|||
|
|
@ -74,6 +74,13 @@ def _to_annotation(chunk: MistralConversationContentChunk) -> ChatCompletionAnno
|
|||
|
||||
|
||||
class MistralConversationsConfig(MistralConfig):
|
||||
@property
|
||||
def reserved_request_body_keys(self) -> frozenset[str]:
|
||||
"""Sanitized Conversations fields extra_body may not clobber: ``tools`` is
|
||||
allowlist-checked, ``store`` is pinned to False, and ``inputs`` / ``model``
|
||||
are built from the authenticated request."""
|
||||
return frozenset({"tools", "store", "inputs", "model"})
|
||||
|
||||
def should_fake_stream(
|
||||
self,
|
||||
model: Optional[str],
|
||||
|
|
|
|||
|
|
@ -135,6 +135,40 @@ def test_transform_request_drops_other_builtin_connectors():
|
|||
assert {"type": "code_interpreter"} not in body["tools"]
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"request_params",
|
||||
[
|
||||
{"tools": [{"type": "web_search"}]},
|
||||
{"tools": [{"type": "web_search_premium"}]},
|
||||
{"web_search_options": {}},
|
||||
{"web_search_options": {"premium": True}},
|
||||
{"web_search_options": {"premium": "yes"}},
|
||||
{"web_search_options": {}, "tools": [{"type": "code_interpreter"}]},
|
||||
{"web_search_options": {}, "tools": [{"type": "WEB_SEARCH"}]},
|
||||
{"tools": [{"type": "web_search"}, {"type": "function", "function": {"name": "f"}}]},
|
||||
{"web_search_options": {"premium": True}, "tools": [{"type": "web_search"}]},
|
||||
],
|
||||
)
|
||||
def test_wire_connectors_never_exceed_allowlist_checked_names(request_params):
|
||||
"""Invariant behind the tool allowlist: every non-function connector the
|
||||
Conversations transform puts on the wire must have been named by
|
||||
extract_request_tool_names for the same request, so the key/team allowlist
|
||||
always sees what will actually be sent. Drift between the extractor and the
|
||||
transform reopens a bypass; this pins them together."""
|
||||
from litellm.proxy.guardrails.tool_name_extraction import extract_request_tool_names
|
||||
|
||||
checked_names = set(extract_request_tool_names("/v1/chat/completions", request_params))
|
||||
wire_body = MistralConversationsConfig().transform_request(
|
||||
model="mistral-medium-latest",
|
||||
messages=[{"role": "user", "content": "hi"}],
|
||||
optional_params=dict(request_params),
|
||||
litellm_params={},
|
||||
headers={},
|
||||
)
|
||||
wire_connectors = {str(tool.get("type")) for tool in wire_body["tools"] if tool.get("type") != "function"}
|
||||
assert wire_connectors <= checked_names
|
||||
|
||||
|
||||
def test_transform_request_maps_random_seed_from_extra_body():
|
||||
cfg = MistralConversationsConfig()
|
||||
body = cfg.transform_request(
|
||||
|
|
@ -299,6 +333,37 @@ async def test_completion_routes_web_search_to_conversations(sync_mode, web_sear
|
|||
assert response.choices[0].message.annotations[0]["url_citation"]["url"] == ("https://uefa.com/euro2024")
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_extra_body_cannot_override_sanitized_tools(respx_mock):
|
||||
"""Regression: extra_body must not clobber the allowlist-checked tools list
|
||||
(or store/inputs/model) after transform_request sanitized them."""
|
||||
litellm.disable_aiohttp_transport = True
|
||||
|
||||
conversations_route = respx_mock.post("https://api.mistral.ai/v1/conversations").respond(
|
||||
json=CONVERSATIONS_RESPONSE
|
||||
)
|
||||
|
||||
litellm.completion(
|
||||
model="mistral/mistral-medium-latest",
|
||||
messages=[{"role": "user", "content": "Who won the last Euro?"}],
|
||||
web_search_options={},
|
||||
extra_body={
|
||||
"tools": [{"type": "web_search_premium"}],
|
||||
"store": True,
|
||||
"model": "mistral-large-latest",
|
||||
"completion_args": {"temperature": 0.9},
|
||||
},
|
||||
)
|
||||
|
||||
import json
|
||||
|
||||
sent = json.loads(conversations_route.calls[0].request.content)
|
||||
assert sent["tools"] == [{"type": "web_search"}]
|
||||
assert sent["store"] is False
|
||||
assert sent["model"] == "mistral-medium-latest"
|
||||
assert sent["completion_args"] == {"temperature": 0.9}
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_completion_without_web_search_uses_chat_completions(respx_mock):
|
||||
litellm.disable_aiohttp_transport = True
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue