From 51f8fdf2090681ebd456c9c0b383c1fbf030ea9c Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 12:05:29 -0700 Subject: [PATCH] fix(guardrails): judge a named tool by its name alone only when the name is its type's own --- .../guardrail_hooks/tool_permission.py | 8 +++-- .../guardrail_hooks/test_tool_permission.py | 31 ++++++++++++++----- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/tool_permission.py b/litellm/proxy/guardrails/guardrail_hooks/tool_permission.py index 832638c77a0..f463f8069f6 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/tool_permission.py +++ b/litellm/proxy/guardrails/guardrail_hooks/tool_permission.py @@ -34,6 +34,7 @@ from litellm.proxy.guardrails.anthropic_sse import ( ) from litellm.responses.additional_tools import ADDITIONAL_TOOLS_INPUT_ITEM_TYPE from litellm.types.guardrails import GuardrailEventHooks, LitellmParams +from litellm.types.llms.anthropic import ANTHROPIC_HOSTED_TOOLS from litellm.types.proxy.guardrails.guardrail_hooks.tool_permission import ( PermissionError, ToolPermissionRule, @@ -61,7 +62,7 @@ _FUNCTION_CALLING_CONFIG_KEYS: Final = ("functionCallingConfig", "function_calli _ALLOWED_FUNCTION_NAMES_KEYS: Final = ("allowedFunctionNames", "allowed_function_names") _FORCED_FUNCTION_CALLING_MODES: Final = frozenset({"ANY", "VALIDATED"}) _NAME_IDENTIFIED_TYPES: Final = frozenset({"function", "custom", "namespace", *_HOSTED_SERVER_NAME_FIELDS}) -_CLIENT_RUN_TYPE_PREFIXES: Final = ("bash_", "text_editor_", "computer_", "memory_") +_TEXT_EDITOR_TOOL_NAMES: Final = frozenset({"str_replace_editor", "str_replace_based_edit_tool"}) _TYPE_VERSION_SUFFIX: Final = re.compile(r"_\d{8}$") _NAMED_CHOICE_TYPES: Final = frozenset({"tool", "function", "custom", "allowed_tools", "any", "auto", "none"}) _DECODED_ARGUMENTS: Final = TypeAdapter(object) @@ -70,10 +71,11 @@ _ToolTarget: TypeAlias = tuple[str | None, str | None] def _name_identifies_tool(name: str, tool_type: str) -> bool: + stem: Final = _TYPE_VERSION_SUFFIX.sub("", tool_type) return ( tool_type in _NAME_IDENTIFIED_TYPES - or tool_type.startswith(_CLIENT_RUN_TYPE_PREFIXES) - or _TYPE_VERSION_SUFFIX.sub("", tool_type) == name + or name == stem + or (stem == ANTHROPIC_HOSTED_TOOLS.TEXT_EDITOR.value and name in _TEXT_EDITOR_TOOL_NAMES) ) diff --git a/tests/unit/proxy/guardrails/guardrail_hooks/test_tool_permission.py b/tests/unit/proxy/guardrails/guardrail_hooks/test_tool_permission.py index a54de960932..98f6e2025f5 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_tool_permission.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_tool_permission.py @@ -2312,22 +2312,37 @@ class TestToolPermissionGuardrailAnthropicMessages: assert result["tools"] == [tool] @pytest.mark.asyncio - async def test_pre_call_blocks_an_anthropic_server_tool_whose_name_is_not_its_own(self) -> None: + @pytest.mark.parametrize( + "tool, call_type", + [ + ({"type": "web_search_20250305", "name": "get_weather"}, "anthropic_messages"), + ( + { + "type": "computer_20251124", + "name": "get_weather", + "function": {"parameters": {"display_width_px": 1024, "display_height_px": 768}}, + }, + "acompletion", + ), + ({"type": "computer_use", "name": "get_weather", "environment": "browser"}, "acompletion"), + ({"type": "text_editor_20250728", "name": "get_weather"}, "anthropic_messages"), + ], + ids=["server_tool", "anthropic_computer_tool", "gemini_computer_use", "text_editor"], + ) + async def test_pre_call_blocks_a_provider_defined_tool_whose_name_is_not_its_own( + self, tool: dict[str, object], call_type: CallTypesLiteral + ) -> None: guardrail = self._pre_call( [{"id": "allow_weather", "tool_name": r"^get_weather$", "decision": "allow"}], "block", default_action="deny", ) - data = { - "model": "gemini-3", - "messages": [{"role": "user", "content": "hi"}], - "tools": [{"type": "web_search_20250305", "name": "get_weather"}], - } + data = {"model": "gemini-3", "messages": [{"role": "user", "content": "hi"}], "tools": [tool]} with pytest.raises(HTTPException) as excinfo: - await self._run_pre_call(guardrail, data, "anthropic_messages") + await self._run_pre_call(guardrail, data, call_type) - assert excinfo.value.detail["detection_message"] == "Tool 'web_search_20250305' denied by default action" + assert excinfo.value.detail["detection_message"] == f"Tool '{tool['type']}' denied by default action" @pytest.mark.asyncio async def test_pre_call_blocks_a_tool_whose_type_is_not_a_string_by_its_name(self) -> None: