From 0d2d7b6e99c122db9908f04f7ef3030edc51bf0d Mon Sep 17 00:00:00 2001 From: yucheng Date: Wed, 23 Sep 2026 19:11:25 +0000 Subject: [PATCH] fix(guardrails): refuse serialized tool output attachments and scoped media in the bedrock guardrail During-call requests could smuggle attachments past the guardrail two ways: a tool message's JSON-serialized output list was never inspected, and video_url/input_audio payloads in messages dropped by latest-role scoping evaded the unscoped refusal pass. Payload-less attachment shells now fall through instead of refusing, matching the scoped builder, and payload-less file/media parts no longer reach non-bedrock guardrail inputs. Co-authored-by: feng.tsai Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../chat/guardrail_translation/handler.py | 168 +++++++++++------- .../guardrail_translation/handler.py | 4 +- .../guardrail_hooks/bedrock_guardrails.py | 55 ++++-- .../observability/test_guardrail_effects.py | 158 ++++++++++++++++ .../test_openai_chat_guardrail_handler.py | 27 +++ ...test_openai_responses_guardrail_handler.py | 26 +++ .../test_bedrock_guardrails.py | 166 +++++++++++++++++ 7 files changed, 528 insertions(+), 76 deletions(-) diff --git a/litellm/llms/openai/chat/guardrail_translation/handler.py b/litellm/llms/openai/chat/guardrail_translation/handler.py index c25528e0905..e7a69fa2222 100644 --- a/litellm/llms/openai/chat/guardrail_translation/handler.py +++ b/litellm/llms/openai/chat/guardrail_translation/handler.py @@ -86,9 +86,11 @@ def _image_part_ref(content_item: Mapping[str, object], scan_attachments: bool) return None -def _file_part_ref(content_item: Mapping[str, object]) -> str: +def _file_part_ref(content_item: Mapping[str, object]) -> str | None: """Identify a ``type == "file"`` part by whichever reference field it carries.""" file_part: Final = content_item.get("file") + if not file_part: + return None if not isinstance(file_part, dict): return "file" return next( @@ -101,14 +103,31 @@ def _file_part_ref(content_item: Mapping[str, object]) -> str: ) -def _media_part_ref(content_item: Mapping[str, object]) -> str: +def _media_part_ref(content_item: Mapping[str, object]) -> str | None: """Identify a ``video_url`` or ``input_audio`` part by its nested url when present.""" part_type: Final = cast("str", content_item.get("type")) # cast-ok: caller gates on the two media types media: Final = content_item.get(part_type) + if not media: + return None media_url: Final = media.get("url") if isinstance(media, dict) else None return media_url if isinstance(media_url, str) and media_url else part_type +def _attachment_part_refs(content_item: Mapping[str, object], scan_attachments: bool) -> tuple[str | None, str | None]: + """The (image, file) reference a content part contributes to the guardrail inputs.""" + image_ref: Final = ( + _image_part_ref(content_item, scan_attachments) if content_item.get("type") == "image_url" else None + ) + if not scan_attachments: + return image_ref, None + part_type: Final = content_item.get("type") + if part_type == "file": + return image_ref, _file_part_ref(content_item) + if part_type in ("video_url", "input_audio"): + return image_ref, _media_part_ref(content_item) + return image_ref, None + + class OpenAIChatCompletionsHandler(BaseTranslation): """ Handler for processing OpenAI chat completions messages with guardrails. @@ -383,17 +402,11 @@ class OpenAIChatCompletionsHandler(BaseTranslation): text_task_mappings.append((msg_idx, int(content_idx))) # Extract images (image_url) - if content_item.get("type") == "image_url": - image_ref = _image_part_ref(content_item, scan_attachments) - if image_ref is not None: - images_to_check.append(image_ref) - - # Extract non-image file attachments - if scan_attachments and content_item.get("type") == "file": - files_to_check.append(_file_part_ref(content_item)) - - if scan_attachments and content_item.get("type") in ("video_url", "input_audio"): - files_to_check.append(_media_part_ref(content_item)) + image_ref, file_ref = _attachment_part_refs(content_item, scan_attachments) + if image_ref is not None: + images_to_check.append(image_ref) + if file_ref is not None: + files_to_check.append(file_ref) # Extract tool calls (typically in assistant messages) tool_calls: Final = message.get("tool_calls", None) @@ -509,60 +522,87 @@ class OpenAIChatCompletionsHandler(BaseTranslation): # Step 2: Apply guardrail to all texts and tool calls in batch if texts_to_check or tool_calls_to_check: - # Use the real request_data if provided (proxy path), otherwise - # create a standalone dict (SDK / direct-call path). - if request_data is None: - request_data = {"response": response} - else: - if "response" not in request_data: - request_data["response"] = response - - self.merge_user_api_key_metadata_into_request(request_data, user_api_key_dict) - - inputs: Final = GenericGuardrailAPIInputs(texts=texts_to_check) - if images_to_check: - inputs["images"] = images_to_check - if tool_calls_to_check: - inputs["tool_calls"] = tool_calls_to_check - # Include model information from the response if available - if hasattr(response, "model") and response.model: - inputs["model"] = response.model - - guardrailed_inputs: Final = await guardrail_to_apply.apply_guardrail( - inputs=inputs, + await self._apply_guardrail_to_output_choices( + response=response, + texts_to_check=texts_to_check, + images_to_check=images_to_check, + tool_calls_to_check=tool_calls_to_check, + text_task_mappings=text_task_mappings, + tool_call_task_mappings=tool_call_task_mappings, request_data=request_data, - input_type="response", - logging_obj=litellm_logging_obj, + user_api_key_dict=user_api_key_dict, + guardrail_to_apply=guardrail_to_apply, + litellm_logging_obj=litellm_logging_obj, ) - guardrailed_texts: Final = guardrailed_inputs.get("texts", []) - returned_tool_calls: Final = guardrailed_inputs.get("tool_calls") - guardrailed_tool_calls: Final[list[dict[str, object]]] = ( - cast(list[dict[str, object]], returned_tool_calls) - if isinstance(returned_tool_calls, list) and len(returned_tool_calls) == len(tool_calls_to_check) - else tool_calls_to_check - ) - - # Step 3: Map guardrail responses back to original response structure - if guardrailed_texts and texts_to_check: - await self._apply_guardrail_responses_to_output_texts( - response=response, - responses=guardrailed_texts, - task_mappings=text_task_mappings, - ) - - # Step 4: Apply guardrailed tool calls back to response - if guardrailed_tool_calls: - await self._apply_guardrail_responses_to_output_tool_calls( - response=response, - tool_calls=guardrailed_tool_calls, - task_mappings=tool_call_task_mappings, - ) - verbose_proxy_logger.debug("OpenAI Chat Completions: Processed output response: %s", response) return response + async def _apply_guardrail_to_output_choices( + self, + response: "ModelResponse", + texts_to_check: Sequence[str], + images_to_check: Sequence[str], + tool_calls_to_check: Sequence[dict[str, object]], + text_task_mappings: Sequence[tuple[int, int | None]], + tool_call_task_mappings: Sequence[tuple[int, int]], + request_data: dict | None, + user_api_key_dict: "UserAPIKeyAuth | None", + guardrail_to_apply: "CustomGuardrail", + litellm_logging_obj: "LiteLLMLoggingObj | None", + ) -> None: + """Apply the guardrail to the extracted output texts and tool calls in batch.""" + # Use the real request_data if provided (proxy path), otherwise + # create a standalone dict (SDK / direct-call path). + if request_data is None: + request_data = {"response": response} + else: + if "response" not in request_data: + request_data["response"] = response + + self.merge_user_api_key_metadata_into_request(request_data, user_api_key_dict) + + inputs: Final = GenericGuardrailAPIInputs(texts=list(texts_to_check)) + if images_to_check: + inputs["images"] = list(images_to_check) + if tool_calls_to_check: + inputs["tool_calls"] = list(tool_calls_to_check) + # Include model information from the response if available + if hasattr(response, "model") and response.model: + inputs["model"] = response.model + + guardrailed_inputs: Final = await guardrail_to_apply.apply_guardrail( + inputs=inputs, + request_data=request_data, + input_type="response", + logging_obj=litellm_logging_obj, + ) + + guardrailed_texts: Final = guardrailed_inputs.get("texts", []) + returned_tool_calls: Final = guardrailed_inputs.get("tool_calls") + guardrailed_tool_calls: Final[Sequence[dict[str, object]]] = ( + cast(list[dict[str, object]], returned_tool_calls) + if isinstance(returned_tool_calls, list) and len(returned_tool_calls) == len(tool_calls_to_check) + else tool_calls_to_check + ) + + # Step 3: Map guardrail responses back to original response structure + if guardrailed_texts and texts_to_check: + await self._apply_guardrail_responses_to_output_texts( + response=response, + responses=guardrailed_texts, + task_mappings=text_task_mappings, + ) + + # Step 4: Apply guardrailed tool calls back to response + if guardrailed_tool_calls: + await self._apply_guardrail_responses_to_output_tool_calls( + response=response, + tool_calls=guardrailed_tool_calls, + task_mappings=tool_call_task_mappings, + ) + async def process_output_streaming_response( self, responses_so_far: list["ModelResponseStream"], @@ -1099,8 +1139,8 @@ class OpenAIChatCompletionsHandler(BaseTranslation): async def _apply_guardrail_responses_to_output_texts( self, response: "ModelResponse", - responses: list[str], - task_mappings: list[tuple[int, int | None]], + responses: Sequence[str], + task_mappings: Sequence[tuple[int, int | None]], ) -> None: """ Apply guardrail text responses back to output response. @@ -1130,8 +1170,8 @@ class OpenAIChatCompletionsHandler(BaseTranslation): async def _apply_guardrail_responses_to_output_tool_calls( self, response: "ModelResponse", - tool_calls: list[dict[str, Any]], - task_mappings: list[tuple[int, int]], + tool_calls: Sequence[dict[str, Any]], + task_mappings: Sequence[tuple[int, int]], ) -> None: """ Apply guardrailed tool calls back to the output response. diff --git a/litellm/llms/openai/responses/guardrail_translation/handler.py b/litellm/llms/openai/responses/guardrail_translation/handler.py index 51271b044ab..a65f3e6bc6b 100644 --- a/litellm/llms/openai/responses/guardrail_translation/handler.py +++ b/litellm/llms/openai/responses/guardrail_translation/handler.py @@ -470,8 +470,8 @@ def _image_url_part_ref(content_item: Mapping[str, object], scan_attachments: bo if not isinstance(image_url, dict): return None url: Final = image_url.get("url") - if isinstance(url, str) and url: - return url + if url: + return cast("str", url) # cast-ok: base forwarded the raw url value if not scan_attachments: return None file_id: Final = image_url.get("file_id") diff --git a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py index fccab6ef1bf..02c245f16b9 100644 --- a/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py +++ b/litellm/proxy/guardrails/guardrail_hooks/bedrock_guardrails.py @@ -130,6 +130,9 @@ _UNSCANNABLE_ATTACHMENT_PAYLOAD_KEYS: Final[Mapping[str, str]] = MappingProxyTyp "input_audio": "input_audio", } ) +_TOOL_OUTPUT_ATTACHMENT_TYPES: Final[frozenset[str]] = frozenset( + ("input_file", "file", "document", "input_image", "image_url", "image") +) _NO_TRACING_DETAIL: Final[GuardrailTracingDetail] = {} # Resource-less, detect-only InvokeGuardrailChecks API (no guardrail resource required). _BEDROCK_INVOKE_GUARDRAIL_CHECKS_PATH: Final = "/guardrail-checks/invoke" @@ -170,13 +173,13 @@ _GROUNDING_SOURCE_TRUSTED_ROLES: Final = frozenset({"system", "developer"}) _MAX_IMAGE_BYTES: Final = 4 * 1024 * 1024 _MAX_IMAGES_PER_APPLY_GUARDRAIL_CALL: Final = 20 -_APPLY_GUARDRAIL_IMAGE_FORMATS: Final[ - dict[str, BedrockGuardrailImageFormat] -] = { # mutable-ok: module-level lookup table, never mutated - "png": "png", - "jpeg": "jpeg", - "jpg": "jpeg", -} +_APPLY_GUARDRAIL_IMAGE_FORMATS: Final[Mapping[str, BedrockGuardrailImageFormat]] = MappingProxyType( + { + "png": "png", + "jpeg": "jpeg", + "jpg": "jpeg", + } +) class QualifiedTextBlock(NamedTuple): @@ -259,6 +262,23 @@ def _decoded_base64_length(encoded: str) -> int: return len(encoded) * 3 // 4 - encoded[-2:].count("=") +def _serialized_tool_output_parts(content: str) -> tuple[Mapping[str, object], ...]: + """Attachment-shaped members of a ``tool`` message's JSON-serialized output.""" + if not content.lstrip().startswith("["): + return () + try: + decoded: Final = json.loads(content) + except ValueError: + return () + if not isinstance(decoded, list): + return () + return tuple( + cast(Mapping[str, object], item) # cast-ok: narrowed to dict in the generator's condition + for item in decoded + if isinstance(item, dict) + ) + + def _content_leaf_parts(content: object) -> tuple[object, ...]: """Leaf content parts with Anthropic tool_result blocks flattened. @@ -439,7 +459,9 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): ) return cleaned or None - async def _create_bedrock_input_content_request(self, messages: list[AllMessageValues] | None) -> BedrockRequest: + async def _create_bedrock_input_content_request( + self, messages: "Sequence[AllMessageValues] | None" + ) -> BedrockRequest: """Create a bedrock request for the input content - the LLM request.""" bedrock_request: Final[BedrockRequest] = BedrockRequest(source="INPUT") if messages is None: @@ -667,8 +689,9 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): return part_map: Final = cast(Mapping[str, object], part) # cast-ok: narrowed to dict on the line above part_type: Final = part_map.get("type") - if part_type in ("file", "document"): - self._handle_unscannable_attachment(reason="a document/file attachment cannot be scanned") + payload_key: Final = _UNSCANNABLE_ATTACHMENT_PAYLOAD_KEYS.get(str(part_type)) + if payload_key is not None and part_map.get(payload_key): + self._handle_unscannable_attachment(reason="a document, file, video or audio attachment cannot be scanned") if part_type == "image_url": image_url: Final = self._get_image_url(item=part_map) if image_url is None: @@ -692,6 +715,18 @@ class BedrockGuardrail(CustomGuardrail, BaseAWSLLM): for message in messages: for leaf in _content_leaf_parts(message.get("content")): self._refuse_unscannable_part(part=leaf) + self._refuse_serialized_tool_output(message=message) + + def _refuse_serialized_tool_output(self, message: Mapping[str, object]) -> None: + """Refuse an attachment hiding in a ``tool`` message's serialized output.""" + if message.get("role") != "tool": + return + content: Final = message.get("content") + if not isinstance(content, str): + return + for part in _serialized_tool_output_parts(content): + if part.get("type") in _TOOL_OUTPUT_ATTACHMENT_TYPES: + self._handle_unscannable_attachment(reason="a tool output attachment cannot be scanned") async def _build_image_content_item(self, image_url: str) -> BedrockContentItem: """Refuse remote urls with the same substring test diff --git a/tests/integration/observability/test_guardrail_effects.py b/tests/integration/observability/test_guardrail_effects.py index 7770d266367..852fbf2c416 100644 --- a/tests/integration/observability/test_guardrail_effects.py +++ b/tests/integration/observability/test_guardrail_effects.py @@ -1088,3 +1088,161 @@ def test_bedrock_during_call_refuses_chat_video_part(gateway: Gateway, tmp_path: assert "cannot be scanned" in response.text, response.text assert observed.get("/__observations").json()["requests"] == [] assert policy.drain() == () + + +@pytest.mark.covers("other.observability.guardrails.bedrock_during_call_refuses_function_call_output_file") +def test_bedrock_during_call_refuses_responses_function_call_output_file(gateway: Gateway, tmp_path: Path) -> None: + with wire_server(_allow) as policy: + params: Final = _bedrock_policy(policy.url) + params["mode"] = "during_call" + path: Final = _guardrail_config(tmp_path, "bedrock-dfco-" + uuid.uuid4().hex, params, "bedrock-dfco.yaml") + with owned_proxy(gateway, tmp_path, {}, config=path) as candidate, candidate.scenario() as scenario: + model: Final = scenario.model() + with httpx.Client(base_url=gateway.upstream_url, timeout=5, trust_env=False) as observed: + observed.get("/__observations") + response: Final = candidate.request( + "POST", + "/v1/responses", + { + "model": model, + "input": [ + {"type": "function_call", "call_id": "c1", "name": "lookup", "arguments": "{}"}, + { + "type": "function_call_output", + "call_id": "c1", + "output": [ + { + "type": "input_file", + "filename": "a.pdf", + "file_data": f"data:application/pdf;base64,{PDF_BASE64}", + } + ], + }, + { + "type": "message", + "role": "user", + "content": [{"type": "input_text", "text": "hi"}], + }, + ], + }, + ) + assert response.status_code == 400, response.text + assert "cannot be scanned" in response.text, response.text + assert observed.get("/__observations").json()["requests"] == [] + assert policy.drain() == () + + +@pytest.mark.parametrize( + "media_part", + [ + {"type": "video_url", "video_url": {"url": "https://synthetic.example/clip.mp4"}}, + {"type": "input_audio", "input_audio": {"data": "AAAA", "format": "wav"}}, + ], +) +@pytest.mark.covers("other.observability.guardrails.bedrock_latest_role_during_call_refuses_scoped_media") +def test_bedrock_latest_role_during_call_refuses_scoped_media( + gateway: Gateway, tmp_path: Path, media_part: dict[str, JsonValue] +) -> None: + with wire_server(_allow) as policy: + params: Final = _bedrock_policy(policy.url) + params["mode"] = "during_call" + params["experimental_use_latest_role_message_only"] = True + path: Final = _guardrail_config(tmp_path, "bedrock-latdur-" + uuid.uuid4().hex, params, "bedrock-latdur.yaml") + with owned_proxy(gateway, tmp_path, {}, config=path) as candidate, candidate.scenario() as scenario: + model: Final = scenario.model() + with httpx.Client(base_url=gateway.upstream_url, timeout=5, trust_env=False) as observed: + observed.get("/__observations") + response: Final = candidate.request( + "POST", + "/v1/chat/completions", + { + "model": model, + "messages": [ + {"role": "user", "content": [{"type": "text", "text": "watch this"}, media_part]}, + {"role": "assistant", "content": "ok"}, + {"role": "user", "content": "next"}, + ], + }, + ) + assert response.status_code == 400, response.text + assert "cannot be scanned" in response.text, response.text + assert observed.get("/__observations").json()["requests"] == [] + assert policy.drain() == () + + +@pytest.mark.parametrize( + ("shell_part", "expected_status"), + [ + ({"type": "input_audio"}, 200), + ({"type": "video_url"}, 200), + ({"type": "file"}, 500), # the provider side rejects a payload-less file part downstream + ], +) +@pytest.mark.covers("other.observability.guardrails.bedrock_pre_call_drops_unscannable_shell_part") +def test_bedrock_pre_call_drops_unscannable_shell_part( + gateway: Gateway, tmp_path: Path, shell_part: dict[str, JsonValue], expected_status: int +) -> None: + def guardrail(request: Request) -> Reply: + assert request.target == "/guardrail/synthetic-guardrail/version/1/apply" + body: Final = json.loads(request.body) + assert body["content"] == [{"text": {"text": "hello"}}] + return Reply(body=b'{"action":"NONE","outputs":[],"assessments":[]}') + + with wire_server(guardrail) as policy: + path: Final = _guardrail_config( + tmp_path, "bedrock-shell-" + uuid.uuid4().hex, _bedrock_policy(policy.url), "bedrock-shell.yaml" + ) + with owned_proxy(gateway, tmp_path, {}, config=path) as candidate, candidate.scenario() as scenario: + model: Final = scenario.model() + with httpx.Client(base_url=gateway.upstream_url, timeout=5, trust_env=False) as observed: + observed.get("/__observations") + response: Final = candidate.request( + "POST", + "/v1/chat/completions", + { + "model": model, + "messages": [{"role": "user", "content": [{"type": "text", "text": "hello"}, shell_part]}], + }, + ) + assert response.status_code == expected_status, response.text + assert "cannot be scanned" not in response.text, response.text + if expected_status == 200: + assert len(observed.get("/__observations").json()["requests"]) == 1 + assert len(policy.drain()) == 1 + + +@pytest.mark.covers("other.observability.guardrails.non_bedrock_policy_forwards_non_string_image_url") +def test_non_bedrock_policy_forwards_non_string_image_url(gateway: Gateway, tmp_path: Path) -> None: + def sink(_request: Request) -> Reply: + return Reply(body=b'{"action":"NONE"}') + + with wire_server(sink) as policy: + params: Final = { + "guardrail": "generic_guardrail_api", + "mode": "pre_call", + "default_on": True, + "api_base": policy.url, + "api_key": "synthetic-guardrail-key", + } + path: Final = _guardrail_config(tmp_path, "generic-img-" + uuid.uuid4().hex, params, "generic-img.yaml") + with owned_proxy(gateway, tmp_path, {}, config=path) as candidate, candidate.scenario() as scenario: + model: Final = scenario.model() + response: Final = candidate.request( + "POST", + "/v1/responses", + { + "model": model, + "input": [ + { + "type": "message", + "role": "user", + "content": [ + {"type": "input_text", "text": "look"}, + {"type": "image_url", "image_url": {"url": 5}}, + ], + } + ], + }, + ) + assert response.status_code == 500, response.text + assert "input_value=5" in response.text, response.text diff --git a/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_chat_guardrail_handler.py b/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_chat_guardrail_handler.py index 317e742b08f..f2a175ad313 100644 --- a/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_chat_guardrail_handler.py +++ b/tests/test_litellm/llms/openai/chat/guardrail_translation/test_openai_chat_guardrail_handler.py @@ -253,3 +253,30 @@ class TestOpenAIChatHandlerAttachmentsDefaultScope: assert guardrail.calls == 1 assert guardrail.inputs is not None assert guardrail.inputs["images"] == [""] + + @pytest.mark.asyncio + @pytest.mark.parametrize( + "shell", + [ + {"type": "video_url"}, + {"type": "input_audio"}, + {"type": "file"}, + {"type": "video_url", "video_url": {}}, + {"type": "input_audio", "input_audio": ""}, + ], + ) + async def test_bare_attachment_shells_yield_no_files_entry(self, shell): + """A payload-less attachment part carries nothing the guardrail can refuse or scan.""" + guardrail = ScanningGuardrail() + data = { + "model": "gpt-4o", + "messages": [{"role": "user", "content": [{"type": "text", "text": "look"}, shell]}], + } + + await OpenAIChatCompletionsHandler().process_input_messages(data=data, guardrail_to_apply=guardrail) + + assert guardrail.calls == 1 + assert guardrail.inputs is not None + assert guardrail.inputs["texts"] == ["look"] + assert "files" not in guardrail.inputs + assert "images" not in guardrail.inputs diff --git a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py index e5c1e4bad03..a7c0c5c17e0 100644 --- a/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py +++ b/tests/test_litellm/llms/openai/responses/test_openai_responses_guardrail_handler.py @@ -3612,3 +3612,29 @@ class TestOpenAIResponsesHandlerAttachmentsDefaultScope: assert guardrail.calls == 0, "non-scanning guardrail fired on function_call_output attachments" assert result["input"] == input_data + + +@pytest.mark.asyncio +async def test_non_string_image_url_value_is_forwarded_to_guardrail(): + """Base forwarded image_url.url untouched; a non-string value must reach images, not be dropped.""" + handler = OpenAIResponsesHandler() + guardrail = ScanningGuardrail() + data = { + "input": [ + { + "type": "message", + "role": "user", + "content": [ + {"type": "input_text", "text": "look"}, + {"type": "image_url", "image_url": {"url": 5}}, + ], + } + ], + "model": "gpt-4o", + } + + await handler.process_input_messages(data, guardrail) + + assert guardrail.calls == 1 + assert guardrail.inputs is not None + assert guardrail.inputs["images"] == [5] diff --git a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py index 283b6e0e7ed..6f583e4efda 100644 --- a/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py +++ b/tests/test_litellm/proxy/guardrails/guardrail_hooks/test_bedrock_guardrails.py @@ -7167,3 +7167,169 @@ def test_apply_masking_to_messages_masks_tool_result_inner_list_items() -> None: ], } ] + + +@pytest.mark.asyncio +async def test_during_call_hook_refuses_serialized_tool_output_attachment(): + """A tool message's JSON-serialized output list can hide an attachment part that + never becomes a leaf; the unscoped pass has to refuse it before any scan.""" + guardrail = BedrockGuardrail( + guardrail_name="bedrock-tool-output-file", + guardrailIdentifier="test-guardrail", + guardrailVersion="DRAFT", + event_hook=GuardrailEventHooks.during_call, + default_on=True, + ) + data = { + "model": "gpt-4o-mini", + "messages": [ + { + "role": "tool", + "tool_call_id": "c1", + "content": json.dumps( + [ + { + "type": "input_file", + "filename": "a.pdf", + "file_data": "data:application/pdf;base64,JVBERi0=", + } + ] + ), + }, + {"role": "user", "content": "hi"}, + ], + } + + with ( + patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post, + pytest.raises(HTTPException) as exc_info, + ): + await guardrail.async_moderation_hook( + data=data, + user_api_key_dict=UserAPIKeyAuth(), + call_type=CallTypes.acompletion.value, + ) + + assert exc_info.value.status_code == 400 + mock_post.assert_not_called() + + +@pytest.mark.asyncio +async def test_during_call_hook_refuses_serialized_tool_output_attachment_unknown_shape_falls_through(): + """Non-attachment members of the serialized tool output are left to the normal scan.""" + guardrail = BedrockGuardrail( + guardrail_name="bedrock-tool-output-text", + guardrailIdentifier="test-guardrail", + guardrailVersion="DRAFT", + event_hook=GuardrailEventHooks.during_call, + default_on=True, + ) + data = { + "model": "gpt-4o-mini", + "messages": [ + { + "role": "tool", + "tool_call_id": "c1", + "content": json.dumps([{"type": "output_text", "text": "memo"}]), + }, + {"role": "user", "content": "hi"}, + ], + } + mock_credentials = MagicMock() + mock_credentials.access_key = "test-access-key" + mock_credentials.secret_key = "test-secret-key" + mock_credentials.token = None + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.json.return_value = {"action": "NONE", "assessments": []} + + with ( + patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")), + patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post, + ): + mock_post.return_value = mock_response + await guardrail.async_moderation_hook( + data=data, + user_api_key_dict=UserAPIKeyAuth(), + call_type=CallTypes.acompletion.value, + ) + + +@pytest.mark.asyncio +@pytest.mark.parametrize( + "media_part", + [ + {"type": "video_url", "video_url": {"url": "https://synthetic.example/clip.mp4"}}, + {"type": "input_audio", "input_audio": {"data": "AAAA", "format": "wav"}}, + ], +) +async def test_during_call_hook_refuses_scoped_out_media_attachment(media_part): + """Media payloads in a message latest-role scoping drops are refused, not evaded.""" + guardrail = BedrockGuardrail( + guardrail_name="bedrock-scoped-media", + guardrailIdentifier="test-guardrail", + guardrailVersion="DRAFT", + event_hook=GuardrailEventHooks.during_call, + default_on=True, + experimental_use_latest_role_message_only=True, + ) + data = { + "model": "gpt-4o-mini", + "messages": [ + {"role": "user", "content": [{"type": "text", "text": "watch this"}, media_part]}, + {"role": "assistant", "content": "ok"}, + {"role": "user", "content": "next"}, + ], + } + + with ( + patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post, + pytest.raises(HTTPException) as exc_info, + ): + await guardrail.async_moderation_hook( + data=data, + user_api_key_dict=UserAPIKeyAuth(), + call_type=CallTypes.acompletion.value, + ) + + assert exc_info.value.status_code == 400 + mock_post.assert_not_called() + + +@pytest.mark.asyncio +async def test_during_call_hook_drops_bare_attachment_shells(): + """A payload-less shell carries nothing the guardrail can refuse; scoping keeps it out.""" + guardrail = BedrockGuardrail( + guardrail_name="bedrock-shells", + guardrailIdentifier="test-guardrail", + guardrailVersion="DRAFT", + event_hook=GuardrailEventHooks.during_call, + default_on=True, + ) + data = { + "model": "gpt-4o-mini", + "messages": [ + { + "role": "user", + "content": [{"type": "text", "text": "hello"}, {"type": "file"}, {"type": "input_audio"}], + } + ], + } + mock_credentials = MagicMock() + mock_credentials.access_key = "test-access-key" + mock_credentials.secret_key = "test-secret-key" + mock_credentials.token = None + mock_response = MagicMock() + mock_response.status_code = 200 + mock_response.json.return_value = {"action": "NONE", "assessments": []} + + with ( + patch.object(guardrail, "_load_credentials", return_value=(mock_credentials, "us-east-1")), + patch.object(guardrail.async_handler, "post", new_callable=AsyncMock) as mock_post, + ): + mock_post.return_value = mock_response + await guardrail.async_moderation_hook( + data=data, + user_api_key_dict=UserAPIKeyAuth(), + call_type=CallTypes.acompletion.value, + )