[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:
co-authored by
OpenAI Codex
parent
877f6df889
commit
7173c64e2e
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user