Skip to content

Commit 6cc393f

Browse files
authored
[api] Refactor /create/file API with better validation checks (#4003)
1 parent 24f5067 commit 6cc393f

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

apps/filebrowser/src/filebrowser/api.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -603,10 +603,21 @@ def touch(request):
603603
path = request.POST.get('path')
604604
name = request.POST.get('name')
605605

606+
# Check if path and name are provided
607+
if not path or not name:
608+
return HttpResponse("Missing parameters: path and name are required.", status=400)
609+
610+
# Validate the 'name' parameter for invalid characters
606611
if name and (posixpath.sep in name):
607-
return HttpResponse(f"Error creating {name} file: Slashes are not allowed in filename.", status=400)
612+
return HttpResponse(f"Slashes are not allowed in filename. Please choose a different name.", status=400)
613+
614+
file_path = request.fs.join(path, name)
615+
616+
# Check if the file already exists
617+
if request.fs.isfile(file_path):
618+
return HttpResponse(f"Error creating {name} file: File already exists.", status=409)
608619

609-
request.fs.create(request.fs.join(path, name))
620+
request.fs.create(file_path)
610621
return HttpResponse(status=201)
611622

612623

apps/filebrowser/src/filebrowser/api_test.py

+65-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from django.core.files.uploadedfile import SimpleUploadedFile
2222

2323
from aws.s3.s3fs import S3ListAllBucketsException
24-
from filebrowser.api import copy, get_all_filesystems, listdir_paged, mkdir, move, rename, upload_file
24+
from filebrowser.api import copy, get_all_filesystems, listdir_paged, mkdir, move, rename, touch, upload_file
2525
from filebrowser.conf import (
2626
MAX_FILE_SIZE_UPLOAD_LIMIT,
2727
RESTRICT_FILE_EXTENSIONS,
@@ -331,6 +331,70 @@ def test_file_upload_failure(self):
331331
reset()
332332

333333

334+
class TestTouchAPI:
335+
def test_touch_success(self):
336+
request = Mock(
337+
method='POST',
338+
POST={'path': 's3a://test-bucket/test-user/', 'name': 'test_file.txt'},
339+
fs=Mock(
340+
isfile=Mock(return_value=False),
341+
join=Mock(return_value='s3a://test-bucket/test-user/test_file.txt'),
342+
create=Mock(),
343+
),
344+
)
345+
response = touch(request)
346+
347+
assert response.status_code == 201
348+
request.fs.create.assert_called_once_with('s3a://test-bucket/test-user/test_file.txt')
349+
350+
def test_touch_file_exists(self):
351+
request = Mock(
352+
method='POST',
353+
POST={'path': 's3a://test-bucket/test-user/', 'name': 'test_file.txt'},
354+
fs=Mock(
355+
isfile=Mock(return_value=True),
356+
join=Mock(return_value='s3a://test-bucket/test-user/test_file.txt'),
357+
),
358+
)
359+
response = touch(request)
360+
361+
assert response.status_code == 409
362+
assert response.content.decode('utf-8') == 'Error creating test_file.txt file: File already exists.'
363+
364+
def test_touch_invalid_name(self):
365+
request = Mock(
366+
method='POST',
367+
POST={'path': 's3a://test-bucket/test-user/', 'name': 'test/file.txt'},
368+
fs=Mock(),
369+
)
370+
response = touch(request)
371+
372+
assert response.status_code == 400
373+
assert response.content.decode('utf-8') == 'Slashes are not allowed in filename. Please choose a different name.'
374+
375+
def test_touch_no_path(self):
376+
request = Mock(
377+
method='POST',
378+
POST={'name': 'test_file.txt'},
379+
fs=Mock(),
380+
)
381+
response = touch(request)
382+
383+
assert response.status_code == 400
384+
assert response.content.decode('utf-8') == 'Missing parameters: path and name are required.'
385+
386+
def test_touch_no_name(self):
387+
request = Mock(
388+
method='POST',
389+
POST={'path': 's3a://test-bucket/test-user/'},
390+
fs=Mock(),
391+
)
392+
response = touch(request)
393+
394+
assert response.status_code == 400
395+
assert response.content.decode('utf-8') == 'Missing parameters: path and name are required.'
396+
397+
334398
class TestMkdirAPI:
335399
def test_mkdir_success(self):
336400
request = Mock(

0 commit comments

Comments
 (0)