Debian Codegraph @

tests/requests_tests/tests.py

raw
1 import copy
2 from io import BytesIO
3 from itertools import chain
4 from unittest import mock
5 from urllib.parse import urlencode
6
7 from django.core.exceptions import BadRequest, DisallowedHost
8 from django.core.files.uploadedfile import InMemoryUploadedFile
9 from django.core.files.uploadhandler import MemoryFileUploadHandler
10 from django.core.handlers.wsgi import LimitedStream, WSGIRequest
11 from django.http import (
12 HttpHeaders,
13 HttpRequest,
14 RawPostDataException,
15 UnreadablePostError,
16 )
17 from django.http.multipartparser import (
18 MAX_TOTAL_HEADER_SIZE,
19 LazyStream,
20 MultiPartParserError,
21 )
22 from django.http.request import split_domain_port
23 from django.test import RequestFactory, SimpleTestCase, override_settings
24 from django.test.client import BOUNDARY, MULTIPART_CONTENT, FakePayload
25
26
27 class ErrorFileUploadHandler(MemoryFileUploadHandler):
28 def handle_raw_input(
29 self, input_data, META, content_length, boundary, encoding=None
30 ):
31 raise ValueError
32
33
34 class CustomFileUploadHandler(MemoryFileUploadHandler):
35 def handle_raw_input(
36 self, input_data, META, content_length, boundary, encoding=None
37 ):
38 return ("_POST", "_FILES")
39
40
41 class RequestsTests(SimpleTestCase):
42 def test_httprequest(self):
43 request = HttpRequest()
44 self.assertEqual(list(request.GET), [])
45 self.assertEqual(list(request.POST), [])
46 self.assertEqual(list(request.COOKIES), [])
47 self.assertEqual(list(request.META), [])
48
49 # .GET and .POST should be QueryDicts
50 self.assertEqual(request.GET.urlencode(), "")
51 self.assertEqual(request.POST.urlencode(), "")
52
53 # and FILES should be MultiValueDict
54 self.assertEqual(request.FILES.getlist("foo"), [])
55
56 self.assertIsNone(request.content_type)
57 self.assertIsNone(request.content_params)
58
59 def test_httprequest_full_path(self):
60 request = HttpRequest()
61 request.path = "/;some/?awful/=path/foo:bar/"
62 request.path_info = "/prefix" + request.path
63 request.META["QUERY_STRING"] = ";some=query&+query=string"
64 expected = "/%3Bsome/%3Fawful/%3Dpath/foo:bar/?;some=query&+query=string"
65 self.assertEqual(request.get_full_path(), expected)
66 self.assertEqual(request.get_full_path_info(), "/prefix" + expected)
67
68 def test_httprequest_full_path_with_query_string_and_fragment(self):
69 request = HttpRequest()
70 request.path = "/foo#bar"
71 request.path_info = "/prefix" + request.path
72 request.META["QUERY_STRING"] = "baz#quux"
73 self.assertEqual(request.get_full_path(), "/foo%23bar?baz#quux")
74 self.assertEqual(request.get_full_path_info(), "/prefix/foo%23bar?baz#quux")
75
76 def test_httprequest_repr(self):
77 request = HttpRequest()
78 request.path = "/somepath/"
79 request.method = "GET"
80 request.GET = {"get-key": "get-value"}
81 request.POST = {"post-key": "post-value"}
82 request.COOKIES = {"post-key": "post-value"}
83 request.META = {"post-key": "post-value"}
84 self.assertEqual(repr(request), "<HttpRequest: GET '/somepath/'>")
85
86 def test_httprequest_repr_invalid_method_and_path(self):
87 request = HttpRequest()
88 self.assertEqual(repr(request), "<HttpRequest>")
89 request = HttpRequest()
90 request.method = "GET"
91 self.assertEqual(repr(request), "<HttpRequest>")
92 request = HttpRequest()
93 request.path = ""
94 self.assertEqual(repr(request), "<HttpRequest>")
95
96 def test_wsgirequest(self):
97 request = WSGIRequest(
98 {
99 "PATH_INFO": "bogus",
100 "REQUEST_METHOD": "bogus",
101 "CONTENT_TYPE": "text/html; charset=utf8",
102 "wsgi.input": BytesIO(b""),
103 }
104 )
105 self.assertEqual(list(request.GET), [])
106 self.assertEqual(list(request.POST), [])
107 self.assertEqual(list(request.COOKIES), [])
108 self.assertEqual(
109 set(request.META),
110 {
111 "PATH_INFO",
112 "REQUEST_METHOD",
113 "SCRIPT_NAME",
114 "CONTENT_TYPE",
115 "wsgi.input",
116 },
117 )
118 self.assertEqual(request.META["PATH_INFO"], "bogus")
119 self.assertEqual(request.META["REQUEST_METHOD"], "bogus")
120 self.assertEqual(request.META["SCRIPT_NAME"], "")
121 self.assertEqual(request.content_type, "text/html")
122 self.assertEqual(request.content_params, {"charset": "utf8"})
123
124 def test_wsgirequest_with_script_name(self):
125 """
126 The request's path is correctly assembled, regardless of whether or
127 not the SCRIPT_NAME has a trailing slash (#20169).
128 """
129 # With trailing slash
130 request = WSGIRequest(
131 {
132 "PATH_INFO": "/somepath/",
133 "SCRIPT_NAME": "/PREFIX/",
134 "REQUEST_METHOD": "get",
135 "wsgi.input": BytesIO(b""),
136 }
137 )
138 self.assertEqual(request.path, "/PREFIX/somepath/")
139 # Without trailing slash
140 request = WSGIRequest(
141 {
142 "PATH_INFO": "/somepath/",
143 "SCRIPT_NAME": "/PREFIX",
144 "REQUEST_METHOD": "get",
145 "wsgi.input": BytesIO(b""),
146 }
147 )
148 self.assertEqual(request.path, "/PREFIX/somepath/")
149
150 def test_wsgirequest_script_url_double_slashes(self):
151 """
152 WSGI squashes multiple successive slashes in PATH_INFO, WSGIRequest
153 should take that into account when populating request.path and
154 request.META['SCRIPT_NAME'] (#17133).
155 """
156 request = WSGIRequest(
157 {
158 "SCRIPT_URL": "/mst/milestones//accounts/login//help",
159 "PATH_INFO": "/milestones/accounts/login/help",
160 "REQUEST_METHOD": "get",
161 "wsgi.input": BytesIO(b""),
162 }
163 )
164 self.assertEqual(request.path, "/mst/milestones/accounts/login/help")
165 self.assertEqual(request.META["SCRIPT_NAME"], "/mst")
166
167 def test_wsgirequest_with_force_script_name(self):
168 """
169 The FORCE_SCRIPT_NAME setting takes precedence over the request's
170 SCRIPT_NAME environment parameter (#20169).
171 """
172 with override_settings(FORCE_SCRIPT_NAME="/FORCED_PREFIX/"):
173 request = WSGIRequest(
174 {
175 "PATH_INFO": "/somepath/",
176 "SCRIPT_NAME": "/PREFIX/",
177 "REQUEST_METHOD": "get",
178 "wsgi.input": BytesIO(b""),
179 }
180 )
181 self.assertEqual(request.path, "/FORCED_PREFIX/somepath/")
182
183 def test_wsgirequest_path_with_force_script_name_trailing_slash(self):
184 """
185 The request's path is correctly assembled, regardless of whether or not
186 the FORCE_SCRIPT_NAME setting has a trailing slash (#20169).
187 """
188 # With trailing slash
189 with override_settings(FORCE_SCRIPT_NAME="/FORCED_PREFIX/"):
190 request = WSGIRequest(
191 {
192 "PATH_INFO": "/somepath/",
193 "REQUEST_METHOD": "get",
194 "wsgi.input": BytesIO(b""),
195 }
196 )
197 self.assertEqual(request.path, "/FORCED_PREFIX/somepath/")
198 # Without trailing slash
199 with override_settings(FORCE_SCRIPT_NAME="/FORCED_PREFIX"):
200 request = WSGIRequest(
201 {
202 "PATH_INFO": "/somepath/",
203 "REQUEST_METHOD": "get",
204 "wsgi.input": BytesIO(b""),
205 }
206 )
207 self.assertEqual(request.path, "/FORCED_PREFIX/somepath/")
208
209 def test_wsgirequest_repr(self):
210 request = WSGIRequest({"REQUEST_METHOD": "get", "wsgi.input": BytesIO(b"")})
211 self.assertEqual(repr(request), "<WSGIRequest: GET '/'>")
212 request = WSGIRequest(
213 {
214 "PATH_INFO": "/somepath/",
215 "REQUEST_METHOD": "get",
216 "wsgi.input": BytesIO(b""),
217 }
218 )
219 request.GET = {"get-key": "get-value"}
220 request.POST = {"post-key": "post-value"}
221 request.COOKIES = {"post-key": "post-value"}
222 request.META = {"post-key": "post-value"}
223 self.assertEqual(repr(request), "<WSGIRequest: GET '/somepath/'>")
224
225 def test_wsgirequest_path_info(self):
226 def wsgi_str(path_info, encoding="utf-8"):
227 path_info = path_info.encode(
228 encoding
229 ) # Actual URL sent by the browser (bytestring)
230 path_info = path_info.decode(
231 "iso-8859-1"
232 ) # Value in the WSGI environ dict (native string)
233 return path_info
234
235 # Regression for #19468
236 request = WSGIRequest(
237 {
238 "PATH_INFO": wsgi_str("/سلام/"),
239 "REQUEST_METHOD": "get",
240 "wsgi.input": BytesIO(b""),
241 }
242 )
243 self.assertEqual(request.path, "/سلام/")
244
245 # The URL may be incorrectly encoded in a non-UTF-8 encoding (#26971)
246 request = WSGIRequest(
247 {
248 "PATH_INFO": wsgi_str("/café/", encoding="iso-8859-1"),
249 "REQUEST_METHOD": "get",
250 "wsgi.input": BytesIO(b""),
251 }
252 )
253 # Since it's impossible to decide the (wrong) encoding of the URL, it's
254 # left percent-encoded in the path.
255 self.assertEqual(request.path, "/caf%E9/")
256
257 def test_wsgirequest_copy(self):
258 request = WSGIRequest({"REQUEST_METHOD": "get", "wsgi.input": BytesIO(b"")})
259 request_copy = copy.copy(request)
260 self.assertIs(request_copy.environ, request.environ)
261
262 def test_limited_stream(self):
263 # Read all of a limited stream
264 stream = LimitedStream(BytesIO(b"test"), 2)
265 self.assertEqual(stream.read(), b"te")
266 # Reading again returns nothing.
267 self.assertEqual(stream.read(), b"")
268
269 # Read a number of characters greater than the stream has to offer
270 stream = LimitedStream(BytesIO(b"test"), 2)
271 self.assertEqual(stream.read(5), b"te")
272 # Reading again returns nothing.
273 self.assertEqual(stream.readline(5), b"")
274
275 # Read sequentially from a stream
276 stream = LimitedStream(BytesIO(b"12345678"), 8)
277 self.assertEqual(stream.read(5), b"12345")
278 self.assertEqual(stream.read(5), b"678")
279 # Reading again returns nothing.
280 self.assertEqual(stream.readline(5), b"")
281
282 # Read lines from a stream
283 stream = LimitedStream(BytesIO(b"1234\n5678\nabcd\nefgh\nijkl"), 24)
284 # Read a full line, unconditionally
285 self.assertEqual(stream.readline(), b"1234\n")
286 # Read a number of characters less than a line
287 self.assertEqual(stream.readline(2), b"56")
288 # Read the rest of the partial line
289 self.assertEqual(stream.readline(), b"78\n")
290 # Read a full line, with a character limit greater than the line length
291 self.assertEqual(stream.readline(6), b"abcd\n")
292 # Read the next line, deliberately terminated at the line end
293 self.assertEqual(stream.readline(4), b"efgh")
294 # Read the next line... just the line end
295 self.assertEqual(stream.readline(), b"\n")
296 # Read everything else.
297 self.assertEqual(stream.readline(), b"ijkl")
298
299 # Regression for #15018
300 # If a stream contains a newline, but the provided length
301 # is less than the number of provided characters, the newline
302 # doesn't reset the available character count
303 stream = LimitedStream(BytesIO(b"1234\nabcdef"), 9)
304 self.assertEqual(stream.readline(10), b"1234\n")
305 self.assertEqual(stream.readline(3), b"abc")
306 # Now expire the available characters
307 self.assertEqual(stream.readline(3), b"d")
308 # Reading again returns nothing.
309 self.assertEqual(stream.readline(2), b"")
310
311 # Same test, but with read, not readline.
312 stream = LimitedStream(BytesIO(b"1234\nabcdef"), 9)
313 self.assertEqual(stream.read(6), b"1234\na")
314 self.assertEqual(stream.read(2), b"bc")
315 self.assertEqual(stream.read(2), b"d")
316 self.assertEqual(stream.read(2), b"")
317 self.assertEqual(stream.read(), b"")
318
319 def test_stream_read(self):
320 payload = FakePayload("name=value")
321 request = WSGIRequest(
322 {
323 "REQUEST_METHOD": "POST",
324 "CONTENT_TYPE": "application/x-www-form-urlencoded",
325 "CONTENT_LENGTH": len(payload),
326 "wsgi.input": payload,
327 },
328 )
329 self.assertEqual(request.read(), b"name=value")
330
331 def test_stream_readline(self):
332 payload = FakePayload("name=value\nother=string")
333 request = WSGIRequest(
334 {
335 "REQUEST_METHOD": "POST",
336 "CONTENT_TYPE": "application/x-www-form-urlencoded",
337 "CONTENT_LENGTH": len(payload),
338 "wsgi.input": payload,
339 },
340 )
341 self.assertEqual(request.readline(), b"name=value\n")
342 self.assertEqual(request.readline(), b"other=string")
343
344 def test_read_after_value(self):
345 """
346 Reading from request is allowed after accessing request contents as
347 POST or body.
348 """
349 payload = FakePayload("name=value")
350 request = WSGIRequest(
351 {
352 "REQUEST_METHOD": "POST",
353 "CONTENT_TYPE": "application/x-www-form-urlencoded",
354 "CONTENT_LENGTH": len(payload),
355 "wsgi.input": payload,
356 }
357 )
358 self.assertEqual(request.POST, {"name": ["value"]})
359 self.assertEqual(request.body, b"name=value")
360 self.assertEqual(request.read(), b"name=value")
361
362 def test_value_after_read(self):
363 """
364 Construction of POST or body is not allowed after reading
365 from request.
366 """
367 payload = FakePayload("name=value")
368 request = WSGIRequest(
369 {
370 "REQUEST_METHOD": "POST",
371 "CONTENT_TYPE": "application/x-www-form-urlencoded",
372 "CONTENT_LENGTH": len(payload),
373 "wsgi.input": payload,
374 }
375 )
376 self.assertEqual(request.read(2), b"na")
377 with self.assertRaises(RawPostDataException):
378 request.body
379 self.assertEqual(request.POST, {})
380
381 def test_non_ascii_POST(self):
382 payload = FakePayload(urlencode({"key": "España"}))
383 request = WSGIRequest(
384 {
385 "REQUEST_METHOD": "POST",
386 "CONTENT_LENGTH": len(payload),
387 "CONTENT_TYPE": "application/x-www-form-urlencoded",
388 "wsgi.input": payload,
389 }
390 )
391 self.assertEqual(request.POST, {"key": ["España"]})
392
393 def test_non_utf8_charset_POST_bad_request(self):
394 payload = FakePayload(urlencode({"key": "España".encode("latin-1")}))
395 request = WSGIRequest(
396 {
397 "REQUEST_METHOD": "POST",
398 "CONTENT_LENGTH": len(payload),
399 "CONTENT_TYPE": "application/x-www-form-urlencoded; charset=iso-8859-1",
400 "wsgi.input": payload,
401 }
402 )
403 msg = (
404 "HTTP requests with the 'application/x-www-form-urlencoded' content type "
405 "must be UTF-8 encoded."
406 )
407 with self.assertRaisesMessage(BadRequest, msg):
408 request.POST
409 with self.assertRaisesMessage(BadRequest, msg):
410 request.FILES
411
412 def test_utf8_charset_POST(self):
413 for charset in ["utf-8", "UTF-8"]:
414 with self.subTest(charset=charset):
415 payload = FakePayload(urlencode({"key": "España"}))
416 request = WSGIRequest(
417 {
418 "REQUEST_METHOD": "POST",
419 "CONTENT_LENGTH": len(payload),
420 "CONTENT_TYPE": (
421 f"application/x-www-form-urlencoded; charset={charset}"
422 ),
423 "wsgi.input": payload,
424 }
425 )
426 self.assertEqual(request.POST, {"key": ["España"]})
427
428 def test_body_after_POST_multipart_form_data(self):
429 """
430 Reading body after parsing multipart/form-data is not allowed
431 """
432 # Because multipart is used for large amounts of data i.e. file uploads,
433 # we don't want the data held in memory twice, and we don't want to
434 # silence the error by setting body = '' either.
435 payload = FakePayload(
436 "\r\n".join(
437 [
438 "--boundary",
439 'Content-Disposition: form-data; name="name"',
440 "",
441 "value",
442 "--boundary--",
443 ]
444 )
445 )
446 request = WSGIRequest(
447 {
448 "REQUEST_METHOD": "POST",
449 "CONTENT_TYPE": "multipart/form-data; boundary=boundary",
450 "CONTENT_LENGTH": len(payload),
451 "wsgi.input": payload,
452 }
453 )
454 self.assertEqual(request.POST, {"name": ["value"]})
455 with self.assertRaises(RawPostDataException):
456 request.body
457
458 def test_body_after_POST_multipart_related(self):
459 """
460 Reading body after parsing multipart that isn't form-data is allowed
461 """
462 # Ticket #9054
463 # There are cases in which the multipart data is related instead of
464 # being a binary upload, in which case it should still be accessible
465 # via body.
466 payload_data = b"\r\n".join(
467 [
468 b"--boundary",
469 b'Content-ID: id; name="name"',
470 b"",
471 b"value",
472 b"--boundary--",
473 ]
474 )
475 payload = FakePayload(payload_data)
476 request = WSGIRequest(
477 {
478 "REQUEST_METHOD": "POST",
479 "CONTENT_TYPE": "multipart/related; boundary=boundary",
480 "CONTENT_LENGTH": len(payload),
481 "wsgi.input": payload,
482 }
483 )
484 self.assertEqual(request.POST, {})
485 self.assertEqual(request.body, payload_data)
486
487 def test_POST_multipart_with_content_length_zero(self):
488 """
489 Multipart POST requests with Content-Length >= 0 are valid and need to
490 be handled.
491 """
492 # According to RFC 9110 Section 8.6 every POST with Content-Length >= 0
493 # is a valid request, so ensure that we handle Content-Length == 0.
494 payload = FakePayload(
495 "\r\n".join(
496 [
497 "--boundary",
498 'Content-Disposition: form-data; name="name"',
499 "",
500 "value",
501 "--boundary--",
502 ]
503 )
504 )
505 request = WSGIRequest(
506 {
507 "REQUEST_METHOD": "POST",
508 "CONTENT_TYPE": "multipart/form-data; boundary=boundary",
509 "CONTENT_LENGTH": 0,
510 "wsgi.input": payload,
511 }
512 )
513 self.assertEqual(request.POST, {})
514
515 @override_settings(
516 FILE_UPLOAD_HANDLERS=["requests_tests.tests.ErrorFileUploadHandler"]
517 )
518 def test_POST_multipart_handler_error(self):
519 payload = FakePayload(
520 "\r\n".join(
521 [
522 f"--{BOUNDARY}",
523 'Content-Disposition: form-data; name="name"',
524 "",
525 "value",
526 f"--{BOUNDARY}--",
527 ]
528 )
529 )
530 request = WSGIRequest(
531 {
532 "REQUEST_METHOD": "POST",
533 "CONTENT_TYPE": MULTIPART_CONTENT,
534 "CONTENT_LENGTH": len(payload),
535 "wsgi.input": payload,
536 }
537 )
538 with self.assertRaises(ValueError):
539 request.POST
540
541 @override_settings(
542 FILE_UPLOAD_HANDLERS=["requests_tests.tests.CustomFileUploadHandler"]
543 )
544 def test_POST_multipart_handler_parses_input(self):
545 payload = FakePayload(
546 "\r\n".join(
547 [
548 f"--{BOUNDARY}",
549 'Content-Disposition: form-data; name="name"',
550 "",
551 "value",
552 f"--{BOUNDARY}--",
553 ]
554 )
555 )
556 request = WSGIRequest(
557 {
558 "REQUEST_METHOD": "POST",
559 "CONTENT_TYPE": MULTIPART_CONTENT,
560 "CONTENT_LENGTH": len(payload),
561 "wsgi.input": payload,
562 }
563 )
564 self.assertEqual(request.POST, "_POST")
565 self.assertEqual(request.FILES, "_FILES")
566
567 def test_request_methods_with_content(self):
568 for method in ["GET", "PUT", "DELETE"]:
569 with self.subTest(method=method):
570 payload = FakePayload(urlencode({"key": "value"}))
571 request = WSGIRequest(
572 {
573 "REQUEST_METHOD": method,
574 "CONTENT_LENGTH": len(payload),
575 "CONTENT_TYPE": "application/x-www-form-urlencoded",
576 "wsgi.input": payload,
577 }
578 )
579 self.assertEqual(request.POST, {})
580
581 def test_POST_content_type_json(self):
582 payload = FakePayload(
583 "\r\n".join(
584 [
585 '{"pk": 1, "model": "store.book", "fields": {"name": "Mostly Ha',
586 'rmless", "author": ["Douglas", Adams"]}}',
587 ]
588 )
589 )
590 request = WSGIRequest(
591 {
592 "REQUEST_METHOD": "POST",
593 "CONTENT_TYPE": "application/json",
594 "CONTENT_LENGTH": len(payload),
595 "wsgi.input": payload,
596 }
597 )
598 self.assertEqual(request.POST, {})
599 self.assertEqual(request.FILES, {})
600
601 _json_payload = [
602 'Content-Disposition: form-data; name="JSON"',
603 "Content-Type: application/json",
604 "",
605 '{"pk": 1, "model": "store.book", "fields": {"name": "Mostly Harmless", '
606 '"author": ["Douglas", Adams"]}}',
607 ]
608
609 def test_POST_form_data_json(self):
610 payload = FakePayload(
611 "\r\n".join([f"--{BOUNDARY}", *self._json_payload, f"--{BOUNDARY}--"])
612 )
613 request = WSGIRequest(
614 {
615 "REQUEST_METHOD": "POST",
616 "CONTENT_TYPE": MULTIPART_CONTENT,
617 "CONTENT_LENGTH": len(payload),
618 "wsgi.input": payload,
619 }
620 )
621 self.assertEqual(
622 request.POST,
623 {
624 "JSON": [
625 '{"pk": 1, "model": "store.book", "fields": {"name": "Mostly '
626 'Harmless", "author": ["Douglas", Adams"]}}'
627 ],
628 },
629 )
630
631 def test_POST_multipart_json(self):
632 payload = FakePayload(
633 "\r\n".join(
634 [
635 f"--{BOUNDARY}",
636 'Content-Disposition: form-data; name="name"',
637 "",
638 "value",
639 f"--{BOUNDARY}",
640 *self._json_payload,
641 f"--{BOUNDARY}--",
642 ]
643 )
644 )
645 request = WSGIRequest(
646 {
647 "REQUEST_METHOD": "POST",
648 "CONTENT_TYPE": MULTIPART_CONTENT,
649 "CONTENT_LENGTH": len(payload),
650 "wsgi.input": payload,
651 }
652 )
653 self.assertEqual(
654 request.POST,
655 {
656 "name": ["value"],
657 "JSON": [
658 '{"pk": 1, "model": "store.book", "fields": {"name": "Mostly '
659 'Harmless", "author": ["Douglas", Adams"]}}'
660 ],
661 },
662 )
663
664 def test_POST_multipart_json_csv(self):
665 payload = FakePayload(
666 "\r\n".join(
667 [
668 f"--{BOUNDARY}",
669 'Content-Disposition: form-data; name="name"',
670 "",
671 "value",
672 f"--{BOUNDARY}",
673 *self._json_payload,
674 f"--{BOUNDARY}",
675 'Content-Disposition: form-data; name="CSV"',
676 "Content-Type: text/csv",
677 "",
678 "Framework,ID.Django,1.Flask,2.",
679 f"--{BOUNDARY}--",
680 ]
681 )
682 )
683 request = WSGIRequest(
684 {
685 "REQUEST_METHOD": "POST",
686 "CONTENT_TYPE": MULTIPART_CONTENT,
687 "CONTENT_LENGTH": len(payload),
688 "wsgi.input": payload,
689 }
690 )
691 self.assertEqual(
692 request.POST,
693 {
694 "name": ["value"],
695 "JSON": [
696 '{"pk": 1, "model": "store.book", "fields": {"name": "Mostly '
697 'Harmless", "author": ["Douglas", Adams"]}}'
698 ],
699 "CSV": ["Framework,ID.Django,1.Flask,2."],
700 },
701 )
702
703 def test_POST_multipart_with_file(self):
704 payload = FakePayload(
705 "\r\n".join(
706 [
707 f"--{BOUNDARY}",
708 'Content-Disposition: form-data; name="name"',
709 "",
710 "value",
711 f"--{BOUNDARY}",
712 *self._json_payload,
713 f"--{BOUNDARY}",
714 'Content-Disposition: form-data; name="File"; filename="test.csv"',
715 "Content-Type: application/octet-stream",
716 "",
717 "Framework,ID",
718 "Django,1",
719 "Flask,2",
720 f"--{BOUNDARY}--",
721 ]
722 )
723 )
724 request = WSGIRequest(
725 {
726 "REQUEST_METHOD": "POST",
727 "CONTENT_TYPE": MULTIPART_CONTENT,
728 "CONTENT_LENGTH": len(payload),
729 "wsgi.input": payload,
730 }
731 )
732 self.assertEqual(
733 request.POST,
734 {
735 "name": ["value"],
736 "JSON": [
737 '{"pk": 1, "model": "store.book", "fields": {"name": "Mostly '
738 'Harmless", "author": ["Douglas", Adams"]}}'
739 ],
740 },
741 )
742 self.assertEqual(len(request.FILES), 1)
743 self.assertIsInstance((request.FILES["File"]), InMemoryUploadedFile)
744
745 def test_base64_invalid_encoding(self):
746 payload = FakePayload(
747 "\r\n".join(
748 [
749 f"--{BOUNDARY}",
750 'Content-Disposition: form-data; name="file"; filename="test.txt"',
751 "Content-Type: application/octet-stream",
752 "Content-Transfer-Encoding: base64",
753 "",
754 f"\r\nZsg£\r\n--{BOUNDARY}--",
755 ]
756 )
757 )
758 request = WSGIRequest(
759 {
760 "REQUEST_METHOD": "POST",
761 "CONTENT_TYPE": MULTIPART_CONTENT,
762 "CONTENT_LENGTH": len(payload),
763 "wsgi.input": payload,
764 }
765 )
766 msg = "Could not decode base64 data."
767 with self.assertRaisesMessage(MultiPartParserError, msg):
768 request.POST
769
770 def test_POST_binary_only(self):
771 payload = b"\r\n\x01\x00\x00\x00ab\x00\x00\xcd\xcc,@"
772 environ = {
773 "REQUEST_METHOD": "POST",
774 "CONTENT_TYPE": "application/octet-stream",
775 "CONTENT_LENGTH": len(payload),
776 "wsgi.input": BytesIO(payload),
777 }
778 request = WSGIRequest(environ)
779 self.assertEqual(request.POST, {})
780 self.assertEqual(request.FILES, {})
781 self.assertEqual(request.body, payload)
782
783 # Same test without specifying content-type
784 environ.update({"CONTENT_TYPE": "", "wsgi.input": BytesIO(payload)})
785 request = WSGIRequest(environ)
786 self.assertEqual(request.POST, {})
787 self.assertEqual(request.FILES, {})
788 self.assertEqual(request.body, payload)
789
790 def test_read_by_lines(self):
791 payload = FakePayload("name=value")
792 request = WSGIRequest(
793 {
794 "REQUEST_METHOD": "POST",
795 "CONTENT_TYPE": "application/x-www-form-urlencoded",
796 "CONTENT_LENGTH": len(payload),
797 "wsgi.input": payload,
798 }
799 )
800 self.assertEqual(list(request), [b"name=value"])
801
802 def test_POST_after_body_read(self):
803 """
804 POST should be populated even if body is read first
805 """
806 payload = FakePayload("name=value")
807 request = WSGIRequest(
808 {
809 "REQUEST_METHOD": "POST",
810 "CONTENT_TYPE": "application/x-www-form-urlencoded",
811 "CONTENT_LENGTH": len(payload),
812 "wsgi.input": payload,
813 }
814 )
815 request.body # evaluate
816 self.assertEqual(request.POST, {"name": ["value"]})
817
818 def test_POST_after_body_read_and_stream_read(self):
819 """
820 POST should be populated even if body is read first, and then
821 the stream is read second.
822 """
823 payload = FakePayload("name=value")
824 request = WSGIRequest(
825 {
826 "REQUEST_METHOD": "POST",
827 "CONTENT_TYPE": "application/x-www-form-urlencoded",
828 "CONTENT_LENGTH": len(payload),
829 "wsgi.input": payload,
830 }
831 )
832 request.body # evaluate
833 self.assertEqual(request.read(1), b"n")
834 self.assertEqual(request.POST, {"name": ["value"]})
835
836 def test_multipart_post_field_with_base64(self):
837 payload = FakePayload(
838 "\r\n".join(
839 [
840 f"--{BOUNDARY}",
841 'Content-Disposition: form-data; name="name"',
842 "Content-Transfer-Encoding: base64",
843 "",
844 "dmFsdWU=",
845 f"--{BOUNDARY}--",
846 "",
847 ]
848 )
849 )
850 request = WSGIRequest(
851 {
852 "REQUEST_METHOD": "POST",
853 "CONTENT_TYPE": MULTIPART_CONTENT,
854 "CONTENT_LENGTH": len(payload),
855 "wsgi.input": payload,
856 }
857 )
858 request.body # evaluate
859 self.assertEqual(request.POST, {"name": ["value"]})
860
861 def test_multipart_post_field_with_invalid_base64(self):
862 payload = FakePayload(
863 "\r\n".join(
864 [
865 f"--{BOUNDARY}",
866 'Content-Disposition: form-data; name="name"',
867 "Content-Transfer-Encoding: base64",
868 "",
869 "123",
870 f"--{BOUNDARY}--",
871 "",
872 ]
873 )
874 )
875 request = WSGIRequest(
876 {
877 "REQUEST_METHOD": "POST",
878 "CONTENT_TYPE": MULTIPART_CONTENT,
879 "CONTENT_LENGTH": len(payload),
880 "wsgi.input": payload,
881 }
882 )
883 request.body # evaluate
884 self.assertEqual(request.POST, {"name": ["123"]})
885
886 def test_multipart_file_upload_base64_whitespace_heavy(self):
887 # Fake a file upload with base64-encoded content including mostly
888 # whitespaces across chunk boundaries.
889 payload = FakePayload(
890 "\r\n".join(
891 [
892 f"--{BOUNDARY}",
893 'Content-Disposition: form-data; name="file"; filename="test.txt"',
894 "Content-Type: application/octet-stream",
895 "Content-Transfer-Encoding: base64",
896 "",
897 ]
898 )
899 )
900 # "AAAA" decodes to b"\x00\x00\x00". Whitespace (70000 bytes) spans the
901 # default 64KB chunk boundary, hence the alignment loop is exercised.
902 payload.write(b"\r\n" + b"AAA" + b" " * 70000 + b"A" + b"\r\n")
903 payload.write("--" + BOUNDARY + "--\r\n")
904 request = WSGIRequest(
905 {
906 "REQUEST_METHOD": "POST",
907 "CONTENT_TYPE": MULTIPART_CONTENT,
908 "CONTENT_LENGTH": len(payload),
909 "wsgi.input": payload,
910 }
911 )
912 reads = []
913 original_read = LazyStream.read
914
915 def counting_read(self_stream, size=None):
916 reads.append(size)
917 return original_read(self_stream, size)
918
919 with mock.patch.object(LazyStream, "read", counting_read):
920 files = request.FILES
921
922 self.assertEqual(len(files), 1)
923 self.assertEqual(files["file"].read(), b"\x00\x00\x00")
924
925 # The alignment loop must read in `chunk-sized` units rather than one
926 # byte at a time, otherwise each whitespace byte triggers a separate
927 # read() call with a costly internal unget() cycle.
928 # Parsing this payload should issue exactly 8 LazyStream.read() calls:
929 # 1. main_stream.read(1) -- BoundaryIter.__init__ probe, preamble
930 # 2. sub_stream.read(1024) -- parse_boundary_stream, preamble headers
931 # 3. main_stream.read(1) -- BoundaryIter.__init__ probe, file field
932 # 4. field_stream.read(1024) -- parse_boundary_stream, file headers
933 # 5. field_stream.read(65536)-- base64 alignment loop: one chunk-sized
934 # read to find the non-whitespace bytes
935 # needed to complete the 4-byte base64
936 # group that spans the chunk boundary
937 # 6. main_stream.read(1) -- BoundaryIter.__init__ probe, epilogue
938 # 7. sub_stream.read(1024) -- parse_boundary_stream, epilogue headers
939 # 8. main_stream.read(1) -- BoundaryIter.__init__ probe, exhausted
940 # stream; returns b"" and stops iteration
941 # A byte-at-a-time implementation of read() in step 5 would do instead
942 # one read(1) per whitespace byte past the chunk boundary (4488 calls).
943 self.assertEqual(reads, [1, 1024, 1, 1024, 65536, 1, 1024, 1])
944
945 def test_POST_after_body_read_and_stream_read_multipart(self):
946 """
947 POST should be populated even if body is read first, and then
948 the stream is read second. Using multipart/form-data instead of urlencoded.
949 """
950 payload = FakePayload(
951 "\r\n".join(
952 [
953 "--boundary",
954 'Content-Disposition: form-data; name="name"',
955 "",
956 "value",
957 "--boundary--" "",
958 ]
959 )
960 )
961 request = WSGIRequest(
962 {
963 "REQUEST_METHOD": "POST",
964 "CONTENT_TYPE": "multipart/form-data; boundary=boundary",
965 "CONTENT_LENGTH": len(payload),
966 "wsgi.input": payload,
967 }
968 )
969 request.body # evaluate
970 # Consume enough data to mess up the parsing:
971 self.assertEqual(request.read(13), b"--boundary\r\nC")
972 self.assertEqual(request.POST, {"name": ["value"]})
973
974 def test_POST_immutable_for_multipart(self):
975 """
976 MultiPartParser.parse() leaves request.POST immutable.
977 """
978 payload = FakePayload(
979 "\r\n".join(
980 [
981 "--boundary",
982 'Content-Disposition: form-data; name="name"',
983 "",
984 "value",
985 "--boundary--",
986 ]
987 )
988 )
989 request = WSGIRequest(
990 {
991 "REQUEST_METHOD": "POST",
992 "CONTENT_TYPE": "multipart/form-data; boundary=boundary",
993 "CONTENT_LENGTH": len(payload),
994 "wsgi.input": payload,
995 }
996 )
997 self.assertFalse(request.POST._mutable)
998
999 def test_multipart_without_boundary(self):
1000 request = WSGIRequest(
1001 {
1002 "REQUEST_METHOD": "POST",
1003 "CONTENT_TYPE": "multipart/form-data;",
1004 "CONTENT_LENGTH": 0,
1005 "wsgi.input": FakePayload(),
1006 }
1007 )
1008 with self.assertRaisesMessage(
1009 MultiPartParserError, "Invalid boundary in multipart: None"
1010 ):
1011 request.POST
1012
1013 def test_multipart_non_ascii_content_type(self):
1014 request = WSGIRequest(
1015 {
1016 "REQUEST_METHOD": "POST",
1017 "CONTENT_TYPE": "multipart/form-data; boundary = \xe0",
1018 "CONTENT_LENGTH": 0,
1019 "wsgi.input": FakePayload(),
1020 }
1021 )
1022 msg = (
1023 "Invalid non-ASCII Content-Type in multipart: multipart/form-data; "
1024 "boundary = à"
1025 )
1026 with self.assertRaisesMessage(MultiPartParserError, msg):
1027 request.POST
1028
1029 def test_multipart_with_header_fields_too_large(self):
1030 payload = FakePayload(
1031 "\r\n".join(
1032 [
1033 "--boundary",
1034 'Content-Disposition: form-data; name="name"',
1035 "X-Long-Header: %s" % ("-" * (MAX_TOTAL_HEADER_SIZE + 1)),
1036 "",
1037 "value",
1038 "--boundary--",
1039 ]
1040 )
1041 )
1042 request = WSGIRequest(
1043 {
1044 "REQUEST_METHOD": "POST",
1045 "CONTENT_TYPE": "multipart/form-data; boundary=boundary",
1046 "CONTENT_LENGTH": len(payload),
1047 "wsgi.input": payload,
1048 }
1049 )
1050 msg = "Request max total header size exceeded."
1051 with self.assertRaisesMessage(MultiPartParserError, msg):
1052 request.POST
1053
1054 def test_POST_connection_error(self):
1055 """
1056 If wsgi.input.read() raises an exception while trying to read() the
1057 POST, the exception is identifiable (not a generic OSError).
1058 """
1059
1060 class ExplodingBytesIO(BytesIO):
1061 def read(self, size=-1, /):
1062 raise OSError("kaboom!")
1063
1064 payload = b"name=value"
1065 request = WSGIRequest(
1066 {
1067 "REQUEST_METHOD": "POST",
1068 "CONTENT_TYPE": "application/x-www-form-urlencoded",
1069 "CONTENT_LENGTH": len(payload),
1070 "wsgi.input": ExplodingBytesIO(payload),
1071 }
1072 )
1073 with self.assertRaises(UnreadablePostError):
1074 request.body
1075
1076 def test_set_encoding_clears_POST(self):
1077 payload = FakePayload(
1078 "\r\n".join(
1079 [
1080 f"--{BOUNDARY}",
1081 'Content-Disposition: form-data; name="name"',
1082 "",
1083 "Hello Günter",
1084 f"--{BOUNDARY}--",
1085 "",
1086 ]
1087 )
1088 )
1089 request = WSGIRequest(
1090 {
1091 "REQUEST_METHOD": "POST",
1092 "CONTENT_TYPE": MULTIPART_CONTENT,
1093 "CONTENT_LENGTH": len(payload),
1094 "wsgi.input": payload,
1095 }
1096 )
1097 self.assertEqual(request.POST, {"name": ["Hello Günter"]})
1098 request.encoding = "iso-8859-16"
1099 # FIXME: POST should be accessible after changing the encoding
1100 # (refs #14035).
1101 # self.assertEqual(request.POST, {"name": ["Hello GĂŒnter"]})
1102
1103 def test_set_encoding_clears_GET(self):
1104 payload = FakePayload("")
1105 request = WSGIRequest(
1106 {
1107 "REQUEST_METHOD": "GET",
1108 "wsgi.input": payload,
1109 "QUERY_STRING": "name=Hello%20G%C3%BCnter",
1110 }
1111 )
1112 self.assertEqual(request.GET, {"name": ["Hello Günter"]})
1113 request.encoding = "iso-8859-16"
1114 self.assertEqual(request.GET, {"name": ["Hello G\u0102\u0152nter"]})
1115
1116 def test_FILES_connection_error(self):
1117 """
1118 If wsgi.input.read() raises an exception while trying to read() the
1119 FILES, the exception is identifiable (not a generic OSError).
1120 """
1121
1122 class ExplodingBytesIO(BytesIO):
1123 def read(self, size=-1, /):
1124 raise OSError("kaboom!")
1125
1126 payload = b"x"
1127 request = WSGIRequest(
1128 {
1129 "REQUEST_METHOD": "POST",
1130 "CONTENT_TYPE": "multipart/form-data; boundary=foo_",
1131 "CONTENT_LENGTH": len(payload),
1132 "wsgi.input": ExplodingBytesIO(payload),
1133 }
1134 )
1135 with self.assertRaises(UnreadablePostError):
1136 request.FILES
1137
1138 def test_copy(self):
1139 request = HttpRequest()
1140 request_copy = copy.copy(request)
1141 self.assertIs(request_copy.resolver_match, request.resolver_match)
1142
1143 def test_deepcopy(self):
1144 request = RequestFactory().get("/")
1145 request.session = {}
1146 request_copy = copy.deepcopy(request)
1147 request.session["key"] = "value"
1148 self.assertEqual(request_copy.session, {})
1149
1150
1151 class MemoryFileUploadHandlerTests(SimpleTestCase):
1152 def test_handle_raw_input_wsgi_request_within_limit_activated(self):
1153
1154 class WSGIRequest:
1155 def __init__(self, body):
1156 self._stream = LimitedStream(BytesIO(body), len(body))
1157
1158 handler = MemoryFileUploadHandler()
1159 with self.settings(FILE_UPLOAD_MAX_MEMORY_SIZE=10):
1160 handler.handle_raw_input(WSGIRequest(b"x" * 5), {}, 5, None)
1161 self.assertIs(handler.activated, True)
1162
1163 def test_handle_raw_input_wsgi_request_exceeds_limit_deactivated(self):
1164
1165 class WSGIRequest:
1166 def __init__(self, body):
1167 self._stream = LimitedStream(BytesIO(body), len(body))
1168
1169 handler = MemoryFileUploadHandler()
1170 with self.settings(FILE_UPLOAD_MAX_MEMORY_SIZE=10):
1171 handler.handle_raw_input(WSGIRequest(b"x" * 15), {}, 15, None)
1172 self.assertIs(handler.activated, False)
1173
1174 def test_handle_raw_input_seekable_within_limit_activated(self):
1175 handler = MemoryFileUploadHandler()
1176 with self.settings(FILE_UPLOAD_MAX_MEMORY_SIZE=10):
1177 # content_length param is understated (0) but actual size is 10.
1178 handler.handle_raw_input(BytesIO(b"x" * 10), {}, 0, None)
1179 self.assertIs(handler.activated, True)
1180
1181 def test_handle_raw_input_seekable_exceeds_limit_deactivated(self):
1182 handler = MemoryFileUploadHandler()
1183 with self.settings(FILE_UPLOAD_MAX_MEMORY_SIZE=10):
1184 # content_length param is understated (0) but actual size is 15.
1185 handler.handle_raw_input(BytesIO(b"x" * 15), {}, 0, None)
1186 self.assertIs(handler.activated, False)
1187
1188
1189 class HostValidationTests(SimpleTestCase):
1190 poisoned_hosts = [
1191 "[email protected]",
1192 "example.com:[email protected]",
1193 "example.com:[email protected]:80",
1194 "example.com:80/badpath",
1195 "example.com: recovermypassword.com",
1196 ]
1197
1198 @override_settings(
1199 USE_X_FORWARDED_HOST=False,
1200 ALLOWED_HOSTS=[
1201 "forward.com",
1202 "example.com",
1203 "internal.com",
1204 "12.34.56.78",
1205 "[2001:19f0:feee::dead:beef:cafe]",
1206 "xn--4ca9at.com",
1207 ".multitenant.com",
1208 "INSENSITIVE.com",
1209 "[::ffff:169.254.169.254]",
1210 ],
1211 )
1212 def test_http_get_host(self):
1213 # Check if X_FORWARDED_HOST is provided.
1214 request = HttpRequest()
1215 request.META = {
1216 "HTTP_X_FORWARDED_HOST": "forward.com",
1217 "HTTP_HOST": "example.com",
1218 "SERVER_NAME": "internal.com",
1219 "SERVER_PORT": 80,
1220 }
1221 # X_FORWARDED_HOST is ignored.
1222 self.assertEqual(request.get_host(), "example.com")
1223
1224 # Check if X_FORWARDED_HOST isn't provided.
1225 request = HttpRequest()
1226 request.META = {
1227 "HTTP_HOST": "example.com",
1228 "SERVER_NAME": "internal.com",
1229 "SERVER_PORT": 80,
1230 }
1231 self.assertEqual(request.get_host(), "example.com")
1232
1233 # Check if HTTP_HOST isn't provided.
1234 request = HttpRequest()
1235 request.META = {
1236 "SERVER_NAME": "internal.com",
1237 "SERVER_PORT": 80,
1238 }
1239 self.assertEqual(request.get_host(), "internal.com")
1240
1241 # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
1242 request = HttpRequest()
1243 request.META = {
1244 "SERVER_NAME": "internal.com",
1245 "SERVER_PORT": 8042,
1246 }
1247 self.assertEqual(request.get_host(), "internal.com:8042")
1248
1249 legit_hosts = [
1250 "example.com",
1251 "example.com:80",
1252 "12.34.56.78",
1253 "12.34.56.78:443",
1254 "[2001:19f0:feee::dead:beef:cafe]",
1255 "[2001:19f0:feee::dead:beef:cafe]:8080",
1256 "xn--4ca9at.com", # Punycode for öäü.com
1257 "anything.multitenant.com",
1258 "multitenant.com",
1259 "insensitive.com",
1260 "example.com.",
1261 "example.com.:80",
1262 "[::ffff:169.254.169.254]",
1263 ]
1264
1265 for host in legit_hosts:
1266 request = HttpRequest()
1267 request.META = {
1268 "HTTP_HOST": host,
1269 }
1270 request.get_host()
1271
1272 # Poisoned host headers are rejected as suspicious
1273 for host in chain(self.poisoned_hosts, ["other.com", "example.com.."]):
1274 with self.assertRaises(DisallowedHost):
1275 request = HttpRequest()
1276 request.META = {
1277 "HTTP_HOST": host,
1278 }
1279 request.get_host()
1280
1281 @override_settings(USE_X_FORWARDED_HOST=True, ALLOWED_HOSTS=["*"])
1282 def test_http_get_host_with_x_forwarded_host(self):
1283 # Check if X_FORWARDED_HOST is provided.
1284 request = HttpRequest()
1285 request.META = {
1286 "HTTP_X_FORWARDED_HOST": "forward.com",
1287 "HTTP_HOST": "example.com",
1288 "SERVER_NAME": "internal.com",
1289 "SERVER_PORT": 80,
1290 }
1291 # X_FORWARDED_HOST is obeyed.
1292 self.assertEqual(request.get_host(), "forward.com")
1293
1294 # Check if X_FORWARDED_HOST isn't provided.
1295 request = HttpRequest()
1296 request.META = {
1297 "HTTP_HOST": "example.com",
1298 "SERVER_NAME": "internal.com",
1299 "SERVER_PORT": 80,
1300 }
1301 self.assertEqual(request.get_host(), "example.com")
1302
1303 # Check if HTTP_HOST isn't provided.
1304 request = HttpRequest()
1305 request.META = {
1306 "SERVER_NAME": "internal.com",
1307 "SERVER_PORT": 80,
1308 }
1309 self.assertEqual(request.get_host(), "internal.com")
1310
1311 # Check if HTTP_HOST isn't provided, and we're on a nonstandard port
1312 request = HttpRequest()
1313 request.META = {
1314 "SERVER_NAME": "internal.com",
1315 "SERVER_PORT": 8042,
1316 }
1317 self.assertEqual(request.get_host(), "internal.com:8042")
1318
1319 # Poisoned host headers are rejected as suspicious
1320 legit_hosts = [
1321 "example.com",
1322 "example.com:80",
1323 "12.34.56.78",
1324 "12.34.56.78:443",
1325 "[2001:19f0:feee::dead:beef:cafe]",
1326 "[2001:19f0:feee::dead:beef:cafe]:8080",
1327 "xn--4ca9at.com", # Punycode for öäü.com
1328 ]
1329
1330 for host in legit_hosts:
1331 request = HttpRequest()
1332 request.META = {
1333 "HTTP_HOST": host,
1334 }
1335 request.get_host()
1336
1337 for host in self.poisoned_hosts:
1338 with self.assertRaises(DisallowedHost):
1339 request = HttpRequest()
1340 request.META = {
1341 "HTTP_HOST": host,
1342 }
1343 request.get_host()
1344
1345 @override_settings(USE_X_FORWARDED_PORT=False)
1346 def test_get_port(self):
1347 request = HttpRequest()
1348 request.META = {
1349 "SERVER_PORT": "8080",
1350 "HTTP_X_FORWARDED_PORT": "80",
1351 }
1352 # Shouldn't use the X-Forwarded-Port header
1353 self.assertEqual(request.get_port(), "8080")
1354
1355 request = HttpRequest()
1356 request.META = {
1357 "SERVER_PORT": "8080",
1358 }
1359 self.assertEqual(request.get_port(), "8080")
1360
1361 @override_settings(USE_X_FORWARDED_PORT=True)
1362 def test_get_port_with_x_forwarded_port(self):
1363 request = HttpRequest()
1364 request.META = {
1365 "SERVER_PORT": "8080",
1366 "HTTP_X_FORWARDED_PORT": "80",
1367 }
1368 # Should use the X-Forwarded-Port header
1369 self.assertEqual(request.get_port(), "80")
1370
1371 request = HttpRequest()
1372 request.META = {
1373 "SERVER_PORT": "8080",
1374 }
1375 self.assertEqual(request.get_port(), "8080")
1376
1377 @override_settings(DEBUG=True, ALLOWED_HOSTS=[])
1378 def test_host_validation_in_debug_mode(self):
1379 """
1380 If ALLOWED_HOSTS is empty and DEBUG is True, variants of localhost are
1381 allowed.
1382 """
1383 valid_hosts = ["localhost", "subdomain.localhost", "127.0.0.1", "[::1]"]
1384 for host in valid_hosts:
1385 request = HttpRequest()
1386 request.META = {"HTTP_HOST": host}
1387 self.assertEqual(request.get_host(), host)
1388
1389 # Other hostnames raise a DisallowedHost.
1390 with self.assertRaises(DisallowedHost):
1391 request = HttpRequest()
1392 request.META = {"HTTP_HOST": "example.com"}
1393 request.get_host()
1394
1395 @override_settings(ALLOWED_HOSTS=[])
1396 def test_get_host_suggestion_of_allowed_host(self):
1397 """
1398 get_host() makes helpful suggestions if a valid-looking host is not in
1399 ALLOWED_HOSTS.
1400 """
1401 msg_invalid_host = "Invalid HTTP_HOST header: %r."
1402 msg_suggestion = msg_invalid_host + " You may need to add %r to ALLOWED_HOSTS."
1403 msg_suggestion2 = (
1404 msg_invalid_host
1405 + " The domain name provided is not valid according to RFC 1034/1035"
1406 )
1407
1408 for host in [ # Valid-looking hosts
1409 "example.com",
1410 "12.34.56.78",
1411 "[2001:19f0:feee::dead:beef:cafe]",
1412 "xn--4ca9at.com", # Punycode for öäü.com
1413 ]:
1414 request = HttpRequest()
1415 request.META = {"HTTP_HOST": host}
1416 with self.assertRaisesMessage(
1417 DisallowedHost, msg_suggestion % (host, host)
1418 ):
1419 request.get_host()
1420
1421 for domain, port in [ # Valid-looking hosts with a port number
1422 ("example.com", 80),
1423 ("12.34.56.78", 443),
1424 ("[2001:19f0:feee::dead:beef:cafe]", 8080),
1425 ]:
1426 host = "%s:%s" % (domain, port)
1427 request = HttpRequest()
1428 request.META = {"HTTP_HOST": host}
1429 with self.assertRaisesMessage(
1430 DisallowedHost, msg_suggestion % (host, domain)
1431 ):
1432 request.get_host()
1433
1434 for host in self.poisoned_hosts:
1435 request = HttpRequest()
1436 request.META = {"HTTP_HOST": host}
1437 with self.assertRaisesMessage(DisallowedHost, msg_invalid_host % host):
1438 request.get_host()
1439
1440 request = HttpRequest()
1441 request.META = {"HTTP_HOST": "invalid_hostname.com"}
1442 with self.assertRaisesMessage(
1443 DisallowedHost, msg_suggestion2 % "invalid_hostname.com"
1444 ):
1445 request.get_host()
1446
1447 def test_split_domain_port(self):
1448 for host, expected in [
1449 ("<invalid>", ("", "")),
1450 ("<invalid>:8080", ("", "")),
1451 ("example.com 8080", ("", "")),
1452 ("example.com:invalid", ("", "")),
1453 ("[::1]", ("[::1]", "")),
1454 ("[::1]:8080", ("[::1]", "8080")),
1455 ("[::ffff:127.0.0.1]", ("[::ffff:127.0.0.1]", "")),
1456 ("[::ffff:127.0.0.1]:8080", ("[::ffff:127.0.0.1]", "8080")),
1457 (
1458 "[1851:0000:3238:DEF1:0177:0000:0000:0125]",
1459 ("[1851:0000:3238:def1:0177:0000:0000:0125]", ""),
1460 ),
1461 (
1462 "[1851:0000:3238:DEF1:0177:0000:0000:0125]:8080",
1463 ("[1851:0000:3238:def1:0177:0000:0000:0125]", "8080"),
1464 ),
1465 ("127.0.0.1", ("127.0.0.1", "")),
1466 ("127.0.0.1:8080", ("127.0.0.1", "8080")),
1467 ("example.com", ("example.com", "")),
1468 ("example.com:8080", ("example.com", "8080")),
1469 ("example.com.", ("example.com", "")),
1470 ("example.com.:8080", ("example.com", "8080")),
1471 ("xn--n28h.test", ("xn--n28h.test", "")),
1472 ("xn--n28h.test:8080", ("xn--n28h.test", "8080")),
1473 ("subdomain.example.com", ("subdomain.example.com", "")),
1474 ("subdomain.example.com:8080", ("subdomain.example.com", "8080")),
1475 ]:
1476 with self.subTest(host=host):
1477 self.assertEqual(split_domain_port(host), expected)
1478
1479
1480 class BuildAbsoluteURITests(SimpleTestCase):
1481 factory = RequestFactory()
1482
1483 def test_absolute_url(self):
1484 request = HttpRequest()
1485 url = "https://www.example.com/asdf"
1486 self.assertEqual(request.build_absolute_uri(location=url), url)
1487
1488 def test_host_retrieval(self):
1489 request = HttpRequest()
1490 request.get_host = lambda: "www.example.com"
1491 request.path = ""
1492 self.assertEqual(
1493 request.build_absolute_uri(location="/path/with:colons"),
1494 "http://www.example.com/path/with:colons",
1495 )
1496
1497 def test_request_path_begins_with_two_slashes(self):
1498 # //// creates a request with a path beginning with //
1499 request = self.factory.get("////absolute-uri")
1500 tests = (
1501 # location isn't provided
1502 (None, "http://testserver//absolute-uri"),
1503 # An absolute URL
1504 ("http://example.com/?foo=bar", "http://example.com/?foo=bar"),
1505 # A schema-relative URL
1506 ("//example.com/?foo=bar", "http://example.com/?foo=bar"),
1507 # Relative URLs
1508 ("/foo/bar/", "http://testserver/foo/bar/"),
1509 ("/foo/./bar/", "http://testserver/foo/bar/"),
1510 ("/foo/../bar/", "http://testserver/bar/"),
1511 ("///foo/bar/", "http://testserver/foo/bar/"),
1512 )
1513 for location, expected_url in tests:
1514 with self.subTest(location=location):
1515 self.assertEqual(
1516 request.build_absolute_uri(location=location), expected_url
1517 )
1518
1519
1520 class RequestHeadersTests(SimpleTestCase):
1521 ENVIRON = {
1522 # Non-headers are ignored.
1523 "PATH_INFO": "/somepath/",
1524 "REQUEST_METHOD": "get",
1525 "wsgi.input": BytesIO(b""),
1526 "SERVER_NAME": "internal.com",
1527 "SERVER_PORT": 80,
1528 # These non-HTTP prefixed headers are included.
1529 "CONTENT_TYPE": "text/html",
1530 "CONTENT_LENGTH": "100",
1531 # All HTTP-prefixed headers are included.
1532 "HTTP_ACCEPT": "*",
1533 "HTTP_HOST": "example.com",
1534 "HTTP_USER_AGENT": "python-requests/1.2.0",
1535 }
1536
1537 def test_base_request_headers(self):
1538 request = HttpRequest()
1539 request.META = self.ENVIRON
1540 self.assertEqual(
1541 dict(request.headers),
1542 {
1543 "Content-Type": "text/html",
1544 "Content-Length": "100",
1545 "Accept": "*",
1546 "Host": "example.com",
1547 "User-Agent": "python-requests/1.2.0",
1548 },
1549 )
1550
1551 def test_wsgi_request_headers(self):
1552 request = WSGIRequest(self.ENVIRON)
1553 self.assertEqual(
1554 dict(request.headers),
1555 {
1556 "Content-Type": "text/html",
1557 "Content-Length": "100",
1558 "Accept": "*",
1559 "Host": "example.com",
1560 "User-Agent": "python-requests/1.2.0",
1561 },
1562 )
1563
1564 def test_wsgi_request_headers_getitem(self):
1565 request = WSGIRequest(self.ENVIRON)
1566 self.assertEqual(request.headers["User-Agent"], "python-requests/1.2.0")
1567 self.assertEqual(request.headers["user-agent"], "python-requests/1.2.0")
1568 self.assertEqual(request.headers["user_agent"], "python-requests/1.2.0")
1569 self.assertEqual(request.headers["Content-Type"], "text/html")
1570 self.assertEqual(request.headers["Content-Length"], "100")
1571
1572 def test_wsgi_request_headers_get(self):
1573 request = WSGIRequest(self.ENVIRON)
1574 self.assertEqual(request.headers.get("User-Agent"), "python-requests/1.2.0")
1575 self.assertEqual(request.headers.get("user-agent"), "python-requests/1.2.0")
1576 self.assertEqual(request.headers.get("Content-Type"), "text/html")
1577 self.assertEqual(request.headers.get("Content-Length"), "100")
1578
1579
1580 class HttpHeadersTests(SimpleTestCase):
1581 def test_basic(self):
1582 environ = {
1583 "CONTENT_TYPE": "text/html",
1584 "CONTENT_LENGTH": "100",
1585 "HTTP_HOST": "example.com",
1586 }
1587 headers = HttpHeaders(environ)
1588 self.assertEqual(sorted(headers), ["Content-Length", "Content-Type", "Host"])
1589 self.assertEqual(
1590 headers,
1591 {
1592 "Content-Type": "text/html",
1593 "Content-Length": "100",
1594 "Host": "example.com",
1595 },
1596 )
1597
1598 def test_parse_header_name(self):
1599 tests = (
1600 ("PATH_INFO", None),
1601 ("HTTP_ACCEPT", "Accept"),
1602 ("HTTP_USER_AGENT", "User-Agent"),
1603 ("HTTP_X_FORWARDED_PROTO", "X-Forwarded-Proto"),
1604 ("CONTENT_TYPE", "Content-Type"),
1605 ("CONTENT_LENGTH", "Content-Length"),
1606 )
1607 for header, expected in tests:
1608 with self.subTest(header=header):
1609 self.assertEqual(HttpHeaders.parse_header_name(header), expected)