mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(guardrails): judge a named tool by its name alone only when the name is its type's own
This commit is contained in:
parent
3927751bb1
commit
51f8fdf209
2 changed files with 28 additions and 11 deletions
|
|
@ -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)
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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:
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue