From 6a9beed59640bca1f39179c44e169599a6bfa663 Mon Sep 17 00:00:00 2001 From: ElDjeee Date: Fri, 3 Jul 2026 17:22:50 +0200 Subject: [PATCH] fix(proxy): enforce allowed_tools on built-in web search tools The key/team allowed_tools allowlist only extracted function tool names, so a caller restricted by it could still reach a provider's built-in web search via tools=[{"type":"web_search"}] or web_search_options and bill the per-call search, bypassing the allowlist. extract_request_tool_names now surfaces web_search / web_search_premium tool types and the web_search_options shorthand as tool names, so check_tools_allowlist denies them with 403 tool_access_denied unless allowed_tools lists them --- .../chat/guardrail_translation/handler.py | 19 ++++- .../proxy/test_tools_allowlist_enforcement.py | 70 +++++++++++++++++-- 2 files changed, 81 insertions(+), 8 deletions(-) diff --git a/litellm/llms/openai/chat/guardrail_translation/handler.py b/litellm/llms/openai/chat/guardrail_translation/handler.py index d41c8557d72..f9b9be3b91c 100644 --- a/litellm/llms/openai/chat/guardrail_translation/handler.py +++ b/litellm/llms/openai/chat/guardrail_translation/handler.py @@ -213,16 +213,31 @@ class OpenAIChatCompletionsHandler(BaseTranslation): return data def extract_request_tool_names(self, data: dict) -> list[str]: - """Extract tool names from OpenAI chat completions request (tools[].function.name, functions[].name).""" + """Extract tool names from an OpenAI chat completions request. + + Covers function tools (``tools[].function.name`` and ``functions[].name``), + built-in tool types such as ``web_search`` / ``web_search_premium`` (whose + name is the type itself), and the ``web_search_options`` shorthand, so the + key/team tool allowlist enforces built-in tools and not just functions. + """ names: Final[list[str]] = [] for tool in data.get("tools") or []: - if isinstance(tool, dict) and tool.get("type") == "function": + if not isinstance(tool, dict): + continue + tool_type = tool.get("type") + if tool_type == "function": fn = tool.get("function") if isinstance(fn, dict) and fn.get("name"): names.append(str(fn["name"])) + elif tool_type in ("web_search", "web_search_premium"): + names.append(str(tool_type)) for fn in data.get("functions") or []: if isinstance(fn, dict) and fn.get("name"): names.append(str(fn["name"])) + web_search_options = data.get("web_search_options") + if web_search_options is not None: + premium = isinstance(web_search_options, dict) and web_search_options.get("premium") is True + names.append("web_search_premium" if premium else "web_search") return names def _extract_inputs( diff --git a/tests/test_litellm/proxy/test_tools_allowlist_enforcement.py b/tests/test_litellm/proxy/test_tools_allowlist_enforcement.py index 3e9c7c14b95..69d7e00bca7 100644 --- a/tests/test_litellm/proxy/test_tools_allowlist_enforcement.py +++ b/tests/test_litellm/proxy/test_tools_allowlist_enforcement.py @@ -52,15 +52,28 @@ class TestExtractRequestToolNames: "run_sql", ] + def test_openai_chat_builtin_web_search_tools(self): + data = {"tools": [{"type": "web_search"}, {"type": "web_search_premium"}]} + assert extract_request_tool_names("/v1/chat/completions", data) == [ + "web_search", + "web_search_premium", + ] + + def test_openai_chat_web_search_options_shorthand(self): + data = {"web_search_options": {}} + assert extract_request_tool_names("/v1/chat/completions", data) == ["web_search"] + + def test_openai_chat_web_search_options_premium(self): + data = {"web_search_options": {"premium": True}} + assert extract_request_tool_names("/v1/chat/completions", data) == ["web_search_premium"] + def test_openai_responses_function_tools(self): data = { "tools": [ {"type": "function", "name": "get_current_weather", "description": "x"}, ] } - assert extract_request_tool_names("/v1/responses", data) == [ - "get_current_weather" - ] + assert extract_request_tool_names("/v1/responses", data) == ["get_current_weather"] def test_openai_responses_mcp_tools(self): data = { @@ -129,9 +142,7 @@ class TestExtractRequestToolNames: }, ] } - assert extract_request_tool_names("/generate_content", data) == [ - "schedule_meeting" - ] + assert extract_request_tool_names("/generate_content", data) == ["schedule_meeting"] def test_mcp_call_tool_name(self): data = {"name": "my_tool", "arguments": {}} @@ -227,6 +238,53 @@ class TestCheckToolsAllowlist: assert exc_info.value.type == ProxyErrorTypes.tool_access_denied assert "restricted_tool" in str(exc_info.value.message) + @pytest.mark.asyncio + @pytest.mark.parametrize( + "body", + [ + {"tools": [{"type": "web_search"}]}, + {"tools": [{"type": "web_search_premium"}]}, + {"web_search_options": {}}, + ], + ) + async def test_builtin_web_search_blocked_when_not_allowed(self, body): + """Regression: built-in web search must not bypass the tool allowlist.""" + token = _token(metadata={"allowed_tools": ["get_weather"]}) + with pytest.raises(ProxyException) as exc_info: + await check_tools_allowlist( + request_body=body, + valid_token=token, + team_object=None, + route="/v1/chat/completions", + ) + assert exc_info.value.type == ProxyErrorTypes.tool_access_denied + assert "web_search" in str(exc_info.value.message) + + @pytest.mark.asyncio + async def test_builtin_web_search_allowed_when_listed(self): + token = _token(metadata={"allowed_tools": ["web_search"]}) + await check_tools_allowlist( + request_body={"web_search_options": {}}, + valid_token=token, + team_object=None, + route="/v1/chat/completions", + ) + + @pytest.mark.asyncio + async def test_premium_web_search_not_granted_by_standard_allowlist(self): + """Regression: allowing standard web_search must not grant the premium connector + via web_search_options={"premium": True}.""" + token = _token(metadata={"allowed_tools": ["web_search"]}) + with pytest.raises(ProxyException) as exc_info: + await check_tools_allowlist( + request_body={"web_search_options": {"premium": True}}, + valid_token=token, + team_object=None, + route="/v1/chat/completions", + ) + assert exc_info.value.type == ProxyErrorTypes.tool_access_denied + assert "web_search_premium" in str(exc_info.value.message) + @pytest.mark.asyncio async def test_team_allowlist_used_when_key_empty(self): token = _token(