mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
fix(guardrails): judge the function a tool names beside a declaration list or a top-level name
This commit is contained in:
parent
51f8fdf209
commit
dc821b68fc
2 changed files with 70 additions and 5 deletions
|
|
@ -109,7 +109,10 @@ def _request_tool_names(tool: object) -> tuple[str, ...]:
|
|||
server_name: Final = (
|
||||
_non_empty_string(fields.get(server_field)) if fields is not None and server_field is not None else None
|
||||
)
|
||||
return anthropic_tool_names(tool) + tuple(name for name in (custom_name, server_name) if name is not None)
|
||||
function: Final = _object_mapping(fields.get("function")) if fields is not None else None
|
||||
function_name: Final = _non_empty_string(function.get("name")) if function is not None else None
|
||||
extra_names: Final = tuple(name for name in (function_name, custom_name, server_name) if name is not None)
|
||||
return tuple(dict.fromkeys(anthropic_tool_names(tool) + extra_names))
|
||||
|
||||
|
||||
def _namespace_members(tool: object) -> Sequence[object]:
|
||||
|
|
@ -943,10 +946,12 @@ class ToolPermissionGuardrail(CustomGuardrail):
|
|||
else ()
|
||||
)
|
||||
keyed: Final = _keyed_tool_fields(tool)
|
||||
if keyed is not None:
|
||||
return member_targets + tuple(_part_target(keyed, key) for key in _part_keys(keyed))
|
||||
if group is not None:
|
||||
return member_targets
|
||||
part_targets: Final = tuple(_part_target(keyed, key) for key in _part_keys(keyed)) if keyed is not None else ()
|
||||
return member_targets + part_targets + self._own_tool_targets(tool)
|
||||
|
||||
def _own_tool_targets(self, tool: object) -> tuple[_ToolTarget, ...]:
|
||||
if _keyed_tool_fields(tool) is not None or _namespace_members(tool):
|
||||
return ()
|
||||
raw_type: Final = self._get_mapping_value(tool, "type")
|
||||
tool_type: Final = raw_type if isinstance(raw_type, str) else None
|
||||
anthropic_custom: Final = (
|
||||
|
|
@ -1038,6 +1043,8 @@ class ToolPermissionGuardrail(CustomGuardrail):
|
|||
fields: Final = _object_mapping(tool)
|
||||
if fields is None or (group is None and keyed is None):
|
||||
return () if any(map(is_denied, self._get_request_tool_targets(tool))) else (tool,)
|
||||
if any(map(is_denied, self._own_tool_targets(tool))):
|
||||
return ()
|
||||
kept_members: Final = _kept_members(group, is_denied)
|
||||
denied_keys: Final = (
|
||||
frozenset(key for key in _part_keys(keyed) if is_denied(_part_target(keyed, key)))
|
||||
|
|
|
|||
|
|
@ -2344,6 +2344,64 @@ class TestToolPermissionGuardrailAnthropicMessages:
|
|||
|
||||
assert excinfo.value.detail["detection_message"] == f"Tool '{tool['type']}' denied by default action"
|
||||
|
||||
@pytest.mark.asyncio
|
||||
@pytest.mark.parametrize("on_disallowed_action", ["block", "rewrite"])
|
||||
@pytest.mark.parametrize(
|
||||
"tool",
|
||||
[
|
||||
{"type": "function", "function": {"name": "delete_db"}, "functionDeclarations": [{"name": "get_weather"}]},
|
||||
{"type": "function", "function": {"name": "delete_db"}, "function_declarations": [{"name": "get_weather"}]},
|
||||
{"type": "custom", "name": "get_weather", "function": {"name": "delete_db"}},
|
||||
{"type": "mcp", "server_label": "get_weather", "function": {"name": "delete_db"}},
|
||||
{
|
||||
"type": "computer_20251124",
|
||||
"name": "computer",
|
||||
"function": {"name": "delete_db", "parameters": {"display_width_px": 1024, "display_height_px": 768}},
|
||||
},
|
||||
],
|
||||
ids=[
|
||||
"declarations_list",
|
||||
"snake_case_declarations_list",
|
||||
"custom_top_level_name",
|
||||
"mcp_server_label",
|
||||
"computer_top_level_name",
|
||||
],
|
||||
)
|
||||
async def test_pre_call_judges_the_function_a_tool_runs_behind_an_allowed_name(
|
||||
self, tool: dict[str, object], on_disallowed_action: Literal["block", "rewrite"]
|
||||
) -> None:
|
||||
guardrail = self._pre_call(
|
||||
[{"id": "allow_known", "tool_name": r"^(get_weather|computer)$", "decision": "allow"}],
|
||||
on_disallowed_action,
|
||||
default_action="deny",
|
||||
)
|
||||
weather = {"type": "function", "function": {"name": "get_weather"}}
|
||||
data = {"model": "gemini-3", "messages": [{"role": "user", "content": "hi"}], "tools": [weather, tool]}
|
||||
|
||||
if on_disallowed_action == "block":
|
||||
with pytest.raises(HTTPException) as excinfo:
|
||||
await self._run_pre_call(guardrail, data, "acompletion")
|
||||
assert excinfo.value.detail["detection_message"] == "Tool 'delete_db' denied by default action"
|
||||
return
|
||||
|
||||
result = await self._run_pre_call(guardrail, data, "acompletion")
|
||||
|
||||
assert result["tools"] == [weather]
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_pre_call_allows_a_custom_tool_by_its_function_name(self) -> None:
|
||||
guardrail = self._pre_call(
|
||||
[{"id": "allow_weather", "tool_name": r"^get_weather$", "decision": "allow"}],
|
||||
"block",
|
||||
default_action="deny",
|
||||
)
|
||||
tool = {"type": "custom", "function": {"name": "get_weather"}}
|
||||
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_a_tool_whose_type_is_not_a_string_by_its_name(self) -> None:
|
||||
guardrail = self._pre_call(
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue