Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fix /upload 500'ing when presented a very large image (#10029)
Browse files Browse the repository at this point in the history
* Fix /upload 500'ing when presented a very large image

Catch DecompressionBombError and re-raise as ThumbnailErrors

* Set PIL's MAX_IMAGE_PIXELS to match homeserver.yaml

to get it to bomb out quicker, to load less into memory
in the case of super large images

* Add changelog entry for 10029
  • Loading branch information
t3chguy authored May 21, 2021
1 parent 21bd230 commit e8ac9ac
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions changelog.d/10029.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed a bug with very high resolution image uploads throwing internal server errors.
2 changes: 2 additions & 0 deletions synapse/rest/media/v1/media_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ def __init__(self, hs: "HomeServer"):
self.max_upload_size = hs.config.max_upload_size
self.max_image_pixels = hs.config.max_image_pixels

Thumbnailer.set_limits(self.max_image_pixels)

self.primary_base_path = hs.config.media_store_path # type: str
self.filepaths = MediaFilePaths(self.primary_base_path) # type: MediaFilePaths

Expand Down
9 changes: 9 additions & 0 deletions synapse/rest/media/v1/thumbnailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,22 @@ class Thumbnailer:

FORMATS = {"image/jpeg": "JPEG", "image/png": "PNG"}

@staticmethod
def set_limits(max_image_pixels: int):
Image.MAX_IMAGE_PIXELS = max_image_pixels

def __init__(self, input_path: str):
try:
self.image = Image.open(input_path)
except OSError as e:
# If an error occurs opening the image, a thumbnail won't be able to
# be generated.
raise ThumbnailError from e
except Image.DecompressionBombError as e:
# If an image decompression bomb error occurs opening the image,
# then the image exceeds the pixel limit and a thumbnail won't
# be able to be generated.
raise ThumbnailError from e

self.width, self.height = self.image.size
self.transpose_method = None
Expand Down

0 comments on commit e8ac9ac

Please sign in to comment.