2011-05-08 00:59:16 +08:00
|
|
|
import base64
|
2011-03-28 10:11:19 +08:00
|
|
|
import hashlib
|
2010-12-04 15:28:12 +08:00
|
|
|
import os
|
2008-07-20 20:44:41 +08:00
|
|
|
import shutil
|
2016-06-28 23:21:26 +08:00
|
|
|
import sys
|
2012-10-31 05:20:42 +08:00
|
|
|
import tempfile as sys_tempfile
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2017-01-07 19:11:46 +08:00
|
|
|
from io import BytesIO, StringIO
|
2017-01-26 21:25:15 +08:00
|
|
|
from urllib.parse import quote
|
2008-07-20 20:44:41 +08:00
|
|
|
|
2008-07-27 06:48:51 +08:00
|
|
|
from django.core.files import temp as tempfile
|
2008-07-20 20:44:41 +08:00
|
|
|
from django.core.files.uploadedfile import SimpleUploadedFile
|
2018-06-13 02:42:20 +08:00
|
|
|
from django.http.multipartparser import (
|
|
|
|
MultiPartParser, MultiPartParserError, parse_header,
|
|
|
|
)
|
2015-04-18 05:38:20 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase, client, override_settings
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2011-10-14 02:51:33 +08:00
|
|
|
from . import uploadhandler
|
2012-10-31 05:20:42 +08:00
|
|
|
from .models import FileModel
|
2008-07-20 20:44:41 +08:00
|
|
|
|
2012-06-08 00:08:47 +08:00
|
|
|
UNICODE_FILENAME = 'test-0123456789_中文_Orléans.jpg'
|
2015-02-22 01:56:36 +08:00
|
|
|
MEDIA_ROOT = sys_tempfile.mkdtemp()
|
2012-10-31 05:20:42 +08:00
|
|
|
UPLOAD_TO = os.path.join(MEDIA_ROOT, 'test_upload')
|
2009-04-05 01:34:58 +08:00
|
|
|
|
2013-11-03 03:37:48 +08:00
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
@override_settings(MEDIA_ROOT=MEDIA_ROOT, ROOT_URLCONF='file_uploads.urls', MIDDLEWARE=[])
|
2008-07-01 23:10:51 +08:00
|
|
|
class FileUploadTests(TestCase):
|
2014-01-14 23:43:27 +08:00
|
|
|
|
2012-10-31 05:20:42 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2019-01-31 23:12:55 +08:00
|
|
|
os.makedirs(MEDIA_ROOT, exist_ok=True)
|
2012-10-31 05:20:42 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
shutil.rmtree(MEDIA_ROOT)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().tearDownClass()
|
2012-10-31 05:20:42 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
def test_simple_upload(self):
|
2012-05-19 23:43:34 +08:00
|
|
|
with open(__file__, 'rb') as fp:
|
2012-05-05 20:01:38 +08:00
|
|
|
post_data = {
|
|
|
|
'name': 'Ringo',
|
|
|
|
'file_field': fp,
|
|
|
|
}
|
2014-01-14 23:43:27 +08:00
|
|
|
response = self.client.post('/upload/', post_data)
|
2008-07-01 23:10:51 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_large_upload(self):
|
2014-05-26 04:08:05 +08:00
|
|
|
file = tempfile.NamedTemporaryFile
|
2015-02-22 02:18:54 +08:00
|
|
|
with file(suffix=".file1") as file1, file(suffix=".file2") as file2:
|
2014-05-26 04:08:05 +08:00
|
|
|
file1.write(b'a' * (2 ** 21))
|
|
|
|
file1.seek(0)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2014-05-26 04:08:05 +08:00
|
|
|
file2.write(b'a' * (10 * 2 ** 20))
|
|
|
|
file2.seek(0)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2014-05-26 04:08:05 +08:00
|
|
|
post_data = {
|
|
|
|
'name': 'Ringo',
|
|
|
|
'file_field1': file1,
|
|
|
|
'file_field2': file2,
|
|
|
|
}
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2014-05-26 04:08:05 +08:00
|
|
|
for key in list(post_data):
|
|
|
|
try:
|
|
|
|
post_data[key + '_hash'] = hashlib.sha1(post_data[key].read()).hexdigest()
|
|
|
|
post_data[key].seek(0)
|
|
|
|
except AttributeError:
|
2018-02-08 03:20:04 +08:00
|
|
|
post_data[key + '_hash'] = hashlib.sha1(post_data[key].encode()).hexdigest()
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2014-05-26 04:08:05 +08:00
|
|
|
response = self.client.post('/verify/', post_data)
|
2008-07-01 23:10:51 +08:00
|
|
|
|
2014-05-26 04:08:05 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2009-04-05 01:34:58 +08:00
|
|
|
|
2014-09-03 01:23:51 +08:00
|
|
|
def _test_base64_upload(self, content, encode=base64.b64encode):
|
2012-10-20 21:36:24 +08:00
|
|
|
payload = client.FakePayload("\r\n".join([
|
2011-05-08 00:59:16 +08:00
|
|
|
'--' + client.BOUNDARY,
|
|
|
|
'Content-Disposition: form-data; name="file"; filename="test.txt"',
|
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'Content-Transfer-Encoding: base64',
|
2013-10-27 03:15:03 +08:00
|
|
|
'']))
|
2018-02-08 03:20:04 +08:00
|
|
|
payload.write(b'\r\n' + encode(content.encode()) + b'\r\n')
|
2012-10-20 21:36:24 +08:00
|
|
|
payload.write('--' + client.BOUNDARY + '--\r\n')
|
2011-05-08 00:59:16 +08:00
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': len(payload),
|
2013-11-03 03:37:48 +08:00
|
|
|
'CONTENT_TYPE': client.MULTIPART_CONTENT,
|
2014-01-14 23:43:27 +08:00
|
|
|
'PATH_INFO': "/echo_content/",
|
2011-05-08 00:59:16 +08:00
|
|
|
'REQUEST_METHOD': 'POST',
|
2013-11-03 03:37:48 +08:00
|
|
|
'wsgi.input': payload,
|
2011-05-08 00:59:16 +08:00
|
|
|
}
|
|
|
|
response = self.client.request(**r)
|
2017-02-08 02:23:57 +08:00
|
|
|
self.assertEqual(response.json()['file'], content)
|
2012-10-12 05:20:25 +08:00
|
|
|
|
|
|
|
def test_base64_upload(self):
|
|
|
|
self._test_base64_upload("This data will be transmitted base64-encoded.")
|
|
|
|
|
|
|
|
def test_big_base64_upload(self):
|
|
|
|
self._test_base64_upload("Big data" * 68000) # > 512Kb
|
2011-05-08 00:59:16 +08:00
|
|
|
|
2014-09-03 01:23:51 +08:00
|
|
|
def test_big_base64_newlines_upload(self):
|
2016-12-01 18:38:01 +08:00
|
|
|
self._test_base64_upload("Big data" * 68000, encode=base64.encodebytes)
|
2014-09-03 01:23:51 +08:00
|
|
|
|
2009-04-05 01:34:58 +08:00
|
|
|
def test_unicode_file_name(self):
|
2017-01-27 02:54:16 +08:00
|
|
|
with sys_tempfile.TemporaryDirectory() as temp_dir:
|
|
|
|
# This file contains Chinese symbols and an accented char in the name.
|
|
|
|
with open(os.path.join(temp_dir, UNICODE_FILENAME), 'w+b') as file1:
|
|
|
|
file1.write(b'b' * (2 ** 10))
|
|
|
|
file1.seek(0)
|
|
|
|
response = self.client.post('/unicode_name/', {'file_unicode': file1})
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2008-07-27 06:48:51 +08:00
|
|
|
|
2014-07-12 20:08:50 +08:00
|
|
|
def test_unicode_file_name_rfc2231(self):
|
|
|
|
"""
|
|
|
|
Test receiving file upload when filename is encoded with RFC2231
|
|
|
|
(#22971).
|
|
|
|
"""
|
|
|
|
payload = client.FakePayload()
|
|
|
|
payload.write('\r\n'.join([
|
|
|
|
'--' + client.BOUNDARY,
|
2017-01-26 21:25:15 +08:00
|
|
|
'Content-Disposition: form-data; name="file_unicode"; filename*=UTF-8\'\'%s' % quote(UNICODE_FILENAME),
|
2014-07-12 20:08:50 +08:00
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
|
|
|
'You got pwnd.\r\n',
|
|
|
|
'\r\n--' + client.BOUNDARY + '--\r\n'
|
|
|
|
]))
|
|
|
|
|
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': len(payload),
|
|
|
|
'CONTENT_TYPE': client.MULTIPART_CONTENT,
|
|
|
|
'PATH_INFO': "/unicode_name/",
|
|
|
|
'REQUEST_METHOD': 'POST',
|
|
|
|
'wsgi.input': payload,
|
|
|
|
}
|
|
|
|
response = self.client.request(**r)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_unicode_name_rfc2231(self):
|
|
|
|
"""
|
|
|
|
Test receiving file upload when filename is encoded with RFC2231
|
|
|
|
(#22971).
|
|
|
|
"""
|
|
|
|
payload = client.FakePayload()
|
2015-09-12 07:33:12 +08:00
|
|
|
payload.write(
|
|
|
|
'\r\n'.join([
|
|
|
|
'--' + client.BOUNDARY,
|
2017-01-26 21:25:15 +08:00
|
|
|
'Content-Disposition: form-data; name*=UTF-8\'\'file_unicode; filename*=UTF-8\'\'%s' % quote(
|
2015-09-12 07:33:12 +08:00
|
|
|
UNICODE_FILENAME
|
|
|
|
),
|
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
|
|
|
'You got pwnd.\r\n',
|
|
|
|
'\r\n--' + client.BOUNDARY + '--\r\n'
|
|
|
|
])
|
|
|
|
)
|
2014-07-12 20:08:50 +08:00
|
|
|
|
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': len(payload),
|
|
|
|
'CONTENT_TYPE': client.MULTIPART_CONTENT,
|
|
|
|
'PATH_INFO': "/unicode_name/",
|
|
|
|
'REQUEST_METHOD': 'POST',
|
|
|
|
'wsgi.input': payload,
|
|
|
|
}
|
|
|
|
response = self.client.request(**r)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2020-02-21 13:25:22 +08:00
|
|
|
def test_unicode_file_name_rfc2231_with_double_quotes(self):
|
|
|
|
payload = client.FakePayload()
|
|
|
|
payload.write('\r\n'.join([
|
|
|
|
'--' + client.BOUNDARY,
|
|
|
|
'Content-Disposition: form-data; name="file_unicode"; '
|
|
|
|
'filename*="UTF-8\'\'%s"' % quote(UNICODE_FILENAME),
|
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
|
|
|
'You got pwnd.\r\n',
|
|
|
|
'\r\n--' + client.BOUNDARY + '--\r\n',
|
|
|
|
]))
|
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': len(payload),
|
|
|
|
'CONTENT_TYPE': client.MULTIPART_CONTENT,
|
|
|
|
'PATH_INFO': '/unicode_name/',
|
|
|
|
'REQUEST_METHOD': 'POST',
|
|
|
|
'wsgi.input': payload,
|
|
|
|
}
|
|
|
|
response = self.client.request(**r)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
def test_unicode_name_rfc2231_with_double_quotes(self):
|
|
|
|
payload = client.FakePayload()
|
|
|
|
payload.write('\r\n'.join([
|
|
|
|
'--' + client.BOUNDARY,
|
|
|
|
'Content-Disposition: form-data; name*="UTF-8\'\'file_unicode"; '
|
|
|
|
'filename*="UTF-8\'\'%s"' % quote(UNICODE_FILENAME),
|
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
|
|
|
'You got pwnd.\r\n',
|
|
|
|
'\r\n--' + client.BOUNDARY + '--\r\n'
|
|
|
|
]))
|
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': len(payload),
|
|
|
|
'CONTENT_TYPE': client.MULTIPART_CONTENT,
|
|
|
|
'PATH_INFO': '/unicode_name/',
|
|
|
|
'REQUEST_METHOD': 'POST',
|
|
|
|
'wsgi.input': payload,
|
|
|
|
}
|
|
|
|
response = self.client.request(**r)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2016-03-07 20:06:46 +08:00
|
|
|
def test_blank_filenames(self):
|
|
|
|
"""
|
|
|
|
Receiving file upload when filename is blank (before and after
|
|
|
|
sanitization) should be okay.
|
|
|
|
"""
|
2020-06-09 00:55:27 +08:00
|
|
|
filenames = [
|
|
|
|
'',
|
|
|
|
# Normalized by MultiPartParser.IE_sanitize().
|
|
|
|
'C:\\Windows\\',
|
|
|
|
# Normalized by os.path.basename().
|
|
|
|
'/',
|
|
|
|
'ends-with-slash/',
|
|
|
|
]
|
2016-03-07 20:06:46 +08:00
|
|
|
payload = client.FakePayload()
|
|
|
|
for i, name in enumerate(filenames):
|
|
|
|
payload.write('\r\n'.join([
|
|
|
|
'--' + client.BOUNDARY,
|
|
|
|
'Content-Disposition: form-data; name="file%s"; filename="%s"' % (i, name),
|
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
|
|
|
'You got pwnd.\r\n'
|
|
|
|
]))
|
|
|
|
payload.write('\r\n--' + client.BOUNDARY + '--\r\n')
|
|
|
|
|
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': len(payload),
|
|
|
|
'CONTENT_TYPE': client.MULTIPART_CONTENT,
|
|
|
|
'PATH_INFO': '/echo/',
|
|
|
|
'REQUEST_METHOD': 'POST',
|
|
|
|
'wsgi.input': payload,
|
|
|
|
}
|
|
|
|
response = self.client.request(**r)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
|
|
|
# Empty filenames should be ignored
|
2017-02-08 02:23:57 +08:00
|
|
|
received = response.json()
|
2016-03-07 20:06:46 +08:00
|
|
|
for i, name in enumerate(filenames):
|
|
|
|
self.assertIsNone(received.get('file%s' % i))
|
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
def test_dangerous_file_names(self):
|
|
|
|
"""Uploaded file names should be sanitized before ever reaching the view."""
|
|
|
|
# This test simulates possible directory traversal attacks by a
|
2008-07-27 06:48:51 +08:00
|
|
|
# malicious uploader We have to do some monkeybusiness here to construct
|
2008-07-01 23:10:51 +08:00
|
|
|
# a malicious payload with an invalid file name (containing os.sep or
|
|
|
|
# os.pardir). This similar to what an attacker would need to do when
|
|
|
|
# trying such an attack.
|
|
|
|
scary_file_names = [
|
|
|
|
"/tmp/hax0rd.txt", # Absolute path, *nix-style.
|
2015-01-20 22:54:12 +08:00
|
|
|
"C:\\Windows\\hax0rd.txt", # Absolute path, win-style.
|
2008-07-01 23:10:51 +08:00
|
|
|
"C:/Windows/hax0rd.txt", # Absolute path, broken-style.
|
|
|
|
"\\tmp\\hax0rd.txt", # Absolute path, broken in a different way.
|
|
|
|
"/tmp\\hax0rd.txt", # Absolute path, broken by mixing.
|
|
|
|
"subdir/hax0rd.txt", # Descendant path, *nix-style.
|
|
|
|
"subdir\\hax0rd.txt", # Descendant path, win-style.
|
|
|
|
"sub/dir\\hax0rd.txt", # Descendant path, mixed.
|
|
|
|
"../../hax0rd.txt", # Relative path, *nix-style.
|
|
|
|
"..\\..\\hax0rd.txt", # Relative path, win-style.
|
|
|
|
"../..\\hax0rd.txt" # Relative path, mixed.
|
|
|
|
]
|
2008-07-27 06:48:51 +08:00
|
|
|
|
2012-10-20 21:36:24 +08:00
|
|
|
payload = client.FakePayload()
|
2008-07-01 23:10:51 +08:00
|
|
|
for i, name in enumerate(scary_file_names):
|
2012-10-20 21:36:24 +08:00
|
|
|
payload.write('\r\n'.join([
|
2008-07-01 23:10:51 +08:00
|
|
|
'--' + client.BOUNDARY,
|
|
|
|
'Content-Disposition: form-data; name="file%s"; filename="%s"' % (i, name),
|
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
2012-10-20 21:36:24 +08:00
|
|
|
'You got pwnd.\r\n'
|
|
|
|
]))
|
|
|
|
payload.write('\r\n--' + client.BOUNDARY + '--\r\n')
|
2008-07-27 06:48:51 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': len(payload),
|
2013-11-03 03:37:48 +08:00
|
|
|
'CONTENT_TYPE': client.MULTIPART_CONTENT,
|
2014-01-14 23:43:27 +08:00
|
|
|
'PATH_INFO': "/echo/",
|
2008-07-01 23:10:51 +08:00
|
|
|
'REQUEST_METHOD': 'POST',
|
2013-11-03 03:37:48 +08:00
|
|
|
'wsgi.input': payload,
|
2008-07-01 23:10:51 +08:00
|
|
|
}
|
|
|
|
response = self.client.request(**r)
|
|
|
|
# The filenames should have been sanitized by the time it got to the view.
|
2017-02-08 02:23:57 +08:00
|
|
|
received = response.json()
|
2008-07-01 23:10:51 +08:00
|
|
|
for i, name in enumerate(scary_file_names):
|
2014-04-27 01:18:45 +08:00
|
|
|
got = received["file%s" % i]
|
2008-07-01 23:10:51 +08:00
|
|
|
self.assertEqual(got, "hax0rd.txt")
|
2008-07-27 06:48:51 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
def test_filename_overflow(self):
|
|
|
|
"""File names over 256 characters (dangerous on some platforms) get fixed up."""
|
2013-09-05 00:08:13 +08:00
|
|
|
long_str = 'f' * 300
|
|
|
|
cases = [
|
|
|
|
# field name, filename, expected
|
|
|
|
('long_filename', '%s.txt' % long_str, '%s.txt' % long_str[:251]),
|
|
|
|
('long_extension', 'foo.%s' % long_str, '.%s' % long_str[:254]),
|
|
|
|
('no_extension', long_str, long_str[:255]),
|
|
|
|
('no_filename', '.%s' % long_str, '.%s' % long_str[:254]),
|
|
|
|
('long_everything', '%s.%s' % (long_str, long_str), '.%s' % long_str[:254]),
|
|
|
|
]
|
|
|
|
payload = client.FakePayload()
|
|
|
|
for name, filename, _ in cases:
|
|
|
|
payload.write("\r\n".join([
|
|
|
|
'--' + client.BOUNDARY,
|
2014-11-27 08:41:27 +08:00
|
|
|
'Content-Disposition: form-data; name="{}"; filename="{}"',
|
2013-09-05 00:08:13 +08:00
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
|
|
|
'Oops.',
|
|
|
|
''
|
|
|
|
]).format(name, filename))
|
|
|
|
payload.write('\r\n--' + client.BOUNDARY + '--\r\n')
|
2008-07-01 23:10:51 +08:00
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': len(payload),
|
2013-11-03 03:37:48 +08:00
|
|
|
'CONTENT_TYPE': client.MULTIPART_CONTENT,
|
2014-01-14 23:43:27 +08:00
|
|
|
'PATH_INFO': "/echo/",
|
2008-07-01 23:10:51 +08:00
|
|
|
'REQUEST_METHOD': 'POST',
|
2013-11-03 03:37:48 +08:00
|
|
|
'wsgi.input': payload,
|
2008-07-01 23:10:51 +08:00
|
|
|
}
|
2014-05-26 04:08:05 +08:00
|
|
|
response = self.client.request(**r)
|
2017-02-08 02:23:57 +08:00
|
|
|
result = response.json()
|
2013-09-05 00:08:13 +08:00
|
|
|
for name, _, expected in cases:
|
|
|
|
got = result[name]
|
2014-11-27 08:41:27 +08:00
|
|
|
self.assertEqual(expected, got, 'Mismatch for {}'.format(name))
|
2014-10-28 18:02:56 +08:00
|
|
|
self.assertLess(len(got), 256,
|
2013-09-05 00:08:13 +08:00
|
|
|
"Got a long file name (%s characters)." % len(got))
|
2008-07-27 06:48:51 +08:00
|
|
|
|
2014-11-30 00:41:06 +08:00
|
|
|
def test_file_content(self):
|
|
|
|
file = tempfile.NamedTemporaryFile
|
2015-02-22 02:18:54 +08:00
|
|
|
with file(suffix=".ctype_extra") as no_content_type, file(suffix=".ctype_extra") as simple_file:
|
2014-11-30 00:41:06 +08:00
|
|
|
no_content_type.write(b'no content')
|
|
|
|
no_content_type.seek(0)
|
|
|
|
|
|
|
|
simple_file.write(b'text content')
|
|
|
|
simple_file.seek(0)
|
|
|
|
simple_file.content_type = 'text/plain'
|
|
|
|
|
|
|
|
string_io = StringIO('string content')
|
|
|
|
bytes_io = BytesIO(b'binary content')
|
|
|
|
|
|
|
|
response = self.client.post('/echo_content/', {
|
|
|
|
'no_content_type': no_content_type,
|
|
|
|
'simple_file': simple_file,
|
|
|
|
'string': string_io,
|
|
|
|
'binary': bytes_io,
|
|
|
|
})
|
2017-02-08 02:23:57 +08:00
|
|
|
received = response.json()
|
2014-11-30 00:41:06 +08:00
|
|
|
self.assertEqual(received['no_content_type'], 'no content')
|
|
|
|
self.assertEqual(received['simple_file'], 'text content')
|
|
|
|
self.assertEqual(received['string'], 'string content')
|
|
|
|
self.assertEqual(received['binary'], 'binary content')
|
|
|
|
|
2013-04-20 01:20:23 +08:00
|
|
|
def test_content_type_extra(self):
|
|
|
|
"""Uploaded files may have content type parameters available."""
|
2014-05-26 04:08:05 +08:00
|
|
|
file = tempfile.NamedTemporaryFile
|
2015-02-22 02:18:54 +08:00
|
|
|
with file(suffix=".ctype_extra") as no_content_type, file(suffix=".ctype_extra") as simple_file:
|
2014-05-26 04:08:05 +08:00
|
|
|
no_content_type.write(b'something')
|
|
|
|
no_content_type.seek(0)
|
2013-04-20 01:20:23 +08:00
|
|
|
|
2014-05-26 04:08:05 +08:00
|
|
|
simple_file.write(b'something')
|
|
|
|
simple_file.seek(0)
|
|
|
|
simple_file.content_type = 'text/plain; test-key=test_value'
|
2013-04-20 01:20:23 +08:00
|
|
|
|
2014-05-26 04:08:05 +08:00
|
|
|
response = self.client.post('/echo_content_type_extra/', {
|
|
|
|
'no_content_type': no_content_type,
|
|
|
|
'simple_file': simple_file,
|
|
|
|
})
|
2017-02-08 02:23:57 +08:00
|
|
|
received = response.json()
|
2014-05-26 04:08:05 +08:00
|
|
|
self.assertEqual(received['no_content_type'], {})
|
|
|
|
self.assertEqual(received['simple_file'], {'test-key': 'test_value'})
|
2013-04-20 01:20:23 +08:00
|
|
|
|
2011-06-28 18:17:56 +08:00
|
|
|
def test_truncated_multipart_handled_gracefully(self):
|
|
|
|
"""
|
|
|
|
If passed an incomplete multipart message, MultiPartParser does not
|
|
|
|
attempt to read beyond the end of the stream, and simply will handle
|
|
|
|
the part that can be parsed gracefully.
|
|
|
|
"""
|
2012-10-20 21:36:24 +08:00
|
|
|
payload_str = "\r\n".join([
|
2011-06-28 18:17:56 +08:00
|
|
|
'--' + client.BOUNDARY,
|
|
|
|
'Content-Disposition: form-data; name="file"; filename="foo.txt"',
|
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
|
|
|
'file contents'
|
|
|
|
'--' + client.BOUNDARY + '--',
|
|
|
|
'',
|
2012-10-20 21:36:24 +08:00
|
|
|
])
|
|
|
|
payload = client.FakePayload(payload_str[:-10])
|
2011-06-28 18:17:56 +08:00
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': len(payload),
|
|
|
|
'CONTENT_TYPE': client.MULTIPART_CONTENT,
|
2014-01-14 23:43:27 +08:00
|
|
|
'PATH_INFO': '/echo/',
|
2011-06-28 18:17:56 +08:00
|
|
|
'REQUEST_METHOD': 'POST',
|
2012-10-20 21:36:24 +08:00
|
|
|
'wsgi.input': payload,
|
2011-06-28 18:17:56 +08:00
|
|
|
}
|
2017-02-08 02:23:57 +08:00
|
|
|
self.assertEqual(self.client.request(**r).json(), {})
|
2011-06-28 18:17:56 +08:00
|
|
|
|
|
|
|
def test_empty_multipart_handled_gracefully(self):
|
|
|
|
"""
|
|
|
|
If passed an empty multipart message, MultiPartParser will return
|
|
|
|
an empty QueryDict.
|
|
|
|
"""
|
|
|
|
r = {
|
|
|
|
'CONTENT_LENGTH': 0,
|
|
|
|
'CONTENT_TYPE': client.MULTIPART_CONTENT,
|
2014-01-14 23:43:27 +08:00
|
|
|
'PATH_INFO': '/echo/',
|
2011-06-28 18:17:56 +08:00
|
|
|
'REQUEST_METHOD': 'POST',
|
2012-05-19 23:43:34 +08:00
|
|
|
'wsgi.input': client.FakePayload(b''),
|
2011-06-28 18:17:56 +08:00
|
|
|
}
|
2017-02-08 02:23:57 +08:00
|
|
|
self.assertEqual(self.client.request(**r).json(), {})
|
2011-06-28 18:17:56 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
def test_custom_upload_handler(self):
|
2014-05-26 04:08:05 +08:00
|
|
|
file = tempfile.NamedTemporaryFile
|
|
|
|
with file() as smallfile, file() as bigfile:
|
|
|
|
# A small file (under the 5M quota)
|
|
|
|
smallfile.write(b'a' * (2 ** 21))
|
|
|
|
smallfile.seek(0)
|
|
|
|
|
|
|
|
# A big file (over the quota)
|
|
|
|
bigfile.write(b'a' * (10 * 2 ** 20))
|
|
|
|
bigfile.seek(0)
|
|
|
|
|
|
|
|
# Small file posting should work.
|
2017-02-08 02:23:57 +08:00
|
|
|
self.assertIn('f', self.client.post('/quota/', {'f': smallfile}).json())
|
2014-05-26 04:08:05 +08:00
|
|
|
|
|
|
|
# Large files don't go through.
|
2017-02-08 02:23:57 +08:00
|
|
|
self.assertNotIn('f', self.client.post("/quota/", {'f': bigfile}).json())
|
2008-07-27 06:48:51 +08:00
|
|
|
|
2008-07-01 23:10:51 +08:00
|
|
|
def test_broken_custom_upload_handler(self):
|
2014-05-26 04:08:05 +08:00
|
|
|
with tempfile.NamedTemporaryFile() as file:
|
|
|
|
file.write(b'a' * (2 ** 21))
|
|
|
|
file.seek(0)
|
|
|
|
|
2019-03-16 07:27:57 +08:00
|
|
|
msg = 'You cannot alter upload handlers after the upload has been processed.'
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
self.client.post('/quota/broken/', {'f': file})
|
2008-07-08 06:06:32 +08:00
|
|
|
|
|
|
|
def test_fileupload_getlist(self):
|
2014-05-26 04:08:05 +08:00
|
|
|
file = tempfile.NamedTemporaryFile
|
|
|
|
with file() as file1, file() as file2, file() as file2a:
|
|
|
|
file1.write(b'a' * (2 ** 23))
|
|
|
|
file1.seek(0)
|
|
|
|
|
|
|
|
file2.write(b'a' * (2 * 2 ** 18))
|
|
|
|
file2.seek(0)
|
|
|
|
|
|
|
|
file2a.write(b'a' * (5 * 2 ** 20))
|
|
|
|
file2a.seek(0)
|
|
|
|
|
|
|
|
response = self.client.post('/getlist_count/', {
|
|
|
|
'file1': file1,
|
|
|
|
'field1': 'test',
|
|
|
|
'field2': 'test3',
|
|
|
|
'field3': 'test5',
|
|
|
|
'field4': 'test6',
|
|
|
|
'field5': 'test7',
|
|
|
|
'file2': (file2, file2a)
|
|
|
|
})
|
2017-02-08 02:23:57 +08:00
|
|
|
got = response.json()
|
2014-05-26 04:08:05 +08:00
|
|
|
self.assertEqual(got.get('file1'), 1)
|
|
|
|
self.assertEqual(got.get('file2'), 2)
|
2008-07-20 20:44:41 +08:00
|
|
|
|
2014-05-26 04:52:47 +08:00
|
|
|
def test_fileuploads_closed_at_request_end(self):
|
|
|
|
file = tempfile.NamedTemporaryFile
|
|
|
|
with file() as f1, file() as f2a, file() as f2b:
|
|
|
|
response = self.client.post('/fd_closing/t/', {
|
|
|
|
'file': f1,
|
|
|
|
'file2': (f2a, f2b),
|
|
|
|
})
|
|
|
|
|
|
|
|
request = response.wsgi_request
|
2016-10-27 15:53:39 +08:00
|
|
|
# The files were parsed.
|
2014-05-26 04:52:47 +08:00
|
|
|
self.assertTrue(hasattr(request, '_files'))
|
|
|
|
|
|
|
|
file = request._files['file']
|
|
|
|
self.assertTrue(file.closed)
|
|
|
|
|
|
|
|
files = request._files.getlist('file2')
|
|
|
|
self.assertTrue(files[0].closed)
|
|
|
|
self.assertTrue(files[1].closed)
|
|
|
|
|
|
|
|
def test_no_parsing_triggered_by_fd_closing(self):
|
|
|
|
file = tempfile.NamedTemporaryFile
|
|
|
|
with file() as f1, file() as f2a, file() as f2b:
|
|
|
|
response = self.client.post('/fd_closing/f/', {
|
|
|
|
'file': f1,
|
|
|
|
'file2': (f2a, f2b),
|
|
|
|
})
|
|
|
|
|
|
|
|
request = response.wsgi_request
|
2016-10-27 15:53:39 +08:00
|
|
|
# The fd closing logic doesn't trigger parsing of the stream
|
2014-05-26 04:52:47 +08:00
|
|
|
self.assertFalse(hasattr(request, '_files'))
|
|
|
|
|
2008-08-31 03:56:14 +08:00
|
|
|
def test_file_error_blocking(self):
|
|
|
|
"""
|
|
|
|
The server should not block when there are upload errors (bug #8622).
|
|
|
|
This can happen if something -- i.e. an exception handler -- tries to
|
|
|
|
access POST while handling an error in parsing POST. This shouldn't
|
|
|
|
cause an infinite loop!
|
|
|
|
"""
|
|
|
|
class POSTAccessingHandler(client.ClientHandler):
|
|
|
|
"""A handler that'll access POST during an exception."""
|
|
|
|
def handle_uncaught_exception(self, request, resolver, exc_info):
|
2017-01-21 21:13:44 +08:00
|
|
|
ret = super().handle_uncaught_exception(request, resolver, exc_info)
|
2013-10-19 20:31:38 +08:00
|
|
|
request.POST # evaluate
|
2008-08-31 03:56:14 +08:00
|
|
|
return ret
|
2010-10-11 20:55:17 +08:00
|
|
|
|
2008-08-31 03:56:14 +08:00
|
|
|
# Maybe this is a little more complicated that it needs to be; but if
|
|
|
|
# the django.test.client.FakePayload.read() implementation changes then
|
|
|
|
# this test would fail. So we need to know exactly what kind of error
|
|
|
|
# it raises when there is an attempt to read more than the available bytes:
|
|
|
|
try:
|
2012-05-19 23:43:34 +08:00
|
|
|
client.FakePayload(b'a').read(2)
|
2012-08-15 02:51:50 +08:00
|
|
|
except Exception as err:
|
|
|
|
reference_error = err
|
2008-08-31 03:56:14 +08:00
|
|
|
|
|
|
|
# install the custom handler that tries to access request.POST
|
|
|
|
self.client.handler = POSTAccessingHandler()
|
|
|
|
|
2012-05-19 23:43:34 +08:00
|
|
|
with open(__file__, 'rb') as fp:
|
2012-05-05 20:01:38 +08:00
|
|
|
post_data = {
|
|
|
|
'name': 'Ringo',
|
|
|
|
'file_field': fp,
|
|
|
|
}
|
|
|
|
try:
|
2014-01-14 23:43:27 +08:00
|
|
|
self.client.post('/upload_errors/', post_data)
|
2012-05-05 20:01:38 +08:00
|
|
|
except reference_error.__class__ as err:
|
2019-10-21 16:55:05 +08:00
|
|
|
self.assertNotEqual(
|
|
|
|
str(err),
|
|
|
|
str(reference_error),
|
2012-05-05 20:01:38 +08:00
|
|
|
"Caught a repeated exception that'll cause an infinite loop in file uploads."
|
|
|
|
)
|
|
|
|
except Exception as err:
|
|
|
|
# CustomUploadError is the error that should have been raised
|
|
|
|
self.assertEqual(err.__class__, uploadhandler.CustomUploadError)
|
2011-05-23 07:56:42 +08:00
|
|
|
|
|
|
|
def test_filename_case_preservation(self):
|
|
|
|
"""
|
|
|
|
The storage backend shouldn't mess with the case of the filenames
|
|
|
|
uploaded.
|
|
|
|
"""
|
|
|
|
# Synthesize the contents of a file upload with a mixed case filename
|
|
|
|
# so we don't have to carry such a file in the Django tests source code
|
|
|
|
# tree.
|
|
|
|
vars = {'boundary': 'oUrBoUnDaRyStRiNg'}
|
|
|
|
post_data = [
|
|
|
|
'--%(boundary)s',
|
2013-10-20 07:33:10 +08:00
|
|
|
'Content-Disposition: form-data; name="file_field"; filename="MiXeD_cAsE.txt"',
|
2011-05-23 07:56:42 +08:00
|
|
|
'Content-Type: application/octet-stream',
|
|
|
|
'',
|
|
|
|
'file contents\n'
|
|
|
|
'',
|
|
|
|
'--%(boundary)s--\r\n',
|
|
|
|
]
|
|
|
|
response = self.client.post(
|
2014-01-14 23:43:27 +08:00
|
|
|
'/filename_case/',
|
2011-05-23 07:56:42 +08:00
|
|
|
'\r\n'.join(post_data) % vars,
|
|
|
|
'multipart/form-data; boundary=%(boundary)s' % vars
|
|
|
|
)
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
id = int(response.content)
|
|
|
|
obj = FileModel.objects.get(pk=id)
|
|
|
|
# The name of the file uploaded and the file stored in the server-side
|
|
|
|
# shouldn't differ.
|
|
|
|
self.assertEqual(os.path.basename(obj.testfile.path), 'MiXeD_cAsE.txt')
|
2008-08-31 03:56:14 +08:00
|
|
|
|
2013-11-03 12:36:09 +08:00
|
|
|
|
2012-10-31 05:20:42 +08:00
|
|
|
@override_settings(MEDIA_ROOT=MEDIA_ROOT)
|
2015-04-18 05:38:20 +08:00
|
|
|
class DirectoryCreationTests(SimpleTestCase):
|
2008-07-20 20:44:41 +08:00
|
|
|
"""
|
2008-07-27 06:48:51 +08:00
|
|
|
Tests for error handling during directory creation
|
2008-07-20 20:44:41 +08:00
|
|
|
via _save_FIELD_file (ticket #6450)
|
|
|
|
"""
|
2012-10-31 05:20:42 +08:00
|
|
|
@classmethod
|
|
|
|
def setUpClass(cls):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().setUpClass()
|
2019-01-31 23:12:55 +08:00
|
|
|
os.makedirs(MEDIA_ROOT, exist_ok=True)
|
2012-10-31 05:20:42 +08:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def tearDownClass(cls):
|
|
|
|
shutil.rmtree(MEDIA_ROOT)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().tearDownClass()
|
2012-10-31 05:20:42 +08:00
|
|
|
|
2008-07-20 20:44:41 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.obj = FileModel()
|
|
|
|
|
2016-06-28 23:21:26 +08:00
|
|
|
@unittest.skipIf(sys.platform == 'win32', "Python on Windows doesn't have working os.chmod().")
|
2008-07-20 20:44:41 +08:00
|
|
|
def test_readonly_root(self):
|
|
|
|
"""Permission errors are not swallowed"""
|
2012-10-31 05:20:42 +08:00
|
|
|
os.chmod(MEDIA_ROOT, 0o500)
|
|
|
|
self.addCleanup(os.chmod, MEDIA_ROOT, 0o700)
|
2017-01-25 23:13:08 +08:00
|
|
|
with self.assertRaises(PermissionError):
|
2015-05-26 07:09:01 +08:00
|
|
|
self.obj.testfile.save('foo.txt', SimpleUploadedFile('foo.txt', b'x'), save=False)
|
2008-07-20 20:44:41 +08:00
|
|
|
|
|
|
|
def test_not_a_directory(self):
|
|
|
|
# Create a file with the upload directory name
|
2012-05-19 23:43:34 +08:00
|
|
|
open(UPLOAD_TO, 'wb').close()
|
2012-10-31 05:20:42 +08:00
|
|
|
self.addCleanup(os.remove, UPLOAD_TO)
|
2019-01-28 23:01:35 +08:00
|
|
|
msg = '%s exists and is not a directory.' % UPLOAD_TO
|
|
|
|
with self.assertRaisesMessage(FileExistsError, msg):
|
2014-05-26 04:08:05 +08:00
|
|
|
with SimpleUploadedFile('foo.txt', b'x') as file:
|
2015-05-26 07:09:01 +08:00
|
|
|
self.obj.testfile.save('foo.txt', file, save=False)
|
2012-09-08 03:34:48 +08:00
|
|
|
|
2009-05-09 01:22:34 +08:00
|
|
|
|
2018-06-13 02:42:20 +08:00
|
|
|
class MultiParserTests(SimpleTestCase):
|
2009-05-09 01:22:34 +08:00
|
|
|
|
|
|
|
def test_empty_upload_handlers(self):
|
|
|
|
# We're not actually parsing here; just checking if the parser properly
|
|
|
|
# instantiates with empty upload handlers.
|
2013-10-19 20:31:38 +08:00
|
|
|
MultiPartParser({
|
2013-11-03 03:37:48 +08:00
|
|
|
'CONTENT_TYPE': 'multipart/form-data; boundary=_foo',
|
|
|
|
'CONTENT_LENGTH': '1'
|
2009-05-09 01:22:34 +08:00
|
|
|
}, StringIO('x'), [], 'utf-8')
|
2014-07-12 20:08:50 +08:00
|
|
|
|
2018-06-13 02:42:20 +08:00
|
|
|
def test_invalid_content_type(self):
|
|
|
|
with self.assertRaisesMessage(MultiPartParserError, 'Invalid Content-Type: text/plain'):
|
|
|
|
MultiPartParser({
|
|
|
|
'CONTENT_TYPE': 'text/plain',
|
|
|
|
'CONTENT_LENGTH': '1',
|
|
|
|
}, StringIO('x'), [], 'utf-8')
|
|
|
|
|
|
|
|
def test_negative_content_length(self):
|
|
|
|
with self.assertRaisesMessage(MultiPartParserError, 'Invalid content length: -1'):
|
|
|
|
MultiPartParser({
|
|
|
|
'CONTENT_TYPE': 'multipart/form-data; boundary=_foo',
|
|
|
|
'CONTENT_LENGTH': -1,
|
|
|
|
}, StringIO('x'), [], 'utf-8')
|
|
|
|
|
|
|
|
def test_bad_type_content_length(self):
|
|
|
|
multipart_parser = MultiPartParser({
|
|
|
|
'CONTENT_TYPE': 'multipart/form-data; boundary=_foo',
|
|
|
|
'CONTENT_LENGTH': 'a',
|
|
|
|
}, StringIO('x'), [], 'utf-8')
|
|
|
|
self.assertEqual(multipart_parser._content_length, 0)
|
|
|
|
|
2014-07-12 20:08:50 +08:00
|
|
|
def test_rfc2231_parsing(self):
|
|
|
|
test_data = (
|
|
|
|
(b"Content-Type: application/x-stuff; title*=us-ascii'en-us'This%20is%20%2A%2A%2Afun%2A%2A%2A",
|
|
|
|
"This is ***fun***"),
|
|
|
|
(b"Content-Type: application/x-stuff; title*=UTF-8''foo-%c3%a4.html",
|
|
|
|
"foo-ä.html"),
|
|
|
|
(b"Content-Type: application/x-stuff; title*=iso-8859-1''foo-%E4.html",
|
|
|
|
"foo-ä.html"),
|
|
|
|
)
|
|
|
|
for raw_line, expected_title in test_data:
|
|
|
|
parsed = parse_header(raw_line)
|
|
|
|
self.assertEqual(parsed[1]['title'], expected_title)
|
2015-01-24 20:14:30 +08:00
|
|
|
|
|
|
|
def test_rfc2231_wrong_title(self):
|
|
|
|
"""
|
|
|
|
Test wrongly formatted RFC 2231 headers (missing double single quotes).
|
|
|
|
Parsing should not crash (#24209).
|
|
|
|
"""
|
|
|
|
test_data = (
|
|
|
|
(b"Content-Type: application/x-stuff; title*='This%20is%20%2A%2A%2Afun%2A%2A%2A",
|
|
|
|
b"'This%20is%20%2A%2A%2Afun%2A%2A%2A"),
|
|
|
|
(b"Content-Type: application/x-stuff; title*='foo.html",
|
|
|
|
b"'foo.html"),
|
|
|
|
(b"Content-Type: application/x-stuff; title*=bar.html",
|
|
|
|
b"bar.html"),
|
|
|
|
)
|
|
|
|
for raw_line, expected_title in test_data:
|
|
|
|
parsed = parse_header(raw_line)
|
|
|
|
self.assertEqual(parsed[1]['title'], expected_title)
|