| 1 |
import base64 |
| 2 |
import hashlib |
| 3 |
import os |
| 4 |
import shutil |
| 5 |
import sys |
| 6 |
import tempfile as sys_tempfile |
| 7 |
import unittest |
| 8 |
from io import BytesIO, StringIO |
| 9 |
from unittest import mock |
| 10 |
from urllib.parse import quote |
| 11 |
|
| 12 |
from django.conf import DEFAULT_STORAGE_ALIAS |
| 13 |
from django.core.exceptions import SuspiciousFileOperation |
| 14 |
from django.core.files import temp as tempfile |
| 15 |
from django.core.files.storage import default_storage |
| 16 |
from django.core.files.uploadedfile import SimpleUploadedFile, UploadedFile |
| 17 |
from django.http.multipartparser import ( |
| 18 |
FILE, |
| 19 |
MAX_TOTAL_HEADER_SIZE, |
| 20 |
MultiPartParser, |
| 21 |
MultiPartParserError, |
| 22 |
Parser, |
| 23 |
) |
| 24 |
from django.test import SimpleTestCase, TestCase, client, override_settings |
| 25 |
|
| 26 |
from . import uploadhandler |
| 27 |
from .models import FileModel |
| 28 |
|
| 29 |
UNICODE_FILENAME = "test-0123456789_中文_Orléans.jpg" |
| 30 |
MEDIA_ROOT = sys_tempfile.mkdtemp() |
| 31 |
UPLOAD_FOLDER = "test_upload" |
| 32 |
UPLOAD_TO = os.path.join(MEDIA_ROOT, UPLOAD_FOLDER) |
| 33 |
|
| 34 |
CANDIDATE_TRAVERSAL_FILE_NAMES = [ |
| 35 |
"/tmp/hax0rd.txt", # Absolute path, *nix-style. |
| 36 |
"C:\\Windows\\hax0rd.txt", # Absolute path, win-style. |
| 37 |
"C:/Windows/hax0rd.txt", # Absolute path, broken-style. |
| 38 |
"\\tmp\\hax0rd.txt", # Absolute path, broken in a different way. |
| 39 |
"/tmp\\hax0rd.txt", # Absolute path, broken by mixing. |
| 40 |
"subdir/hax0rd.txt", # Descendant path, *nix-style. |
| 41 |
"subdir\\hax0rd.txt", # Descendant path, win-style. |
| 42 |
"sub/dir\\hax0rd.txt", # Descendant path, mixed. |
| 43 |
"../../hax0rd.txt", # Relative path, *nix-style. |
| 44 |
"..\\..\\hax0rd.txt", # Relative path, win-style. |
| 45 |
"../..\\hax0rd.txt", # Relative path, mixed. |
| 46 |
"../hax0rd.txt", # HTML entities. |
| 47 |
"../hax0rd.txt", # HTML entities. |
| 48 |
] |
| 49 |
|
| 50 |
CANDIDATE_INVALID_FILE_NAMES = [ |
| 51 |
"/tmp/", # Directory, *nix-style. |
| 52 |
"c:\\tmp\\", # Directory, win-style. |
| 53 |
"/tmp/.", # Directory dot, *nix-style. |
| 54 |
"c:\\tmp\\.", # Directory dot, *nix-style. |
| 55 |
"/tmp/..", # Parent directory, *nix-style. |
| 56 |
"c:\\tmp\\..", # Parent directory, win-style. |
| 57 |
"", # Empty filename. |
| 58 |
] |
| 59 |
|
| 60 |
|
| 61 |
@override_settings( |
| 62 |
MEDIA_ROOT=MEDIA_ROOT, ROOT_URLCONF="file_uploads.urls", MIDDLEWARE=[] |
| 63 |
) |
| 64 |
class FileUploadTests(TestCase): |
| 65 |
@classmethod |
| 66 |
def setUpClass(cls): |
| 67 |
super().setUpClass() |
| 68 |
os.makedirs(MEDIA_ROOT, exist_ok=True) |
| 69 |
cls.addClassCleanup(shutil.rmtree, MEDIA_ROOT) |
| 70 |
|
| 71 |
def test_upload_name_is_validated(self): |
| 72 |
candidates = [ |
| 73 |
"/tmp/", |
| 74 |
"/tmp/..", |
| 75 |
"/tmp/.", |
| 76 |
] |
| 77 |
if sys.platform == "win32": |
| 78 |
candidates.extend( |
| 79 |
[ |
| 80 |
"c:\\tmp\\", |
| 81 |
"c:\\tmp\\..", |
| 82 |
"c:\\tmp\\.", |
| 83 |
] |
| 84 |
) |
| 85 |
for file_name in candidates: |
| 86 |
with self.subTest(file_name=file_name): |
| 87 |
self.assertRaises(SuspiciousFileOperation, UploadedFile, name=file_name) |
| 88 |
|
| 89 |
def test_simple_upload(self): |
| 90 |
with open(__file__, "rb") as fp: |
| 91 |
post_data = { |
| 92 |
"name": "Ringo", |
| 93 |
"file_field": fp, |
| 94 |
} |
| 95 |
response = self.client.post("/upload/", post_data) |
| 96 |
self.assertEqual(response.status_code, 200) |
| 97 |
|
| 98 |
def test_large_upload(self): |
| 99 |
file = tempfile.NamedTemporaryFile |
| 100 |
with file(suffix=".file1") as file1, file(suffix=".file2") as file2: |
| 101 |
file1.write(b"a" * (2**21)) |
| 102 |
file1.seek(0) |
| 103 |
|
| 104 |
file2.write(b"a" * (10 * 2**20)) |
| 105 |
file2.seek(0) |
| 106 |
|
| 107 |
post_data = { |
| 108 |
"name": "Ringo", |
| 109 |
"file_field1": file1, |
| 110 |
"file_field2": file2, |
| 111 |
} |
| 112 |
|
| 113 |
for key in list(post_data): |
| 114 |
try: |
| 115 |
post_data[key + "_hash"] = hashlib.sha1( |
| 116 |
post_data[key].read() |
| 117 |
).hexdigest() |
| 118 |
post_data[key].seek(0) |
| 119 |
except AttributeError: |
| 120 |
post_data[key + "_hash"] = hashlib.sha1( |
| 121 |
post_data[key].encode() |
| 122 |
).hexdigest() |
| 123 |
|
| 124 |
response = self.client.post("/verify/", post_data) |
| 125 |
|
| 126 |
self.assertEqual(response.status_code, 200) |
| 127 |
|
| 128 |
def _test_base64_upload(self, content, encode=base64.b64encode): |
| 129 |
payload = client.FakePayload( |
| 130 |
"\r\n".join( |
| 131 |
[ |
| 132 |
"--" + client.BOUNDARY, |
| 133 |
'Content-Disposition: form-data; name="file"; filename="test.txt"', |
| 134 |
"Content-Type: application/octet-stream", |
| 135 |
"Content-Transfer-Encoding: base64", |
| 136 |
"", |
| 137 |
] |
| 138 |
) |
| 139 |
) |
| 140 |
payload.write(b"\r\n" + encode(content.encode()) + b"\r\n") |
| 141 |
payload.write("--" + client.BOUNDARY + "--\r\n") |
| 142 |
r = { |
| 143 |
"CONTENT_LENGTH": len(payload), |
| 144 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 145 |
"PATH_INFO": "/echo_content/", |
| 146 |
"REQUEST_METHOD": "POST", |
| 147 |
"wsgi.input": payload, |
| 148 |
} |
| 149 |
response = self.client.request(**r) |
| 150 |
self.assertEqual(response.json()["file"], content) |
| 151 |
|
| 152 |
def test_base64_upload(self): |
| 153 |
self._test_base64_upload("This data will be transmitted base64-encoded.") |
| 154 |
|
| 155 |
def test_big_base64_upload(self): |
| 156 |
self._test_base64_upload("Big data" * 68000) # > 512Kb |
| 157 |
|
| 158 |
def test_big_base64_newlines_upload(self): |
| 159 |
self._test_base64_upload("Big data" * 68000, encode=base64.encodebytes) |
| 160 |
|
| 161 |
def test_base64_invalid_upload(self): |
| 162 |
payload = client.FakePayload( |
| 163 |
"\r\n".join( |
| 164 |
[ |
| 165 |
"--" + client.BOUNDARY, |
| 166 |
'Content-Disposition: form-data; name="file"; filename="test.txt"', |
| 167 |
"Content-Type: application/octet-stream", |
| 168 |
"Content-Transfer-Encoding: base64", |
| 169 |
"", |
| 170 |
] |
| 171 |
) |
| 172 |
) |
| 173 |
payload.write(b"\r\n!\r\n") |
| 174 |
payload.write("--" + client.BOUNDARY + "--\r\n") |
| 175 |
r = { |
| 176 |
"CONTENT_LENGTH": len(payload), |
| 177 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 178 |
"PATH_INFO": "/echo_content/", |
| 179 |
"REQUEST_METHOD": "POST", |
| 180 |
"wsgi.input": payload, |
| 181 |
} |
| 182 |
response = self.client.request(**r) |
| 183 |
self.assertEqual(response.json()["file"], "") |
| 184 |
|
| 185 |
def test_unicode_file_name(self): |
| 186 |
with sys_tempfile.TemporaryDirectory() as temp_dir: |
| 187 |
# This file contains Chinese symbols and an accented char in the name. |
| 188 |
with open(os.path.join(temp_dir, UNICODE_FILENAME), "w+b") as file1: |
| 189 |
file1.write(b"b" * (2**10)) |
| 190 |
file1.seek(0) |
| 191 |
response = self.client.post("/unicode_name/", {"file_unicode": file1}) |
| 192 |
self.assertEqual(response.status_code, 200) |
| 193 |
|
| 194 |
def test_unicode_file_name_rfc2231(self): |
| 195 |
""" |
| 196 |
Receiving file upload when filename is encoded with RFC 2231. |
| 197 |
""" |
| 198 |
payload = client.FakePayload() |
| 199 |
payload.write( |
| 200 |
"\r\n".join( |
| 201 |
[ |
| 202 |
"--" + client.BOUNDARY, |
| 203 |
'Content-Disposition: form-data; name="file_unicode"; ' |
| 204 |
"filename*=UTF-8''%s" % quote(UNICODE_FILENAME), |
| 205 |
"Content-Type: application/octet-stream", |
| 206 |
"", |
| 207 |
"You got pwnd.\r\n", |
| 208 |
"\r\n--" + client.BOUNDARY + "--\r\n", |
| 209 |
] |
| 210 |
) |
| 211 |
) |
| 212 |
|
| 213 |
r = { |
| 214 |
"CONTENT_LENGTH": len(payload), |
| 215 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 216 |
"PATH_INFO": "/unicode_name/", |
| 217 |
"REQUEST_METHOD": "POST", |
| 218 |
"wsgi.input": payload, |
| 219 |
} |
| 220 |
response = self.client.request(**r) |
| 221 |
self.assertEqual(response.status_code, 200) |
| 222 |
|
| 223 |
def test_unicode_name_rfc2231(self): |
| 224 |
""" |
| 225 |
Receiving file upload when filename is encoded with RFC 2231. |
| 226 |
""" |
| 227 |
payload = client.FakePayload() |
| 228 |
payload.write( |
| 229 |
"\r\n".join( |
| 230 |
[ |
| 231 |
"--" + client.BOUNDARY, |
| 232 |
"Content-Disposition: form-data; name*=UTF-8''file_unicode; " |
| 233 |
"filename*=UTF-8''%s" % quote(UNICODE_FILENAME), |
| 234 |
"Content-Type: application/octet-stream", |
| 235 |
"", |
| 236 |
"You got pwnd.\r\n", |
| 237 |
"\r\n--" + client.BOUNDARY + "--\r\n", |
| 238 |
] |
| 239 |
) |
| 240 |
) |
| 241 |
|
| 242 |
r = { |
| 243 |
"CONTENT_LENGTH": len(payload), |
| 244 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 245 |
"PATH_INFO": "/unicode_name/", |
| 246 |
"REQUEST_METHOD": "POST", |
| 247 |
"wsgi.input": payload, |
| 248 |
} |
| 249 |
response = self.client.request(**r) |
| 250 |
self.assertEqual(response.status_code, 200) |
| 251 |
|
| 252 |
def test_unicode_file_name_rfc2231_with_double_quotes(self): |
| 253 |
payload = client.FakePayload() |
| 254 |
payload.write( |
| 255 |
"\r\n".join( |
| 256 |
[ |
| 257 |
"--" + client.BOUNDARY, |
| 258 |
'Content-Disposition: form-data; name="file_unicode"; ' |
| 259 |
"filename*=\"UTF-8''%s\"" % quote(UNICODE_FILENAME), |
| 260 |
"Content-Type: application/octet-stream", |
| 261 |
"", |
| 262 |
"You got pwnd.\r\n", |
| 263 |
"\r\n--" + client.BOUNDARY + "--\r\n", |
| 264 |
] |
| 265 |
) |
| 266 |
) |
| 267 |
r = { |
| 268 |
"CONTENT_LENGTH": len(payload), |
| 269 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 270 |
"PATH_INFO": "/unicode_name/", |
| 271 |
"REQUEST_METHOD": "POST", |
| 272 |
"wsgi.input": payload, |
| 273 |
} |
| 274 |
response = self.client.request(**r) |
| 275 |
self.assertEqual(response.status_code, 200) |
| 276 |
|
| 277 |
def test_unicode_name_rfc2231_with_double_quotes(self): |
| 278 |
payload = client.FakePayload() |
| 279 |
payload.write( |
| 280 |
"\r\n".join( |
| 281 |
[ |
| 282 |
"--" + client.BOUNDARY, |
| 283 |
"Content-Disposition: form-data; name*=\"UTF-8''file_unicode\"; " |
| 284 |
"filename*=\"UTF-8''%s\"" % quote(UNICODE_FILENAME), |
| 285 |
"Content-Type: application/octet-stream", |
| 286 |
"", |
| 287 |
"You got pwnd.\r\n", |
| 288 |
"\r\n--" + client.BOUNDARY + "--\r\n", |
| 289 |
] |
| 290 |
) |
| 291 |
) |
| 292 |
r = { |
| 293 |
"CONTENT_LENGTH": len(payload), |
| 294 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 295 |
"PATH_INFO": "/unicode_name/", |
| 296 |
"REQUEST_METHOD": "POST", |
| 297 |
"wsgi.input": payload, |
| 298 |
} |
| 299 |
response = self.client.request(**r) |
| 300 |
self.assertEqual(response.status_code, 200) |
| 301 |
|
| 302 |
def test_blank_filenames(self): |
| 303 |
""" |
| 304 |
Receiving file upload when filename is blank (before and after |
| 305 |
sanitization) should be okay. |
| 306 |
""" |
| 307 |
filenames = [ |
| 308 |
"", |
| 309 |
# Normalized by MultiPartParser.IE_sanitize(). |
| 310 |
"C:\\Windows\\", |
| 311 |
# Normalized by os.path.basename(). |
| 312 |
"/", |
| 313 |
"ends-with-slash/", |
| 314 |
] |
| 315 |
payload = client.FakePayload() |
| 316 |
for i, name in enumerate(filenames): |
| 317 |
payload.write( |
| 318 |
"\r\n".join( |
| 319 |
[ |
| 320 |
"--" + client.BOUNDARY, |
| 321 |
'Content-Disposition: form-data; name="file%s"; filename="%s"' |
| 322 |
% (i, name), |
| 323 |
"Content-Type: application/octet-stream", |
| 324 |
"", |
| 325 |
"You got pwnd.\r\n", |
| 326 |
] |
| 327 |
) |
| 328 |
) |
| 329 |
payload.write("\r\n--" + client.BOUNDARY + "--\r\n") |
| 330 |
|
| 331 |
r = { |
| 332 |
"CONTENT_LENGTH": len(payload), |
| 333 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 334 |
"PATH_INFO": "/echo/", |
| 335 |
"REQUEST_METHOD": "POST", |
| 336 |
"wsgi.input": payload, |
| 337 |
} |
| 338 |
response = self.client.request(**r) |
| 339 |
self.assertEqual(response.status_code, 200) |
| 340 |
|
| 341 |
# Empty filenames should be ignored |
| 342 |
received = response.json() |
| 343 |
for i, name in enumerate(filenames): |
| 344 |
self.assertIsNone(received.get("file%s" % i)) |
| 345 |
|
| 346 |
def test_non_printable_chars_in_file_names(self): |
| 347 |
file_name = "non-\x00printable\x00\n_chars.txt\x00" |
| 348 |
payload = client.FakePayload() |
| 349 |
payload.write( |
| 350 |
"\r\n".join( |
| 351 |
[ |
| 352 |
"--" + client.BOUNDARY, |
| 353 |
f'Content-Disposition: form-data; name="file"; ' |
| 354 |
f'filename="{file_name}"', |
| 355 |
"Content-Type: application/octet-stream", |
| 356 |
"", |
| 357 |
"You got pwnd.\r\n", |
| 358 |
] |
| 359 |
) |
| 360 |
) |
| 361 |
payload.write("\r\n--" + client.BOUNDARY + "--\r\n") |
| 362 |
r = { |
| 363 |
"CONTENT_LENGTH": len(payload), |
| 364 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 365 |
"PATH_INFO": "/echo/", |
| 366 |
"REQUEST_METHOD": "POST", |
| 367 |
"wsgi.input": payload, |
| 368 |
} |
| 369 |
response = self.client.request(**r) |
| 370 |
# Non-printable chars are sanitized. |
| 371 |
received = response.json() |
| 372 |
self.assertEqual(received["file"], "non-printable_chars.txt") |
| 373 |
|
| 374 |
def test_dangerous_file_names(self): |
| 375 |
"""Uploaded file names should be sanitized before ever reaching the view.""" |
| 376 |
# This test simulates possible directory traversal attacks by a |
| 377 |
# malicious uploader We have to do some monkeybusiness here to construct |
| 378 |
# a malicious payload with an invalid file name (containing os.sep or |
| 379 |
# os.pardir). This similar to what an attacker would need to do when |
| 380 |
# trying such an attack. |
| 381 |
payload = client.FakePayload() |
| 382 |
for i, name in enumerate(CANDIDATE_TRAVERSAL_FILE_NAMES): |
| 383 |
payload.write( |
| 384 |
"\r\n".join( |
| 385 |
[ |
| 386 |
"--" + client.BOUNDARY, |
| 387 |
'Content-Disposition: form-data; name="file%s"; filename="%s"' |
| 388 |
% (i, name), |
| 389 |
"Content-Type: application/octet-stream", |
| 390 |
"", |
| 391 |
"You got pwnd.\r\n", |
| 392 |
] |
| 393 |
) |
| 394 |
) |
| 395 |
payload.write("\r\n--" + client.BOUNDARY + "--\r\n") |
| 396 |
|
| 397 |
r = { |
| 398 |
"CONTENT_LENGTH": len(payload), |
| 399 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 400 |
"PATH_INFO": "/echo/", |
| 401 |
"REQUEST_METHOD": "POST", |
| 402 |
"wsgi.input": payload, |
| 403 |
} |
| 404 |
response = self.client.request(**r) |
| 405 |
# The filenames should have been sanitized by the time it got to the view. |
| 406 |
received = response.json() |
| 407 |
for i, name in enumerate(CANDIDATE_TRAVERSAL_FILE_NAMES): |
| 408 |
got = received["file%s" % i] |
| 409 |
self.assertEqual(got, "hax0rd.txt") |
| 410 |
|
| 411 |
def test_filename_overflow(self): |
| 412 |
"""File names over 256 characters (dangerous on some platforms) get fixed up.""" |
| 413 |
long_str = "f" * 300 |
| 414 |
cases = [ |
| 415 |
# field name, filename, expected |
| 416 |
("long_filename", "%s.txt" % long_str, "%s.txt" % long_str[:251]), |
| 417 |
("long_extension", "foo.%s" % long_str, ".%s" % long_str[:254]), |
| 418 |
("no_extension", long_str, long_str[:255]), |
| 419 |
("no_filename", ".%s" % long_str, ".%s" % long_str[:254]), |
| 420 |
("long_everything", "%s.%s" % (long_str, long_str), ".%s" % long_str[:254]), |
| 421 |
] |
| 422 |
payload = client.FakePayload() |
| 423 |
for name, filename, _ in cases: |
| 424 |
payload.write( |
| 425 |
"\r\n".join( |
| 426 |
[ |
| 427 |
"--" + client.BOUNDARY, |
| 428 |
'Content-Disposition: form-data; name="{}"; filename="{}"', |
| 429 |
"Content-Type: application/octet-stream", |
| 430 |
"", |
| 431 |
"Oops.", |
| 432 |
"", |
| 433 |
] |
| 434 |
).format(name, filename) |
| 435 |
) |
| 436 |
payload.write("\r\n--" + client.BOUNDARY + "--\r\n") |
| 437 |
r = { |
| 438 |
"CONTENT_LENGTH": len(payload), |
| 439 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 440 |
"PATH_INFO": "/echo/", |
| 441 |
"REQUEST_METHOD": "POST", |
| 442 |
"wsgi.input": payload, |
| 443 |
} |
| 444 |
response = self.client.request(**r) |
| 445 |
result = response.json() |
| 446 |
for name, _, expected in cases: |
| 447 |
got = result[name] |
| 448 |
self.assertEqual(expected, got, "Mismatch for {}".format(name)) |
| 449 |
self.assertLess( |
| 450 |
len(got), 256, "Got a long file name (%s characters)." % len(got) |
| 451 |
) |
| 452 |
|
| 453 |
def test_file_content(self): |
| 454 |
file = tempfile.NamedTemporaryFile |
| 455 |
with ( |
| 456 |
file(suffix=".ctype_extra") as no_content_type, |
| 457 |
file(suffix=".ctype_extra") as simple_file, |
| 458 |
): |
| 459 |
no_content_type.write(b"no content") |
| 460 |
no_content_type.seek(0) |
| 461 |
|
| 462 |
simple_file.write(b"text content") |
| 463 |
simple_file.seek(0) |
| 464 |
simple_file.content_type = "text/plain" |
| 465 |
|
| 466 |
string_io = StringIO("string content") |
| 467 |
bytes_io = BytesIO(b"binary content") |
| 468 |
|
| 469 |
response = self.client.post( |
| 470 |
"/echo_content/", |
| 471 |
{ |
| 472 |
"no_content_type": no_content_type, |
| 473 |
"simple_file": simple_file, |
| 474 |
"string": string_io, |
| 475 |
"binary": bytes_io, |
| 476 |
}, |
| 477 |
) |
| 478 |
received = response.json() |
| 479 |
self.assertEqual(received["no_content_type"], "no content") |
| 480 |
self.assertEqual(received["simple_file"], "text content") |
| 481 |
self.assertEqual(received["string"], "string content") |
| 482 |
self.assertEqual(received["binary"], "binary content") |
| 483 |
|
| 484 |
def test_content_type_extra(self): |
| 485 |
"""Uploaded files may have content type parameters available.""" |
| 486 |
file = tempfile.NamedTemporaryFile |
| 487 |
with ( |
| 488 |
file(suffix=".ctype_extra") as no_content_type, |
| 489 |
file(suffix=".ctype_extra") as simple_file, |
| 490 |
): |
| 491 |
no_content_type.write(b"something") |
| 492 |
no_content_type.seek(0) |
| 493 |
|
| 494 |
simple_file.write(b"something") |
| 495 |
simple_file.seek(0) |
| 496 |
simple_file.content_type = "text/plain; test-key=test_value" |
| 497 |
|
| 498 |
response = self.client.post( |
| 499 |
"/echo_content_type_extra/", |
| 500 |
{ |
| 501 |
"no_content_type": no_content_type, |
| 502 |
"simple_file": simple_file, |
| 503 |
}, |
| 504 |
) |
| 505 |
received = response.json() |
| 506 |
self.assertEqual(received["no_content_type"], {}) |
| 507 |
self.assertEqual(received["simple_file"], {"test-key": "test_value"}) |
| 508 |
|
| 509 |
def test_truncated_multipart_handled_gracefully(self): |
| 510 |
""" |
| 511 |
If passed an incomplete multipart message, MultiPartParser does not |
| 512 |
attempt to read beyond the end of the stream, and simply will handle |
| 513 |
the part that can be parsed gracefully. |
| 514 |
""" |
| 515 |
payload_str = "\r\n".join( |
| 516 |
[ |
| 517 |
"--" + client.BOUNDARY, |
| 518 |
'Content-Disposition: form-data; name="file"; filename="foo.txt"', |
| 519 |
"Content-Type: application/octet-stream", |
| 520 |
"", |
| 521 |
"file contents" "--" + client.BOUNDARY + "--", |
| 522 |
"", |
| 523 |
] |
| 524 |
) |
| 525 |
payload = client.FakePayload(payload_str[:-10]) |
| 526 |
r = { |
| 527 |
"CONTENT_LENGTH": len(payload), |
| 528 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 529 |
"PATH_INFO": "/echo/", |
| 530 |
"REQUEST_METHOD": "POST", |
| 531 |
"wsgi.input": payload, |
| 532 |
} |
| 533 |
self.assertEqual(self.client.request(**r).json(), {}) |
| 534 |
|
| 535 |
def test_empty_multipart_handled_gracefully(self): |
| 536 |
""" |
| 537 |
If passed an empty multipart message, MultiPartParser will return |
| 538 |
an empty QueryDict. |
| 539 |
""" |
| 540 |
r = { |
| 541 |
"CONTENT_LENGTH": 0, |
| 542 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 543 |
"PATH_INFO": "/echo/", |
| 544 |
"REQUEST_METHOD": "POST", |
| 545 |
"wsgi.input": client.FakePayload(b""), |
| 546 |
} |
| 547 |
self.assertEqual(self.client.request(**r).json(), {}) |
| 548 |
|
| 549 |
def test_custom_upload_handler(self): |
| 550 |
file = tempfile.NamedTemporaryFile |
| 551 |
with file() as smallfile, file() as bigfile: |
| 552 |
# A small file (under the 5M quota) |
| 553 |
smallfile.write(b"a" * (2**21)) |
| 554 |
smallfile.seek(0) |
| 555 |
|
| 556 |
# A big file (over the quota) |
| 557 |
bigfile.write(b"a" * (10 * 2**20)) |
| 558 |
bigfile.seek(0) |
| 559 |
|
| 560 |
# Small file posting should work. |
| 561 |
self.assertIn("f", self.client.post("/quota/", {"f": smallfile}).json()) |
| 562 |
|
| 563 |
# Large files don't go through. |
| 564 |
self.assertNotIn("f", self.client.post("/quota/", {"f": bigfile}).json()) |
| 565 |
|
| 566 |
def test_broken_custom_upload_handler(self): |
| 567 |
with tempfile.NamedTemporaryFile() as file: |
| 568 |
file.write(b"a" * (2**21)) |
| 569 |
file.seek(0) |
| 570 |
|
| 571 |
msg = ( |
| 572 |
"You cannot alter upload handlers after the upload has been processed." |
| 573 |
) |
| 574 |
with self.assertRaisesMessage(AttributeError, msg): |
| 575 |
self.client.post("/quota/broken/", {"f": file}) |
| 576 |
|
| 577 |
def test_stop_upload_temporary_file_handler(self): |
| 578 |
with tempfile.NamedTemporaryFile() as temp_file: |
| 579 |
temp_file.write(b"a") |
| 580 |
temp_file.seek(0) |
| 581 |
response = self.client.post("/temp_file/stop_upload/", {"file": temp_file}) |
| 582 |
temp_path = response.json()["temp_path"] |
| 583 |
self.assertIs(os.path.exists(temp_path), False) |
| 584 |
|
| 585 |
def test_upload_interrupted_temporary_file_handler(self): |
| 586 |
# Simulate an interrupted upload by omitting the closing boundary. |
| 587 |
class MockedParser(Parser): |
| 588 |
def __iter__(self): |
| 589 |
for item in super().__iter__(): |
| 590 |
item_type, meta_data, field_stream = item |
| 591 |
yield item_type, meta_data, field_stream |
| 592 |
if item_type == FILE: |
| 593 |
return |
| 594 |
|
| 595 |
with tempfile.NamedTemporaryFile() as temp_file: |
| 596 |
temp_file.write(b"a") |
| 597 |
temp_file.seek(0) |
| 598 |
with mock.patch( |
| 599 |
"django.http.multipartparser.Parser", |
| 600 |
MockedParser, |
| 601 |
): |
| 602 |
response = self.client.post( |
| 603 |
"/temp_file/upload_interrupted/", |
| 604 |
{"file": temp_file}, |
| 605 |
) |
| 606 |
temp_path = response.json()["temp_path"] |
| 607 |
self.assertIs(os.path.exists(temp_path), False) |
| 608 |
|
| 609 |
def test_upload_large_header_fields(self): |
| 610 |
payload = client.FakePayload( |
| 611 |
"\r\n".join( |
| 612 |
[ |
| 613 |
"--" + client.BOUNDARY, |
| 614 |
'Content-Disposition: form-data; name="my_file"; ' |
| 615 |
'filename="test.txt"', |
| 616 |
"Content-Type: text/plain", |
| 617 |
"X-Long-Header: %s" % ("-" * 500), |
| 618 |
"", |
| 619 |
"file contents", |
| 620 |
"--" + client.BOUNDARY + "--\r\n", |
| 621 |
] |
| 622 |
), |
| 623 |
) |
| 624 |
r = { |
| 625 |
"CONTENT_LENGTH": len(payload), |
| 626 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 627 |
"PATH_INFO": "/echo_content/", |
| 628 |
"REQUEST_METHOD": "POST", |
| 629 |
"wsgi.input": payload, |
| 630 |
} |
| 631 |
response = self.client.request(**r) |
| 632 |
self.assertEqual(response.status_code, 200) |
| 633 |
self.assertEqual(response.json(), {"my_file": "file contents"}) |
| 634 |
|
| 635 |
def test_upload_header_fields_too_large(self): |
| 636 |
payload = client.FakePayload( |
| 637 |
"\r\n".join( |
| 638 |
[ |
| 639 |
"--" + client.BOUNDARY, |
| 640 |
'Content-Disposition: form-data; name="my_file"; ' |
| 641 |
'filename="test.txt"', |
| 642 |
"Content-Type: text/plain", |
| 643 |
"X-Long-Header: %s" % ("-" * (MAX_TOTAL_HEADER_SIZE + 1)), |
| 644 |
"", |
| 645 |
"file contents", |
| 646 |
"--" + client.BOUNDARY + "--\r\n", |
| 647 |
] |
| 648 |
), |
| 649 |
) |
| 650 |
r = { |
| 651 |
"CONTENT_LENGTH": len(payload), |
| 652 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 653 |
"PATH_INFO": "/echo_content/", |
| 654 |
"REQUEST_METHOD": "POST", |
| 655 |
"wsgi.input": payload, |
| 656 |
} |
| 657 |
response = self.client.request(**r) |
| 658 |
self.assertEqual(response.status_code, 400) |
| 659 |
|
| 660 |
def test_fileupload_getlist(self): |
| 661 |
file = tempfile.NamedTemporaryFile |
| 662 |
with file() as file1, file() as file2, file() as file2a: |
| 663 |
file1.write(b"a" * (2**23)) |
| 664 |
file1.seek(0) |
| 665 |
|
| 666 |
file2.write(b"a" * (2 * 2**18)) |
| 667 |
file2.seek(0) |
| 668 |
|
| 669 |
file2a.write(b"a" * (5 * 2**20)) |
| 670 |
file2a.seek(0) |
| 671 |
|
| 672 |
response = self.client.post( |
| 673 |
"/getlist_count/", |
| 674 |
{ |
| 675 |
"file1": file1, |
| 676 |
"field1": "test", |
| 677 |
"field2": "test3", |
| 678 |
"field3": "test5", |
| 679 |
"field4": "test6", |
| 680 |
"field5": "test7", |
| 681 |
"file2": (file2, file2a), |
| 682 |
}, |
| 683 |
) |
| 684 |
got = response.json() |
| 685 |
self.assertEqual(got.get("file1"), 1) |
| 686 |
self.assertEqual(got.get("file2"), 2) |
| 687 |
|
| 688 |
def test_fileuploads_closed_at_request_end(self): |
| 689 |
file = tempfile.NamedTemporaryFile |
| 690 |
with file() as f1, file() as f2a, file() as f2b: |
| 691 |
response = self.client.post( |
| 692 |
"/fd_closing/t/", |
| 693 |
{ |
| 694 |
"file": f1, |
| 695 |
"file2": (f2a, f2b), |
| 696 |
}, |
| 697 |
) |
| 698 |
|
| 699 |
request = response.wsgi_request |
| 700 |
# The files were parsed. |
| 701 |
self.assertTrue(hasattr(request, "_files")) |
| 702 |
|
| 703 |
file = request._files["file"] |
| 704 |
self.assertTrue(file.closed) |
| 705 |
|
| 706 |
files = request._files.getlist("file2") |
| 707 |
self.assertTrue(files[0].closed) |
| 708 |
self.assertTrue(files[1].closed) |
| 709 |
|
| 710 |
def test_no_parsing_triggered_by_fd_closing(self): |
| 711 |
file = tempfile.NamedTemporaryFile |
| 712 |
with file() as f1, file() as f2a, file() as f2b: |
| 713 |
response = self.client.post( |
| 714 |
"/fd_closing/f/", |
| 715 |
{ |
| 716 |
"file": f1, |
| 717 |
"file2": (f2a, f2b), |
| 718 |
}, |
| 719 |
) |
| 720 |
|
| 721 |
request = response.wsgi_request |
| 722 |
# The fd closing logic doesn't trigger parsing of the stream |
| 723 |
self.assertFalse(hasattr(request, "_files")) |
| 724 |
|
| 725 |
def test_file_error_blocking(self): |
| 726 |
""" |
| 727 |
The server should not block when there are upload errors (bug #8622). |
| 728 |
This can happen if something -- i.e. an exception handler -- tries to |
| 729 |
access POST while handling an error in parsing POST. This shouldn't |
| 730 |
cause an infinite loop! |
| 731 |
""" |
| 732 |
|
| 733 |
class POSTAccessingHandler(client.ClientHandler): |
| 734 |
"""A handler that'll access POST during an exception.""" |
| 735 |
|
| 736 |
def handle_uncaught_exception(self, request, resolver, exc_info): |
| 737 |
ret = super().handle_uncaught_exception(request, resolver, exc_info) |
| 738 |
request.POST # evaluate |
| 739 |
return ret |
| 740 |
|
| 741 |
# Maybe this is a little more complicated that it needs to be; but if |
| 742 |
# the django.test.client.FakePayload.read() implementation changes then |
| 743 |
# this test would fail. So we need to know exactly what kind of error |
| 744 |
# it raises when there is an attempt to read more than the available bytes: |
| 745 |
try: |
| 746 |
client.FakePayload(b"a").read(2) |
| 747 |
except Exception as err: |
| 748 |
reference_error = err |
| 749 |
|
| 750 |
# install the custom handler that tries to access request.POST |
| 751 |
self.client.handler = POSTAccessingHandler() |
| 752 |
|
| 753 |
with open(__file__, "rb") as fp: |
| 754 |
post_data = { |
| 755 |
"name": "Ringo", |
| 756 |
"file_field": fp, |
| 757 |
} |
| 758 |
try: |
| 759 |
self.client.post("/upload_errors/", post_data) |
| 760 |
except reference_error.__class__ as err: |
| 761 |
self.assertNotEqual( |
| 762 |
str(err), |
| 763 |
str(reference_error), |
| 764 |
"Caught a repeated exception that'll cause an infinite loop in " |
| 765 |
"file uploads.", |
| 766 |
) |
| 767 |
except Exception as err: |
| 768 |
# CustomUploadError is the error that should have been raised |
| 769 |
self.assertEqual(err.__class__, uploadhandler.CustomUploadError) |
| 770 |
|
| 771 |
def test_filename_case_preservation(self): |
| 772 |
""" |
| 773 |
The storage backend shouldn't mess with the case of the filenames |
| 774 |
uploaded. |
| 775 |
""" |
| 776 |
# Synthesize the contents of a file upload with a mixed case filename |
| 777 |
# so we don't have to carry such a file in the Django tests source code |
| 778 |
# tree. |
| 779 |
vars = {"boundary": "oUrBoUnDaRyStRiNg"} |
| 780 |
post_data = [ |
| 781 |
"--%(boundary)s", |
| 782 |
'Content-Disposition: form-data; name="file_field"; ' |
| 783 |
'filename="MiXeD_cAsE.txt"', |
| 784 |
"Content-Type: application/octet-stream", |
| 785 |
"", |
| 786 |
"file contents\n", |
| 787 |
"--%(boundary)s--\r\n", |
| 788 |
] |
| 789 |
response = self.client.post( |
| 790 |
"/filename_case/", |
| 791 |
"\r\n".join(post_data) % vars, |
| 792 |
"multipart/form-data; boundary=%(boundary)s" % vars, |
| 793 |
) |
| 794 |
self.assertEqual(response.status_code, 200) |
| 795 |
id = int(response.content) |
| 796 |
obj = FileModel.objects.get(pk=id) |
| 797 |
# The name of the file uploaded and the file stored in the server-side |
| 798 |
# shouldn't differ. |
| 799 |
self.assertEqual(os.path.basename(obj.testfile.path), "MiXeD_cAsE.txt") |
| 800 |
|
| 801 |
def test_filename_traversal_upload(self): |
| 802 |
os.makedirs(UPLOAD_TO, exist_ok=True) |
| 803 |
tests = [ |
| 804 |
"../test.txt", |
| 805 |
"../test.txt", |
| 806 |
] |
| 807 |
for file_name in tests: |
| 808 |
with self.subTest(file_name=file_name): |
| 809 |
payload = client.FakePayload() |
| 810 |
payload.write( |
| 811 |
"\r\n".join( |
| 812 |
[ |
| 813 |
"--" + client.BOUNDARY, |
| 814 |
'Content-Disposition: form-data; name="my_file"; ' |
| 815 |
'filename="%s";' % file_name, |
| 816 |
"Content-Type: text/plain", |
| 817 |
"", |
| 818 |
"file contents.\r\n", |
| 819 |
"\r\n--" + client.BOUNDARY + "--\r\n", |
| 820 |
] |
| 821 |
), |
| 822 |
) |
| 823 |
r = { |
| 824 |
"CONTENT_LENGTH": len(payload), |
| 825 |
"CONTENT_TYPE": client.MULTIPART_CONTENT, |
| 826 |
"PATH_INFO": "/upload_traversal/", |
| 827 |
"REQUEST_METHOD": "POST", |
| 828 |
"wsgi.input": payload, |
| 829 |
} |
| 830 |
response = self.client.request(**r) |
| 831 |
result = response.json() |
| 832 |
self.assertEqual(response.status_code, 200) |
| 833 |
self.assertEqual(result["file_name"], "test.txt") |
| 834 |
self.assertIs( |
| 835 |
os.path.exists(os.path.join(MEDIA_ROOT, "test.txt")), |
| 836 |
False, |
| 837 |
) |
| 838 |
self.assertIs( |
| 839 |
os.path.exists(os.path.join(UPLOAD_TO, "test.txt")), |
| 840 |
True, |
| 841 |
) |
| 842 |
|
| 843 |
|
| 844 |
@override_settings(MEDIA_ROOT=MEDIA_ROOT) |
| 845 |
class DirectoryCreationTests(SimpleTestCase): |
| 846 |
""" |
| 847 |
Tests for error handling during directory creation |
| 848 |
via _save_FIELD_file (ticket #6450) |
| 849 |
""" |
| 850 |
|
| 851 |
@classmethod |
| 852 |
def setUpClass(cls): |
| 853 |
super().setUpClass() |
| 854 |
os.makedirs(MEDIA_ROOT, exist_ok=True) |
| 855 |
cls.addClassCleanup(shutil.rmtree, MEDIA_ROOT) |
| 856 |
|
| 857 |
def setUp(self): |
| 858 |
self.obj = FileModel() |
| 859 |
|
| 860 |
@unittest.skipIf( |
| 861 |
sys.platform == "win32", "Python on Windows doesn't have working os.chmod()." |
| 862 |
) |
| 863 |
@override_settings( |
| 864 |
STORAGES={ |
| 865 |
DEFAULT_STORAGE_ALIAS: { |
| 866 |
"BACKEND": "django.core.files.storage.FileSystemStorage", |
| 867 |
} |
| 868 |
} |
| 869 |
) |
| 870 |
def test_readonly_root(self): |
| 871 |
"""Permission errors are not swallowed""" |
| 872 |
os.chmod(MEDIA_ROOT, 0o500) |
| 873 |
self.addCleanup(os.chmod, MEDIA_ROOT, 0o700) |
| 874 |
with self.assertRaises(PermissionError): |
| 875 |
self.obj.testfile.save( |
| 876 |
"foo.txt", SimpleUploadedFile("foo.txt", b"x"), save=False |
| 877 |
) |
| 878 |
|
| 879 |
def test_not_a_directory(self): |
| 880 |
default_storage.delete(UPLOAD_TO) |
| 881 |
# Create a file with the upload directory name |
| 882 |
with SimpleUploadedFile(UPLOAD_TO, b"x") as file: |
| 883 |
default_storage.save(UPLOAD_FOLDER, file) |
| 884 |
self.addCleanup(default_storage.delete, UPLOAD_TO) |
| 885 |
msg = "%s exists and is not a directory." % UPLOAD_TO |
| 886 |
with self.assertRaisesMessage(FileExistsError, msg): |
| 887 |
with SimpleUploadedFile("foo.txt", b"x") as file: |
| 888 |
self.obj.testfile.save("foo.txt", file, save=False) |
| 889 |
|
| 890 |
|
| 891 |
class MultiParserTests(SimpleTestCase): |
| 892 |
def test_empty_upload_handlers(self): |
| 893 |
# We're not actually parsing here; just checking if the parser properly |
| 894 |
# instantiates with empty upload handlers. |
| 895 |
MultiPartParser( |
| 896 |
{ |
| 897 |
"CONTENT_TYPE": "multipart/form-data; boundary=_foo", |
| 898 |
"CONTENT_LENGTH": "1", |
| 899 |
}, |
| 900 |
StringIO("x"), |
| 901 |
[], |
| 902 |
"utf-8", |
| 903 |
) |
| 904 |
|
| 905 |
def test_invalid_content_type(self): |
| 906 |
with self.assertRaisesMessage( |
| 907 |
MultiPartParserError, "Invalid Content-Type: text/plain" |
| 908 |
): |
| 909 |
MultiPartParser( |
| 910 |
{ |
| 911 |
"CONTENT_TYPE": "text/plain", |
| 912 |
"CONTENT_LENGTH": "1", |
| 913 |
}, |
| 914 |
StringIO("x"), |
| 915 |
[], |
| 916 |
"utf-8", |
| 917 |
) |
| 918 |
|
| 919 |
def test_negative_content_length(self): |
| 920 |
with self.assertRaisesMessage( |
| 921 |
MultiPartParserError, "Invalid content length: -1" |
| 922 |
): |
| 923 |
MultiPartParser( |
| 924 |
{ |
| 925 |
"CONTENT_TYPE": "multipart/form-data; boundary=_foo", |
| 926 |
"CONTENT_LENGTH": -1, |
| 927 |
}, |
| 928 |
StringIO("x"), |
| 929 |
[], |
| 930 |
"utf-8", |
| 931 |
) |
| 932 |
|
| 933 |
def test_bad_type_content_length(self): |
| 934 |
multipart_parser = MultiPartParser( |
| 935 |
{ |
| 936 |
"CONTENT_TYPE": "multipart/form-data; boundary=_foo", |
| 937 |
"CONTENT_LENGTH": "a", |
| 938 |
}, |
| 939 |
StringIO("x"), |
| 940 |
[], |
| 941 |
"utf-8", |
| 942 |
) |
| 943 |
self.assertEqual(multipart_parser._content_length, 0) |
| 944 |
|
| 945 |
def test_sanitize_file_name(self): |
| 946 |
parser = MultiPartParser( |
| 947 |
{ |
| 948 |
"CONTENT_TYPE": "multipart/form-data; boundary=_foo", |
| 949 |
"CONTENT_LENGTH": "1", |
| 950 |
}, |
| 951 |
StringIO("x"), |
| 952 |
[], |
| 953 |
"utf-8", |
| 954 |
) |
| 955 |
for file_name in CANDIDATE_TRAVERSAL_FILE_NAMES: |
| 956 |
with self.subTest(file_name=file_name): |
| 957 |
self.assertEqual(parser.sanitize_file_name(file_name), "hax0rd.txt") |
| 958 |
|
| 959 |
def test_sanitize_invalid_file_name(self): |
| 960 |
parser = MultiPartParser( |
| 961 |
{ |
| 962 |
"CONTENT_TYPE": "multipart/form-data; boundary=_foo", |
| 963 |
"CONTENT_LENGTH": "1", |
| 964 |
}, |
| 965 |
StringIO("x"), |
| 966 |
[], |
| 967 |
"utf-8", |
| 968 |
) |
| 969 |
for file_name in CANDIDATE_INVALID_FILE_NAMES: |
| 970 |
with self.subTest(file_name=file_name): |
| 971 |
self.assertIsNone(parser.sanitize_file_name(file_name)) |