diff --git a/docs/usage/security.md b/docs/usage/security.md index 1cc91c3a8a..b1e9d481cf 100644 --- a/docs/usage/security.md +++ b/docs/usage/security.md @@ -85,6 +85,21 @@ significantly reduce the attack surface for these types of abuse. Also, consider setting `VLLM_MEDIA_URL_ALLOW_REDIRECTS=0` to prevent HTTP redirects from being followed to bypass domain restrictions. +### 5. **Restrict Media Decode Sizes:** + +Compressed media files can expand into gigabytes of memory during decoding. vLLM +enforces decode-size limits to prevent out-of-memory denial of service: + +| Environment Variable | Default | Description | +| --- | --- | --- | +| `VLLM_MAX_IMAGE_PIXELS` | `178956970` (~179M pixels) | Maximum decoded image size in pixels. Images exceeding this are rejected before raster memory is allocated. Default matches PIL's built-in 2x decompression-bomb threshold (~680 MB for RGB). | +| `VLLM_MAX_AUDIO_CLIP_FILESIZE_MB` | `25` | Maximum filesize in MB for a single audio file. | +| `VLLM_MAX_AUDIO_DECODE_DURATION_S` | `600` | Maximum decoded audio duration in seconds. Prevents compressed audio from expanding into gigabytes of float32 PCM. | + +Setting any of these to `0` disables the corresponding limit. This is **not +recommended** for deployments exposed to untrusted users, as it removes the +protection against resource-exhaustion attacks. + ## Security and Firewalls: Protecting Exposed vLLM Systems While vLLM is designed to allow unsafe network services to be isolated to diff --git a/tests/multimodal/media/test_image.py b/tests/multimodal/media/test_image.py index 65196d7805..c84343a378 100644 --- a/tests/multimodal/media/test_image.py +++ b/tests/multimodal/media/test_image.py @@ -205,3 +205,57 @@ def test_image_media_io_load_file(tmp_path): with pytest.raises(ValueError, match="Failed to load image"): image_io.load_file(truncated_real_file) + + +def test_image_pixel_limit_respected(): + """A small image within the pixel limit loads successfully.""" + import vllm.envs as envs + + image = Image.new("RGB", (100, 100), (255, 0, 0)) + from io import BytesIO + + buf = BytesIO() + image.save(buf, format="PNG") + data = buf.getvalue() + + assert envs.VLLM_MAX_IMAGE_PIXELS >= 100 * 100 + + image_io = ImageMediaIO() + result = image_io.load_bytes(data) + assert result.media.size == (100, 100) + + +def test_image_pixel_limit_rejected(monkeypatch): + """An image exceeding the pixel limit is rejected before raster decode.""" + import vllm.envs as envs + + monkeypatch.setattr(envs, "VLLM_MAX_IMAGE_PIXELS", 100) + + image = Image.new("RGB", (20, 20), (0, 255, 0)) + from io import BytesIO + + buf = BytesIO() + image.save(buf, format="PNG") + data = buf.getvalue() + + image_io = ImageMediaIO() + with pytest.raises(ValueError, match="exceed"): + image_io.load_bytes(data) + + +def test_image_pixel_limit_disabled(monkeypatch): + """Setting VLLM_MAX_IMAGE_PIXELS=0 disables the pixel limit.""" + import vllm.envs as envs + + monkeypatch.setattr(envs, "VLLM_MAX_IMAGE_PIXELS", 0) + + image = Image.new("RGB", (1000, 1000), (0, 0, 255)) + from io import BytesIO + + buf = BytesIO() + image.save(buf, format="PNG") + data = buf.getvalue() + + image_io = ImageMediaIO() + result = image_io.load_bytes(data) + assert result.media.size == (1000, 1000) diff --git a/vllm/envs.py b/vllm/envs.py index ab6184d22d..1f94be8ac5 100755 --- a/vllm/envs.py +++ b/vllm/envs.py @@ -79,6 +79,7 @@ if TYPE_CHECKING: VLLM_MAX_AUDIO_CLIP_FILESIZE_MB: int = 25 VLLM_MAX_AUDIO_DECODE_DURATION_S: int = 600 VLLM_MAX_AUDIO_PREPROCESS_WORKERS: int = max(1, min(os.cpu_count() or 1, 2)) + VLLM_MAX_IMAGE_PIXELS: int = 178_956_970 VLLM_VIDEO_LOADER_BACKEND: str = "opencv" VLLM_MEDIA_CONNECTOR: str = "http" VLLM_MM_HASHER_ALGORITHM: str = "blake3" @@ -954,6 +955,13 @@ environment_variables: dict[str, Callable[[], Any]] = { str(max(1, min(os.cpu_count() or 1, 2))), ) ), + # Maximum decoded image size in pixels. Small compressed images can + # expand into gigabytes of raster memory. This limit is enforced before + # decoding so the memory is never allocated. Default matches PIL's + # built-in 2x decompression-bomb threshold (~179M pixels, ~680 MB RGB). + "VLLM_MAX_IMAGE_PIXELS": lambda: int( + os.getenv("VLLM_MAX_IMAGE_PIXELS", "178956970") + ), # Backend for Video IO — selects the frame-sampling algorithm. # - "opencv": uniform sampling. # - "opencv_dynamic": duration-aware dynamic sampling. @@ -2083,6 +2091,7 @@ def compile_factors() -> dict[str, object]: "VLLM_MAX_AUDIO_CLIP_FILESIZE_MB", "VLLM_MAX_AUDIO_DECODE_DURATION_S", "VLLM_MAX_AUDIO_PREPROCESS_WORKERS", + "VLLM_MAX_IMAGE_PIXELS", "VLLM_VIDEO_LOADER_BACKEND", "VLLM_MEDIA_CONNECTOR", "VLLM_OBJECT_STORAGE_SHM_BUFFER_NAME", diff --git a/vllm/multimodal/media/image.py b/vllm/multimodal/media/image.py index c1a01d555b..1ae57d9d66 100644 --- a/vllm/multimodal/media/image.py +++ b/vllm/multimodal/media/image.py @@ -9,6 +9,7 @@ import pybase64 import torch from PIL import Image +import vllm.envs as envs from vllm.utils.serial_utils import tensor2base64 from ..image import convert_image_mode, normalize_image, rgba_to_rgb @@ -72,6 +73,14 @@ class ImageMediaIO(MediaIO[Image.Image]): def load_bytes(self, data: bytes) -> MediaWithBytes[Image.Image]: try: image = Image.open(BytesIO(data)) + w, h = image.size + max_pixels = envs.VLLM_MAX_IMAGE_PIXELS + if max_pixels > 0 and w * h > max_pixels: + raise ValueError( + f"Image dimensions {w}x{h} ({w * h} pixels) exceed " + f"the maximum of {max_pixels} pixels. Set " + f"VLLM_MAX_IMAGE_PIXELS to increase this limit." + ) image = normalize_image(image) image.load() image = self._convert_image_mode(image) diff --git a/vllm/multimodal/video.py b/vllm/multimodal/video.py index 8cd4870026..725e33e3f8 100644 --- a/vllm/multimodal/video.py +++ b/vllm/multimodal/video.py @@ -13,6 +13,7 @@ import numpy as np import numpy.typing as npt import torch +from vllm import envs from vllm.logger import init_logger from vllm.utils.import_utils import PlaceholderModule from vllm.utils.mem_constants import MiB_bytes @@ -88,6 +89,18 @@ def get_video_loader_backend_for_processor( return VIDEO_LOADER_REGISTRY.get_backend_for_video_processor(video_processor) +def _check_frame_pixel_limit(width: int, height: int) -> None: + """Reject video frames exceeding VLLM_MAX_IMAGE_PIXELS before decoding.""" + max_pixels = envs.VLLM_MAX_IMAGE_PIXELS + if max_pixels > 0 and width * height > max_pixels: + raise ValueError( + f"Video frame dimensions {width}x{height} " + f"({width * height} pixels) exceed the maximum of " + f"{max_pixels} pixels. Set VLLM_MAX_IMAGE_PIXELS to " + f"increase this limit." + ) + + def resize_video(frames: npt.NDArray, size: tuple[int, int]) -> npt.NDArray: num_frames, _, _, channels = frames.shape new_height, new_width = size @@ -733,6 +746,7 @@ class PyNvVideoCodecVideoBackendMixin: temp_file.write(data) gpu_source = cls._read_source_metadata(temp_path, nvc) + _check_frame_pixel_limit(gpu_source.width, gpu_source.height) source = cls._prepare_source(gpu_source.source) frame_idx = cls.compute_frames_index_to_sample( source=source, target=target, **kwargs @@ -835,6 +849,10 @@ class VideoBackend( if backend == "opencv": cap = cls.open_video_capture(data) + _check_frame_pixel_limit( + int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), + int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), + ) source = cls._prepare_source(cls.get_video_metadata(cap)) frame_idx = cls.compute_frames_index_to_sample( source=source, target=target, **kwargs @@ -850,6 +868,8 @@ class VideoBackend( "frame_recovery is only available for `opencv` backend" ) with av.open(BytesIO(data)) as container: + stream = container.streams.video[0] + _check_frame_pixel_limit(stream.width, stream.height) source = cls._prepare_source(cls.get_metadata(container)) frame_idx = cls.compute_frames_index_to_sample( source=source, target=target, **kwargs @@ -1609,6 +1629,10 @@ class Molmo2VideoBackend(VideoLoader, OpenCVVideoBackendMixin): **kwargs, ) -> tuple[npt.NDArray, dict[str, Any]]: cap = cls.open_video_capture(data) + _check_frame_pixel_limit( + int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), + int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), + ) source = OpenCVVideoBackendMixin.get_video_metadata(cap) target = VideoTargetMetadata( @@ -1758,6 +1782,10 @@ class OpenCVDynamicOpenPanguVideoBackend(VideoLoader, OpenCVVideoBackendMixin): Tuple of (frames_array, metadata_dict) """ cap = cls.open_video_capture(data) + _check_frame_pixel_limit( + int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), + int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)), + ) source = OpenCVVideoBackendMixin.get_video_metadata(cap) diff --git a/vllm/transformers_utils/processors/nano_nemotron_vl.py b/vllm/transformers_utils/processors/nano_nemotron_vl.py index 76b73d2163..d48a29d6b4 100644 --- a/vllm/transformers_utils/processors/nano_nemotron_vl.py +++ b/vllm/transformers_utils/processors/nano_nemotron_vl.py @@ -44,12 +44,6 @@ AUDIO_CONTEXT = "" # MAX_FRAMES = 16 DEFAULT_NUM_TILES = 12 -# Configure PIL to handle large images without warnings -# This prevents DecompressionBombWarning for legitimate large images -Image.MAX_IMAGE_PIXELS = None # Disable the limit entirely -# Alternative: Set a specific higher limit -# Image.MAX_IMAGE_PIXELS = 300000000 # ~300M pixels - def calculate_timestamps( indices: list[int] | torch.Tensor, diff --git a/vllm/transformers_utils/processors/nemotron_vl.py b/vllm/transformers_utils/processors/nemotron_vl.py index 6163144bbb..9c5436aa27 100644 --- a/vllm/transformers_utils/processors/nemotron_vl.py +++ b/vllm/transformers_utils/processors/nemotron_vl.py @@ -10,12 +10,6 @@ from vllm.tokenizers.hf import HfTokenizer from .internvl import InternVLImageProcessor, InternVLProcessor -# Configure PIL to handle large images without warnings -# This prevents DecompressionBombWarning for legitimate large images -Image.MAX_IMAGE_PIXELS = None # Disable the limit entirely -# Alternative: Set a specific higher limit -# Image.MAX_IMAGE_PIXELS = 300000000 # ~300M pixels - def build_transform(input_size: int): return T.Compose(