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:
Juan Pérez de Algaba
2026-08-10 15:49:17 +00:00
committed by GitHub
co-authored by Cursor
parent 37fbf52084
commit 640a09086d
5 changed files with 126 additions and 7 deletions
+43 -2
View File
@@ -13,6 +13,7 @@ import vllm.envs as envs
from vllm.logger import init_logger
from vllm.multimodal.audio import resample_audio_pyav
from vllm.utils.import_utils import PlaceholderModule
from vllm.utils.mem_constants import MiB_bytes
from vllm.utils.serial_utils import tensor2base64
from vllm.utils.sparse_utils import check_sparse_tensor_invariants_threadsafe
@@ -50,6 +51,7 @@ def load_audio_pyav(
sr: float | None = 22050,
mono: bool = True,
max_duration_s: float | None = None,
max_decode_bytes: int | None = None,
) -> tuple[npt.NDArray, float]:
"""Load an audio file using PyAV (FFmpeg), returning float32 mono waveform.
@@ -103,6 +105,7 @@ def load_audio_pyav(
int(sr * max_duration_s) if max_duration_s is not None else None
)
total_samples = 0
total_decode_bytes = 0
chunks: list[npt.NDArray] = []
needs_resampling = not math.isclose(
@@ -122,10 +125,12 @@ def load_audio_pyav(
for out_frame in resampler.resample(frame):
arr = out_frame.to_ndarray()
total_samples += arr.shape[-1]
total_decode_bytes += arr.nbytes
chunks.append(arr)
else:
arr = frame.to_ndarray()
total_samples += arr.shape[-1]
total_decode_bytes += arr.nbytes
chunks.append(arr)
if max_samples is not None and total_samples > max_samples:
@@ -136,6 +141,18 @@ def load_audio_pyav(
f"VLLM_MAX_AUDIO_DECODE_DURATION_S to "
f"increase this limit."
)
if (
max_decode_bytes is not None
and total_decode_bytes > max_decode_bytes
):
raise ValueError(
f"Audio decode exceeded "
f"{max_decode_bytes / MiB_bytes:.0f} MiB memory "
f"limit ({total_decode_bytes / MiB_bytes:.0f} MiB "
f"decoded so far). Set "
f"VLLM_MAX_AUDIO_DECODE_BYTES to increase this "
f"limit."
)
except (ValueError, ImportError):
raise
except Exception as e:
@@ -160,6 +177,7 @@ def load_audio_soundfile(
sr: float | None = 22050,
mono: bool = True,
max_duration_s: float | None = None,
max_decode_bytes: int | None = None,
) -> tuple[np.ndarray, int]:
"""Load audio via soundfile"""
with soundfile.SoundFile(path) as f:
@@ -174,6 +192,16 @@ def load_audio_soundfile(
f"VLLM_MAX_AUDIO_DECODE_DURATION_S to "
f"increase this limit."
)
if max_decode_bytes is not None:
estimated_bytes = f.frames * f.channels * np.dtype(np.float32).itemsize
if estimated_bytes > max_decode_bytes:
raise ValueError(
f"Audio would allocate {estimated_bytes / MiB_bytes:.0f} "
f"MiB of PCM ({f.frames} frames x {f.channels} channels"
f" x 4B), exceeding the "
f"{max_decode_bytes / MiB_bytes:.0f} MiB limit. Set "
f"VLLM_MAX_AUDIO_DECODE_BYTES to increase this limit."
)
y = f.read(dtype="float32", always_2d=False).T
if mono and y.ndim > 1:
@@ -191,10 +219,15 @@ def load_audio(
sr: float | None = 22050,
mono: bool = True,
max_duration_s: float | None = None,
max_decode_bytes: int | None = None,
):
try:
return load_audio_soundfile(
path, sr=sr, mono=mono, max_duration_s=max_duration_s
path,
sr=sr,
mono=mono,
max_duration_s=max_duration_s,
max_decode_bytes=max_decode_bytes,
)
except ImportError as exc:
# soundfile (or resampy) is not installed — fall through to pyav.
@@ -212,7 +245,13 @@ def load_audio(
if isinstance(path, BytesIO):
path.seek(0)
try:
return load_audio_pyav(path, sr=sr, mono=mono, max_duration_s=max_duration_s)
return load_audio_pyav(
path,
sr=sr,
mono=mono,
max_duration_s=max_duration_s,
max_decode_bytes=max_decode_bytes,
)
except ImportError:
raise # Let PlaceholderModule's message ("install vllm[audio]") propagate.
except Exception as pyav_exc:
@@ -241,6 +280,7 @@ class AudioMediaIO(MediaIO[tuple[npt.NDArray, float]]):
BytesIO(data),
sr=None,
max_duration_s=envs.VLLM_MAX_AUDIO_DECODE_DURATION_S,
max_decode_bytes=envs.VLLM_MAX_AUDIO_DECODE_BYTES,
)
def load_base64(
@@ -255,6 +295,7 @@ class AudioMediaIO(MediaIO[tuple[npt.NDArray, float]]):
filepath,
sr=None,
max_duration_s=envs.VLLM_MAX_AUDIO_DECODE_DURATION_S,
max_decode_bytes=envs.VLLM_MAX_AUDIO_DECODE_BYTES,
)
def encode_base64(