Self hosted FLOSS fitness/workout, nutrition and weight tracker
Reject animated PNGs, truncated files and decompression bombs
Roland Geider37e2928c2f01
diff --git a/wger/utils/images.py b/wger/utils/images.pyindex 98af5f7c6..758a027ae 100644--- a/wger/utils/images.py+++ b/wger/utils/images.py@@ -16,10 +16,7 @@from django.core.exceptions import ValidationError# Third Party-from PIL import (- Image,- UnidentifiedImageError,-)+from PIL import ImageMAX_FILE_SIZE_MB = 20@@ -37,7 +34,10 @@ def validate_image_static_no_animation(value):value.open()img = Image.open(value)img_format = img.format.lower()- except UnidentifiedImageError:+ except Image.DecompressionBombError:+ raise ValidationError('The image dimensions are too large.')+ except OSError:+ # Unidentified as well as cut off within the headerraise ValidationError('File is not a valid image.')# Supported types@@ -47,5 +47,5 @@ def validate_image_static_no_animation(value):)# Check for animation- if img_format in ('webp', 'avif') and getattr(img, 'is_animated'):+ if img_format in ('png', 'webp', 'avif') and getattr(img, 'is_animated'):raise ValidationError('Animated images are not supported.')diff --git a/wger/utils/tests/test_images.py b/wger/utils/tests/test_images.pyindex c2bc8b3e5..aeba8e0ed 100644--- a/wger/utils/tests/test_images.py+++ b/wger/utils/tests/test_images.py@@ -15,7 +15,10 @@# Standard Libraryimport iofrom tempfile import NamedTemporaryFile-from unittest import skipUnless+from unittest import (+ mock,+ skipUnless,+)# Djangofrom django.core.exceptions import ValidationError@@ -73,6 +76,20 @@ class ValidateImageStaticNoAnimationTestCase(SimpleTestCase):with self.assertRaisesMessage(ValidationError, 'Animated images are not supported'):validate_image_static_no_animation(upload(make_image('AVIF', frames=2)))+ def test_animated_png_is_rejected(self):+ with self.assertRaisesMessage(ValidationError, 'Animated images are not supported'):+ validate_image_static_no_animation(upload(make_image('PNG', frames=2)))++ def test_truncated_image_is_rejected(self):+ with self.assertRaisesMessage(ValidationError, 'File is not a valid image'):+ validate_image_static_no_animation(upload(make_image('JPEG')[:200]))++ def test_decompression_bomb_is_rejected(self):+ # 16 pixels are more than twice the limit, which is where Pillow raises+ with mock.patch.object(Image, 'MAX_IMAGE_PIXELS', 7):+ with self.assertRaisesMessage(ValidationError, 'The image dimensions are too large'):+ validate_image_static_no_animation(upload(make_image('PNG')))+def test_files_that_are_no_image_are_rejected(self):for content in (b'', b'not an image'):with self.subTest(content=content):