diff --git a/litellm/litellm_core_utils/cloud_storage_security.py b/litellm/litellm_core_utils/cloud_storage_security.py index c4b0cd3d292..47438b561db 100644 --- a/litellm/litellm_core_utils/cloud_storage_security.py +++ b/litellm/litellm_core_utils/cloud_storage_security.py @@ -8,6 +8,8 @@ from itertools import accumulate, chain, repeat, takewhile from typing import Final from urllib.parse import quote, unquote +from pydantic import TypeAdapter + from litellm._uuid import uuid from litellm.litellm_core_utils.core_helpers import as_object_list, as_str_mapping @@ -85,6 +87,8 @@ _EMBEDDED_S3_URI: Final = re.compile(r"s3://[^;]*") _MODEL_FILE_ID_MAPPING_KEY: Final = "model_file_id_mapping" _KEYS_THAT_MAY_HOLD_A_FILE_ID: Final = frozenset({"file_id", "file_url", "file_uri", "fileUri"}) _KEYS_THAT_MAY_HIDE_AN_S3_URI: Final = _KEYS_THAT_MAY_HOLD_A_FILE_ID | {_MODEL_FILE_ID_MAPPING_KEY} +_SCALAR_TYPES: Final = frozenset({int, float, bool, type(None)}) +_OBJECT_TUPLE: Final = TypeAdapter(tuple[object, ...]) def is_s3_uri(value: str) -> bool: @@ -111,6 +115,12 @@ def _with_embedded_s3_uris(file_id: str) -> Iterator[str]: return chain((file_id,), _s3_uris_embedded_in_file_id(file_id)) +def _sequence_items(value: object) -> Sequence[object] | None: + if isinstance(value, tuple): + return _OBJECT_TUPLE.validate_python(value) + return as_object_list(value) + + @dataclass(frozen=True, slots=True) class _BodyNode: value: object @@ -128,7 +138,7 @@ def _child_nodes(node: _BodyNode) -> Iterator[_BodyNode]: if fields is not None: inside_media_part: Final = node.inside_media_part or _is_media_part(fields, node.key) return (_BodyNode(child, child_key, inside_media_part) for child_key, child in fields.items()) - items: Final = as_object_list(node.value) + items: Final = _sequence_items(node.value) return (_BodyNode(item, None, node.inside_media_part) for item in items) if items is not None else iter(()) @@ -192,8 +202,8 @@ def _may_name_an_s3_uri(value: object) -> bool: fields: Final = as_str_mapping(value) if fields is not None: return not _KEYS_THAT_MAY_HIDE_AN_S3_URI.isdisjoint(fields) or any(map(_may_name_an_s3_uri, fields.values())) - items: Final = as_object_list(value) - return items is not None and any(map(_may_name_an_s3_uri, items)) + items: Final = _sequence_items(value) + return items is not None and not _SCALAR_TYPES.issuperset(map(type, items)) and any(map(_may_name_an_s3_uri, items)) def _may_carry_s3_media(request_body: Mapping[str, object]) -> bool: diff --git a/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py b/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py index af4ec9e6d27..855df362608 100644 --- a/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py +++ b/tests/unit/proxy/pass_through_endpoints/test_pass_through_endpoints.py @@ -13472,15 +13472,29 @@ _ADAPTER_BODY_WITH_S3_IMAGE: Final = json.dumps( ], } ).encode() +_ADAPTER_PYTHON_LITERAL_BODY_WITH_S3_IMAGE_IN_TUPLES: Final = repr( + { + "model": "unknown-model", + "messages": ( + {"role": "user", "content": ({"type": "image_url", "image_url": {"url": "s3://private-bucket/cat.png"}},)}, + ), + } +).encode() @pytest.mark.asyncio +@pytest.mark.parametrize( + "body", + [_ADAPTER_BODY_WITH_S3_IMAGE, _ADAPTER_PYTHON_LITERAL_BODY_WITH_S3_IMAGE_IN_TUPLES], + ids=["json", "python_literal_with_tuples"], +) async def test_chat_completion_pass_through_endpoint_refuses_an_s3_image_in_an_unlisted_bucket( monkeypatch: pytest.MonkeyPatch, + body: bytes, ): proxy_logging: Final = _stub_adapter_proxy(monkeypatch) request: Final = MagicMock(spec=Request) - request.body = AsyncMock(return_value=_ADAPTER_BODY_WITH_S3_IMAGE) + request.body = AsyncMock(return_value=body) with pytest.raises(ProxyException) as raised: await chat_completion_pass_through_endpoint(