1
0
mirror of https://github.com/django/django.git synced 2025-05-29 18:26:29 +00:00

Refs #34100 -- Made file upload tests use Storage.exists() where appropriate.

This commit is contained in:
Francesco Panico 2022-12-30 12:47:59 +01:00 committed by Mariusz Felisiak
parent 6e9e7ec472
commit c179ad9fe7
4 changed files with 17 additions and 9 deletions

View File

@ -25,7 +25,8 @@ from .models import FileModel
UNICODE_FILENAME = "test-0123456789_中文_Orléans.jpg" UNICODE_FILENAME = "test-0123456789_中文_Orléans.jpg"
MEDIA_ROOT = sys_tempfile.mkdtemp() MEDIA_ROOT = sys_tempfile.mkdtemp()
UPLOAD_TO = os.path.join(MEDIA_ROOT, "test_upload") UPLOAD_FOLDER = "test_upload"
UPLOAD_TO = os.path.join(MEDIA_ROOT, UPLOAD_FOLDER)
CANDIDATE_TRAVERSAL_FILE_NAMES = [ CANDIDATE_TRAVERSAL_FILE_NAMES = [
"/tmp/hax0rd.txt", # Absolute path, *nix-style. "/tmp/hax0rd.txt", # Absolute path, *nix-style.

View File

@ -55,7 +55,7 @@ class TraversalUploadHandler(FileUploadHandler):
"""A handler with potential directory-traversal vulnerability.""" """A handler with potential directory-traversal vulnerability."""
def __init__(self, request=None): def __init__(self, request=None):
from .views import UPLOAD_TO from .tests import UPLOAD_TO
super().__init__(request) super().__init__(request)
self.upload_dir = UPLOAD_TO self.upload_dir = UPLOAD_TO

View File

@ -6,7 +6,7 @@ from django.core.files.uploadhandler import TemporaryFileUploadHandler
from django.http import HttpResponse, HttpResponseServerError, JsonResponse from django.http import HttpResponse, HttpResponseServerError, JsonResponse
from .models import FileModel from .models import FileModel
from .tests import UNICODE_FILENAME, UPLOAD_TO from .tests import UNICODE_FILENAME, UPLOAD_FOLDER
from .uploadhandler import ( from .uploadhandler import (
ErroringUploadHandler, ErroringUploadHandler,
QuotaUploadHandler, QuotaUploadHandler,
@ -68,9 +68,13 @@ def file_upload_unicode_name(request):
# Check to make sure the exotic characters are preserved even # Check to make sure the exotic characters are preserved even
# through file save. # through file save.
uni_named_file = request.FILES["file_unicode"] uni_named_file = request.FILES["file_unicode"]
FileModel.objects.create(testfile=uni_named_file) file_model = FileModel.objects.create(testfile=uni_named_file)
full_name = "%s/%s" % (UPLOAD_TO, uni_named_file.name) full_name = f"{UPLOAD_FOLDER}/{uni_named_file.name}"
return HttpResponse() if os.path.exists(full_name) else HttpResponseServerError() return (
HttpResponse()
if file_model.testfile.storage.exists(full_name)
else HttpResponseServerError()
)
def file_upload_echo(request): def file_upload_echo(request):

View File

@ -120,9 +120,12 @@ class FileFieldTests(TestCase):
with TemporaryUploadedFile( with TemporaryUploadedFile(
"foo.txt", "text/plain", 1, "utf-8" "foo.txt", "text/plain", 1, "utf-8"
) as tmp_file: ) as tmp_file:
Document.objects.create(myfile=tmp_file) document = Document.objects.create(myfile=tmp_file)
self.assertTrue( self.assertIs(
os.path.exists(os.path.join(tmp_dir, "unused", "foo.txt")) document.myfile.storage.exists(
os.path.join("unused", "foo.txt")
),
True,
) )
def test_pickle(self): def test_pickle(self):