Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
import re
from collections.abc import AsyncIterable, Mapping, Sequence
from datetime import datetime
from typing import TYPE_CHECKING, Any, Final, Literal, Optional
from typing import TYPE_CHECKING, Any, Final, Literal, Optional, TypeAlias
from urllib.parse import urlparse

import httpx
from fastapi import HTTPException
from pydantic import BaseModel, ConfigDict, ValidationError, field_validator

from litellm._logging import verbose_proxy_logger
from litellm._uuid import uuid
Expand All @@ -40,12 +41,55 @@
from litellm.types.utils import (
CallTypes,
CallTypesLiteral,
ChatCompletionDeltaCustomToolCall,
ChatCompletionDeltaToolCall,
ChatCompletionMessageCustomToolCall,
ChatCompletionMessageToolCall,
ChatCompletionToolCallChunk,
Choices,
GenericGuardrailAPIInputs,
ModelResponse,
ModelResponseStream,
)

ToolCallLike: TypeAlias = (
ChatCompletionMessageToolCall
| ChatCompletionDeltaToolCall
| ChatCompletionMessageCustomToolCall
| ChatCompletionDeltaCustomToolCall
| ChatCompletionToolCallChunk
)


class _ToolCallFunctionSlice(BaseModel):
model_config = ConfigDict(from_attributes=True, extra="ignore")

name: str | None = None
arguments: str | None = None

@field_validator("name", "arguments", mode="before")
@classmethod
def _coerce_to_scannable_text(cls, value: object) -> str | None:
"""Accept any shape a client can put here and render it scannable.

The OpenAI request path forwards client-supplied ``tool_calls`` verbatim, so a
client can post a dict for ``arguments`` or a non-string for ``name``. Rejecting
either would fail validation for the whole slice, which reads as an unscannable
tool call and skips it silently -- the one outcome a scanner must never have.
A caller could otherwise suppress the scan on a tool call just by sending
``"name": 123``.
"""
if value is None or isinstance(value, str):
return value
return json.dumps(value) if isinstance(value, (dict, list)) else str(value)


class _ToolCallSlice(BaseModel):
model_config = ConfigDict(from_attributes=True, extra="ignore")

function: _ToolCallFunctionSlice | None = None


if TYPE_CHECKING:
from litellm.litellm_core_utils.litellm_logging import Logging as LiteLLMLoggingObj
from litellm.types.proxy.guardrails.guardrail_hooks.base import GuardrailConfigModel
Expand Down Expand Up @@ -643,19 +687,11 @@ def _build_error_detail(
self,
scan_result: Mapping[str, object],
is_response: bool = False,
also_hide: str | None = None,
) -> Mapping[str, Mapping[str, object]]:
"""Build enhanced error detail with scan information.

``also_hide`` names one more scan field to withhold, for the caller that knows
its AIRS verdict carries model-generated content under a key that is normally
caller input.
"""
"""Build enhanced error detail with scan information."""
action_type: Final = "Response" if is_response else "Prompt"
code_suffix: Final = "_response_blocked" if is_response else "_blocked"

hidden_fields: Final = self._CLIENT_HIDDEN_SCAN_FIELDS.union(() if also_hide is None else (also_hide,))

category: Final = scan_result.get("category", "unknown")
default_msg: Final = f"{action_type} blocked by PANW Prisma AI Security policy (Category: {category})"

Expand All @@ -675,7 +711,7 @@ def _build_error_detail(
**{
key: value
for key, value in scan_result.items()
if not key.startswith("_") and key not in hidden_fields
if not key.startswith("_") and key not in self._CLIENT_HIDDEN_SCAN_FIELDS
},
"message": error_msg,
"type": "guardrail_violation",
Expand Down Expand Up @@ -1407,55 +1443,25 @@ async def _scan_tool_calls_for_guardrail(
request_data: dict,
start_time: datetime,
) -> None:
"""Scan tool call arguments with allow/block/mask treatment (in-place modification).

Each tool call is sent as a ``tool_event`` using the canonical PANW
AIRS schema::
"""Scan tool calls with allow/block/mask treatment (in-place modification).

{
"metadata": {
"ecosystem": "openai",
"method": "tools/call",
"server_name": "litellm",
"tool_invoked": "<function_name>",
},
"input": "<args_json>", # optional, omitted for empty args
}

Empty-arg invocations are still reported (without ``input``) so AIRS
can enforce tool-name-based policies.
Tool name and arguments go out as plain prompt/response text, newline separated:
the AIRS ``tool_event`` schema only accepts ``ecosystem: "mcp"``, which
OpenAI-format tool calls are not. A name-only call is still scanned so
tool-name policies keep firing on empty arguments.
"""
for tool_call in tool_calls:
# --- extract tool_name and args_text --------------------------
tool_name: str | None = None
args_text: str | None = None

if hasattr(tool_call, "function") and hasattr(tool_call.function, "arguments"):
args_text = tool_call.function.arguments
tool_name = getattr(tool_call.function, "name", None)
elif isinstance(tool_call, dict):
func = tool_call.get("function", {})
if isinstance(func, dict):
args_text = func.get("arguments")
tool_name = func.get("name")

# --- build tool_event payload (canonical PANW schema) -----------
tool_event: dict[str, object] = {
"metadata": {
"ecosystem": "openai",
"method": "tools/call",
"server_name": "litellm",
"tool_invoked": tool_name or "unknown",
},
}
if args_text and args_text.strip():
tool_event["input"] = args_text
tool_name, args_text = self._get_tool_call_function(tool_call)
scanned_args = args_text if args_text and args_text.strip() else None
scan_text = "\n".join(part for part in (tool_name, scanned_args) if part)
if not scan_text.strip():
continue

scan_result = await self._call_panw_api(
is_response=False, # tool_event is always request-side in AIRS schema
content=scan_text,
is_response=is_response,
metadata=metadata,
call_id=call_id,
tool_event=tool_event,
)

if scan_result.get("_is_transient") or scan_result.get("_always_block"):
Expand All @@ -1467,44 +1473,68 @@ async def _scan_tool_calls_for_guardrail(
event_type=event_type,
is_response=is_response,
)
continue # fallback_on_error="allow" — leave args unchanged
continue

self._record_scan_id(request_data, scan_result)

action = scan_result.get("action", "block")
# Always is_response=False for masked data lookup because
# tool_event scans are request-side in AIRS schema and
# AIRS returns prompt_masked_data for them.
masked_text = self._get_masked_text(scan_result, is_response=False)
masked_args = self._masked_tool_call_arguments(
self._get_masked_text(scan_result, is_response=is_response),
scanned_name=bool(tool_name),
scanned_args=scanned_args,
)

if action == "allow":
if masked_text:
self._set_tool_call_arguments(tool_call, masked_text)
elif masked_text and (
if masked_args:
self._set_tool_call_arguments(tool_call, masked_args)
elif masked_args and (
(is_response and self.mask_response_content) or (not is_response and self.mask_request_content)
):
self._set_tool_call_arguments(tool_call, masked_text)
self._set_tool_call_arguments(tool_call, masked_args)
else:
# tool_event scans are request-side in the AIRS schema, so AIRS returns
# the model's own tool arguments under prompt_masked_data. On a
# response-side block that is generated content, not caller input, and
# the class-level default only withholds response_masked_data — which is
# empty on this path. Withhold it explicitly so the 400 does not become
# the content channel this branch declined to deliver.
error_detail = self._build_error_detail(
scan_result,
is_response=is_response,
also_hide="prompt_masked_data" if is_response else None,
)
# Tool calls now go out as ordinary prompt/response text, so a
# response-side scan reports the model's arguments under
# response_masked_data, which _CLIENT_HIDDEN_SCAN_FIELDS already
# withholds. prompt_masked_data is the caller's own input again and
# must keep reaching them -- it is one of the fields LIT-5638 asks for.
error_detail = self._build_error_detail(scan_result, is_response=is_response)
raise HTTPException(status_code=400, detail=error_detail)

@staticmethod
def _set_tool_call_arguments(tool_call, masked_text: str) -> None:
"""Set masked text on a tool call's function arguments, handling both object and dict forms."""
if hasattr(tool_call, "function"):
tool_call.function.arguments = masked_text
elif isinstance(tool_call, dict) and isinstance(tool_call.get("function"), dict):
def _masked_tool_call_arguments(
masked_text: str | None,
*,
scanned_name: bool,
scanned_args: str | None,
) -> str | None:
"""Recover the arguments slice of a masked scan, or None when it cannot be applied."""
if masked_text is None or scanned_args is None:
return None
if not scanned_name:
return masked_text
_, separator, masked_args = masked_text.partition("\n")
return masked_args if separator else None

@staticmethod
def _get_tool_call_function(tool_call: ToolCallLike) -> tuple[str | None, str | None]:
"""Read a tool call's function name and arguments; (None, None) for non-function shapes."""
try:
parsed: Final = _ToolCallSlice.model_validate(tool_call, from_attributes=True)
except ValidationError:
return (None, None)
if parsed.function is None:
return (None, None)
return (parsed.function.name, parsed.function.arguments)
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved

@staticmethod
def _set_tool_call_arguments(tool_call: ToolCallLike, masked_text: str) -> None:
"""Set masked text on the function arguments of a call that _get_tool_call_function accepted."""
if isinstance(tool_call, dict):
tool_call["function"]["arguments"] = masked_text
return
if isinstance(tool_call, ChatCompletionMessageCustomToolCall | ChatCompletionDeltaCustomToolCall):
return
tool_call.function.arguments = masked_text

@staticmethod
def _is_anthropic_request(
Expand Down
Loading
Loading