[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")
+51 -3
View File
@@ -2,15 +2,19 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
from collections.abc import AsyncIterator, Iterator
from io import BytesIO
from pathlib import Path
from unittest.mock import patch
import numpy as np
import pytest
from PIL import Image
import vllm.envs as envs
from vllm.connections import HTTPConnection
from vllm.connections import HTTPConnection, MediaRequestDownloadBudget
from vllm.exceptions import VLLMValidationError
from vllm.multimodal.media import AudioMediaIO, MediaConnector
from vllm.multimodal.media import AudioMediaIO, ImageMediaIO, MediaConnector
from vllm.multimodal.media.base import MediaDecodedBudget
from vllm.utils.mem_constants import KiB_bytes, MiB_bytes
pytestmark = [pytest.mark.cpu_test, pytest.mark.skip_global_cleanup]
@@ -87,6 +91,46 @@ def test_audio_load_bytes_rejects_oversized(monkeypatch: pytest.MonkeyPatch):
AudioMediaIO().load_bytes(b"\x00" * (MiB_bytes + 1))
def test_request_audio_budget_limits_multiple_clips_before_decode(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 24)
budget = MediaDecodedBudget()
clip = np.zeros(4, dtype=np.float32)
def decode(_source, **kwargs):
if kwargs["max_decode_bytes"] < clip.nbytes:
raise ValueError("VLLM_MAX_AUDIO_DECODE_BYTES")
return clip, 16000
with patch("vllm.multimodal.media.audio.load_audio", side_effect=decode) as load:
audio_io = AudioMediaIO(decoded_budget=budget)
audio_io.load_bytes(b"first")
with pytest.raises(ValueError, match="VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES"):
audio_io.load_bytes(b"second")
assert budget.used == clip.nbytes
assert load.call_count == 2
def test_request_decoded_budget_is_shared_by_image_and_audio(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES", 24)
budget = MediaDecodedBudget()
image_bytes = BytesIO()
Image.new("RGB", (2, 3)).save(image_bytes, format="PNG")
ImageMediaIO(decoded_budget=budget).load_bytes(image_bytes.getvalue())
with (
patch("vllm.multimodal.media.audio.load_audio") as decode,
pytest.raises(ValueError, match="VLLM_MAX_MEDIA_REQUEST_DECODE_BYTES"),
):
AudioMediaIO(decoded_budget=budget).load_bytes(b"audio")
decode.assert_not_called()
def test_audio_load_file_rejects_oversized(
monkeypatch: pytest.MonkeyPatch, tmp_path: Path
):
@@ -138,12 +182,16 @@ async def test_audio_connector_threads_byte_limit_to_http_reader(
monkeypatch: pytest.MonkeyPatch,
):
monkeypatch.setattr(envs, "VLLM_MAX_AUDIO_CLIP_FILESIZE_MB", 1)
budget = MediaRequestDownloadBudget()
class _Connection:
async def async_get_bytes(self, _url: str, **kwargs):
assert kwargs["max_bytes"] == MiB_bytes
assert kwargs["request_budget"] is budget
raise VLLMValidationError("Maximum file size exceeded")
connector = MediaConnector(connection=_Connection())
connector = MediaConnector( # type: ignore[arg-type]
connection=_Connection(), download_budget=budget
)
with pytest.raises(VLLMValidationError, match="Maximum file size exceeded"):
await connector.fetch_audio_async("https://example.test/audio")
+50 -1
View File
@@ -9,7 +9,13 @@ from unittest.mock import AsyncMock, patch
import pytest
import vllm.envs as envs
from vllm.connections import HTTPConnection, HTTPResponseSizeExceededError
from vllm.connections import (
HTTPConnection,
HTTPResponseSizeExceededError,
MediaRequestDownloadBudget,
)
pytestmark = [pytest.mark.cpu_test, pytest.mark.skip_global_cleanup]
_ONE_MIB = 1024 * 1024
_SMALL_BODY = b"a" * (_ONE_MIB // 2)
@@ -150,6 +156,49 @@ async def test_async_get_bytes_with_limit_streams_instead_of_materializing() ->
assert response.content.iterated == 2
def test_request_download_budget_counts_across_responses(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DOWNLOAD_SIZE_MB", 1)
budget = MediaRequestDownloadBudget()
connection = HTTPConnection()
first = _SyncResponse([b"a" * (3 * _ONE_MIB // 4)])
second = _SyncResponse([b"b" * (_ONE_MIB // 2)])
with patch.object(connection, "get_response", side_effect=[first, second]):
connection.get_bytes("http://example.com/one", request_budget=budget)
with pytest.raises(ValueError, match="VLLM_MAX_MEDIA_REQUEST_DOWNLOAD_SIZE_MB"):
connection.get_bytes("http://example.com/two", request_budget=budget)
assert budget.used == 3 * _ONE_MIB // 4
assert second.iterated == 1
@pytest.mark.asyncio
async def test_async_request_download_budget_counts_across_responses(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(envs, "VLLM_MAX_MEDIA_REQUEST_DOWNLOAD_SIZE_MB", 1)
budget = MediaRequestDownloadBudget()
connection = HTTPConnection()
first = _AsyncResponse([b"a" * (3 * _ONE_MIB // 4)])
second = _AsyncResponse([b"b" * (_ONE_MIB // 2)])
with patch.object(
connection, "get_async_response", new=AsyncMock(side_effect=[first, second])
):
await connection.async_get_bytes(
"http://example.com/one", request_budget=budget
)
with pytest.raises(ValueError, match="VLLM_MAX_MEDIA_REQUEST_DOWNLOAD_SIZE_MB"):
await connection.async_get_bytes(
"http://example.com/two", request_budget=budget
)
assert budget.used == 3 * _ONE_MIB // 4
assert second.content.iterated == 1
def test_get_bytes_rejects_content_length_over_limit(
monkeypatch: pytest.MonkeyPatch, local_media_server: str
) -> None: