fix(security): prevent image decompression bomb OOM denial of service (#47010)
Signed-off-by: jperezde <jperezde@redhat.com>
This commit is contained in:
@@ -205,3 +205,57 @@ def test_image_media_io_load_file(tmp_path):
|
||||
|
||||
with pytest.raises(ValueError, match="Failed to load image"):
|
||||
image_io.load_file(truncated_real_file)
|
||||
|
||||
|
||||
def test_image_pixel_limit_respected():
|
||||
"""A small image within the pixel limit loads successfully."""
|
||||
import vllm.envs as envs
|
||||
|
||||
image = Image.new("RGB", (100, 100), (255, 0, 0))
|
||||
from io import BytesIO
|
||||
|
||||
buf = BytesIO()
|
||||
image.save(buf, format="PNG")
|
||||
data = buf.getvalue()
|
||||
|
||||
assert envs.VLLM_MAX_IMAGE_PIXELS >= 100 * 100
|
||||
|
||||
image_io = ImageMediaIO()
|
||||
result = image_io.load_bytes(data)
|
||||
assert result.media.size == (100, 100)
|
||||
|
||||
|
||||
def test_image_pixel_limit_rejected(monkeypatch):
|
||||
"""An image exceeding the pixel limit is rejected before raster decode."""
|
||||
import vllm.envs as envs
|
||||
|
||||
monkeypatch.setattr(envs, "VLLM_MAX_IMAGE_PIXELS", 100)
|
||||
|
||||
image = Image.new("RGB", (20, 20), (0, 255, 0))
|
||||
from io import BytesIO
|
||||
|
||||
buf = BytesIO()
|
||||
image.save(buf, format="PNG")
|
||||
data = buf.getvalue()
|
||||
|
||||
image_io = ImageMediaIO()
|
||||
with pytest.raises(ValueError, match="exceed"):
|
||||
image_io.load_bytes(data)
|
||||
|
||||
|
||||
def test_image_pixel_limit_disabled(monkeypatch):
|
||||
"""Setting VLLM_MAX_IMAGE_PIXELS=0 disables the pixel limit."""
|
||||
import vllm.envs as envs
|
||||
|
||||
monkeypatch.setattr(envs, "VLLM_MAX_IMAGE_PIXELS", 0)
|
||||
|
||||
image = Image.new("RGB", (1000, 1000), (0, 0, 255))
|
||||
from io import BytesIO
|
||||
|
||||
buf = BytesIO()
|
||||
image.save(buf, format="PNG")
|
||||
data = buf.getvalue()
|
||||
|
||||
image_io = ImageMediaIO()
|
||||
result = image_io.load_bytes(data)
|
||||
assert result.media.size == (1000, 1000)
|
||||
|
||||
Reference in New Issue
Block a user