Skip to content

Commit

Permalink
Fix: bring the image size limit of PIL in line
Browse files Browse the repository at this point in the history
  • Loading branch information
ahyangyi authored and TrueBrain committed Jun 15, 2023
1 parent 116da8e commit c539d57
Showing 1 changed file with 6 additions and 5 deletions.
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


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.")
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.")

self.size = im.size

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

0 comments on commit c539d57

Please sign in to comment.