diff --git a/litellm/proxy/image_endpoints/endpoints.py b/litellm/proxy/image_endpoints/endpoints.py index 5b90c0ff830..c742e7cd6e9 100644 --- a/litellm/proxy/image_endpoints/endpoints.py +++ b/litellm/proxy/image_endpoints/endpoints.py @@ -6,6 +6,7 @@ from typing import Final, get_type_hints import orjson from fastapi import APIRouter, Depends, File, HTTPException, Request, Response, UploadFile, status from fastapi.responses import ORJSONResponse +from starlette.datastructures import UploadFile as StarletteUploadFile import litellm from litellm.litellm_core_utils.prompt_templates.common_utils import ( @@ -294,12 +295,11 @@ async def image_edit_api( ######################################################### # Read request body and convert UploadFiles to BytesIO ######################################################### - data: Final = dict( - coerce_numeric_form_fields( - parsed_body=await _read_request_body(request=request), - numeric_fields=IMAGE_EDIT_NUMERIC_FORM_FIELDS, - ) + parsed_body: Final = coerce_numeric_form_fields( + parsed_body=await _read_request_body(request=request), + numeric_fields=IMAGE_EDIT_NUMERIC_FORM_FIELDS, ) + data: Final = {key: value for key, value in parsed_body.items() if not isinstance(value, StarletteUploadFile)} image_files: Final = await batch_to_bytesio(image) mask_files: Final = await batch_to_bytesio(mask) if image_files: diff --git a/tests/test_litellm/proxy/image_endpoints/test_endpoints.py b/tests/test_litellm/proxy/image_endpoints/test_endpoints.py index 31b87530c94..a730409e7f3 100644 --- a/tests/test_litellm/proxy/image_endpoints/test_endpoints.py +++ b/tests/test_litellm/proxy/image_endpoints/test_endpoints.py @@ -2,12 +2,13 @@ import asyncio import copy import logging from collections.abc import Iterator, Mapping +from io import BytesIO from types import SimpleNamespace from typing import Any, Dict import orjson import pytest -from fastapi import FastAPI, HTTPException +from fastapi import FastAPI, HTTPException, UploadFile from fastapi.testclient import TestClient from starlette.requests import Request from starlette.responses import Response @@ -172,6 +173,53 @@ def test_image_edit_multipart_n_that_is_not_a_number_is_left_alone(monkeypatch): assert captured["n"] == "two" +PNG_BYTES = b"\x89PNG\r\n\x1a\n" +MASK_BYTES = b"\x89PNG\r\n\x1a\nmask" + + +def test_image_edit_multipart_array_aliases_do_not_leak_upload_objects_to_the_provider(monkeypatch): + """`image[]`/`mask[]` (what the OpenAI SDK sends) used to survive in the body as + raw UploadFile objects and get forwarded as text provider params.""" + captured: dict[str, Any] = {} + soap = PNG_BYTES + soap_two = PNG_BYTES + b"2" + mask = MASK_BYTES + mask_two = MASK_BYTES + b"2" + + response = _image_edit_client(monkeypatch, captured).post( + "/v1/images/edits", + files=[ + ("image[]", ("soap.png", soap, "image/png")), + ("image[]", ("soap-2.png", soap_two, "image/png")), + ("mask[]", ("mask.png", mask, "image/png")), + ("mask[]", ("mask-2.png", mask_two, "image/png")), + ("reference", ("ref.png", PNG_BYTES, "image/png")), + ], + data={"model": "gpt-image-1", "prompt": "studio ghibli", "seed": "42"}, + ) + + assert response.status_code == 200 + assert "image[]" not in captured + assert "mask[]" not in captured + assert "reference" not in captured + assert captured["seed"] == "42" + assert not any(isinstance(value, UploadFile) for value in captured.values()) + + images = captured["image"] + masks = captured["mask"] + assert isinstance(images, list) and len(images) == 2 + assert isinstance(masks, list) and len(masks) == 2 + for value, name, content in ( + (images[0], "soap.png", soap), + (images[1], "soap-2.png", soap_two), + (masks[0], "mask.png", mask), + (masks[1], "mask-2.png", mask_two), + ): + assert isinstance(value, BytesIO) + assert value.name == name + assert value.getvalue() == content + + @pytest.mark.asyncio async def test_a_model_the_router_cannot_serve_answers_an_openai_typed_error(monkeypatch: pytest.MonkeyPatch): """A bare HTTPException carries no type or param, so the tail used to ship the