[Security] Bound decoded and downloaded media per request (#61226)

Signed-off-by: juanpas97 <juan.perezalgaba@gmail.com>
Signed-off-by: Juan Pérez de Algaba <jperezde@redhat.com>
Co-authored-by: OpenAI Codex <codex@openai.com>
This commit is contained in:
Juan Pérez de Algaba
2026-10-13 10:17:14 +00:00
committed by GitHub
co-authored by OpenAI Codex
parent 877f6df889
commit 7173c64e2e
16 changed files with 565 additions and 50 deletions
+100
View File
@@ -1,5 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import threading
from concurrent.futures import ThreadPoolExecutor
from io import BytesIO
from pathlib import Path
from unittest.mock import patch
@@ -9,6 +11,7 @@ import pybase64 as base64
import pytest
import soundfile as sf
import vllm.envs as envs
from vllm.multimodal.media import AudioMediaIO
from vllm.multimodal.media import audio as audio_module
from vllm.multimodal.media.audio import (
@@ -16,6 +19,7 @@ from vllm.multimodal.media.audio import (
load_audio_soundfile,
load_audio_torchcodec,
)
from vllm.multimodal.media.base import MediaDecodedBudget
from ...conftest import AudioTestAssets
@@ -118,6 +122,102 @@ def _make_flac_bytes(frames: int, channels: int, samplerate: int) -> bytes:
return buf.getvalue()
@pytest.mark.skip_global_cleanup
def test_request_budget_rejects_second_decoded_audio_clip(monkeypatch):
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 384)
clip = _make_flac_bytes(frames=64, channels=1, samplerate=16000)
audio_io = AudioMediaIO(decoded_budget=MediaDecodedBudget())
audio_io.load_bytes(clip)
with pytest.raises(ValueError, match="VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES"):
audio_io.load_bytes(clip)
@pytest.mark.skip_global_cleanup
def test_request_budget_allows_parallel_audio_decodes(monkeypatch):
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 512)
monkeypatch.setattr(envs, "VLLM_MAX_AUDIO_DECODE_BYTES", 256)
budget = MediaDecodedBudget()
audio_io = AudioMediaIO(decoded_budget=budget)
decoding = threading.Barrier(2)
def decode(*args, **kwargs):
assert kwargs["max_decode_bytes"] == 256
decoding.wait(timeout=5)
return np.zeros(32, dtype=np.float32), 16000
with (
patch.object(audio_module, "load_audio", side_effect=decode),
ThreadPoolExecutor(max_workers=2) as pool,
):
results = list(pool.map(audio_io.load_bytes, [b"a", b"b"]))
assert len(results) == 2
assert budget.used == 256
@pytest.mark.skip_global_cleanup
def test_request_budget_waits_for_inflight_audio_decode(monkeypatch):
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 256)
monkeypatch.setattr(envs, "VLLM_MAX_AUDIO_DECODE_BYTES", 256)
budget = MediaDecodedBudget()
audio_io = AudioMediaIO(decoded_budget=budget)
decoding = threading.Event()
finish_decoding = threading.Event()
second_reservation = threading.Event()
reserve = budget.reserve
decode_calls = 0
def track_reservation(max_bytes):
if decoding.is_set():
second_reservation.set()
return reserve(max_bytes)
def decode(*args, **kwargs):
nonlocal decode_calls
decode_calls += 1
decoding.set()
assert finish_decoding.wait(timeout=5)
return np.zeros(64, dtype=np.float32), 16000
with (
patch.object(budget, "reserve", side_effect=track_reservation),
patch.object(audio_module, "load_audio", side_effect=decode),
ThreadPoolExecutor(max_workers=2) as pool,
):
first = pool.submit(audio_io.load_bytes, b"a")
assert decoding.wait(timeout=5)
second = pool.submit(audio_io.load_bytes, b"b")
try:
assert second_reservation.wait(timeout=5)
finally:
finish_decoding.set()
first.result(timeout=5)
with pytest.raises(ValueError, match="VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES"):
second.result(timeout=5)
assert decode_calls == 1
assert budget.used == 256
@pytest.mark.skip_global_cleanup
def test_request_budget_releases_failed_audio_reservation(monkeypatch):
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 256)
monkeypatch.setattr(envs, "VLLM_MAX_AUDIO_DECODE_BYTES", 256)
budget = MediaDecodedBudget()
audio_io = AudioMediaIO(decoded_budget=budget)
audio = (np.zeros(64, dtype=np.float32), 16000)
with patch.object(
audio_module, "load_audio", side_effect=[ValueError("invalid audio"), audio]
):
with pytest.raises(ValueError, match="invalid audio"):
audio_io.load_bytes(b"a")
assert audio_io.load_bytes(b"b")[0].nbytes == 256
assert budget.used == 256
def test_small_file_passes_memory_guard():
"""A small valid file should pass both duration and memory guards."""
payload = _make_flac_bytes(frames=16000, channels=1, samplerate=16000)
+64 -2
View File
@@ -11,9 +11,9 @@ from vllm.multimodal.image import (
ALLOWED_IMAGE_FORMATS,
open_image,
)
from vllm.multimodal.media import ImageMediaIO
from vllm.multimodal.media import ImageMediaIO, MediaConnector, MediaDecodedBudget
pytestmark = pytest.mark.cpu_test
pytestmark = [pytest.mark.cpu_test, pytest.mark.skip_global_cleanup]
ASSETS_DIR = Path(__file__).parent.parent / "assets"
assert ASSETS_DIR.exists()
@@ -288,6 +288,68 @@ def test_image_pixel_limit_disabled(monkeypatch):
assert result.media.size == (1000, 1000)
def _png_bytes(width: int, height: int) -> bytes:
from io import BytesIO
buf = BytesIO()
Image.new("RGB", (width, height), (1, 2, 3)).save(buf, format="PNG")
return buf.getvalue()
def test_request_decoded_budget_rejects_aggregate(monkeypatch):
import vllm.envs as envs
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 600)
budget = MediaDecodedBudget()
image_io = ImageMediaIO(decoded_budget=budget)
data = _png_bytes(10, 10)
image_io.load_bytes(data)
with pytest.raises(ValueError, match="VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES"):
image_io.load_bytes(data)
def test_request_decoded_budget_accepts_images_within_limit(monkeypatch):
import vllm.envs as envs
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 800)
budget = MediaDecodedBudget()
image_io = ImageMediaIO(decoded_budget=budget)
data = _png_bytes(10, 10)
image_io.load_bytes(data)
image_io.load_bytes(data)
assert budget.used == 800
def test_request_decoded_budget_disabled(monkeypatch):
import vllm.envs as envs
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 0)
budget = MediaDecodedBudget()
image_io = ImageMediaIO(decoded_budget=budget)
data = _png_bytes(100, 100)
image_io.load_bytes(data)
image_io.load_bytes(data)
assert budget.used == 0
def test_media_connector_enforces_request_decoded_budget(monkeypatch):
import pybase64
import vllm.envs as envs
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 600)
b64 = pybase64.b64encode(_png_bytes(10, 10)).decode()
url = f"data:image/png;base64,{b64}"
connector = MediaConnector(decoded_budget=MediaDecodedBudget())
connector.fetch_image(url)
with pytest.raises(ValueError, match="VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES"):
connector.fetch_image(url)
def test_image_media_io_rejects_postscript_payload():
"""EPS/PostScript bytes must not reach an external renderer."""
import pybase64 as base64
@@ -13,11 +13,16 @@ from unittest.mock import AsyncMock, MagicMock, patch
import aiohttp
import pytest
from vllm.connections import MediaDownloadSizeExceededError
from vllm.connections import (
MediaDownloadSizeExceededError,
MediaRequestDownloadSizeExceededError,
)
from vllm.entrypoints.serve import create_error_response
from vllm.exceptions import VLLMClientError, VLLMUnprocessableEntityError
from vllm.multimodal.media import MediaConnector
pytestmark = [pytest.mark.cpu_test, pytest.mark.skip_global_cleanup]
class TestVLLMUnprocessableEntityError:
"""Tests for VLLMUnprocessableEntityError exception."""
@@ -148,6 +153,21 @@ class TestMediaConnectorErrorHandling:
assert "VLLM_MAX_MEDIA_DOWNLOAD_SIZE_MB=256" in str(exc_info.value)
def test_fetch_image_request_download_size_limit_error(self):
connector = MediaConnector()
with patch.object(
connector.connection, "get_bytes", new_callable=MagicMock
) as mock_get:
mock_get.side_effect = MediaRequestDownloadSizeExceededError(
"VLLM_MAX_MEDIA_REQUEST_DOWNLOAD_SIZE_MB=256"
)
with pytest.raises(VLLMUnprocessableEntityError) as exc_info:
connector.fetch_image("https://example.com/image.jpg")
assert "VLLM_MAX_MEDIA_REQUEST_DOWNLOAD_SIZE_MB=256" in str(exc_info.value)
class TestErrorResponse:
"""Tests for error response creation."""
+15
View File
@@ -9,8 +9,10 @@ import numpy as np
import numpy.typing as npt
import pybase64
import pytest
import torch
from PIL import Image
import vllm.envs as envs
from vllm.assets.base import get_vllm_public_assets
from vllm.assets.video import (
video_get_metadata,
@@ -18,6 +20,7 @@ from vllm.assets.video import (
video_to_pil_images_list,
)
from vllm.multimodal.media import ImageMediaIO, VideoMediaIO
from vllm.multimodal.media.base import MediaDecodedBudget
from vllm.multimodal.video import (
PYNVVIDEOCODEC_VIDEO_BACKEND,
VIDEO_LOADER_REGISTRY,
@@ -47,6 +50,18 @@ class Assert10Frames1FPSVideoLoader(VideoLoader):
return FAKE_OUTPUT_2
@pytest.mark.skip_global_cleanup
def test_video_request_budget_counts_torch_frames(monkeypatch: pytest.MonkeyPatch):
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 16)
budget = MediaDecodedBudget()
video_io = VideoMediaIO(ImageMediaIO(decoded_budget=budget))
frames = torch.zeros((2, 2, 2, 3), dtype=torch.uint8)
video_io.video_loader = SimpleNamespace(load_bytes=lambda *_args, **_kwargs: frames)
with pytest.raises(ValueError, match="VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES"):
video_io.load_bytes(b"video")
def test_video_media_io_kwargs(monkeypatch: pytest.MonkeyPatch):
with monkeypatch.context() as m:
m.setenv("VLLM_VIDEO_LOADER_BACKEND", "assert_10_frames_1_fps")