Skip to content

Commit 7c0c467

Browse files
BUG: Avoid completely hiding image loading issues like exceeding image size limits (#3221)
Closes #3220.
1 parent 24b81eb commit 7c0c467

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

pypdf/_xobj_image_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ def _handle_flate(
264264
# Stream Dictionary
265265
mode2 = _get_imagemode(color_space, colors, mode)[0]
266266
if mode != mode2:
267-
img = Image.frombytes(mode2, size, data) # reloaded as mode may have change
267+
img = Image.frombytes(mode2, size, data) # reloaded as mode may have changed
268268
if mode == "CMYK":
269269
extension = ".tif"
270270
image_format = "TIFF"

pypdf/filters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ def _apply_alpha(
840840

841841
try: # temporary try/except until other fixes of images
842842
img = Image.open(BytesIO(data))
843-
except Exception:
843+
except Exception as exception:
844+
logger_warning(f"Failed loading image: {exception}", __name__)
844845
img = None # type: ignore
845846
return extension, data, img

tests/test_filters.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,3 +656,19 @@ def test_ccitt_fax_decode__black_is_1():
656656
expected_pixels = list(ImageOps.invert(expected_image_inverted).getdata())
657657
actual_pixels = list(actual_image.getdata())
658658
assert expected_pixels == actual_pixels
659+
660+
661+
@pytest.mark.enable_socket
662+
def test_flate_decode__image_is_none_due_to_size_limit(caplog):
663+
url = "https://github.com/user-attachments/files/19464256/file.pdf"
664+
name = "issue3220.pdf"
665+
reader = PdfReader(BytesIO(get_data_from_url(url, name=name)))
666+
images = reader.pages[0].images
667+
assert len(images) == 1
668+
image = images[0]
669+
assert image.name == "Im0.png"
670+
assert image.image is None
671+
assert (
672+
"Failed loading image: Image size (180000000 pixels) exceeds limit of "
673+
"178956970 pixels, could be decompression bomb DOS attack."
674+
) in caplog.messages

0 commit comments

Comments
 (0)