Self hosted FLOSS fitness/workout, nutrition and weight tracker
Add validators for downloaded images and videos
Roland Geiderde2d6d7e6b45
diff --git a/wger/exercises/sync.py b/wger/exercises/sync.pyindex 95cb0ab33..e8b205743 100644--- a/wger/exercises/sync.py+++ b/wger/exercises/sync.py@@ -17,6 +17,7 @@ import os# Djangofrom django.conf import settings+from django.core.exceptions import ValidationErrorfrom django.core.files import Filefrom django.core.files.temp import NamedTemporaryFile@@ -53,6 +54,7 @@ from wger.exercises.models import (Muscle,Translation,)+from wger.exercises.models.video import validate_videofrom wger.manager.models import (SlotEntry,WorkoutLog,@@ -410,7 +412,11 @@ def download_exercise_images(except ExerciseImage.DoesNotExist:print_fn(' Image not found in local DB, creating now...')retrieved_image = requests.get(image_data['image'], headers=headers)- ExerciseImage.from_json(exercise, retrieved_image, image_data)+ try:+ ExerciseImage.from_json(exercise, retrieved_image, image_data)+ except ValidationError as e:+ print_fn(style_fn(f' invalid image, skipping: {"; ".join(e.messages)}'))+ continueprint_fn(style_fn(' successfully saved'))@@ -466,9 +472,17 @@ def download_exercise_videos(img_temp.write(retrieved_video.content)img_temp.flush()+ # Validate file+ video_file = File(img_temp)+ try:+ validate_video(video_file)+ except ValidationError as e:+ print_fn(style_fn(f' invalid video, skipping: {"; ".join(e.messages)}'))+ continue+video.video.save(os.path.basename(os.path.basename(video_data['video'])),- File(img_temp),+ video_file,)video.save()print_fn(style_fn(' saved successfully'))diff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.pyindex 679bf2ab6..bc3690830 100644--- a/wger/exercises/tests/test_sync.py+++ b/wger/exercises/tests/test_sync.py@@ -13,8 +13,15 @@# You should have received a copy of the GNU Affero General Public License# Standard Library+import iofrom unittest.mock import patch+# Django+from django.core.exceptions import ValidationError++# Third Party+from PIL import Image as PILImage+# wgerfrom wger.core.models import (Language,@@ -26,11 +33,13 @@ from wger.exercises.models import (Exercise,ExerciseCategory,ExerciseImage,+ ExerciseVideo,Muscle,Translation,)from wger.exercises.sync import (download_exercise_images,+ download_exercise_videos,handle_deleted_entries,sync_categories,sync_equipment,@@ -46,6 +55,13 @@ from wger.manager.models import (from wger.utils.requests import wger_headers+def _valid_png_bytes():+ """Return the bytes of a minimal valid PNG, used as fake downloaded image content."""+ buffer = io.BytesIO()+ PILImage.new('RGB', (1, 1)).save(buffer, format='PNG')+ return buffer.getvalue()++class MockLanguageResponse:def __init__(self):self.status_code = 200@@ -574,7 +590,7 @@ class MockExerciseResponse:class MockImageResponse:def __init__(self):self.status_code = 200- self.content = b''+ self.content = _valid_png_bytes()# yapf: disable@staticmethod@@ -624,6 +640,72 @@ class MockImageResponse:# yapf: enable+class MockInvalidImageResponse:+ """Image endpoint returns one record whose downloaded bytes are not a valid image."""++ def __init__(self):+ self.status_code = 200+ self.content = b'<html><script>alert(1)</script></html>'++ @staticmethod+ def json():+ return {+ 'count': 1,+ 'next': None,+ 'previous': None,+ 'results': [+ {+ 'id': 99,+ 'uuid': '00000099-1d00-4e9d-a1a4-5f5ebd15e819',+ 'exercise': 2,+ 'exercise_uuid': 'ae3328ba-9a35-4731-bc23-5da50720c5aa',+ 'image': 'https://wger.de/media/exercise-images/2/evil.jpg',+ 'is_main': False,+ 'style': '4',+ 'license': 1,+ 'license_title': 'Bad image',+ 'license_object_url': '',+ 'license_author': 'Attacker',+ 'license_author_url': '',+ 'license_derivative_source_url': '',+ 'author_history': [],+ }+ ],+ }+++class MockVideoResponse:+ def __init__(self):+ self.status_code = 200+ self.content = b'fake video bytes'++ @staticmethod+ def json():+ return {+ 'count': 1,+ 'next': None,+ 'previous': None,+ 'results': [+ {+ 'id': 88,+ 'uuid': '00000088-1d00-4e9d-a1a4-5f5ebd15e819',+ 'exercise': 2,+ 'exercise_uuid': 'ae3328ba-9a35-4731-bc23-5da50720c5aa',+ 'video': 'https://wger.de/media/exercise-video/2/evil.html',+ 'is_main': False,+ 'license': 1,+ 'license_author': 'Attacker',+ 'size': 123,+ 'width': 0,+ 'height': 0,+ 'codec': '',+ 'codec_long': '',+ 'duration': 0,+ }+ ],+ }++class TestSyncMethods(WgerTestCase):@patch('requests.get', return_value=MockLanguageResponse())def test_language_sync(self, mock_request):@@ -851,3 +933,33 @@ class TestSyncMethods(WgerTestCase):self.assertEqual(new_image.license_id, 1)self.assertEqual(new_image.license_title, 'New Image Title')self.assertEqual(new_image.license_author, 'New Author')++ @patch('requests.get', return_value=MockInvalidImageResponse())+ def test_image_sync_skips_invalid_image(self, mock_request):+ """A download whose bytes are not a valid image is skipped, not stored."""++ count_before = ExerciseImage.objects.count()++ # A malformed image must not abort the run or get stored+ download_exercise_images()++ self.assertFalse(+ ExerciseImage.objects.filter(uuid='00000099-1d00-4e9d-a1a4-5f5ebd15e819').exists()+ )+ self.assertEqual(ExerciseImage.objects.count(), count_before)++ @patch('wger.exercises.sync.validate_video', side_effect=ValidationError('invalid video'))+ @patch('requests.get', return_value=MockVideoResponse())+ def test_video_sync_skips_invalid_video(self, mock_request, mock_validate):+ """A video rejected by validate_video is skipped, not stored."""++ count_before = ExerciseVideo.objects.count()++ # An invalid video must not abort the run or get stored+ download_exercise_videos(lambda x: x)++ mock_validate.assert_called()+ self.assertFalse(+ ExerciseVideo.objects.filter(uuid='00000088-1d00-4e9d-a1a4-5f5ebd15e819').exists()+ )+ self.assertEqual(ExerciseVideo.objects.count(), count_before)diff --git a/wger/nutrition/sync.py b/wger/nutrition/sync.pyindex 4bb51d4d4..3b3f0d9ad 100644--- a/wger/nutrition/sync.py+++ b/wger/nutrition/sync.py@@ -26,6 +26,7 @@ from typing import (# Djangofrom django.conf import settings+from django.core.exceptions import ValidationErrorfrom django.core.files.base import Filefrom django.core.files.storage import default_storagefrom django.db import IntegrityError@@ -193,7 +194,11 @@ def fetch_image_from_wger_instance(ingredient):returnexcept Image.DoesNotExist:retrieved_image = requests.get(image_data['image'], headers=wger_headers())- image = Image.from_json(ingredient, retrieved_image, image_data)+ try:+ image = Image.from_json(ingredient, retrieved_image, image_data)+ except ValidationError as e:+ logger.warning(f'Skipping invalid ingredient image: {"; ".join(e.messages)}')+ returnimage.ingredient.last_image_check = timezone.now()image.ingredient.save()@@ -273,6 +278,9 @@ def fetch_image_from_off(ingredient: Ingredient):except IntegrityError:logger.debug('Ingredient has already an image, skipping...')return+ except ValidationError as e:+ logger.warning(f'Skipping invalid ingredient image: {"; ".join(e.messages)}')+ returnlogger.info('Image successfully saved')@@ -305,7 +313,11 @@ def download_ingredient_images(except Image.DoesNotExist:print_fn(' Image not found in local DB, creating now...')retrieved_image = requests.get(image_data['image'], headers=headers)- Image.from_json(ingredient, retrieved_image, image_data)+ try:+ Image.from_json(ingredient, retrieved_image, image_data)+ except ValidationError as e:+ print_fn(style_fn(f' invalid image, skipping: {"; ".join(e.messages)}'))+ continueprint_fn(style_fn(' successfully saved'))diff --git a/wger/utils/helpers.py b/wger/utils/helpers.pyindex 837ca6787..ecc6f5bd3 100644--- a/wger/utils/helpers.py+++ b/wger/utils/helpers.py@@ -36,6 +36,9 @@ from django.shortcuts import get_object_or_404from django.utils.encoding import force_bytesfrom django.utils.http import urlsafe_base64_encode+# wger+from wger.utils.images import validate_image_static_no_animation+logger = logging.getLogger(__name__)@@ -183,9 +186,13 @@ class BaseImageMixin:img_temp.write(retrieved_image.content)img_temp.flush()+ # Validate image+ image_file = File(img_temp)+ validate_image_static_no_animation(image_file)+self.image.save(os.path.basename(json_data['image']),- File(img_temp),+ image_file,)@classmethod