Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: bring the image size limit of PIL in line #373

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 6 additions & 5 deletions bananas_api/new_upload/readers/heightmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
from ..exceptions import ValidationException
from ...helpers.enums import PackageType

# Limit the size of heightmaps, as otherwise it could allocate a lot of
# memory for clients loading the map.
Image.MAX_IMAGE_PIXELS = 16384 * 16384
TrueBrain marked this conversation as resolved.
Show resolved Hide resolved


def rgb_to_gray(color):
return ((color[0] * 19595) + (color[1] * 38470) + (color[2] * 7471)) // 65536
Expand Down Expand Up @@ -47,14 +51,11 @@ def read(self, fp):

try:
im = Image.open(fp)
except Image.DecompressionBombError:
raise ValidationException("Image is too large.")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the number of pixels is greater than twice [MAX_IMAGE_PIXELS](https://pillow.readthedocs.io/en/stable/reference/Image.html#PIL.Image.MAX_IMAGE_PIXELS), then a DecompressionBombError will be raised instead.

I was wondering why my 20kx20k map wasn't triggering this, but I have to go slightly bigger before it does so :) Seems we need both statements or something to make this work properly :)

except Exception:
raise ValidationException("File is not a valid image.")

# Limit the size of heightmaps, as otherwise it could allocate a lot of
# memory for clients loading the map.
if im.width * im.height > 16384 * 16384:
raise ValidationException("Image is too large.")
TrueBrain marked this conversation as resolved.
Show resolved Hide resolved

self.size = im.size

# The following code is based on https://github.com/OpenTTD/OpenTTD/blob/master/src/heightmap.cpp
Expand Down