Fix DoS via sample-rate forgery bypassing audio decode duration guard (#49948)
Signed-off-by: jperezde <jperezde@redhat.com> Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
37fbf52084
commit
640a09086d
@@ -1,14 +1,16 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
|
||||
from io import BytesIO
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
import numpy as np
|
||||
import pybase64 as base64
|
||||
import pytest
|
||||
import soundfile as sf
|
||||
|
||||
from vllm.multimodal.media import AudioMediaIO
|
||||
from vllm.multimodal.media.audio import load_audio
|
||||
from vllm.multimodal.media.audio import load_audio, load_audio_soundfile
|
||||
|
||||
from ...conftest import AudioTestAssets
|
||||
|
||||
@@ -70,8 +72,6 @@ def test_audio_media_io_encode_base64(dummy_audio):
|
||||
|
||||
def test_load_audio_max_duration_respected(dummy_audio_bytes):
|
||||
"""Valid audio within the duration limit should load successfully."""
|
||||
from io import BytesIO
|
||||
|
||||
y, sr = load_audio(BytesIO(dummy_audio_bytes), sr=None, max_duration_s=3600)
|
||||
assert isinstance(y, np.ndarray)
|
||||
assert len(y) > 0
|
||||
@@ -79,8 +79,6 @@ def test_load_audio_max_duration_respected(dummy_audio_bytes):
|
||||
|
||||
def test_load_audio_max_duration_rejected(dummy_audio_bytes):
|
||||
"""Audio exceeding the duration limit must be rejected during decode."""
|
||||
from io import BytesIO
|
||||
|
||||
with pytest.raises(ValueError, match="exceeds maximum allowed duration"):
|
||||
load_audio(BytesIO(dummy_audio_bytes), sr=None, max_duration_s=0.0001)
|
||||
|
||||
@@ -93,3 +91,70 @@ def test_audio_media_io_from_video(video_assets):
|
||||
audio_ref, sr_ref = load_audio(video_path, sr=None)
|
||||
assert sr == sr_ref
|
||||
np.testing.assert_allclose(audio_ref, audio, atol=1e-4)
|
||||
|
||||
|
||||
def _make_flac_bytes(frames: int, channels: int, samplerate: int) -> bytes:
|
||||
"""Create a minimal FLAC file in memory for testing."""
|
||||
data = np.zeros((frames, channels), dtype=np.int16)
|
||||
buf = BytesIO()
|
||||
sf.write(buf, data, samplerate, format="FLAC")
|
||||
return buf.getvalue()
|
||||
|
||||
|
||||
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)
|
||||
y, sr = load_audio_soundfile(
|
||||
BytesIO(payload),
|
||||
sr=None,
|
||||
max_duration_s=600,
|
||||
max_decode_bytes=256 * 1024 * 1024,
|
||||
)
|
||||
assert isinstance(y, np.ndarray)
|
||||
assert len(y) == 16000
|
||||
|
||||
|
||||
def test_memory_guard_rejects_large_allocation():
|
||||
"""A file whose frames*channels*4 exceeds the byte limit must be
|
||||
rejected before allocating the buffer."""
|
||||
# 100_000 frames * 8 channels * 4 bytes = 3.2 MB
|
||||
payload = _make_flac_bytes(frames=100_000, channels=8, samplerate=48000)
|
||||
# Set limit to 1 MiB — should reject
|
||||
with pytest.raises(ValueError, match="VLLM_MAX_AUDIO_DECODE_BYTES"):
|
||||
load_audio_soundfile(
|
||||
BytesIO(payload),
|
||||
sr=None,
|
||||
max_duration_s=600,
|
||||
max_decode_bytes=1 * 1024 * 1024,
|
||||
)
|
||||
|
||||
|
||||
def test_forged_samplerate_rejected_by_memory_guard():
|
||||
"""The PoC scenario: high sample rate fools the duration guard but
|
||||
the memory guard catches the large frame*channel allocation."""
|
||||
# Forged high sample rate: 655350 Hz, 8 channels, 1M frames
|
||||
# Duration guard sees: 1_000_000 / 655_350 = 1.5s → passes
|
||||
# Memory: 1_000_000 * 8 * 4 = 32 MB
|
||||
payload = _make_flac_bytes(frames=1_000_000, channels=8, samplerate=655350)
|
||||
# Set memory limit to 16 MiB — below the 32 MB allocation
|
||||
with pytest.raises(ValueError, match="VLLM_MAX_AUDIO_DECODE_BYTES"):
|
||||
load_audio_soundfile(
|
||||
BytesIO(payload),
|
||||
sr=None,
|
||||
max_duration_s=600,
|
||||
max_decode_bytes=16 * 1024 * 1024,
|
||||
)
|
||||
|
||||
|
||||
def test_load_audio_threads_max_decode_bytes():
|
||||
"""Verify load_audio passes max_decode_bytes through to backend."""
|
||||
# 50_000 frames * 4 channels * 4 bytes = 800 KB
|
||||
payload = _make_flac_bytes(frames=50_000, channels=4, samplerate=44100)
|
||||
# Limit of 512 KB should reject
|
||||
with pytest.raises(ValueError, match="VLLM_MAX_AUDIO_DECODE_BYTES"):
|
||||
load_audio(
|
||||
BytesIO(payload),
|
||||
sr=None,
|
||||
max_duration_s=600,
|
||||
max_decode_bytes=512 * 1024,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user