Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix failing exif data validation (#15684)

Maciej Korycinski20929abf68d6
diff --git a/saleor/thumbnail/tests/test_utils.py b/saleor/thumbnail/tests/test_utils.py
index 9d7cd2f1e4..db5d308ed6 100644
--- a/saleor/thumbnail/tests/test_utils.py
+++ b/saleor/thumbnail/tests/test_utils.py
@@ -1,8 +1,10 @@
+from unittest import mock
from unittest.mock import MagicMock
import graphene
import pytest
from django.core.files import File
+from PIL.JpegImagePlugin import JpegImageFile
from .. import FILE_NAME_MAX_LENGTH, ThumbnailFormat
from ..models import Thumbnail
@@ -115,6 +117,27 @@ def test_processed_image_preprocess_method_called(category_with_image, thumb_for
preprocess_mock.assert_called_once()
[email protected]("thumb_format", [ThumbnailFormat.WEBP, ThumbnailFormat.AVIF])
[email protected](JpegImageFile, "_getexif")
+def test_processed_image_preprocess_with_exif_corrupted(
+ mocked_getexif, category_with_image, thumb_format
+):
+ # given
+ image_path = category_with_image.background_image.name
+ processed_image = ProcessedImage(image_path, 128, thumb_format)
+ preprocess_method_name = f"preprocess_{thumb_format.upper()}"
+ preprocess_mock = MagicMock()
+ preprocess_mock.side_effect = getattr(processed_image, preprocess_method_name)
+ setattr(processed_image, preprocess_method_name, preprocess_mock)
+ mocked_getexif.side_effect = SyntaxError()
+
+ # when
+ processed_image.create_thumbnail()
+
+ # then
+ preprocess_mock.assert_called_once()
+
+
def test_get_filename_from_url_unique():
# given
file_format = "jpg"
diff --git a/saleor/thumbnail/utils.py b/saleor/thumbnail/utils.py
index adca9198be..102be5f6cc 100644
--- a/saleor/thumbnail/utils.py
+++ b/saleor/thumbnail/utils.py
@@ -173,7 +173,16 @@ class ProcessedImage:
# Ensuring image is properly rotated
if hasattr(image, "_getexif"):
- exif_datadict = image._getexif() # returns None if no EXIF data
+ try:
+ # validation of the exif data was added in separate PR:
+ # https://github.com/saleor/saleor/pull/11224, it means that there is a
+ # possibility that we could have the file with corrupted exif data.
+ # exif data is only used to apply some optional action on the image,
+ # but without it, we are still able to create a thumbnail.
+ exif_datadict = image._getexif() # returns None if no EXIF data
+ except SyntaxError:
+ exif_datadict = None
+
if exif_datadict is not None:
exif = dict(exif_datadict.items())
orientation = exif.get(self.EXIF_ORIENTATION_KEY, None)