Reject oversized media before fully downloading it (#51896)

Signed-off-by: Clinton Thomas <1033162+KernelClint@users.noreply.github.com>
Co-authored-by: Lucas Bourtoule <35483370+dhalf@users.noreply.github.com>
This commit is contained in:
Clinton Thomas
2026-08-24 01:15:09 +00:00
committed by GitHub
co-authored by Lucas Bourtoule
parent 585bb07c70
commit 2ec6f0d71e
13 changed files with 718 additions and 24 deletions
+21
View File
@@ -10,6 +10,7 @@ import pybase64
import torch
import vllm.envs as envs
from vllm.exceptions import VLLMValidationError
from vllm.logger import init_logger
from vllm.multimodal.audio import resample_audio_pyav
from vllm.utils.import_utils import PlaceholderModule
@@ -275,7 +276,20 @@ class AudioMediaIO(MediaIO[tuple[npt.NDArray, float]]):
# for flexible control.
self.kwargs = kwargs
def get_max_bytes(self) -> int:
return int(envs.VLLM_MAX_AUDIO_CLIP_FILESIZE_MB * MiB_bytes)
def _validate_encoded_size(self, size: int) -> None:
max_bytes = self.get_max_bytes()
if size > max_bytes:
raise VLLMValidationError(
"Maximum file size exceeded",
parameter="audio_filesize_mb",
value=size / MiB_bytes,
)
def load_bytes(self, data: bytes) -> tuple[npt.NDArray, float]:
self._validate_encoded_size(len(data))
return load_audio(
BytesIO(data),
sr=None,
@@ -288,6 +302,13 @@ class AudioMediaIO(MediaIO[tuple[npt.NDArray, float]]):
media_type: str,
data: str,
) -> tuple[npt.NDArray, float]:
max_encoded_chars = 4 * ((self.get_max_bytes() + 2) // 3)
if len(data) > max_encoded_chars:
raise VLLMValidationError(
"Maximum file size exceeded",
parameter="audio_filesize_mb",
value=(len(data) * 3 / 4) / MiB_bytes,
)
return self.load_bytes(pybase64.b64decode(data))
def load_file(self, filepath: Path) -> tuple[npt.NDArray, float]:
+4
View File
@@ -79,6 +79,10 @@ class MediaIO(ABC, Generic[_T]):
merged.update(runtime_kwargs)
return merged
def get_max_bytes(self) -> int | None:
"""Return the maximum encoded payload size accepted by this media IO."""
return None
@abstractmethod
def load_bytes(self, data: bytes) -> _T:
raise NotImplementedError
+20 -2
View File
@@ -22,8 +22,12 @@ from PIL import Image, UnidentifiedImageError
from urllib3.util import Url, parse_url
import vllm.envs as envs
from vllm.connections import HTTPConnection, global_http_connection
from vllm.exceptions import VLLMUnprocessableEntityError
from vllm.connections import (
HTTPConnection,
MediaDownloadSizeExceededError,
global_http_connection,
)
from vllm.exceptions import VLLMUnprocessableEntityError, VLLMValidationError
from vllm.logger import init_logger
from vllm.multimodal.video import get_video_loader_backend_for_processor
from vllm.utils.registry import ExtensionManager
@@ -70,6 +74,9 @@ def _wrap_media_fetch_error(
Original exception for transient errors (5xx, 408, 429, network blips)
or other exceptions
"""
if isinstance(exc, VLLMValidationError):
return exc
if isinstance(exc, aiohttp.ClientResponseError):
if exc.status in (408, 429):
return exc
@@ -101,6 +108,13 @@ def _wrap_media_fetch_error(
value=url,
)
if isinstance(exc, MediaDownloadSizeExceededError):
return VLLMUnprocessableEntityError(
f"Failed to fetch media from URL: {exc}",
parameter="image_url",
value=url,
)
if isinstance(exc, ValueError):
return VLLMUnprocessableEntityError(
"Failed to fetch media from URL: Invalid URL",
@@ -363,6 +377,7 @@ class MediaConnector:
if url_spec.scheme and url_spec.scheme.startswith("http"):
self._assert_url_in_allowed_media_domains(url_spec)
max_bytes = media_io.get_max_bytes()
cached = self._get_cached_bytes(url)
if cached is not None:
@@ -374,6 +389,7 @@ class MediaConnector:
url_spec.url,
timeout=fetch_timeout,
allow_redirects=envs.VLLM_MEDIA_URL_ALLOW_REDIRECTS,
max_bytes=max_bytes,
)
except Exception as e:
wrapped = _wrap_media_fetch_error(url, e)
@@ -409,6 +425,7 @@ class MediaConnector:
if url_spec.scheme and url_spec.scheme.startswith("http"):
self._assert_url_in_allowed_media_domains(url_spec)
max_bytes = media_io.get_max_bytes()
cached = await loop.run_in_executor(
global_thread_pool, self._get_cached_bytes, url
@@ -425,6 +442,7 @@ class MediaConnector:
url_spec.url,
timeout=fetch_timeout,
allow_redirects=envs.VLLM_MEDIA_URL_ALLOW_REDIRECTS,
max_bytes=max_bytes,
)
except Exception as e:
wrapped = _wrap_media_fetch_error(url, e)