Self hosted FLOSS fitness/workout, nutrition and weight tracker
Properly validate uploaded video files
Roland Geider9eac1748c2de
diff --git a/wger/exercises/models/video.py b/wger/exercises/models/video.pyindex 0795a125d..11a55e642 100644--- a/wger/exercises/models/video.py+++ b/wger/exercises/models/video.py@@ -20,6 +20,8 @@ import uuid# Djangofrom django.core.exceptions import ValidationError+from django.core.files import File+from django.core.validators import FileExtensionValidatorfrom django.db import modelsfrom django.utils.translation import gettext_lazy as _@@ -46,28 +48,38 @@ from wger.utils.models import (MAX_FILE_SIZE_MB = 100+VIDEO_EXTENSIONS = ['mp4', 'mov', 'webm', 'ogv', 'ogg']++# Box types an MP4 / QuickTime file starts with, at offset 4+ISO_MEDIA_BOXES = (b'ftyp', b'moov', b'mdat', b'wide', b'free', b'skip')+++def has_video_signature(value: File) -> bool:+ """+ Checks the file's magic bytes for an MP4 / QuickTime, WebM or Ogg container+ """+ value.seek(0)+ header = value.read(8)+ value.seek(0)+ return header[4:8] in ISO_MEDIA_BOXES or header.startswith((b'\x1a\x45\xdf\xa3', b'OggS'))+def validate_video(value):if value.size > 1024 * 1024 * MAX_FILE_SIZE_MB:raise ValidationError(_('Maximum file size is %(size)sMB.') % {'size': MAX_FILE_SIZE_MB})- # Editing existing video- if not hasattr(value.file, 'temporary_file_path'):- return+ # The extension decides the content type the file is served with+ FileExtensionValidator(VIDEO_EXTENSIONS)(value)- if value.file.content_type not in ['video/mp4', 'video/webm', 'video/ogg']:- raise ValidationError(_('File type is not supported'))+ if not has_video_signature(value):+ raise ValidationError(_('File is not a valid video'))- # ffmpeg is not installed, skip- if not ffmpeg:+ # ffmpeg is not installed or the upload is only held in memory, skip+ if not ffmpeg or not hasattr(value, 'temporary_file_path'):return- # ffmpeg needs to access this- if not hasattr(value.file, 'temporary_file_path'):- raise ValidationError(_('File type is not supported'))-try:- ffmpeg.probe(value.file.temporary_file_path())+ ffmpeg.probe(value.temporary_file_path())except ffmpeg.Error:raise ValidationError(_('File is not a valid video'))diff --git a/wger/exercises/sync.py b/wger/exercises/sync.pyindex db004173a..2b2040203 100644--- a/wger/exercises/sync.py+++ b/wger/exercises/sync.py@@ -472,17 +472,14 @@ def download_exercise_videos(img_temp.write(retrieved_video.content)img_temp.flush()- # Validate file- video_file = File(img_temp)+ # Validate file, the extension check needs the remote name+ video_file = File(img_temp, name=os.path.basename(video_data['video']))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'])),- video_file,- )+ video.video.save(video_file.name, video_file)video.save()print_fn(style_fn(' saved successfully'))diff --git a/wger/exercises/tests/test_exercise_videos.py b/wger/exercises/tests/test_exercise_videos.pyindex a93291094..29c3bada6 100644--- a/wger/exercises/tests/test_exercise_videos.py+++ b/wger/exercises/tests/test_exercise_videos.py@@ -12,12 +12,103 @@## You should have received a copy of the GNU Affero General Public License+# Standard Library+import pathlib+import tempfile+from unittest.mock import (+ Mock,+ patch,+)++# Django+from django.core.files.uploadedfile import SimpleUploadedFile+from django.urls import reverse+# wgerfrom wger.core.tests import api_base_test+from wger.core.tests.base_testcase import WgerTestCasefrom wger.exercises.models import ExerciseVideofrom wger.exercises.tests.api_mixins import ActstreamUpdateMixin+HTML_PAYLOAD = b'<!DOCTYPE html><script>document.title = "xss"</script>'+VIDEO_HEADER = b'\x00\x00\x00\x14ftypqt \x00\x00\x00\x00qt '+++class ExerciseVideoUploadTestCase(WgerTestCase):+ """+ Tests the file validation when uploading a video+ """++ def setUp(self):+ super().setUp()+ media_root = tempfile.TemporaryDirectory()+ self.addCleanup(media_root.cleanup)+ media_settings = self.settings(MEDIA_ROOT=media_root.name)+ media_settings.enable()+ self.addCleanup(media_settings.disable)+ self.user_login('admin')++ def upload(self, name: str, content: bytes, content_type: str = 'video/mp4'):+ return self.client.post(+ reverse('video-list'),+ {+ 'exercise': 1,+ 'license': 1,+ 'license_author': 'tester',+ 'video': SimpleUploadedFile(name, content, content_type=content_type),+ },+ )++ def assert_rejected(self, name: str, content: bytes, content_type: str = 'video/mp4'):+ # 0 bytes in memory forces the temporary file upload handler+ for max_memory_size in (2621440, 0):+ with (+ self.subTest(max_memory_size=max_memory_size),+ self.settings(FILE_UPLOAD_MAX_MEMORY_SIZE=max_memory_size),+ ):+ count_before = ExerciseVideo.objects.count()+ response = self.upload(name, content, content_type)+ self.assertEqual(response.status_code, 400)+ self.assertIn('video', response.data)+ self.assertEqual(ExerciseVideo.objects.count(), count_before)++ def test_html_file_rejected(self):+ """An HTML file declared as video/mp4 is rejected"""+ self.assert_rejected('poc.html', HTML_PAYLOAD)++ def test_svg_file_rejected(self):+ """An SVG file with a video signature is rejected because of its extension"""+ self.assert_rejected('poc.svg', VIDEO_HEADER + b'<svg onload="alert(1)"/>')++ def test_non_video_content_rejected(self):+ """A file with a video extension but without a video signature is rejected"""+ self.assert_rejected('poc.mp4', HTML_PAYLOAD)+ self.assert_rejected('notes.webm', b'just some text', 'text/plain')++ def test_ffmpeg_probe_rejects_invalid_video(self):+ """A file that ffmpeg can't probe is rejected"""+ ffmpeg = Mock(Error=type('Error', (Exception,), {}))+ ffmpeg.probe.side_effect = ffmpeg.Error++ with (+ patch('wger.exercises.models.video.ffmpeg', ffmpeg),+ self.settings(FILE_UPLOAD_MAX_MEMORY_SIZE=0),+ ):+ response = self.upload('clip.mp4', VIDEO_HEADER + b'\x00' * 64)++ self.assertEqual(response.status_code, 400)+ ffmpeg.probe.assert_called_once()++ def test_video_accepted(self):+ """A file with a video extension and signature is stored"""+ response = self.upload('IMG_0001.MOV', VIDEO_HEADER + b'\x00' * 64, 'video/quicktime')++ self.assertEqual(response.status_code, 201)+ video = ExerciseVideo.objects.get(pk=response.data['id'])+ self.assertEqual(pathlib.Path(video.video.name).suffix, '.MOV')++# TODO: add POST and DELETE testsclass ExerciseVideosApiTestCase(ActstreamUpdateMixin,diff --git a/wger/exercises/tests/test_sync.py b/wger/exercises/tests/test_sync.pyindex 1f9c906f5..53e126550 100644--- a/wger/exercises/tests/test_sync.py+++ b/wger/exercises/tests/test_sync.py@@ -14,11 +14,11 @@# Standard Libraryimport io+import tempfilefrom unittest.mock import patch# Djangofrom django.core.cache import cache-from django.core.exceptions import ValidationError# Third Partyfrom PIL import Image as PILImage@@ -708,6 +708,18 @@ class MockVideoResponse:}+class MockValidVideoResponse(MockVideoResponse):+ def __init__(self):+ super().__init__()+ self.content = b'\x00\x00\x00\x14ftypqt \x00\x00\x00\x00qt ' + b'\x00' * 64++ @staticmethod+ def json():+ data = MockVideoResponse.json()+ data['results'][0]['video'] = 'https://wger.de/media/exercise-video/2/clip.mov'+ return data++class TestSyncMethods(WgerTestCase):@patch('requests.get', return_value=MockLanguageResponse())def test_language_sync(self, mock_request):@@ -965,18 +977,26 @@ class TestSyncMethods(WgerTestCase):)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."""+ def test_video_sync_skips_invalid_video(self, mock_request):+ """A download that is not a video is skipped, not stored."""count_before = ExerciseVideo.objects.count()# An invalid video must not abort the run or get storeddownload_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)++ @patch('requests.get', return_value=MockValidVideoResponse())+ def test_video_sync_stores_valid_video(self, mock_request):+ """A downloaded video is stored under the remote file's extension."""++ with tempfile.TemporaryDirectory() as media_root, self.settings(MEDIA_ROOT=media_root):+ download_exercise_videos(lambda x: x)++ video = ExerciseVideo.objects.get(uuid='00000088-1d00-4e9d-a1a4-5f5ebd15e819')+ self.assertTrue(video.video.name.endswith('.mov'))