[Security] Fix DoS via audio decompression bomb in speech-to-text endpoint (#44970)
Signed-off-by: jperezde <jperezde@redhat.com>
This commit is contained in:
@@ -68,6 +68,23 @@ def test_audio_media_io_encode_base64(dummy_audio):
|
||||
mock_write.assert_called_once()
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
def test_audio_media_io_from_video(video_assets):
|
||||
audio_io = AudioMediaIO()
|
||||
video_path = video_assets[0].video_path
|
||||
|
||||
@@ -115,6 +115,7 @@ class OpenAISpeechToText(OpenAIServing):
|
||||
self.enable_force_include_usage = enable_force_include_usage
|
||||
|
||||
self.max_audio_filesize_mb = envs.VLLM_MAX_AUDIO_CLIP_FILESIZE_MB
|
||||
self.max_audio_decode_duration_s: int = envs.VLLM_MAX_AUDIO_DECODE_DURATION_S
|
||||
if self.model_cls.supports_segment_timestamp:
|
||||
self.tokenizer = cast(
|
||||
PreTrainedTokenizerBase,
|
||||
@@ -216,7 +217,11 @@ class OpenAISpeechToText(OpenAIServing):
|
||||
# pre-requisite for chunking, as it assumes Whisper SR.
|
||||
try:
|
||||
with io.BytesIO(audio_data) as buf:
|
||||
y, sr = load_audio(buf, sr=self.asr_config.sample_rate)
|
||||
y, sr = load_audio(
|
||||
buf,
|
||||
sr=self.asr_config.sample_rate,
|
||||
max_duration_s=self.max_audio_decode_duration_s,
|
||||
)
|
||||
except Exception as exc:
|
||||
raise ValueError("Invalid or unsupported audio file.") from exc
|
||||
|
||||
|
||||
@@ -78,6 +78,7 @@ if TYPE_CHECKING:
|
||||
VLLM_MEDIA_URL_ALLOW_REDIRECTS: bool = True
|
||||
VLLM_MEDIA_LOADING_THREAD_COUNT: int = 8
|
||||
VLLM_MAX_AUDIO_CLIP_FILESIZE_MB: int = 25
|
||||
VLLM_MAX_AUDIO_DECODE_DURATION_S: int = 600
|
||||
VLLM_VIDEO_LOADER_BACKEND: str = "opencv"
|
||||
VLLM_MEDIA_CONNECTOR: str = "http"
|
||||
VLLM_MM_HASHER_ALGORITHM: str = "blake3"
|
||||
@@ -955,6 +956,13 @@ environment_variables: dict[str, Callable[[], Any]] = {
|
||||
"VLLM_MAX_AUDIO_CLIP_FILESIZE_MB": lambda: int(
|
||||
os.getenv("VLLM_MAX_AUDIO_CLIP_FILESIZE_MB", "25")
|
||||
),
|
||||
# Maximum decoded audio duration in seconds. Compressed audio files
|
||||
# (e.g. OPUS at very low bitrate) can expand into gigabytes of float32
|
||||
# PCM. This limit is enforced *during* decoding so the memory is never
|
||||
# allocated. Default is 600s (10 minutes).
|
||||
"VLLM_MAX_AUDIO_DECODE_DURATION_S": lambda: int(
|
||||
os.getenv("VLLM_MAX_AUDIO_DECODE_DURATION_S", "600")
|
||||
),
|
||||
# Backend for Video IO — selects the frame-sampling algorithm.
|
||||
# - "opencv": uniform sampling.
|
||||
# - "opencv_dynamic": duration-aware dynamic sampling.
|
||||
@@ -2156,6 +2164,7 @@ def compile_factors() -> dict[str, object]:
|
||||
"VLLM_MEDIA_URL_ALLOW_REDIRECTS",
|
||||
"VLLM_MEDIA_LOADING_THREAD_COUNT",
|
||||
"VLLM_MAX_AUDIO_CLIP_FILESIZE_MB",
|
||||
"VLLM_MAX_AUDIO_DECODE_DURATION_S",
|
||||
"VLLM_VIDEO_LOADER_BACKEND",
|
||||
"VLLM_MEDIA_CONNECTOR",
|
||||
"VLLM_OBJECT_STORAGE_SHM_BUFFER_NAME",
|
||||
|
||||
@@ -47,6 +47,7 @@ def load_audio_pyav(
|
||||
*,
|
||||
sr: float | None = 22050,
|
||||
mono: bool = True,
|
||||
max_duration_s: float | None = None,
|
||||
) -> tuple[npt.NDArray, float]:
|
||||
"""Load an audio file using PyAV (FFmpeg), returning float32 mono waveform.
|
||||
|
||||
@@ -57,6 +58,10 @@ def load_audio_pyav(
|
||||
Args:
|
||||
path: A :class:`~io.BytesIO` buffer, a filesystem
|
||||
:class:`~pathlib.Path`, or a string path.
|
||||
max_duration_s: If set, abort decoding once the accumulated
|
||||
sample count exceeds this many seconds of audio. Prevents
|
||||
decompression-bomb attacks where a small compressed file
|
||||
expands into gigabytes of PCM.
|
||||
|
||||
Returns:
|
||||
``(waveform, sample_rate)`` where *waveform* is a 1-D float32
|
||||
@@ -72,6 +77,30 @@ def load_audio_pyav(
|
||||
native_sr = stream.rate
|
||||
sr = sr or native_sr
|
||||
|
||||
# Early rejection from container/stream metadata to avoid
|
||||
# wasting resources on decoding decompression bombs.
|
||||
if max_duration_s is not None:
|
||||
metadata_duration_s = None
|
||||
if stream.duration and stream.time_base:
|
||||
metadata_duration_s = float(stream.duration * stream.time_base)
|
||||
elif container.duration:
|
||||
metadata_duration_s = container.duration / 1_000_000
|
||||
if (
|
||||
metadata_duration_s is not None
|
||||
and metadata_duration_s > max_duration_s
|
||||
):
|
||||
raise ValueError(
|
||||
f"Audio exceeds maximum allowed duration of "
|
||||
f"{max_duration_s}s (metadata reports "
|
||||
f"{metadata_duration_s:.1f}s). This limit "
|
||||
f"prevents decompression-bomb attacks."
|
||||
)
|
||||
|
||||
max_samples = (
|
||||
int(sr * max_duration_s) if max_duration_s is not None else None
|
||||
)
|
||||
total_samples = 0
|
||||
|
||||
chunks: list[npt.NDArray] = []
|
||||
needs_resampling = not math.isclose(
|
||||
float(sr),
|
||||
@@ -88,9 +117,21 @@ def load_audio_pyav(
|
||||
if needs_resampling:
|
||||
assert resampler is not None
|
||||
for out_frame in resampler.resample(frame):
|
||||
chunks.append(out_frame.to_ndarray())
|
||||
arr = out_frame.to_ndarray()
|
||||
total_samples += arr.shape[-1]
|
||||
chunks.append(arr)
|
||||
else:
|
||||
chunks.append(frame.to_ndarray())
|
||||
arr = frame.to_ndarray()
|
||||
total_samples += arr.shape[-1]
|
||||
chunks.append(arr)
|
||||
|
||||
if max_samples is not None and total_samples > max_samples:
|
||||
raise ValueError(
|
||||
f"Audio exceeds maximum allowed duration of "
|
||||
f"{max_duration_s}s (decoded {total_samples} "
|
||||
f"samples at {sr}Hz). This limit prevents "
|
||||
f"decompression-bomb attacks."
|
||||
)
|
||||
except (ValueError, ImportError):
|
||||
raise
|
||||
except Exception as e:
|
||||
@@ -114,10 +155,20 @@ def load_audio_soundfile(
|
||||
*,
|
||||
sr: float | None = 22050,
|
||||
mono: bool = True,
|
||||
max_duration_s: float | None = None,
|
||||
) -> tuple[np.ndarray, int]:
|
||||
"""Load audio via soundfile"""
|
||||
with soundfile.SoundFile(path) as f:
|
||||
native_sr = f.samplerate
|
||||
if max_duration_s is not None:
|
||||
file_duration_s = f.frames / native_sr
|
||||
if file_duration_s > max_duration_s:
|
||||
raise ValueError(
|
||||
f"Audio exceeds maximum allowed duration of "
|
||||
f"{max_duration_s}s (file contains "
|
||||
f"{file_duration_s:.1f}s at {native_sr}Hz). "
|
||||
f"This limit prevents decompression-bomb attacks."
|
||||
)
|
||||
y = f.read(dtype="float32", always_2d=False).T
|
||||
|
||||
if mono and y.ndim > 1:
|
||||
@@ -134,9 +185,12 @@ def load_audio(
|
||||
*,
|
||||
sr: float | None = 22050,
|
||||
mono: bool = True,
|
||||
max_duration_s: float | None = None,
|
||||
):
|
||||
try:
|
||||
return load_audio_soundfile(path, sr=sr, mono=mono)
|
||||
return load_audio_soundfile(
|
||||
path, sr=sr, mono=mono, max_duration_s=max_duration_s
|
||||
)
|
||||
except ImportError as exc:
|
||||
# soundfile (or resampy) is not installed — fall through to pyav.
|
||||
# NOTE: this clause must stay BEFORE ``soundfile.LibsndfileError``
|
||||
@@ -153,7 +207,7 @@ def load_audio(
|
||||
if isinstance(path, BytesIO):
|
||||
path.seek(0)
|
||||
try:
|
||||
return load_audio_pyav(path, sr=sr, mono=mono)
|
||||
return load_audio_pyav(path, sr=sr, mono=mono, max_duration_s=max_duration_s)
|
||||
except ImportError:
|
||||
raise # Let PlaceholderModule's message ("install vllm[audio]") propagate.
|
||||
except Exception as pyav_exc:
|
||||
|
||||
Reference in New Issue
Block a user