Skip to content

Commit

Permalink
fix: LSDV-3854-1: Add extension check (#3725)
Browse files Browse the repository at this point in the history
* fix: LSDV-3854: Add extension check

* Add empty extension test (LSDV-3854)
  • Loading branch information
triklozoid committed Apr 3, 2023
1 parent f1eabb1 commit 012f4e1
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
27 changes: 27 additions & 0 deletions label_studio/core/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,33 @@
UPLOAD_DIR = 'upload'
AVATAR_PATH = 'avatars'

SUPPORTED_EXTENSIONS = set(
[
'.aiff',
'.au',
'.bmp',
'.csv',
'.flac',
'.gif',
'.htm',
'.html',
'.jpg',
'.json',
'.m4a',
'.mp3',
'.ogg',
'.png',
'.svg',
'.tsv',
'.txt',
'.wav',
'.webp',
'.xml',
'.mp4',
'.webm',
]
)

# project exports
EXPORT_DIR = os.path.join(BASE_DATA_DIR, 'export')
EXPORT_URL_ROOT = '/export/'
Expand Down
7 changes: 7 additions & 0 deletions label_studio/data_import/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ def check_tasks_max_file_size(value):
raise ValidationError(f'Maximum total size of all files is {settings.TASKS_MAX_FILE_SIZE} bytes, '
f'current size is {total} bytes')

def check_extensions(files):
for filename, file_obj in files.items():
_, ext = os.path.splitext(file_obj.name)
if ext.lower() not in settings.SUPPORTED_EXTENSIONS:
raise ValidationError(f'{ext} extension is not supported')


def check_request_files_size(files):
total = sum([file.size for _, file in files.items()])
Expand Down Expand Up @@ -161,6 +167,7 @@ def load_tasks(request, project):
# take tasks from request FILES
if len(request.FILES):
check_request_files_size(request.FILES)
check_extensions(request.FILES)
for filename, file in request.FILES.items():
file_upload = create_file_upload(request, project, file)
if file_upload.format_could_be_tasks_list:
Expand Down
72 changes: 72 additions & 0 deletions label_studio/tests/data_import.tavern.yml
Original file line number Diff line number Diff line change
Expand Up @@ -733,3 +733,75 @@ stages:
json:
id: !int '{first_task}'
is_labeled: true
---
test_name: extension_check
strict: false
marks:
- usefixtures:
- django_live_url
stages:
- id: signup
type: ref
- name: stage
request:
data:
label_config: <View></View>
title: Check consistency of imported data columns
method: POST
url: '{django_live_url}/api/projects'
response:
save:
json:
project_pk: id
status_code: 201
- name: stage
request:
files:
file: tests/test_suites/samples/test.wrong_ext
headers:
content-type: multipart/form-data
method: POST
url: '{django_live_url}/api/projects/{project_pk}/import?commit_to_project=false'
response:
json:
validation_errors:
non_field_errors:
- '.wrong_ext extension is not supported'

status_code: 400
---
test_name: extension_check_empty
strict: false
marks:
- usefixtures:
- django_live_url
stages:
- id: signup
type: ref
- name: stage
request:
data:
label_config: <View></View>
title: Check consistency of imported data columns
method: POST
url: '{django_live_url}/api/projects'
response:
save:
json:
project_pk: id
status_code: 201
- name: stage
request:
files:
file: tests/test_suites/samples/test_file_without_extension
headers:
content-type: multipart/form-data
method: POST
url: '{django_live_url}/api/projects/{project_pk}/import?commit_to_project=false'
response:
json:
validation_errors:
non_field_errors:
- ' extension is not supported'

status_code: 400
Binary file not shown.
Binary file not shown.

0 comments on commit 012f4e1

Please sign in to comment.