Revert "[Bugfix] Use MediaConnector for image/video URL fetching to prevent S…"

This reverts commit d8edf0c9d5.
This commit is contained in:
Gao Han authored and GitHub committed 2026-06-26 23:10:48 +08:00
1 parent 908e7d32d9
commit d98f94fa29
8 files changed
+243 -997

No files matched your search

+28 -41
View File
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import asyncio
import base64
import dataclasses
import io
import json
@@ -19,6 +20,7 @@ from numbers import Integral
from pathlib import Path
from typing import Annotated, Any, Literal, cast
import httpx
import numpy as np
import vllm.envs as envs
from fastapi import APIRouter, Depends, File, Form, HTTPException, Query, Request, UploadFile, WebSocket
@@ -83,7 +85,6 @@ from vllm.entrypoints.speech_to_text.translation.serving import (
OpenAIServingTranslation,
)
from vllm.logger import init_logger
from vllm.multimodal.media import MediaConnector
from vllm.tasks import POOLING_TASKS
from vllm.tool_parsers import ToolParserManager
from vllm.utils import random_uuid
@@ -2036,23 +2037,17 @@ async def edit_images(
# Match the offline path: RGB normalize when the caller opts into
# Hunyuan-aware behavior. RGBA/P uploads otherwise diverge from offline.
normalize_edit_images_rgb = bot_task is not None or sys_type is not None
pil_images = await _load_input_images(
input_images_list,
engine_client.model_config,
normalize_rgb=normalize_edit_images_rgb,
)
pil_images = await _load_input_images(input_images_list, normalize_rgb=normalize_edit_images_rgb)
prompt["multi_modal_data"] = {}
prompt["multi_modal_data"]["image"] = pil_images
if mask_image is not None:
# Mask role is different (alpha channel matters); never normalize.
loaded = await _load_input_images([mask_image], engine_client.model_config, normalize_rgb=False)
loaded = await _load_input_images([mask_image], normalize_rgb=False)
prompt["multi_modal_data"]["mask_image"] = loaded[0]
if reference_image is not None:
loaded = await _load_input_images(
[reference_image], engine_client.model_config, normalize_rgb=normalize_edit_images_rgb
)
loaded = await _load_input_images([reference_image], normalize_rgb=normalize_edit_images_rgb)
prompt["multi_modal_data"]["reference_image"] = loaded[0]
# 3 Build sample params
@@ -2539,41 +2534,41 @@ def _extract_images_from_result(result: Any) -> list[Any]:
async def _load_input_images(
inputs: list[str],
model_config: Any,
*,
normalize_rgb: bool = True,
) -> list[Image.Image]:
"""Load images from data URIs, http(s) URLs, file:// URIs, or UploadFiles.
http(s) and ``file:`` URLs are fetched through vLLM's ``MediaConnector`` which
respects ``--allowed-media-domains`` and ``--allowed-local-media-path``
to prevent SSRF.
"""
convert to PIL.Image.Image list
"""
if isinstance(inputs, str):
inputs = [inputs]
connector = MediaConnector(
allowed_local_media_path=model_config.allowed_local_media_path,
allowed_media_domains=model_config.allowed_media_domains,
)
images: list[Image.Image] = []
for inp in inputs:
if isinstance(inp, str):
# http(s), data: and file: all handled by MediaConnector
# 1. URL + base64
if isinstance(inp, str) and inp.startswith("data:image"):
try:
# Use RGBA to preserve alpha for transparent PNG.
result = await connector.fetch_image_async(
inp,
image_mode="RGBA",
)
images.append(result.media)
_, b64_data = inp.split(",", 1)
image_bytes = base64.b64decode(b64_data)
img = Image.open(io.BytesIO(image_bytes))
images.append(img)
except Exception as e:
raise ValueError(f"Failed to load image: {e}") from e
raise ValueError(f"Invalid base64 image: {e}")
# 2. URL
elif isinstance(inp, str) and inp.startswith("http"):
async with httpx.AsyncClient(timeout=60) as client:
try:
resp = await client.get(inp)
resp.raise_for_status()
img = Image.open(io.BytesIO(resp.content))
images.append(img)
except Exception as e:
raise ValueError(f"Failed to download image from URL {inp}: {e}")
# 3. UploadFile
elif hasattr(inp, "file"):
# UploadFile
try:
img_data = await inp.read()
img = Image.open(io.BytesIO(img_data))
@@ -3025,7 +3020,6 @@ async def _parse_video_form(
request.image_reference,
request.video_reference,
input_reference_bytes,
model_config=handler._engine_client.model_config,
max_video_frames=decode_spec.max_frames,
video_keep=decode_spec.keep,
)
@@ -3037,15 +3031,8 @@ async def _parse_video_form(
reference_audio: ReferenceAudio | None = None
if request.audio_reference is not None:
audio_connector = MediaConnector(
allowed_local_media_path=handler._engine_client.model_config.allowed_local_media_path,
allowed_media_domains=handler._engine_client.model_config.allowed_media_domains,
)
try:
audio_path = await decode_audio_url(
request.audio_reference.audio_url,
audio_connector,
)
audio_path = await decode_audio_url(request.audio_reference.audio_url)
except InvalidInputReferenceError as exc:
raise HTTPException(400, detail=str(exc)) from exc
reference_audio = ReferenceAudio(path=audio_path)
+83 -105
View File
@@ -10,14 +10,12 @@ import base64
import binascii
from collections import deque
from io import BytesIO
from pathlib import Path
from typing import Any, Literal
import httpx
import numpy as np
import torch
from PIL import Image, UnidentifiedImageError
from vllm.multimodal.media import MediaConnector
from vllm.multimodal.media.base import MediaIO
from vllm_omni.entrypoints.openai.errors import InvalidInputReferenceError
from vllm_omni.entrypoints.openai.protocol.videos import (
@@ -147,12 +145,22 @@ def _decode_base64_image(input_reference: str, *, source: str) -> Image.Image:
raise InvalidInputReferenceError(f"Invalid {source}: image data is empty.")
async def decode_image_url(image_url: str, connector: MediaConnector) -> Image.Image:
"""Fetch and decode an image URL using MediaConnector for SSRF protection."""
try:
return (await connector.fetch_image_async(image_url)).media
except Exception as exc:
raise InvalidInputReferenceError(f"Invalid image_reference.image_url: {exc}") from exc
async def decode_image_url(image_url: str) -> Image.Image:
if image_url.startswith("data:image"):
return _decode_base64_image(image_url, source="image_reference.image_url")
if image_url.startswith(("http://", "https://")):
async with httpx.AsyncClient(timeout=60) as client:
try:
response = await client.get(image_url)
response.raise_for_status()
except httpx.HTTPError as exc:
raise InvalidInputReferenceError(
"Invalid image_reference.image_url: failed to download image."
) from exc
return _decode_image_bytes(response.content, source="image_reference.image_url")
raise InvalidInputReferenceError("Invalid image_reference.image_url: must be an http(s) URL or data URL.")
def _decode_base64_video(
@@ -176,117 +184,96 @@ def _decode_base64_video(
raise InvalidInputReferenceError(f"Invalid {source}: video data is empty.")
class _VideoFramesMediaIO(MediaIO[VideoFrames]):
"""MediaIO that decodes video bytes into VideoFrames with frame extraction."""
def __init__(
self,
*,
source: str = "video_reference.video_url",
max_frames: int | None = None,
keep: Literal["first", "last"] = "first",
) -> None:
self._source = source
self._max_frames = max_frames
self._keep = keep
def load_bytes(self, data: bytes) -> VideoFrames:
return _decode_video_bytes(
data,
source=self._source,
max_frames=self._max_frames,
keep=self._keep,
async def decode_video_url(
video_url: str,
*,
max_frames: int | None = None,
keep: Literal["first", "last"] = "first",
) -> VideoFrames:
if video_url.startswith("data:video"):
return _decode_base64_video(
video_url,
source="video_reference.video_url",
max_frames=max_frames,
keep=keep,
)
def load_base64(self, media_type: str, data: str) -> VideoFrames:
return self.load_bytes(base64.b64decode(data))
if video_url.startswith(("http://", "https://")):
async with httpx.AsyncClient(timeout=60) as client:
try:
response = await client.get(video_url)
response.raise_for_status()
except httpx.HTTPError as exc:
raise InvalidInputReferenceError(
"Invalid video_reference.video_url: failed to download video."
) from exc
return _decode_video_bytes(
response.content,
source="video_reference.video_url",
max_frames=max_frames,
keep=keep,
)
def load_file(self, filepath: Path) -> VideoFrames:
return self.load_bytes(filepath.read_bytes())
raise InvalidInputReferenceError("Invalid video_reference.video_url: must be an http(s) URL or data URL.")
class _AudioFileMediaIO(MediaIO[str]):
"""MediaIO that writes audio bytes to a temp file, returns the path."""
async def decode_audio_url(audio_url: str) -> str:
"""Decode an audio URL or data-URL to a temporary file path."""
import tempfile
def load_bytes(self, data: bytes) -> str:
return self._write_temp(data, suffix=".wav")
audio_bytes: bytes | None = None
def load_base64(self, media_type: str, data: str) -> str:
suffix = ".wav"
ext = media_type.split("/")[-1] if media_type else ""
if audio_url.startswith("data:audio"):
_, b64_data = audio_url.split(",", 1)
try:
audio_bytes = base64.b64decode(b64_data)
except (binascii.Error, ValueError) as exc:
raise InvalidInputReferenceError(
"Invalid audio_reference.audio_url: audio data is not valid base64."
) from exc
elif audio_url.startswith(("http://", "https://")):
async with httpx.AsyncClient(timeout=60) as client:
try:
response = await client.get(audio_url)
response.raise_for_status()
except httpx.HTTPError as exc:
raise InvalidInputReferenceError(
"Invalid audio_reference.audio_url: failed to download audio."
) from exc
audio_bytes = response.content
else:
raise InvalidInputReferenceError("Invalid audio_reference.audio_url: must be an http(s) URL or data URL.")
if not audio_bytes:
raise InvalidInputReferenceError("Invalid audio_reference: audio data is empty.")
suffix = ".wav"
if audio_url.startswith("data:audio/"):
mime = audio_url.split(";")[0].removeprefix("data:")
ext = mime.split("/")[-1]
if ext in ("mpeg", "mp3"):
suffix = ".mp3"
elif ext == "wav":
suffix = ".wav"
elif ext.isalnum() and len(ext) <= 8:
suffix = f".{ext}"
return self._write_temp(base64.b64decode(data), suffix=suffix)
def load_file(self, filepath: Path) -> str:
return str(filepath)
@staticmethod
def _write_temp(data: bytes, *, suffix: str) -> str:
import tempfile
if not data:
raise InvalidInputReferenceError("Invalid audio_reference: audio data is empty.")
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
tmp.write(data)
tmp.close()
return tmp.name
async def decode_video_url(
video_url: str,
connector: MediaConnector,
*,
max_frames: int | None = None,
keep: Literal["first", "last"] = "first",
) -> VideoFrames:
"""Fetch and decode a video URL using MediaConnector for SSRF protection."""
try:
return await connector.load_from_url_async(
video_url,
_VideoFramesMediaIO(max_frames=max_frames, keep=keep),
)
except InvalidInputReferenceError:
raise
except Exception as exc:
raise InvalidInputReferenceError(f"Invalid video_reference.video_url: {exc}") from exc
async def decode_audio_url(
audio_url: str,
connector: MediaConnector,
) -> str:
"""Fetch and decode an audio URL using MediaConnector for SSRF protection."""
try:
return await connector.load_from_url_async(
audio_url,
_AudioFileMediaIO(),
)
except InvalidInputReferenceError:
raise
except Exception as exc:
raise InvalidInputReferenceError(f"Invalid audio_reference.audio_url: {exc}") from exc
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=suffix)
tmp.write(audio_bytes)
tmp.close()
return tmp.name
async def decode_input_reference(
image_reference: ImageReference | None,
video_reference: VideoReference | None,
input_reference_bytes: bytes | None,
model_config: Any,
*,
max_video_frames: int | None = None,
video_keep: Literal["first", "last"] = "first",
) -> Image.Image | VideoFrames | None:
"""Decode media input from multipart bytes, data URLs, or typed references.
"""Decode media input from multipart bytes, data URLs, or typed references."""
http(s) image and video URLs are fetched through vLLM's
``MediaConnector`` which respects ``--allowed-media-domains`` and
``--allowed-local-media-path`` to prevent SSRF.
"""
provided = sum(item is not None for item in (input_reference_bytes, image_reference, video_reference))
if provided > 1:
raise InvalidInputReferenceError("Provide only one of input_reference, image_reference, or video_reference.")
@@ -300,22 +287,13 @@ async def decode_input_reference(
)
if isinstance(image_reference, UrlImageReference):
connector = MediaConnector(
allowed_local_media_path=model_config.allowed_local_media_path,
allowed_media_domains=model_config.allowed_media_domains,
)
return await decode_image_url(image_reference.image_url, connector)
return await decode_image_url(image_reference.image_url)
elif isinstance(image_reference, FileImageReference):
raise InvalidInputReferenceError("Invalid image_reference: file_id is not supported yet.")
if isinstance(video_reference, UrlVideoReference):
connector = MediaConnector(
allowed_local_media_path=model_config.allowed_local_media_path,
allowed_media_domains=model_config.allowed_media_domains,
)
return await decode_video_url(
video_reference.video_url,
connector,
max_frames=max_video_frames,
keep=video_keep,
)