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 file format in upload_url endpoint #3662

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
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
7 changes: 6 additions & 1 deletion contentcuration/contentcuration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from datetime import datetime

import pytz
from celery import states
from django.conf import settings
from django.contrib.auth.base_user import AbstractBaseUser
from django.contrib.auth.base_user import BaseUserManager
Expand Down Expand Up @@ -2197,6 +2196,12 @@ def save(self, set_by_file_on_disk=True, *args, **kwargs):
2. fill the other fields accordingly
"""
from contentcuration.utils.user import calculate_user_storage

# check if the file format exists in file_formats.choices
if self.file_format_id:
if self.file_format_id not in dict(file_formats.choices):
raise ValidationError("Invalid file_format")

if set_by_file_on_disk and self.file_on_disk: # if file_on_disk is supplied, hash out the file
if self.checksum is None or self.checksum == "":
md5 = hashlib.md5()
Expand Down
10 changes: 10 additions & 0 deletions contentcuration/contentcuration/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
from django.conf import settings
from django.core.cache import cache
from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError
from django.utils import timezone
from le_utils.constants import content_kinds
Expand Down Expand Up @@ -669,6 +670,15 @@ def test_duration_check_constraint__not_media(self):
duration=10,
)

def test_invalid_file_format(self):
channel = testdata.channel()
with self.assertRaises(ValidationError, msg="Invalid file_format"):
File.objects.create(
contentnode=create_contentnode(channel.main_tree_id),
preset_id=format_presets.EPUB,
file_format_id='pptx',
)


class AssessmentItemFilePermissionTestCase(PermissionQuerysetTestCase):
@property
Expand Down
16 changes: 16 additions & 0 deletions contentcuration/contentcuration/tests/viewsets/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,22 @@ def test_duration_invalid(self):

self.assertEqual(response.status_code, 400)

def test_invalid_file_format_upload(self):
self.client.force_authenticate(user=self.user)
file = {
"size": 1000,
"checksum": uuid.uuid4().hex,
"name": "le_studio",
"file_format": "ppx",
"preset": format_presets.AUDIO,
"duration": 10.123
}
response = self.client.post(
reverse("file-upload-url"), file, format="json",
)

self.assertEqual(response.status_code, 400)

def test_insufficient_storage(self):
self.file["size"] = 100000000000000

Expand Down
4 changes: 4 additions & 0 deletions contentcuration/contentcuration/viewsets/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from django.core.exceptions import PermissionDenied
from django.http import HttpResponseBadRequest
from le_utils.constants import file_formats
from rest_framework.decorators import action
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
Expand Down Expand Up @@ -164,6 +165,9 @@ def upload_url(self, request):
filepath, checksum_base64, 600, content_length=size
)

if file_format not in dict(file_formats.choices):
return HttpResponseBadRequest("Invalid file_format!")

file = File(
file_size=size,
checksum=checksum,
Expand Down