[Security] Restrict Pillow image formats on untrusted media paths (#60022)

Signed-off-by: Juan Pérez de Algaba <jperezde@redhat.com>
This commit is contained in:
Juan Pérez de Algaba
2026-10-05 10:40:15 +00:00
committed by GitHub
parent edca360f13
commit 710ac56e69
4 changed files with 135 additions and 5 deletions
+74
View File
@@ -1,11 +1,16 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import inspect
from pathlib import Path
import numpy as np
import pytest
from PIL import Image
from vllm.multimodal.image import (
ALLOWED_IMAGE_FORMATS,
open_image,
)
from vllm.multimodal.media import ImageMediaIO
pytestmark = pytest.mark.cpu_test
@@ -281,3 +286,72 @@ def test_image_pixel_limit_disabled(monkeypatch):
image_io = ImageMediaIO()
result = image_io.load_bytes(data)
assert result.media.size == (1000, 1000)
def test_image_media_io_rejects_postscript_payload():
"""EPS/PostScript bytes must not reach an external renderer."""
import pybase64 as base64
eps = b"""%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 10 10
%%EndComments
0 0 moveto 10 10 lineto stroke
showpage
"""
image_io = ImageMediaIO()
with pytest.raises(ValueError, match="Failed to load image"):
image_io.load_bytes(eps)
payload = base64.b64encode(eps).decode("ascii")
with pytest.raises(ValueError, match="Failed to load image"):
image_io.load_base64("image/png", payload)
def test_image_media_io_accepts_common_raster_formats(tmp_path):
"""Common Pillow-writable raster formats remain loadable."""
image_io = ImageMediaIO()
formats = (
("PNG", "ok.png"),
("JPEG", "ok.jpg"),
("WEBP", "ok.webp"),
("GIF", "ok.gif"),
("BMP", "ok.bmp"),
("TIFF", "ok.tiff"),
("PPM", "ok.ppm"),
("TGA", "ok.tga"),
("DDS", "ok.dds"),
("PCX", "ok.pcx"),
("SGI", "ok.sgi"),
("QOI", "ok.qoi"),
("JPEG2000", "ok.jp2"),
)
for fmt, name in formats:
path = tmp_path / name
Image.new("RGB", (4, 4), (1, 2, 3)).save(path, format=fmt)
result = image_io.load_bytes(path.read_bytes())
assert result.media.size == (4, 4)
def test_allowed_image_formats_exclude_external_renderer_plugins():
"""Formats that shell out or use stubs stay off the allowlist."""
denied = {"EPS", "WMF", "BUFR", "GRIB", "HDF5", "MPEG"}
assert denied.isdisjoint(ALLOWED_IMAGE_FORMATS)
def test_kimi_fused_vision_rejects_postscript_payload():
"""Kimi fused vision must use the same image format allowlist."""
from vllm.transformers_utils.processors.kimi_k25_vision_fused import _to_pil
eps = b"""%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 10 10
%%EndComments
0 0 moveto 10 10 lineto stroke
showpage
"""
with pytest.raises(Image.UnidentifiedImageError):
_to_pil(eps)
def test_open_image_does_not_accept_format_override():
"""The allowlist helper must not expose a formats override."""
assert "formats" not in inspect.signature(open_image).parameters
+55
View File
@@ -2,9 +2,64 @@
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import contextlib
from typing import Any
from PIL import Image, ImageOps
# Formats accepted by Image.open for untrusted multimodal media.
#
# Audited against Pillow's registered open plugins (python -m PIL / Image.OPEN):
# Allowed: native still-image / raster readers that decode in-process.
# Denied:
# EPS — may invoke Ghostscript on untrusted PostScript
# WMF — Windows GDI / stub-handler path
# BUFR / GRIB / HDF5 — stub drivers requiring external handlers
# MPEG — identify-only; not a decodable still image
ALLOWED_IMAGE_FORMATS: tuple[str, ...] = (
"AVIF",
"BLP",
"BMP",
"CUR",
"DCX",
"DDS",
"DIB",
"FITS",
"FLI",
"FTEX",
"GBR",
"GIF",
"ICNS",
"ICO",
"IM",
"IMT",
"IPTC",
"JPEG",
"JPEG2000",
"MCIDAS",
"MSP",
"PCD",
"PCX",
"PIXAR",
"PNG",
"PPM",
"PSD",
"QOI",
"SGI",
"SPIDER",
"SUN",
"TGA",
"TIFF",
"WEBP",
"XBM",
"XPM",
"XVTHUMB",
)
def open_image(fp: Any) -> Image.Image:
"""Open image bytes/files with the serving format allowlist."""
return Image.open(fp, formats=ALLOWED_IMAGE_FORMATS)
def rescale_image_size(
image: Image.Image, size_factor: float, transpose: int = -1
+2 -2
View File
@@ -16,7 +16,7 @@ from vllm.utils.sparse_utils import (
safe_to_dense,
)
from ..image import convert_image_mode, normalize_image, rgba_to_rgb
from ..image import convert_image_mode, normalize_image, open_image, rgba_to_rgb
from .base import MediaIO, MediaWithBytes
MAGIC_NUMPY_PREFIX = b"\x93NUMPY" # https://numpy.org/devdocs/reference/generated/numpy.lib.format.html#format-version-1-0
@@ -78,7 +78,7 @@ class ImageMediaIO(MediaIO[Image.Image]):
def load_bytes(self, data: bytes) -> MediaWithBytes[Image.Image]:
try:
image = Image.open(BytesIO(data))
image = open_image(BytesIO(data))
w, h = image.size
max_pixels = envs.VLLM_MAX_IMAGE_PIXELS
if max_pixels > 0 and w * h > max_pixels:
@@ -14,6 +14,7 @@ from PIL import Image
from transformers.image_processing_utils import BaseImageProcessor, BatchFeature
from transformers.utils import TensorType
from vllm.multimodal.image import open_image
from vllm.utils.import_utils import is_numba_available
from vllm.utils.jit_monitor import numba_workqueue_threading_layer
@@ -151,10 +152,10 @@ def _to_pil(data: Any) -> Image.Image:
if isinstance(data, str):
if data.startswith("data:"):
raw_base64 = data.split(",", 1)[1]
return Image.open(io.BytesIO(base64.b64decode(raw_base64))).convert("RGB")
return Image.open(data).convert("RGB")
return open_image(io.BytesIO(base64.b64decode(raw_base64))).convert("RGB")
return open_image(data).convert("RGB")
if isinstance(data, bytes):
return Image.open(io.BytesIO(data)).convert("RGB")
return open_image(io.BytesIO(data)).convert("RGB")
raise ValueError(f"Unsupported data type: {type(data)}")