From 3927751bb10df31ea7e37798c77444989e392fec Mon Sep 17 00:00:00 2001 From: mateo-berri <277851410+mateo-berri@users.noreply.github.com> Date: Fri, 30 Oct 2026 09:01:10 -0700 Subject: [PATCH] fix(guardrails): keep judging Anthropic client tools and type-named server tools by their name --- .../guardrail_hooks/tool_permission.py | 16 ++++- .../guardrail_hooks/test_tool_permission.py | 58 +++++++++++++++++++ 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/guardrails/guardrail_hooks/tool_permission.py b/litellm/proxy/guardrails/guardrail_hooks/tool_permission.py index 42c73921e58..832638c77a0 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/tool_permission.py +++ b/litellm/proxy/guardrails/guardrail_hooks/tool_permission.py @@ -61,12 +61,22 @@ _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_") +_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) _REQUEST_DATA: Final = TypeAdapter(object) _ToolTarget: TypeAlias = tuple[str | None, str | None] +def _name_identifies_tool(name: str, tool_type: str) -> bool: + return ( + tool_type in _NAME_IDENTIFIED_TYPES + or tool_type.startswith(_CLIENT_RUN_TYPE_PREFIXES) + or _TYPE_VERSION_SUFFIX.sub("", tool_type) == name + ) + + def _object_mapping(value: object) -> Mapping[str, object] | None: """Return ``value`` as an opaque mapping when it is a dict.""" return value if isinstance(value, dict) else None @@ -944,9 +954,11 @@ class ToolPermissionGuardrail(CustomGuardrail): ) normalized_type: Final = "function" if tool_type is None or anthropic_custom else tool_type names: Final = _request_tool_names(tool) - type_target: Final = ((None, normalized_type),) if normalized_type not in _NAME_IDENTIFIED_TYPES else () + named_targets: Final = tuple((name, normalized_type) for name in names) + if names and all(_name_identifies_tool(name, normalized_type) for name in names): + return named_targets if names: - return tuple((name, normalized_type) for name in names) + type_target + return (*named_targets, (None, normalized_type)) return ((None, tool_type),) if tool_type is not None else () def _get_legacy_function_name(self, function: object) -> str | None: 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 032641e9c5c..a54de960932 100644 --- a/tests/unit/proxy/guardrails/guardrail_hooks/test_tool_permission.py +++ b/tests/unit/proxy/guardrails/guardrail_hooks/test_tool_permission.py @@ -2271,6 +2271,64 @@ class TestToolPermissionGuardrailAnthropicMessages: assert result["tools"] == [weather] + @pytest.mark.asyncio + @pytest.mark.parametrize( + "rules, tool", + [ + ( + [{"id": "allow_bash", "tool_name": r"^bash$", "decision": "allow"}], + {"type": "bash_20250124", "name": "bash"}, + ), + ( + [{"id": "allow_edit", "tool_name": r"^str_replace_based_edit_tool$", "decision": "allow"}], + {"type": "text_editor_20250728", "name": "str_replace_based_edit_tool"}, + ), + ( + [{"id": "allow_search", "tool_name": r"^web_search$", "decision": "allow"}], + {"type": "web_search_20250305", "name": "web_search"}, + ), + ( + [ + { + "id": "allow_search", + "tool_name": r"^web_search$", + "tool_type": r"^web_search_20250305$", + "decision": "allow", + } + ], + {"type": "web_search_20250305", "name": "web_search"}, + ), + ], + ids=["client_tool_by_name", "client_tool_with_its_own_name", "server_tool_by_name", "name_and_type_rule"], + ) + async def test_pre_call_allows_an_anthropic_defined_tool_that_an_allowlist_names( + self, rules: list[dict[str, object]], tool: dict[str, object] + ) -> None: + guardrail = self._pre_call(rules, "block", default_action="deny") + data = {"model": "claude-sonnet-4-5", "messages": [{"role": "user", "content": "hi"}], "tools": [tool]} + + result = await self._run_pre_call(guardrail, data, "acompletion") + + 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: + 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"}], + } + + with pytest.raises(HTTPException) as excinfo: + await self._run_pre_call(guardrail, data, "anthropic_messages") + + assert excinfo.value.detail["detection_message"] == "Tool 'web_search_20250305' 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: guardrail = self._pre_call(