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

validate image size #615

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions libs/XSSImageCheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
MAX_AVATAR_SIZE = 1024 * 1024
MIN_AVATAR_SIZE = 64
IMG_FORMATS = ["png", "jpeg", "jpg", "gif", "bmp"]

IMG_SIZE = [500, 250]

def is_xss_image(data):
# str(char) works here for both py2 & py3
Expand Down Expand Up @@ -95,6 +95,13 @@ def existing_avatars(dir):
avatars.append(user.avatar)
return avatars

def verify_image_size(image_data):
image = Image.open(io.BytesIO(image_data))
if image.width < IMG_SIZE[0] or image.height < IMG_SIZE[1]:
raise ValidationError(
"Image is too small, minimum size %d x %d"
% (IMG_SIZE[0], IMG_SIZE[1])
)

def avatar_validation(image_data) -> str:
"""Avatar validation check
Expand All @@ -104,6 +111,7 @@ def avatar_validation(image_data) -> str:
if MIN_AVATAR_SIZE < len(image_data) < MAX_AVATAR_SIZE:
ext = imghdr.what("", h=image_data)
if ext in IMG_FORMATS and not is_xss_image(image_data):
verify_image_size(image_data)
return ext
else:
raise ValidationError(
Expand Down Expand Up @@ -131,7 +139,7 @@ def save_avatar(path: str, image_data: bytes) -> str:
os.unlink(image_path)

image = Image.open(io.BytesIO(image_data))
cover = resizeimage.resize_cover(image, [500, 250])
cover = resizeimage.resize_cover(image, IMG_SIZE)
cover.save(image_path, image.format)
return str(base_path)

Expand Down
Loading