1. import importlib
    
  2. import inspect
    
  3. import os
    
  4. import re
    
  5. import sys
    
  6. import tempfile
    
  7. import threading
    
  8. from io import StringIO
    
  9. from pathlib import Path
    
  10. from unittest import mock, skipIf
    
  11. 
    
  12. from django.core import mail
    
  13. from django.core.files.uploadedfile import SimpleUploadedFile
    
  14. from django.db import DatabaseError, connection
    
  15. from django.http import Http404, HttpRequest, HttpResponse
    
  16. from django.shortcuts import render
    
  17. from django.template import TemplateDoesNotExist
    
  18. from django.test import RequestFactory, SimpleTestCase, override_settings
    
  19. from django.test.utils import LoggingCaptureMixin
    
  20. from django.urls import path, reverse
    
  21. from django.urls.converters import IntConverter
    
  22. from django.utils.functional import SimpleLazyObject
    
  23. from django.utils.regex_helper import _lazy_re_compile
    
  24. from django.utils.safestring import mark_safe
    
  25. from django.views.debug import (
    
  26.     CallableSettingWrapper,
    
  27.     ExceptionCycleWarning,
    
  28.     ExceptionReporter,
    
  29. )
    
  30. from django.views.debug import Path as DebugPath
    
  31. from django.views.debug import (
    
  32.     SafeExceptionReporterFilter,
    
  33.     default_urlconf,
    
  34.     get_default_exception_reporter_filter,
    
  35.     technical_404_response,
    
  36.     technical_500_response,
    
  37. )
    
  38. from django.views.decorators.debug import sensitive_post_parameters, sensitive_variables
    
  39. 
    
  40. from ..views import (
    
  41.     custom_exception_reporter_filter_view,
    
  42.     index_page,
    
  43.     multivalue_dict_key_error,
    
  44.     non_sensitive_view,
    
  45.     paranoid_view,
    
  46.     sensitive_args_function_caller,
    
  47.     sensitive_kwargs_function_caller,
    
  48.     sensitive_method_view,
    
  49.     sensitive_view,
    
  50. )
    
  51. 
    
  52. 
    
  53. class User:
    
  54.     def __str__(self):
    
  55.         return "jacob"
    
  56. 
    
  57. 
    
  58. class WithoutEmptyPathUrls:
    
  59.     urlpatterns = [path("url/", index_page, name="url")]
    
  60. 
    
  61. 
    
  62. class CallableSettingWrapperTests(SimpleTestCase):
    
  63.     """Unittests for CallableSettingWrapper"""
    
  64. 
    
  65.     def test_repr(self):
    
  66.         class WrappedCallable:
    
  67.             def __repr__(self):
    
  68.                 return "repr from the wrapped callable"
    
  69. 
    
  70.             def __call__(self):
    
  71.                 pass
    
  72. 
    
  73.         actual = repr(CallableSettingWrapper(WrappedCallable()))
    
  74.         self.assertEqual(actual, "repr from the wrapped callable")
    
  75. 
    
  76. 
    
  77. @override_settings(DEBUG=True, ROOT_URLCONF="view_tests.urls")
    
  78. class DebugViewTests(SimpleTestCase):
    
  79.     def test_files(self):
    
  80.         with self.assertLogs("django.request", "ERROR"):
    
  81.             response = self.client.get("/raises/")
    
  82.         self.assertEqual(response.status_code, 500)
    
  83. 
    
  84.         data = {
    
  85.             "file_data.txt": SimpleUploadedFile("file_data.txt", b"haha"),
    
  86.         }
    
  87.         with self.assertLogs("django.request", "ERROR"):
    
  88.             response = self.client.post("/raises/", data)
    
  89.         self.assertContains(response, "file_data.txt", status_code=500)
    
  90.         self.assertNotContains(response, "haha", status_code=500)
    
  91. 
    
  92.     def test_400(self):
    
  93.         # When DEBUG=True, technical_500_template() is called.
    
  94.         with self.assertLogs("django.security", "WARNING"):
    
  95.             response = self.client.get("/raises400/")
    
  96.         self.assertContains(response, '<div class="context" id="', status_code=400)
    
  97. 
    
  98.     def test_400_bad_request(self):
    
  99.         # When DEBUG=True, technical_500_template() is called.
    
  100.         with self.assertLogs("django.request", "WARNING") as cm:
    
  101.             response = self.client.get("/raises400_bad_request/")
    
  102.         self.assertContains(response, '<div class="context" id="', status_code=400)
    
  103.         self.assertEqual(
    
  104.             cm.records[0].getMessage(),
    
  105.             "Malformed request syntax: /raises400_bad_request/",
    
  106.         )
    
  107. 
    
  108.     # Ensure no 403.html template exists to test the default case.
    
  109.     @override_settings(
    
  110.         TEMPLATES=[
    
  111.             {
    
  112.                 "BACKEND": "django.template.backends.django.DjangoTemplates",
    
  113.             }
    
  114.         ]
    
  115.     )
    
  116.     def test_403(self):
    
  117.         response = self.client.get("/raises403/")
    
  118.         self.assertContains(response, "<h1>403 Forbidden</h1>", status_code=403)
    
  119. 
    
  120.     # Set up a test 403.html template.
    
  121.     @override_settings(
    
  122.         TEMPLATES=[
    
  123.             {
    
  124.                 "BACKEND": "django.template.backends.django.DjangoTemplates",
    
  125.                 "OPTIONS": {
    
  126.                     "loaders": [
    
  127.                         (
    
  128.                             "django.template.loaders.locmem.Loader",
    
  129.                             {
    
  130.                                 "403.html": (
    
  131.                                     "This is a test template for a 403 error "
    
  132.                                     "({{ exception }})."
    
  133.                                 ),
    
  134.                             },
    
  135.                         ),
    
  136.                     ],
    
  137.                 },
    
  138.             }
    
  139.         ]
    
  140.     )
    
  141.     def test_403_template(self):
    
  142.         response = self.client.get("/raises403/")
    
  143.         self.assertContains(response, "test template", status_code=403)
    
  144.         self.assertContains(response, "(Insufficient Permissions).", status_code=403)
    
  145. 
    
  146.     def test_404(self):
    
  147.         response = self.client.get("/raises404/")
    
  148.         self.assertNotContains(
    
  149.             response,
    
  150.             '<pre class="exception_value">',
    
  151.             status_code=404,
    
  152.         )
    
  153.         self.assertContains(
    
  154.             response,
    
  155.             "<p>The current path, <code>not-in-urls</code>, didn’t match any "
    
  156.             "of these.</p>",
    
  157.             status_code=404,
    
  158.             html=True,
    
  159.         )
    
  160. 
    
  161.     def test_404_not_in_urls(self):
    
  162.         response = self.client.get("/not-in-urls")
    
  163.         self.assertNotContains(response, "Raised by:", status_code=404)
    
  164.         self.assertNotContains(
    
  165.             response,
    
  166.             '<pre class="exception_value">',
    
  167.             status_code=404,
    
  168.         )
    
  169.         self.assertContains(
    
  170.             response, "Django tried these URL patterns", status_code=404
    
  171.         )
    
  172.         self.assertContains(
    
  173.             response,
    
  174.             "<p>The current path, <code>not-in-urls</code>, didn’t match any "
    
  175.             "of these.</p>",
    
  176.             status_code=404,
    
  177.             html=True,
    
  178.         )
    
  179.         # Pattern and view name of a RegexURLPattern appear.
    
  180.         self.assertContains(
    
  181.             response, r"^regex-post/(?P&lt;pk&gt;[0-9]+)/$", status_code=404
    
  182.         )
    
  183.         self.assertContains(response, "[name='regex-post']", status_code=404)
    
  184.         # Pattern and view name of a RoutePattern appear.
    
  185.         self.assertContains(response, r"path-post/&lt;int:pk&gt;/", status_code=404)
    
  186.         self.assertContains(response, "[name='path-post']", status_code=404)
    
  187. 
    
  188.     @override_settings(ROOT_URLCONF=WithoutEmptyPathUrls)
    
  189.     def test_404_empty_path_not_in_urls(self):
    
  190.         response = self.client.get("/")
    
  191.         self.assertContains(
    
  192.             response,
    
  193.             "<p>The empty path didn’t match any of these.</p>",
    
  194.             status_code=404,
    
  195.             html=True,
    
  196.         )
    
  197. 
    
  198.     def test_technical_404(self):
    
  199.         response = self.client.get("/technical404/")
    
  200.         self.assertContains(
    
  201.             response,
    
  202.             '<pre class="exception_value">Testing technical 404.</pre>',
    
  203.             status_code=404,
    
  204.             html=True,
    
  205.         )
    
  206.         self.assertContains(response, "Raised by:", status_code=404)
    
  207.         self.assertContains(
    
  208.             response,
    
  209.             "<td>view_tests.views.technical404</td>",
    
  210.             status_code=404,
    
  211.         )
    
  212.         self.assertContains(
    
  213.             response,
    
  214.             "<p>The current path, <code>technical404/</code>, matched the "
    
  215.             "last one.</p>",
    
  216.             status_code=404,
    
  217.             html=True,
    
  218.         )
    
  219. 
    
  220.     def test_classbased_technical_404(self):
    
  221.         response = self.client.get("/classbased404/")
    
  222.         self.assertContains(
    
  223.             response,
    
  224.             "<th>Raised by:</th><td>view_tests.views.Http404View</td>",
    
  225.             status_code=404,
    
  226.             html=True,
    
  227.         )
    
  228. 
    
  229.     def test_technical_500(self):
    
  230.         with self.assertLogs("django.request", "ERROR"):
    
  231.             response = self.client.get("/raises500/")
    
  232.         self.assertContains(
    
  233.             response,
    
  234.             "<th>Raised during:</th><td>view_tests.views.raises500</td>",
    
  235.             status_code=500,
    
  236.             html=True,
    
  237.         )
    
  238.         with self.assertLogs("django.request", "ERROR"):
    
  239.             response = self.client.get("/raises500/", HTTP_ACCEPT="text/plain")
    
  240.         self.assertContains(
    
  241.             response,
    
  242.             "Raised during: view_tests.views.raises500",
    
  243.             status_code=500,
    
  244.         )
    
  245. 
    
  246.     def test_classbased_technical_500(self):
    
  247.         with self.assertLogs("django.request", "ERROR"):
    
  248.             response = self.client.get("/classbased500/")
    
  249.         self.assertContains(
    
  250.             response,
    
  251.             "<th>Raised during:</th><td>view_tests.views.Raises500View</td>",
    
  252.             status_code=500,
    
  253.             html=True,
    
  254.         )
    
  255.         with self.assertLogs("django.request", "ERROR"):
    
  256.             response = self.client.get("/classbased500/", HTTP_ACCEPT="text/plain")
    
  257.         self.assertContains(
    
  258.             response,
    
  259.             "Raised during: view_tests.views.Raises500View",
    
  260.             status_code=500,
    
  261.         )
    
  262. 
    
  263.     def test_non_l10ned_numeric_ids(self):
    
  264.         """
    
  265.         Numeric IDs and fancy traceback context blocks line numbers shouldn't
    
  266.         be localized.
    
  267.         """
    
  268.         with self.settings(DEBUG=True):
    
  269.             with self.assertLogs("django.request", "ERROR"):
    
  270.                 response = self.client.get("/raises500/")
    
  271.             # We look for a HTML fragment of the form
    
  272.             # '<div class="context" id="c38123208">',
    
  273.             # not '<div class="context" id="c38,123,208"'.
    
  274.             self.assertContains(response, '<div class="context" id="', status_code=500)
    
  275.             match = re.search(
    
  276.                 b'<div class="context" id="(?P<id>[^"]+)">', response.content
    
  277.             )
    
  278.             self.assertIsNotNone(match)
    
  279.             id_repr = match["id"]
    
  280.             self.assertFalse(
    
  281.                 re.search(b"[^c0-9]", id_repr),
    
  282.                 "Numeric IDs in debug response HTML page shouldn't be localized "
    
  283.                 "(value: %s)." % id_repr.decode(),
    
  284.             )
    
  285. 
    
  286.     def test_template_exceptions(self):
    
  287.         with self.assertLogs("django.request", "ERROR"):
    
  288.             try:
    
  289.                 self.client.get(reverse("template_exception"))
    
  290.             except Exception:
    
  291.                 raising_loc = inspect.trace()[-1][-2][0].strip()
    
  292.                 self.assertNotEqual(
    
  293.                     raising_loc.find('raise Exception("boom")'),
    
  294.                     -1,
    
  295.                     "Failed to find 'raise Exception' in last frame of "
    
  296.                     "traceback, instead found: %s" % raising_loc,
    
  297.                 )
    
  298. 
    
  299.     @skipIf(
    
  300.         sys.platform == "win32",
    
  301.         "Raises OSError instead of TemplateDoesNotExist on Windows.",
    
  302.     )
    
  303.     def test_safestring_in_exception(self):
    
  304.         with self.assertLogs("django.request", "ERROR"):
    
  305.             response = self.client.get("/safestring_exception/")
    
  306.             self.assertNotContains(
    
  307.                 response,
    
  308.                 "<script>alert(1);</script>",
    
  309.                 status_code=500,
    
  310.                 html=True,
    
  311.             )
    
  312.             self.assertContains(
    
  313.                 response,
    
  314.                 "&lt;script&gt;alert(1);&lt;/script&gt;",
    
  315.                 count=3,
    
  316.                 status_code=500,
    
  317.                 html=True,
    
  318.             )
    
  319. 
    
  320.     def test_template_loader_postmortem(self):
    
  321.         """Tests for not existing file"""
    
  322.         template_name = "notfound.html"
    
  323.         with tempfile.NamedTemporaryFile(prefix=template_name) as tmpfile:
    
  324.             tempdir = os.path.dirname(tmpfile.name)
    
  325.             template_path = os.path.join(tempdir, template_name)
    
  326.             with override_settings(
    
  327.                 TEMPLATES=[
    
  328.                     {
    
  329.                         "BACKEND": "django.template.backends.django.DjangoTemplates",
    
  330.                         "DIRS": [tempdir],
    
  331.                     }
    
  332.                 ]
    
  333.             ), self.assertLogs("django.request", "ERROR"):
    
  334.                 response = self.client.get(
    
  335.                     reverse(
    
  336.                         "raises_template_does_not_exist", kwargs={"path": template_name}
    
  337.                     )
    
  338.                 )
    
  339.             self.assertContains(
    
  340.                 response,
    
  341.                 "%s (Source does not exist)" % template_path,
    
  342.                 status_code=500,
    
  343.                 count=2,
    
  344.             )
    
  345.             # Assert as HTML.
    
  346.             self.assertContains(
    
  347.                 response,
    
  348.                 "<li><code>django.template.loaders.filesystem.Loader</code>: "
    
  349.                 "%s (Source does not exist)</li>"
    
  350.                 % os.path.join(tempdir, "notfound.html"),
    
  351.                 status_code=500,
    
  352.                 html=True,
    
  353.             )
    
  354. 
    
  355.     def test_no_template_source_loaders(self):
    
  356.         """
    
  357.         Make sure if you don't specify a template, the debug view doesn't blow up.
    
  358.         """
    
  359.         with self.assertLogs("django.request", "ERROR"):
    
  360.             with self.assertRaises(TemplateDoesNotExist):
    
  361.                 self.client.get("/render_no_template/")
    
  362. 
    
  363.     @override_settings(ROOT_URLCONF="view_tests.default_urls")
    
  364.     def test_default_urlconf_template(self):
    
  365.         """
    
  366.         Make sure that the default URLconf template is shown instead of the
    
  367.         technical 404 page, if the user has not altered their URLconf yet.
    
  368.         """
    
  369.         response = self.client.get("/")
    
  370.         self.assertContains(
    
  371.             response, "<h1>The install worked successfully! Congratulations!</h1>"
    
  372.         )
    
  373. 
    
  374.     @override_settings(ROOT_URLCONF="view_tests.regression_21530_urls")
    
  375.     def test_regression_21530(self):
    
  376.         """
    
  377.         Regression test for bug #21530.
    
  378. 
    
  379.         If the admin app include is replaced with exactly one url
    
  380.         pattern, then the technical 404 template should be displayed.
    
  381. 
    
  382.         The bug here was that an AttributeError caused a 500 response.
    
  383.         """
    
  384.         response = self.client.get("/")
    
  385.         self.assertContains(
    
  386.             response, "Page not found <span>(404)</span>", status_code=404
    
  387.         )
    
  388. 
    
  389.     def test_template_encoding(self):
    
  390.         """
    
  391.         The templates are loaded directly, not via a template loader, and
    
  392.         should be opened as utf-8 charset as is the default specified on
    
  393.         template engines.
    
  394.         """
    
  395.         with mock.patch.object(DebugPath, "open") as m:
    
  396.             default_urlconf(None)
    
  397.             m.assert_called_once_with(encoding="utf-8")
    
  398.             m.reset_mock()
    
  399.             technical_404_response(mock.MagicMock(), mock.Mock())
    
  400.             m.assert_called_once_with(encoding="utf-8")
    
  401. 
    
  402.     def test_technical_404_converter_raise_404(self):
    
  403.         with mock.patch.object(IntConverter, "to_python", side_effect=Http404):
    
  404.             response = self.client.get("/path-post/1/")
    
  405.             self.assertContains(response, "Page not found", status_code=404)
    
  406. 
    
  407.     def test_exception_reporter_from_request(self):
    
  408.         with self.assertLogs("django.request", "ERROR"):
    
  409.             response = self.client.get("/custom_reporter_class_view/")
    
  410.         self.assertContains(response, "custom traceback text", status_code=500)
    
  411. 
    
  412.     @override_settings(
    
  413.         DEFAULT_EXCEPTION_REPORTER="view_tests.views.CustomExceptionReporter"
    
  414.     )
    
  415.     def test_exception_reporter_from_settings(self):
    
  416.         with self.assertLogs("django.request", "ERROR"):
    
  417.             response = self.client.get("/raises500/")
    
  418.         self.assertContains(response, "custom traceback text", status_code=500)
    
  419. 
    
  420.     @override_settings(
    
  421.         DEFAULT_EXCEPTION_REPORTER="view_tests.views.TemplateOverrideExceptionReporter"
    
  422.     )
    
  423.     def test_template_override_exception_reporter(self):
    
  424.         with self.assertLogs("django.request", "ERROR"):
    
  425.             response = self.client.get("/raises500/")
    
  426.         self.assertContains(
    
  427.             response,
    
  428.             "<h1>Oh no, an error occurred!</h1>",
    
  429.             status_code=500,
    
  430.             html=True,
    
  431.         )
    
  432. 
    
  433.         with self.assertLogs("django.request", "ERROR"):
    
  434.             response = self.client.get("/raises500/", HTTP_ACCEPT="text/plain")
    
  435.         self.assertContains(response, "Oh dear, an error occurred!", status_code=500)
    
  436. 
    
  437. 
    
  438. class DebugViewQueriesAllowedTests(SimpleTestCase):
    
  439.     # May need a query to initialize MySQL connection
    
  440.     databases = {"default"}
    
  441. 
    
  442.     def test_handle_db_exception(self):
    
  443.         """
    
  444.         Ensure the debug view works when a database exception is raised by
    
  445.         performing an invalid query and passing the exception to the debug view.
    
  446.         """
    
  447.         with connection.cursor() as cursor:
    
  448.             try:
    
  449.                 cursor.execute("INVALID SQL")
    
  450.             except DatabaseError:
    
  451.                 exc_info = sys.exc_info()
    
  452. 
    
  453.         rf = RequestFactory()
    
  454.         response = technical_500_response(rf.get("/"), *exc_info)
    
  455.         self.assertContains(response, "OperationalError at /", status_code=500)
    
  456. 
    
  457. 
    
  458. @override_settings(
    
  459.     DEBUG=True,
    
  460.     ROOT_URLCONF="view_tests.urls",
    
  461.     # No template directories are configured, so no templates will be found.
    
  462.     TEMPLATES=[
    
  463.         {
    
  464.             "BACKEND": "django.template.backends.dummy.TemplateStrings",
    
  465.         }
    
  466.     ],
    
  467. )
    
  468. class NonDjangoTemplatesDebugViewTests(SimpleTestCase):
    
  469.     def test_400(self):
    
  470.         # When DEBUG=True, technical_500_template() is called.
    
  471.         with self.assertLogs("django.security", "WARNING"):
    
  472.             response = self.client.get("/raises400/")
    
  473.         self.assertContains(response, '<div class="context" id="', status_code=400)
    
  474. 
    
  475.     def test_400_bad_request(self):
    
  476.         # When DEBUG=True, technical_500_template() is called.
    
  477.         with self.assertLogs("django.request", "WARNING") as cm:
    
  478.             response = self.client.get("/raises400_bad_request/")
    
  479.         self.assertContains(response, '<div class="context" id="', status_code=400)
    
  480.         self.assertEqual(
    
  481.             cm.records[0].getMessage(),
    
  482.             "Malformed request syntax: /raises400_bad_request/",
    
  483.         )
    
  484. 
    
  485.     def test_403(self):
    
  486.         response = self.client.get("/raises403/")
    
  487.         self.assertContains(response, "<h1>403 Forbidden</h1>", status_code=403)
    
  488. 
    
  489.     def test_404(self):
    
  490.         response = self.client.get("/raises404/")
    
  491.         self.assertEqual(response.status_code, 404)
    
  492. 
    
  493.     def test_template_not_found_error(self):
    
  494.         # Raises a TemplateDoesNotExist exception and shows the debug view.
    
  495.         url = reverse(
    
  496.             "raises_template_does_not_exist", kwargs={"path": "notfound.html"}
    
  497.         )
    
  498.         with self.assertLogs("django.request", "ERROR"):
    
  499.             response = self.client.get(url)
    
  500.         self.assertContains(response, '<div class="context" id="', status_code=500)
    
  501. 
    
  502. 
    
  503. class ExceptionReporterTests(SimpleTestCase):
    
  504.     rf = RequestFactory()
    
  505. 
    
  506.     def test_request_and_exception(self):
    
  507.         "A simple exception report can be generated"
    
  508.         try:
    
  509.             request = self.rf.get("/test_view/")
    
  510.             request.user = User()
    
  511.             raise ValueError("Can't find my keys")
    
  512.         except ValueError:
    
  513.             exc_type, exc_value, tb = sys.exc_info()
    
  514.         reporter = ExceptionReporter(request, exc_type, exc_value, tb)
    
  515.         html = reporter.get_traceback_html()
    
  516.         self.assertInHTML("<h1>ValueError at /test_view/</h1>", html)
    
  517.         self.assertIn(
    
  518.             '<pre class="exception_value">Can&#x27;t find my keys</pre>', html
    
  519.         )
    
  520.         self.assertIn("<th>Request Method:</th>", html)
    
  521.         self.assertIn("<th>Request URL:</th>", html)
    
  522.         self.assertIn('<h3 id="user-info">USER</h3>', html)
    
  523.         self.assertIn("<p>jacob</p>", html)
    
  524.         self.assertIn("<th>Exception Type:</th>", html)
    
  525.         self.assertIn("<th>Exception Value:</th>", html)
    
  526.         self.assertIn("<h2>Traceback ", html)
    
  527.         self.assertIn("<h2>Request information</h2>", html)
    
  528.         self.assertNotIn("<p>Request data not supplied</p>", html)
    
  529.         self.assertIn("<p>No POST data</p>", html)
    
  530. 
    
  531.     def test_no_request(self):
    
  532.         "An exception report can be generated without request"
    
  533.         try:
    
  534.             raise ValueError("Can't find my keys")
    
  535.         except ValueError:
    
  536.             exc_type, exc_value, tb = sys.exc_info()
    
  537.         reporter = ExceptionReporter(None, exc_type, exc_value, tb)
    
  538.         html = reporter.get_traceback_html()
    
  539.         self.assertInHTML("<h1>ValueError</h1>", html)
    
  540.         self.assertIn(
    
  541.             '<pre class="exception_value">Can&#x27;t find my keys</pre>', html
    
  542.         )
    
  543.         self.assertNotIn("<th>Request Method:</th>", html)
    
  544.         self.assertNotIn("<th>Request URL:</th>", html)
    
  545.         self.assertNotIn('<h3 id="user-info">USER</h3>', html)
    
  546.         self.assertIn("<th>Exception Type:</th>", html)
    
  547.         self.assertIn("<th>Exception Value:</th>", html)
    
  548.         self.assertIn("<h2>Traceback ", html)
    
  549.         self.assertIn("<h2>Request information</h2>", html)
    
  550.         self.assertIn("<p>Request data not supplied</p>", html)
    
  551. 
    
  552.     def test_sharing_traceback(self):
    
  553.         try:
    
  554.             raise ValueError("Oops")
    
  555.         except ValueError:
    
  556.             exc_type, exc_value, tb = sys.exc_info()
    
  557.         reporter = ExceptionReporter(None, exc_type, exc_value, tb)
    
  558.         html = reporter.get_traceback_html()
    
  559.         self.assertIn(
    
  560.             '<form action="https://dpaste.com/" name="pasteform" '
    
  561.             'id="pasteform" method="post">',
    
  562.             html,
    
  563.         )
    
  564. 
    
  565.     def test_eol_support(self):
    
  566.         """The ExceptionReporter supports Unix, Windows and Macintosh EOL markers"""
    
  567.         LINES = ["print %d" % i for i in range(1, 6)]
    
  568.         reporter = ExceptionReporter(None, None, None, None)
    
  569. 
    
  570.         for newline in ["\n", "\r\n", "\r"]:
    
  571.             fd, filename = tempfile.mkstemp(text=False)
    
  572.             os.write(fd, (newline.join(LINES) + newline).encode())
    
  573.             os.close(fd)
    
  574. 
    
  575.             try:
    
  576.                 self.assertEqual(
    
  577.                     reporter._get_lines_from_file(filename, 3, 2),
    
  578.                     (1, LINES[1:3], LINES[3], LINES[4:]),
    
  579.                 )
    
  580.             finally:
    
  581.                 os.unlink(filename)
    
  582. 
    
  583.     def test_no_exception(self):
    
  584.         "An exception report can be generated for just a request"
    
  585.         request = self.rf.get("/test_view/")
    
  586.         reporter = ExceptionReporter(request, None, None, None)
    
  587.         html = reporter.get_traceback_html()
    
  588.         self.assertInHTML("<h1>Report at /test_view/</h1>", html)
    
  589.         self.assertIn(
    
  590.             '<pre class="exception_value">No exception message supplied</pre>', html
    
  591.         )
    
  592.         self.assertIn("<th>Request Method:</th>", html)
    
  593.         self.assertIn("<th>Request URL:</th>", html)
    
  594.         self.assertNotIn("<th>Exception Type:</th>", html)
    
  595.         self.assertNotIn("<th>Exception Value:</th>", html)
    
  596.         self.assertNotIn("<h2>Traceback ", html)
    
  597.         self.assertIn("<h2>Request information</h2>", html)
    
  598.         self.assertNotIn("<p>Request data not supplied</p>", html)
    
  599. 
    
  600.     def test_suppressed_context(self):
    
  601.         try:
    
  602.             try:
    
  603.                 raise RuntimeError("Can't find my keys")
    
  604.             except RuntimeError:
    
  605.                 raise ValueError("Can't find my keys") from None
    
  606.         except ValueError:
    
  607.             exc_type, exc_value, tb = sys.exc_info()
    
  608. 
    
  609.         reporter = ExceptionReporter(None, exc_type, exc_value, tb)
    
  610.         html = reporter.get_traceback_html()
    
  611.         self.assertInHTML("<h1>ValueError</h1>", html)
    
  612.         self.assertIn(
    
  613.             '<pre class="exception_value">Can&#x27;t find my keys</pre>', html
    
  614.         )
    
  615.         self.assertIn("<th>Exception Type:</th>", html)
    
  616.         self.assertIn("<th>Exception Value:</th>", html)
    
  617.         self.assertIn("<h2>Traceback ", html)
    
  618.         self.assertIn("<h2>Request information</h2>", html)
    
  619.         self.assertIn("<p>Request data not supplied</p>", html)
    
  620.         self.assertNotIn("During handling of the above exception", html)
    
  621. 
    
  622.     def test_innermost_exception_without_traceback(self):
    
  623.         try:
    
  624.             try:
    
  625.                 raise RuntimeError("Oops")
    
  626.             except Exception as exc:
    
  627.                 new_exc = RuntimeError("My context")
    
  628.                 exc.__context__ = new_exc
    
  629.                 raise
    
  630.         except Exception:
    
  631.             exc_type, exc_value, tb = sys.exc_info()
    
  632. 
    
  633.         reporter = ExceptionReporter(None, exc_type, exc_value, tb)
    
  634.         frames = reporter.get_traceback_frames()
    
  635.         self.assertEqual(len(frames), 2)
    
  636.         html = reporter.get_traceback_html()
    
  637.         self.assertInHTML("<h1>RuntimeError</h1>", html)
    
  638.         self.assertIn('<pre class="exception_value">Oops</pre>', html)
    
  639.         self.assertIn("<th>Exception Type:</th>", html)
    
  640.         self.assertIn("<th>Exception Value:</th>", html)
    
  641.         self.assertIn("<h2>Traceback ", html)
    
  642.         self.assertIn("<h2>Request information</h2>", html)
    
  643.         self.assertIn("<p>Request data not supplied</p>", html)
    
  644.         self.assertIn(
    
  645.             "During handling of the above exception (My context), another "
    
  646.             "exception occurred",
    
  647.             html,
    
  648.         )
    
  649.         self.assertInHTML('<li class="frame user">None</li>', html)
    
  650.         self.assertIn("Traceback (most recent call last):\n  None", html)
    
  651. 
    
  652.         text = reporter.get_traceback_text()
    
  653.         self.assertIn("Exception Type: RuntimeError", text)
    
  654.         self.assertIn("Exception Value: Oops", text)
    
  655.         self.assertIn("Traceback (most recent call last):\n  None", text)
    
  656.         self.assertIn(
    
  657.             "During handling of the above exception (My context), another "
    
  658.             "exception occurred",
    
  659.             text,
    
  660.         )
    
  661. 
    
  662.     def test_mid_stack_exception_without_traceback(self):
    
  663.         try:
    
  664.             try:
    
  665.                 raise RuntimeError("Inner Oops")
    
  666.             except Exception as exc:
    
  667.                 new_exc = RuntimeError("My context")
    
  668.                 new_exc.__context__ = exc
    
  669.                 raise RuntimeError("Oops") from new_exc
    
  670.         except Exception:
    
  671.             exc_type, exc_value, tb = sys.exc_info()
    
  672.         reporter = ExceptionReporter(None, exc_type, exc_value, tb)
    
  673.         html = reporter.get_traceback_html()
    
  674.         self.assertInHTML("<h1>RuntimeError</h1>", html)
    
  675.         self.assertIn('<pre class="exception_value">Oops</pre>', html)
    
  676.         self.assertIn("<th>Exception Type:</th>", html)
    
  677.         self.assertIn("<th>Exception Value:</th>", html)
    
  678.         self.assertIn("<h2>Traceback ", html)
    
  679.         self.assertInHTML('<li class="frame user">Traceback: None</li>', html)
    
  680.         self.assertIn(
    
  681.             "During handling of the above exception (Inner Oops), another "
    
  682.             "exception occurred:\n  Traceback: None",
    
  683.             html,
    
  684.         )
    
  685. 
    
  686.         text = reporter.get_traceback_text()
    
  687.         self.assertIn("Exception Type: RuntimeError", text)
    
  688.         self.assertIn("Exception Value: Oops", text)
    
  689.         self.assertIn("Traceback (most recent call last):", text)
    
  690.         self.assertIn(
    
  691.             "During handling of the above exception (Inner Oops), another "
    
  692.             "exception occurred:\n  Traceback: None",
    
  693.             text,
    
  694.         )
    
  695. 
    
  696.     def test_reporting_of_nested_exceptions(self):
    
  697.         request = self.rf.get("/test_view/")
    
  698.         try:
    
  699.             try:
    
  700.                 raise AttributeError(mark_safe("<p>Top level</p>"))
    
  701.             except AttributeError as explicit:
    
  702.                 try:
    
  703.                     raise ValueError(mark_safe("<p>Second exception</p>")) from explicit
    
  704.                 except ValueError:
    
  705.                     raise IndexError(mark_safe("<p>Final exception</p>"))
    
  706.         except Exception:
    
  707.             # Custom exception handler, just pass it into ExceptionReporter
    
  708.             exc_type, exc_value, tb = sys.exc_info()
    
  709. 
    
  710.         explicit_exc = (
    
  711.             "The above exception ({0}) was the direct cause of the following exception:"
    
  712.         )
    
  713.         implicit_exc = (
    
  714.             "During handling of the above exception ({0}), another exception occurred:"
    
  715.         )
    
  716. 
    
  717.         reporter = ExceptionReporter(request, exc_type, exc_value, tb)
    
  718.         html = reporter.get_traceback_html()
    
  719.         # Both messages are twice on page -- one rendered as html,
    
  720.         # one as plain text (for pastebin)
    
  721.         self.assertEqual(
    
  722.             2, html.count(explicit_exc.format("&lt;p&gt;Top level&lt;/p&gt;"))
    
  723.         )
    
  724.         self.assertEqual(
    
  725.             2, html.count(implicit_exc.format("&lt;p&gt;Second exception&lt;/p&gt;"))
    
  726.         )
    
  727.         self.assertEqual(10, html.count("&lt;p&gt;Final exception&lt;/p&gt;"))
    
  728. 
    
  729.         text = reporter.get_traceback_text()
    
  730.         self.assertIn(explicit_exc.format("<p>Top level</p>"), text)
    
  731.         self.assertIn(implicit_exc.format("<p>Second exception</p>"), text)
    
  732.         self.assertEqual(3, text.count("<p>Final exception</p>"))
    
  733. 
    
  734.     def test_reporting_frames_without_source(self):
    
  735.         try:
    
  736.             source = "def funcName():\n    raise Error('Whoops')\nfuncName()"
    
  737.             namespace = {}
    
  738.             code = compile(source, "generated", "exec")
    
  739.             exec(code, namespace)
    
  740.         except Exception:
    
  741.             exc_type, exc_value, tb = sys.exc_info()
    
  742.         request = self.rf.get("/test_view/")
    
  743.         reporter = ExceptionReporter(request, exc_type, exc_value, tb)
    
  744.         frames = reporter.get_traceback_frames()
    
  745.         last_frame = frames[-1]
    
  746.         self.assertEqual(last_frame["context_line"], "<source code not available>")
    
  747.         self.assertEqual(last_frame["filename"], "generated")
    
  748.         self.assertEqual(last_frame["function"], "funcName")
    
  749.         self.assertEqual(last_frame["lineno"], 2)
    
  750.         html = reporter.get_traceback_html()
    
  751.         self.assertIn(
    
  752.             '<span class="fname">generated</span>, line 2, in funcName',
    
  753.             html,
    
  754.         )
    
  755.         self.assertIn(
    
  756.             '<code class="fname">generated</code>, line 2, in funcName',
    
  757.             html,
    
  758.         )
    
  759.         self.assertIn(
    
  760.             '"generated", line 2, in funcName\n    &lt;source code not available&gt;',
    
  761.             html,
    
  762.         )
    
  763.         text = reporter.get_traceback_text()
    
  764.         self.assertIn(
    
  765.             '"generated", line 2, in funcName\n    <source code not available>',
    
  766.             text,
    
  767.         )
    
  768. 
    
  769.     def test_reporting_frames_source_not_match(self):
    
  770.         try:
    
  771.             source = "def funcName():\n    raise Error('Whoops')\nfuncName()"
    
  772.             namespace = {}
    
  773.             code = compile(source, "generated", "exec")
    
  774.             exec(code, namespace)
    
  775.         except Exception:
    
  776.             exc_type, exc_value, tb = sys.exc_info()
    
  777.         with mock.patch(
    
  778.             "django.views.debug.ExceptionReporter._get_source",
    
  779.             return_value=["wrong source"],
    
  780.         ):
    
  781.             request = self.rf.get("/test_view/")
    
  782.             reporter = ExceptionReporter(request, exc_type, exc_value, tb)
    
  783.             frames = reporter.get_traceback_frames()
    
  784.             last_frame = frames[-1]
    
  785.             self.assertEqual(last_frame["context_line"], "<source code not available>")
    
  786.             self.assertEqual(last_frame["filename"], "generated")
    
  787.             self.assertEqual(last_frame["function"], "funcName")
    
  788.             self.assertEqual(last_frame["lineno"], 2)
    
  789.             html = reporter.get_traceback_html()
    
  790.             self.assertIn(
    
  791.                 '<span class="fname">generated</span>, line 2, in funcName',
    
  792.                 html,
    
  793.             )
    
  794.             self.assertIn(
    
  795.                 '<code class="fname">generated</code>, line 2, in funcName',
    
  796.                 html,
    
  797.             )
    
  798.             self.assertIn(
    
  799.                 '"generated", line 2, in funcName\n'
    
  800.                 "    &lt;source code not available&gt;",
    
  801.                 html,
    
  802.             )
    
  803.             text = reporter.get_traceback_text()
    
  804.             self.assertIn(
    
  805.                 '"generated", line 2, in funcName\n    <source code not available>',
    
  806.                 text,
    
  807.             )
    
  808. 
    
  809.     def test_reporting_frames_for_cyclic_reference(self):
    
  810.         try:
    
  811. 
    
  812.             def test_func():
    
  813.                 try:
    
  814.                     raise RuntimeError("outer") from RuntimeError("inner")
    
  815.                 except RuntimeError as exc:
    
  816.                     raise exc.__cause__
    
  817. 
    
  818.             test_func()
    
  819.         except Exception:
    
  820.             exc_type, exc_value, tb = sys.exc_info()
    
  821.         request = self.rf.get("/test_view/")
    
  822.         reporter = ExceptionReporter(request, exc_type, exc_value, tb)
    
  823. 
    
  824.         def generate_traceback_frames(*args, **kwargs):
    
  825.             nonlocal tb_frames
    
  826.             tb_frames = reporter.get_traceback_frames()
    
  827. 
    
  828.         tb_frames = None
    
  829.         tb_generator = threading.Thread(target=generate_traceback_frames, daemon=True)
    
  830.         msg = (
    
  831.             "Cycle in the exception chain detected: exception 'inner' "
    
  832.             "encountered again."
    
  833.         )
    
  834.         with self.assertWarnsMessage(ExceptionCycleWarning, msg):
    
  835.             tb_generator.start()
    
  836.         tb_generator.join(timeout=5)
    
  837.         if tb_generator.is_alive():
    
  838.             # tb_generator is a daemon that runs until the main thread/process
    
  839.             # exits. This is resource heavy when running the full test suite.
    
  840.             # Setting the following values to None makes
    
  841.             # reporter.get_traceback_frames() exit early.
    
  842.             exc_value.__traceback__ = exc_value.__context__ = exc_value.__cause__ = None
    
  843.             tb_generator.join()
    
  844.             self.fail("Cyclic reference in Exception Reporter.get_traceback_frames()")
    
  845.         if tb_frames is None:
    
  846.             # can happen if the thread generating traceback got killed
    
  847.             # or exception while generating the traceback
    
  848.             self.fail("Traceback generation failed")
    
  849.         last_frame = tb_frames[-1]
    
  850.         self.assertIn("raise exc.__cause__", last_frame["context_line"])
    
  851.         self.assertEqual(last_frame["filename"], __file__)
    
  852.         self.assertEqual(last_frame["function"], "test_func")
    
  853. 
    
  854.     def test_request_and_message(self):
    
  855.         "A message can be provided in addition to a request"
    
  856.         request = self.rf.get("/test_view/")
    
  857.         reporter = ExceptionReporter(request, None, "I'm a little teapot", None)
    
  858.         html = reporter.get_traceback_html()
    
  859.         self.assertInHTML("<h1>Report at /test_view/</h1>", html)
    
  860.         self.assertIn(
    
  861.             '<pre class="exception_value">I&#x27;m a little teapot</pre>', html
    
  862.         )
    
  863.         self.assertIn("<th>Request Method:</th>", html)
    
  864.         self.assertIn("<th>Request URL:</th>", html)
    
  865.         self.assertNotIn("<th>Exception Type:</th>", html)
    
  866.         self.assertNotIn("<th>Exception Value:</th>", html)
    
  867.         self.assertIn("<h2>Traceback ", html)
    
  868.         self.assertIn("<h2>Request information</h2>", html)
    
  869.         self.assertNotIn("<p>Request data not supplied</p>", html)
    
  870. 
    
  871.     def test_message_only(self):
    
  872.         reporter = ExceptionReporter(None, None, "I'm a little teapot", None)
    
  873.         html = reporter.get_traceback_html()
    
  874.         self.assertInHTML("<h1>Report</h1>", html)
    
  875.         self.assertIn(
    
  876.             '<pre class="exception_value">I&#x27;m a little teapot</pre>', html
    
  877.         )
    
  878.         self.assertNotIn("<th>Request Method:</th>", html)
    
  879.         self.assertNotIn("<th>Request URL:</th>", html)
    
  880.         self.assertNotIn("<th>Exception Type:</th>", html)
    
  881.         self.assertNotIn("<th>Exception Value:</th>", html)
    
  882.         self.assertIn("<h2>Traceback ", html)
    
  883.         self.assertIn("<h2>Request information</h2>", html)
    
  884.         self.assertIn("<p>Request data not supplied</p>", html)
    
  885. 
    
  886.     def test_non_utf8_values_handling(self):
    
  887.         "Non-UTF-8 exceptions/values should not make the output generation choke."
    
  888.         try:
    
  889. 
    
  890.             class NonUtf8Output(Exception):
    
  891.                 def __repr__(self):
    
  892.                     return b"EXC\xe9EXC"
    
  893. 
    
  894.             somevar = b"VAL\xe9VAL"  # NOQA
    
  895.             raise NonUtf8Output()
    
  896.         except Exception:
    
  897.             exc_type, exc_value, tb = sys.exc_info()
    
  898.         reporter = ExceptionReporter(None, exc_type, exc_value, tb)
    
  899.         html = reporter.get_traceback_html()
    
  900.         self.assertIn("VAL\\xe9VAL", html)
    
  901.         self.assertIn("EXC\\xe9EXC", html)
    
  902. 
    
  903.     def test_local_variable_escaping(self):
    
  904.         """Safe strings in local variables are escaped."""
    
  905.         try:
    
  906.             local = mark_safe("<p>Local variable</p>")
    
  907.             raise ValueError(local)
    
  908.         except Exception:
    
  909.             exc_type, exc_value, tb = sys.exc_info()
    
  910.         html = ExceptionReporter(None, exc_type, exc_value, tb).get_traceback_html()
    
  911.         self.assertIn(
    
  912.             '<td class="code"><pre>&#x27;&lt;p&gt;Local variable&lt;/p&gt;&#x27;</pre>'
    
  913.             "</td>",
    
  914.             html,
    
  915.         )
    
  916. 
    
  917.     def test_unprintable_values_handling(self):
    
  918.         "Unprintable values should not make the output generation choke."
    
  919.         try:
    
  920. 
    
  921.             class OomOutput:
    
  922.                 def __repr__(self):
    
  923.                     raise MemoryError("OOM")
    
  924. 
    
  925.             oomvalue = OomOutput()  # NOQA
    
  926.             raise ValueError()
    
  927.         except Exception:
    
  928.             exc_type, exc_value, tb = sys.exc_info()
    
  929.         reporter = ExceptionReporter(None, exc_type, exc_value, tb)
    
  930.         html = reporter.get_traceback_html()
    
  931.         self.assertIn('<td class="code"><pre>Error in formatting', html)
    
  932. 
    
  933.     def test_too_large_values_handling(self):
    
  934.         "Large values should not create a large HTML."
    
  935.         large = 256 * 1024
    
  936.         repr_of_str_adds = len(repr(""))
    
  937.         try:
    
  938. 
    
  939.             class LargeOutput:
    
  940.                 def __repr__(self):
    
  941.                     return repr("A" * large)
    
  942. 
    
  943.             largevalue = LargeOutput()  # NOQA
    
  944.             raise ValueError()
    
  945.         except Exception:
    
  946.             exc_type, exc_value, tb = sys.exc_info()
    
  947.         reporter = ExceptionReporter(None, exc_type, exc_value, tb)
    
  948.         html = reporter.get_traceback_html()
    
  949.         self.assertEqual(len(html) // 1024 // 128, 0)  # still fit in 128Kb
    
  950.         self.assertIn(
    
  951.             "&lt;trimmed %d bytes string&gt;" % (large + repr_of_str_adds,), html
    
  952.         )
    
  953. 
    
  954.     def test_encoding_error(self):
    
  955.         """
    
  956.         A UnicodeError displays a portion of the problematic string. HTML in
    
  957.         safe strings is escaped.
    
  958.         """
    
  959.         try:
    
  960.             mark_safe("abcdefghijkl<p>mnὀp</p>qrstuwxyz").encode("ascii")
    
  961.         except Exception:
    
  962.             exc_type, exc_value, tb = sys.exc_info()
    
  963.         reporter = ExceptionReporter(None, exc_type, exc_value, tb)
    
  964.         html = reporter.get_traceback_html()
    
  965.         self.assertIn("<h2>Unicode error hint</h2>", html)
    
  966.         self.assertIn("The string that could not be encoded/decoded was: ", html)
    
  967.         self.assertIn("<strong>&lt;p&gt;mnὀp&lt;/p&gt;</strong>", html)
    
  968. 
    
  969.     def test_unfrozen_importlib(self):
    
  970.         """
    
  971.         importlib is not a frozen app, but its loader thinks it's frozen which
    
  972.         results in an ImportError. Refs #21443.
    
  973.         """
    
  974.         try:
    
  975.             request = self.rf.get("/test_view/")
    
  976.             importlib.import_module("abc.def.invalid.name")
    
  977.         except Exception:
    
  978.             exc_type, exc_value, tb = sys.exc_info()
    
  979.         reporter = ExceptionReporter(request, exc_type, exc_value, tb)
    
  980.         html = reporter.get_traceback_html()
    
  981.         self.assertInHTML("<h1>ModuleNotFoundError at /test_view/</h1>", html)
    
  982. 
    
  983.     def test_ignore_traceback_evaluation_exceptions(self):
    
  984.         """
    
  985.         Don't trip over exceptions generated by crafted objects when
    
  986.         evaluating them while cleansing (#24455).
    
  987.         """
    
  988. 
    
  989.         class BrokenEvaluation(Exception):
    
  990.             pass
    
  991. 
    
  992.         def broken_setup():
    
  993.             raise BrokenEvaluation
    
  994. 
    
  995.         request = self.rf.get("/test_view/")
    
  996.         broken_lazy = SimpleLazyObject(broken_setup)
    
  997.         try:
    
  998.             bool(broken_lazy)
    
  999.         except BrokenEvaluation:
    
  1000.             exc_type, exc_value, tb = sys.exc_info()
    
  1001. 
    
  1002.         self.assertIn(
    
  1003.             "BrokenEvaluation",
    
  1004.             ExceptionReporter(request, exc_type, exc_value, tb).get_traceback_html(),
    
  1005.             "Evaluation exception reason not mentioned in traceback",
    
  1006.         )
    
  1007. 
    
  1008.     @override_settings(ALLOWED_HOSTS="example.com")
    
  1009.     def test_disallowed_host(self):
    
  1010.         "An exception report can be generated even for a disallowed host."
    
  1011.         request = self.rf.get("/", HTTP_HOST="evil.com")
    
  1012.         reporter = ExceptionReporter(request, None, None, None)
    
  1013.         html = reporter.get_traceback_html()
    
  1014.         self.assertIn("http://evil.com/", html)
    
  1015. 
    
  1016.     def test_request_with_items_key(self):
    
  1017.         """
    
  1018.         An exception report can be generated for requests with 'items' in
    
  1019.         request GET, POST, FILES, or COOKIES QueryDicts.
    
  1020.         """
    
  1021.         value = '<td>items</td><td class="code"><pre>&#x27;Oops&#x27;</pre></td>'
    
  1022.         # GET
    
  1023.         request = self.rf.get("/test_view/?items=Oops")
    
  1024.         reporter = ExceptionReporter(request, None, None, None)
    
  1025.         html = reporter.get_traceback_html()
    
  1026.         self.assertInHTML(value, html)
    
  1027.         # POST
    
  1028.         request = self.rf.post("/test_view/", data={"items": "Oops"})
    
  1029.         reporter = ExceptionReporter(request, None, None, None)
    
  1030.         html = reporter.get_traceback_html()
    
  1031.         self.assertInHTML(value, html)
    
  1032.         # FILES
    
  1033.         fp = StringIO("filecontent")
    
  1034.         request = self.rf.post("/test_view/", data={"name": "filename", "items": fp})
    
  1035.         reporter = ExceptionReporter(request, None, None, None)
    
  1036.         html = reporter.get_traceback_html()
    
  1037.         self.assertInHTML(
    
  1038.             '<td>items</td><td class="code"><pre>&lt;InMemoryUploadedFile: '
    
  1039.             "items (application/octet-stream)&gt;</pre></td>",
    
  1040.             html,
    
  1041.         )
    
  1042.         # COOKIES
    
  1043.         rf = RequestFactory()
    
  1044.         rf.cookies["items"] = "Oops"
    
  1045.         request = rf.get("/test_view/")
    
  1046.         reporter = ExceptionReporter(request, None, None, None)
    
  1047.         html = reporter.get_traceback_html()
    
  1048.         self.assertInHTML(
    
  1049.             '<td>items</td><td class="code"><pre>&#x27;Oops&#x27;</pre></td>', html
    
  1050.         )
    
  1051. 
    
  1052.     def test_exception_fetching_user(self):
    
  1053.         """
    
  1054.         The error page can be rendered if the current user can't be retrieved
    
  1055.         (such as when the database is unavailable).
    
  1056.         """
    
  1057. 
    
  1058.         class ExceptionUser:
    
  1059.             def __str__(self):
    
  1060.                 raise Exception()
    
  1061. 
    
  1062.         request = self.rf.get("/test_view/")
    
  1063.         request.user = ExceptionUser()
    
  1064. 
    
  1065.         try:
    
  1066.             raise ValueError("Oops")
    
  1067.         except ValueError:
    
  1068.             exc_type, exc_value, tb = sys.exc_info()
    
  1069. 
    
  1070.         reporter = ExceptionReporter(request, exc_type, exc_value, tb)
    
  1071.         html = reporter.get_traceback_html()
    
  1072.         self.assertInHTML("<h1>ValueError at /test_view/</h1>", html)
    
  1073.         self.assertIn('<pre class="exception_value">Oops</pre>', html)
    
  1074.         self.assertIn('<h3 id="user-info">USER</h3>', html)
    
  1075.         self.assertIn("<p>[unable to retrieve the current user]</p>", html)
    
  1076. 
    
  1077.         text = reporter.get_traceback_text()
    
  1078.         self.assertIn("USER: [unable to retrieve the current user]", text)
    
  1079. 
    
  1080.     def test_template_encoding(self):
    
  1081.         """
    
  1082.         The templates are loaded directly, not via a template loader, and
    
  1083.         should be opened as utf-8 charset as is the default specified on
    
  1084.         template engines.
    
  1085.         """
    
  1086.         reporter = ExceptionReporter(None, None, None, None)
    
  1087.         with mock.patch.object(DebugPath, "open") as m:
    
  1088.             reporter.get_traceback_html()
    
  1089.             m.assert_called_once_with(encoding="utf-8")
    
  1090.             m.reset_mock()
    
  1091.             reporter.get_traceback_text()
    
  1092.             m.assert_called_once_with(encoding="utf-8")
    
  1093. 
    
  1094.     @override_settings(ALLOWED_HOSTS=["example.com"])
    
  1095.     def test_get_raw_insecure_uri(self):
    
  1096.         factory = RequestFactory(HTTP_HOST="evil.com")
    
  1097.         tests = [
    
  1098.             ("////absolute-uri", "http://evil.com//absolute-uri"),
    
  1099.             ("/?foo=bar", "http://evil.com/?foo=bar"),
    
  1100.             ("/path/with:colons", "http://evil.com/path/with:colons"),
    
  1101.         ]
    
  1102.         for url, expected in tests:
    
  1103.             with self.subTest(url=url):
    
  1104.                 request = factory.get(url)
    
  1105.                 reporter = ExceptionReporter(request, None, None, None)
    
  1106.                 self.assertEqual(reporter._get_raw_insecure_uri(), expected)
    
  1107. 
    
  1108. 
    
  1109. class PlainTextReportTests(SimpleTestCase):
    
  1110.     rf = RequestFactory()
    
  1111. 
    
  1112.     def test_request_and_exception(self):
    
  1113.         "A simple exception report can be generated"
    
  1114.         try:
    
  1115.             request = self.rf.get("/test_view/")
    
  1116.             request.user = User()
    
  1117.             raise ValueError("Can't find my keys")
    
  1118.         except ValueError:
    
  1119.             exc_type, exc_value, tb = sys.exc_info()
    
  1120.         reporter = ExceptionReporter(request, exc_type, exc_value, tb)
    
  1121.         text = reporter.get_traceback_text()
    
  1122.         self.assertIn("ValueError at /test_view/", text)
    
  1123.         self.assertIn("Can't find my keys", text)
    
  1124.         self.assertIn("Request Method:", text)
    
  1125.         self.assertIn("Request URL:", text)
    
  1126.         self.assertIn("USER: jacob", text)
    
  1127.         self.assertIn("Exception Type:", text)
    
  1128.         self.assertIn("Exception Value:", text)
    
  1129.         self.assertIn("Traceback (most recent call last):", text)
    
  1130.         self.assertIn("Request information:", text)
    
  1131.         self.assertNotIn("Request data not supplied", text)
    
  1132. 
    
  1133.     def test_no_request(self):
    
  1134.         "An exception report can be generated without request"
    
  1135.         try:
    
  1136.             raise ValueError("Can't find my keys")
    
  1137.         except ValueError:
    
  1138.             exc_type, exc_value, tb = sys.exc_info()
    
  1139.         reporter = ExceptionReporter(None, exc_type, exc_value, tb)
    
  1140.         text = reporter.get_traceback_text()
    
  1141.         self.assertIn("ValueError", text)
    
  1142.         self.assertIn("Can't find my keys", text)
    
  1143.         self.assertNotIn("Request Method:", text)
    
  1144.         self.assertNotIn("Request URL:", text)
    
  1145.         self.assertNotIn("USER:", text)
    
  1146.         self.assertIn("Exception Type:", text)
    
  1147.         self.assertIn("Exception Value:", text)
    
  1148.         self.assertIn("Traceback (most recent call last):", text)
    
  1149.         self.assertIn("Request data not supplied", text)
    
  1150. 
    
  1151.     def test_no_exception(self):
    
  1152.         "An exception report can be generated for just a request"
    
  1153.         request = self.rf.get("/test_view/")
    
  1154.         reporter = ExceptionReporter(request, None, None, None)
    
  1155.         reporter.get_traceback_text()
    
  1156. 
    
  1157.     def test_request_and_message(self):
    
  1158.         "A message can be provided in addition to a request"
    
  1159.         request = self.rf.get("/test_view/")
    
  1160.         reporter = ExceptionReporter(request, None, "I'm a little teapot", None)
    
  1161.         reporter.get_traceback_text()
    
  1162. 
    
  1163.     @override_settings(DEBUG=True)
    
  1164.     def test_template_exception(self):
    
  1165.         request = self.rf.get("/test_view/")
    
  1166.         try:
    
  1167.             render(request, "debug/template_error.html")
    
  1168.         except Exception:
    
  1169.             exc_type, exc_value, tb = sys.exc_info()
    
  1170.         reporter = ExceptionReporter(request, exc_type, exc_value, tb)
    
  1171.         text = reporter.get_traceback_text()
    
  1172.         templ_path = Path(
    
  1173.             Path(__file__).parents[1], "templates", "debug", "template_error.html"
    
  1174.         )
    
  1175.         self.assertIn(
    
  1176.             "Template error:\n"
    
  1177.             "In template %(path)s, error at line 2\n"
    
  1178.             "   'cycle' tag requires at least two arguments\n"
    
  1179.             "   1 : Template with error:\n"
    
  1180.             "   2 :  {%% cycle %%} \n"
    
  1181.             "   3 : " % {"path": templ_path},
    
  1182.             text,
    
  1183.         )
    
  1184. 
    
  1185.     def test_request_with_items_key(self):
    
  1186.         """
    
  1187.         An exception report can be generated for requests with 'items' in
    
  1188.         request GET, POST, FILES, or COOKIES QueryDicts.
    
  1189.         """
    
  1190.         # GET
    
  1191.         request = self.rf.get("/test_view/?items=Oops")
    
  1192.         reporter = ExceptionReporter(request, None, None, None)
    
  1193.         text = reporter.get_traceback_text()
    
  1194.         self.assertIn("items = 'Oops'", text)
    
  1195.         # POST
    
  1196.         request = self.rf.post("/test_view/", data={"items": "Oops"})
    
  1197.         reporter = ExceptionReporter(request, None, None, None)
    
  1198.         text = reporter.get_traceback_text()
    
  1199.         self.assertIn("items = 'Oops'", text)
    
  1200.         # FILES
    
  1201.         fp = StringIO("filecontent")
    
  1202.         request = self.rf.post("/test_view/", data={"name": "filename", "items": fp})
    
  1203.         reporter = ExceptionReporter(request, None, None, None)
    
  1204.         text = reporter.get_traceback_text()
    
  1205.         self.assertIn("items = <InMemoryUploadedFile:", text)
    
  1206.         # COOKIES
    
  1207.         rf = RequestFactory()
    
  1208.         rf.cookies["items"] = "Oops"
    
  1209.         request = rf.get("/test_view/")
    
  1210.         reporter = ExceptionReporter(request, None, None, None)
    
  1211.         text = reporter.get_traceback_text()
    
  1212.         self.assertIn("items = 'Oops'", text)
    
  1213. 
    
  1214.     def test_message_only(self):
    
  1215.         reporter = ExceptionReporter(None, None, "I'm a little teapot", None)
    
  1216.         reporter.get_traceback_text()
    
  1217. 
    
  1218.     @override_settings(ALLOWED_HOSTS="example.com")
    
  1219.     def test_disallowed_host(self):
    
  1220.         "An exception report can be generated even for a disallowed host."
    
  1221.         request = self.rf.get("/", HTTP_HOST="evil.com")
    
  1222.         reporter = ExceptionReporter(request, None, None, None)
    
  1223.         text = reporter.get_traceback_text()
    
  1224.         self.assertIn("http://evil.com/", text)
    
  1225. 
    
  1226. 
    
  1227. class ExceptionReportTestMixin:
    
  1228.     # Mixin used in the ExceptionReporterFilterTests and
    
  1229.     # AjaxResponseExceptionReporterFilter tests below
    
  1230.     breakfast_data = {
    
  1231.         "sausage-key": "sausage-value",
    
  1232.         "baked-beans-key": "baked-beans-value",
    
  1233.         "hash-brown-key": "hash-brown-value",
    
  1234.         "bacon-key": "bacon-value",
    
  1235.     }
    
  1236. 
    
  1237.     def verify_unsafe_response(
    
  1238.         self, view, check_for_vars=True, check_for_POST_params=True
    
  1239.     ):
    
  1240.         """
    
  1241.         Asserts that potentially sensitive info are displayed in the response.
    
  1242.         """
    
  1243.         request = self.rf.post("/some_url/", self.breakfast_data)
    
  1244.         response = view(request)
    
  1245.         if check_for_vars:
    
  1246.             # All variables are shown.
    
  1247.             self.assertContains(response, "cooked_eggs", status_code=500)
    
  1248.             self.assertContains(response, "scrambled", status_code=500)
    
  1249.             self.assertContains(response, "sauce", status_code=500)
    
  1250.             self.assertContains(response, "worcestershire", status_code=500)
    
  1251.         if check_for_POST_params:
    
  1252.             for k, v in self.breakfast_data.items():
    
  1253.                 # All POST parameters are shown.
    
  1254.                 self.assertContains(response, k, status_code=500)
    
  1255.                 self.assertContains(response, v, status_code=500)
    
  1256. 
    
  1257.     def verify_safe_response(
    
  1258.         self, view, check_for_vars=True, check_for_POST_params=True
    
  1259.     ):
    
  1260.         """
    
  1261.         Asserts that certain sensitive info are not displayed in the response.
    
  1262.         """
    
  1263.         request = self.rf.post("/some_url/", self.breakfast_data)
    
  1264.         response = view(request)
    
  1265.         if check_for_vars:
    
  1266.             # Non-sensitive variable's name and value are shown.
    
  1267.             self.assertContains(response, "cooked_eggs", status_code=500)
    
  1268.             self.assertContains(response, "scrambled", status_code=500)
    
  1269.             # Sensitive variable's name is shown but not its value.
    
  1270.             self.assertContains(response, "sauce", status_code=500)
    
  1271.             self.assertNotContains(response, "worcestershire", status_code=500)
    
  1272.         if check_for_POST_params:
    
  1273.             for k in self.breakfast_data:
    
  1274.                 # All POST parameters' names are shown.
    
  1275.                 self.assertContains(response, k, status_code=500)
    
  1276.             # Non-sensitive POST parameters' values are shown.
    
  1277.             self.assertContains(response, "baked-beans-value", status_code=500)
    
  1278.             self.assertContains(response, "hash-brown-value", status_code=500)
    
  1279.             # Sensitive POST parameters' values are not shown.
    
  1280.             self.assertNotContains(response, "sausage-value", status_code=500)
    
  1281.             self.assertNotContains(response, "bacon-value", status_code=500)
    
  1282. 
    
  1283.     def verify_paranoid_response(
    
  1284.         self, view, check_for_vars=True, check_for_POST_params=True
    
  1285.     ):
    
  1286.         """
    
  1287.         Asserts that no variables or POST parameters are displayed in the response.
    
  1288.         """
    
  1289.         request = self.rf.post("/some_url/", self.breakfast_data)
    
  1290.         response = view(request)
    
  1291.         if check_for_vars:
    
  1292.             # Show variable names but not their values.
    
  1293.             self.assertContains(response, "cooked_eggs", status_code=500)
    
  1294.             self.assertNotContains(response, "scrambled", status_code=500)
    
  1295.             self.assertContains(response, "sauce", status_code=500)
    
  1296.             self.assertNotContains(response, "worcestershire", status_code=500)
    
  1297.         if check_for_POST_params:
    
  1298.             for k, v in self.breakfast_data.items():
    
  1299.                 # All POST parameters' names are shown.
    
  1300.                 self.assertContains(response, k, status_code=500)
    
  1301.                 # No POST parameters' values are shown.
    
  1302.                 self.assertNotContains(response, v, status_code=500)
    
  1303. 
    
  1304.     def verify_unsafe_email(self, view, check_for_POST_params=True):
    
  1305.         """
    
  1306.         Asserts that potentially sensitive info are displayed in the email report.
    
  1307.         """
    
  1308.         with self.settings(ADMINS=[("Admin", "[email protected]")]):
    
  1309.             mail.outbox = []  # Empty outbox
    
  1310.             request = self.rf.post("/some_url/", self.breakfast_data)
    
  1311.             view(request)
    
  1312.             self.assertEqual(len(mail.outbox), 1)
    
  1313.             email = mail.outbox[0]
    
  1314. 
    
  1315.             # Frames vars are never shown in plain text email reports.
    
  1316.             body_plain = str(email.body)
    
  1317.             self.assertNotIn("cooked_eggs", body_plain)
    
  1318.             self.assertNotIn("scrambled", body_plain)
    
  1319.             self.assertNotIn("sauce", body_plain)
    
  1320.             self.assertNotIn("worcestershire", body_plain)
    
  1321. 
    
  1322.             # Frames vars are shown in html email reports.
    
  1323.             body_html = str(email.alternatives[0][0])
    
  1324.             self.assertIn("cooked_eggs", body_html)
    
  1325.             self.assertIn("scrambled", body_html)
    
  1326.             self.assertIn("sauce", body_html)
    
  1327.             self.assertIn("worcestershire", body_html)
    
  1328. 
    
  1329.             if check_for_POST_params:
    
  1330.                 for k, v in self.breakfast_data.items():
    
  1331.                     # All POST parameters are shown.
    
  1332.                     self.assertIn(k, body_plain)
    
  1333.                     self.assertIn(v, body_plain)
    
  1334.                     self.assertIn(k, body_html)
    
  1335.                     self.assertIn(v, body_html)
    
  1336. 
    
  1337.     def verify_safe_email(self, view, check_for_POST_params=True):
    
  1338.         """
    
  1339.         Asserts that certain sensitive info are not displayed in the email report.
    
  1340.         """
    
  1341.         with self.settings(ADMINS=[("Admin", "[email protected]")]):
    
  1342.             mail.outbox = []  # Empty outbox
    
  1343.             request = self.rf.post("/some_url/", self.breakfast_data)
    
  1344.             view(request)
    
  1345.             self.assertEqual(len(mail.outbox), 1)
    
  1346.             email = mail.outbox[0]
    
  1347. 
    
  1348.             # Frames vars are never shown in plain text email reports.
    
  1349.             body_plain = str(email.body)
    
  1350.             self.assertNotIn("cooked_eggs", body_plain)
    
  1351.             self.assertNotIn("scrambled", body_plain)
    
  1352.             self.assertNotIn("sauce", body_plain)
    
  1353.             self.assertNotIn("worcestershire", body_plain)
    
  1354. 
    
  1355.             # Frames vars are shown in html email reports.
    
  1356.             body_html = str(email.alternatives[0][0])
    
  1357.             self.assertIn("cooked_eggs", body_html)
    
  1358.             self.assertIn("scrambled", body_html)
    
  1359.             self.assertIn("sauce", body_html)
    
  1360.             self.assertNotIn("worcestershire", body_html)
    
  1361. 
    
  1362.             if check_for_POST_params:
    
  1363.                 for k in self.breakfast_data:
    
  1364.                     # All POST parameters' names are shown.
    
  1365.                     self.assertIn(k, body_plain)
    
  1366.                 # Non-sensitive POST parameters' values are shown.
    
  1367.                 self.assertIn("baked-beans-value", body_plain)
    
  1368.                 self.assertIn("hash-brown-value", body_plain)
    
  1369.                 self.assertIn("baked-beans-value", body_html)
    
  1370.                 self.assertIn("hash-brown-value", body_html)
    
  1371.                 # Sensitive POST parameters' values are not shown.
    
  1372.                 self.assertNotIn("sausage-value", body_plain)
    
  1373.                 self.assertNotIn("bacon-value", body_plain)
    
  1374.                 self.assertNotIn("sausage-value", body_html)
    
  1375.                 self.assertNotIn("bacon-value", body_html)
    
  1376. 
    
  1377.     def verify_paranoid_email(self, view):
    
  1378.         """
    
  1379.         Asserts that no variables or POST parameters are displayed in the email report.
    
  1380.         """
    
  1381.         with self.settings(ADMINS=[("Admin", "[email protected]")]):
    
  1382.             mail.outbox = []  # Empty outbox
    
  1383.             request = self.rf.post("/some_url/", self.breakfast_data)
    
  1384.             view(request)
    
  1385.             self.assertEqual(len(mail.outbox), 1)
    
  1386.             email = mail.outbox[0]
    
  1387.             # Frames vars are never shown in plain text email reports.
    
  1388.             body = str(email.body)
    
  1389.             self.assertNotIn("cooked_eggs", body)
    
  1390.             self.assertNotIn("scrambled", body)
    
  1391.             self.assertNotIn("sauce", body)
    
  1392.             self.assertNotIn("worcestershire", body)
    
  1393.             for k, v in self.breakfast_data.items():
    
  1394.                 # All POST parameters' names are shown.
    
  1395.                 self.assertIn(k, body)
    
  1396.                 # No POST parameters' values are shown.
    
  1397.                 self.assertNotIn(v, body)
    
  1398. 
    
  1399. 
    
  1400. @override_settings(ROOT_URLCONF="view_tests.urls")
    
  1401. class ExceptionReporterFilterTests(
    
  1402.     ExceptionReportTestMixin, LoggingCaptureMixin, SimpleTestCase
    
  1403. ):
    
  1404.     """
    
  1405.     Sensitive information can be filtered out of error reports (#14614).
    
  1406.     """
    
  1407. 
    
  1408.     rf = RequestFactory()
    
  1409. 
    
  1410.     def test_non_sensitive_request(self):
    
  1411.         """
    
  1412.         Everything (request info and frame variables) can bee seen
    
  1413.         in the default error reports for non-sensitive requests.
    
  1414.         """
    
  1415.         with self.settings(DEBUG=True):
    
  1416.             self.verify_unsafe_response(non_sensitive_view)
    
  1417.             self.verify_unsafe_email(non_sensitive_view)
    
  1418. 
    
  1419.         with self.settings(DEBUG=False):
    
  1420.             self.verify_unsafe_response(non_sensitive_view)
    
  1421.             self.verify_unsafe_email(non_sensitive_view)
    
  1422. 
    
  1423.     def test_sensitive_request(self):
    
  1424.         """
    
  1425.         Sensitive POST parameters and frame variables cannot be
    
  1426.         seen in the default error reports for sensitive requests.
    
  1427.         """
    
  1428.         with self.settings(DEBUG=True):
    
  1429.             self.verify_unsafe_response(sensitive_view)
    
  1430.             self.verify_unsafe_email(sensitive_view)
    
  1431. 
    
  1432.         with self.settings(DEBUG=False):
    
  1433.             self.verify_safe_response(sensitive_view)
    
  1434.             self.verify_safe_email(sensitive_view)
    
  1435. 
    
  1436.     def test_paranoid_request(self):
    
  1437.         """
    
  1438.         No POST parameters and frame variables can be seen in the
    
  1439.         default error reports for "paranoid" requests.
    
  1440.         """
    
  1441.         with self.settings(DEBUG=True):
    
  1442.             self.verify_unsafe_response(paranoid_view)
    
  1443.             self.verify_unsafe_email(paranoid_view)
    
  1444. 
    
  1445.         with self.settings(DEBUG=False):
    
  1446.             self.verify_paranoid_response(paranoid_view)
    
  1447.             self.verify_paranoid_email(paranoid_view)
    
  1448. 
    
  1449.     def test_multivalue_dict_key_error(self):
    
  1450.         """
    
  1451.         #21098 -- Sensitive POST parameters cannot be seen in the
    
  1452.         error reports for if request.POST['nonexistent_key'] throws an error.
    
  1453.         """
    
  1454.         with self.settings(DEBUG=True):
    
  1455.             self.verify_unsafe_response(multivalue_dict_key_error)
    
  1456.             self.verify_unsafe_email(multivalue_dict_key_error)
    
  1457. 
    
  1458.         with self.settings(DEBUG=False):
    
  1459.             self.verify_safe_response(multivalue_dict_key_error)
    
  1460.             self.verify_safe_email(multivalue_dict_key_error)
    
  1461. 
    
  1462.     def test_custom_exception_reporter_filter(self):
    
  1463.         """
    
  1464.         It's possible to assign an exception reporter filter to
    
  1465.         the request to bypass the one set in DEFAULT_EXCEPTION_REPORTER_FILTER.
    
  1466.         """
    
  1467.         with self.settings(DEBUG=True):
    
  1468.             self.verify_unsafe_response(custom_exception_reporter_filter_view)
    
  1469.             self.verify_unsafe_email(custom_exception_reporter_filter_view)
    
  1470. 
    
  1471.         with self.settings(DEBUG=False):
    
  1472.             self.verify_unsafe_response(custom_exception_reporter_filter_view)
    
  1473.             self.verify_unsafe_email(custom_exception_reporter_filter_view)
    
  1474. 
    
  1475.     def test_sensitive_method(self):
    
  1476.         """
    
  1477.         The sensitive_variables decorator works with object methods.
    
  1478.         """
    
  1479.         with self.settings(DEBUG=True):
    
  1480.             self.verify_unsafe_response(
    
  1481.                 sensitive_method_view, check_for_POST_params=False
    
  1482.             )
    
  1483.             self.verify_unsafe_email(sensitive_method_view, check_for_POST_params=False)
    
  1484. 
    
  1485.         with self.settings(DEBUG=False):
    
  1486.             self.verify_safe_response(
    
  1487.                 sensitive_method_view, check_for_POST_params=False
    
  1488.             )
    
  1489.             self.verify_safe_email(sensitive_method_view, check_for_POST_params=False)
    
  1490. 
    
  1491.     def test_sensitive_function_arguments(self):
    
  1492.         """
    
  1493.         Sensitive variables don't leak in the sensitive_variables decorator's
    
  1494.         frame, when those variables are passed as arguments to the decorated
    
  1495.         function.
    
  1496.         """
    
  1497.         with self.settings(DEBUG=True):
    
  1498.             self.verify_unsafe_response(sensitive_args_function_caller)
    
  1499.             self.verify_unsafe_email(sensitive_args_function_caller)
    
  1500. 
    
  1501.         with self.settings(DEBUG=False):
    
  1502.             self.verify_safe_response(
    
  1503.                 sensitive_args_function_caller, check_for_POST_params=False
    
  1504.             )
    
  1505.             self.verify_safe_email(
    
  1506.                 sensitive_args_function_caller, check_for_POST_params=False
    
  1507.             )
    
  1508. 
    
  1509.     def test_sensitive_function_keyword_arguments(self):
    
  1510.         """
    
  1511.         Sensitive variables don't leak in the sensitive_variables decorator's
    
  1512.         frame, when those variables are passed as keyword arguments to the
    
  1513.         decorated function.
    
  1514.         """
    
  1515.         with self.settings(DEBUG=True):
    
  1516.             self.verify_unsafe_response(sensitive_kwargs_function_caller)
    
  1517.             self.verify_unsafe_email(sensitive_kwargs_function_caller)
    
  1518. 
    
  1519.         with self.settings(DEBUG=False):
    
  1520.             self.verify_safe_response(
    
  1521.                 sensitive_kwargs_function_caller, check_for_POST_params=False
    
  1522.             )
    
  1523.             self.verify_safe_email(
    
  1524.                 sensitive_kwargs_function_caller, check_for_POST_params=False
    
  1525.             )
    
  1526. 
    
  1527.     def test_callable_settings(self):
    
  1528.         """
    
  1529.         Callable settings should not be evaluated in the debug page (#21345).
    
  1530.         """
    
  1531. 
    
  1532.         def callable_setting():
    
  1533.             return "This should not be displayed"
    
  1534. 
    
  1535.         with self.settings(DEBUG=True, FOOBAR=callable_setting):
    
  1536.             response = self.client.get("/raises500/")
    
  1537.             self.assertNotContains(
    
  1538.                 response, "This should not be displayed", status_code=500
    
  1539.             )
    
  1540. 
    
  1541.     def test_callable_settings_forbidding_to_set_attributes(self):
    
  1542.         """
    
  1543.         Callable settings which forbid to set attributes should not break
    
  1544.         the debug page (#23070).
    
  1545.         """
    
  1546. 
    
  1547.         class CallableSettingWithSlots:
    
  1548.             __slots__ = []
    
  1549. 
    
  1550.             def __call__(self):
    
  1551.                 return "This should not be displayed"
    
  1552. 
    
  1553.         with self.settings(DEBUG=True, WITH_SLOTS=CallableSettingWithSlots()):
    
  1554.             response = self.client.get("/raises500/")
    
  1555.             self.assertNotContains(
    
  1556.                 response, "This should not be displayed", status_code=500
    
  1557.             )
    
  1558. 
    
  1559.     def test_dict_setting_with_non_str_key(self):
    
  1560.         """
    
  1561.         A dict setting containing a non-string key should not break the
    
  1562.         debug page (#12744).
    
  1563.         """
    
  1564.         with self.settings(DEBUG=True, FOOBAR={42: None}):
    
  1565.             response = self.client.get("/raises500/")
    
  1566.             self.assertContains(response, "FOOBAR", status_code=500)
    
  1567. 
    
  1568.     def test_sensitive_settings(self):
    
  1569.         """
    
  1570.         The debug page should not show some sensitive settings
    
  1571.         (password, secret key, ...).
    
  1572.         """
    
  1573.         sensitive_settings = [
    
  1574.             "SECRET_KEY",
    
  1575.             "SECRET_KEY_FALLBACKS",
    
  1576.             "PASSWORD",
    
  1577.             "API_KEY",
    
  1578.             "AUTH_TOKEN",
    
  1579.         ]
    
  1580.         for setting in sensitive_settings:
    
  1581.             with self.settings(DEBUG=True, **{setting: "should not be displayed"}):
    
  1582.                 response = self.client.get("/raises500/")
    
  1583.                 self.assertNotContains(
    
  1584.                     response, "should not be displayed", status_code=500
    
  1585.                 )
    
  1586. 
    
  1587.     def test_settings_with_sensitive_keys(self):
    
  1588.         """
    
  1589.         The debug page should filter out some sensitive information found in
    
  1590.         dict settings.
    
  1591.         """
    
  1592.         sensitive_settings = [
    
  1593.             "SECRET_KEY",
    
  1594.             "SECRET_KEY_FALLBACKS",
    
  1595.             "PASSWORD",
    
  1596.             "API_KEY",
    
  1597.             "AUTH_TOKEN",
    
  1598.         ]
    
  1599.         for setting in sensitive_settings:
    
  1600.             FOOBAR = {
    
  1601.                 setting: "should not be displayed",
    
  1602.                 "recursive": {setting: "should not be displayed"},
    
  1603.             }
    
  1604.             with self.settings(DEBUG=True, FOOBAR=FOOBAR):
    
  1605.                 response = self.client.get("/raises500/")
    
  1606.                 self.assertNotContains(
    
  1607.                     response, "should not be displayed", status_code=500
    
  1608.                 )
    
  1609. 
    
  1610.     def test_cleanse_setting_basic(self):
    
  1611.         reporter_filter = SafeExceptionReporterFilter()
    
  1612.         self.assertEqual(reporter_filter.cleanse_setting("TEST", "TEST"), "TEST")
    
  1613.         self.assertEqual(
    
  1614.             reporter_filter.cleanse_setting("PASSWORD", "super_secret"),
    
  1615.             reporter_filter.cleansed_substitute,
    
  1616.         )
    
  1617. 
    
  1618.     def test_cleanse_setting_ignore_case(self):
    
  1619.         reporter_filter = SafeExceptionReporterFilter()
    
  1620.         self.assertEqual(
    
  1621.             reporter_filter.cleanse_setting("password", "super_secret"),
    
  1622.             reporter_filter.cleansed_substitute,
    
  1623.         )
    
  1624. 
    
  1625.     def test_cleanse_setting_recurses_in_dictionary(self):
    
  1626.         reporter_filter = SafeExceptionReporterFilter()
    
  1627.         initial = {"login": "cooper", "password": "secret"}
    
  1628.         self.assertEqual(
    
  1629.             reporter_filter.cleanse_setting("SETTING_NAME", initial),
    
  1630.             {"login": "cooper", "password": reporter_filter.cleansed_substitute},
    
  1631.         )
    
  1632. 
    
  1633.     def test_cleanse_setting_recurses_in_dictionary_with_non_string_key(self):
    
  1634.         reporter_filter = SafeExceptionReporterFilter()
    
  1635.         initial = {("localhost", 8000): {"login": "cooper", "password": "secret"}}
    
  1636.         self.assertEqual(
    
  1637.             reporter_filter.cleanse_setting("SETTING_NAME", initial),
    
  1638.             {
    
  1639.                 ("localhost", 8000): {
    
  1640.                     "login": "cooper",
    
  1641.                     "password": reporter_filter.cleansed_substitute,
    
  1642.                 },
    
  1643.             },
    
  1644.         )
    
  1645. 
    
  1646.     def test_cleanse_setting_recurses_in_list_tuples(self):
    
  1647.         reporter_filter = SafeExceptionReporterFilter()
    
  1648.         initial = [
    
  1649.             {
    
  1650.                 "login": "cooper",
    
  1651.                 "password": "secret",
    
  1652.                 "apps": (
    
  1653.                     {"name": "app1", "api_key": "a06b-c462cffae87a"},
    
  1654.                     {"name": "app2", "api_key": "a9f4-f152e97ad808"},
    
  1655.                 ),
    
  1656.                 "tokens": ["98b37c57-ec62-4e39", "8690ef7d-8004-4916"],
    
  1657.             },
    
  1658.             {"SECRET_KEY": "c4d77c62-6196-4f17-a06b-c462cffae87a"},
    
  1659.         ]
    
  1660.         cleansed = [
    
  1661.             {
    
  1662.                 "login": "cooper",
    
  1663.                 "password": reporter_filter.cleansed_substitute,
    
  1664.                 "apps": (
    
  1665.                     {"name": "app1", "api_key": reporter_filter.cleansed_substitute},
    
  1666.                     {"name": "app2", "api_key": reporter_filter.cleansed_substitute},
    
  1667.                 ),
    
  1668.                 "tokens": reporter_filter.cleansed_substitute,
    
  1669.             },
    
  1670.             {"SECRET_KEY": reporter_filter.cleansed_substitute},
    
  1671.         ]
    
  1672.         self.assertEqual(
    
  1673.             reporter_filter.cleanse_setting("SETTING_NAME", initial),
    
  1674.             cleansed,
    
  1675.         )
    
  1676.         self.assertEqual(
    
  1677.             reporter_filter.cleanse_setting("SETTING_NAME", tuple(initial)),
    
  1678.             tuple(cleansed),
    
  1679.         )
    
  1680. 
    
  1681.     def test_request_meta_filtering(self):
    
  1682.         request = self.rf.get("/", HTTP_SECRET_HEADER="super_secret")
    
  1683.         reporter_filter = SafeExceptionReporterFilter()
    
  1684.         self.assertEqual(
    
  1685.             reporter_filter.get_safe_request_meta(request)["HTTP_SECRET_HEADER"],
    
  1686.             reporter_filter.cleansed_substitute,
    
  1687.         )
    
  1688. 
    
  1689.     def test_exception_report_uses_meta_filtering(self):
    
  1690.         response = self.client.get("/raises500/", HTTP_SECRET_HEADER="super_secret")
    
  1691.         self.assertNotIn(b"super_secret", response.content)
    
  1692.         response = self.client.get(
    
  1693.             "/raises500/",
    
  1694.             HTTP_SECRET_HEADER="super_secret",
    
  1695.             HTTP_ACCEPT="application/json",
    
  1696.         )
    
  1697.         self.assertNotIn(b"super_secret", response.content)
    
  1698. 
    
  1699. 
    
  1700. class CustomExceptionReporterFilter(SafeExceptionReporterFilter):
    
  1701.     cleansed_substitute = "XXXXXXXXXXXXXXXXXXXX"
    
  1702.     hidden_settings = _lazy_re_compile(
    
  1703.         "API|TOKEN|KEY|SECRET|PASS|SIGNATURE|DATABASE_URL", flags=re.I
    
  1704.     )
    
  1705. 
    
  1706. 
    
  1707. @override_settings(
    
  1708.     ROOT_URLCONF="view_tests.urls",
    
  1709.     DEFAULT_EXCEPTION_REPORTER_FILTER="%s.CustomExceptionReporterFilter" % __name__,
    
  1710. )
    
  1711. class CustomExceptionReporterFilterTests(SimpleTestCase):
    
  1712.     def setUp(self):
    
  1713.         get_default_exception_reporter_filter.cache_clear()
    
  1714. 
    
  1715.     def tearDown(self):
    
  1716.         get_default_exception_reporter_filter.cache_clear()
    
  1717. 
    
  1718.     def test_setting_allows_custom_subclass(self):
    
  1719.         self.assertIsInstance(
    
  1720.             get_default_exception_reporter_filter(),
    
  1721.             CustomExceptionReporterFilter,
    
  1722.         )
    
  1723. 
    
  1724.     def test_cleansed_substitute_override(self):
    
  1725.         reporter_filter = get_default_exception_reporter_filter()
    
  1726.         self.assertEqual(
    
  1727.             reporter_filter.cleanse_setting("password", "super_secret"),
    
  1728.             reporter_filter.cleansed_substitute,
    
  1729.         )
    
  1730. 
    
  1731.     def test_hidden_settings_override(self):
    
  1732.         reporter_filter = get_default_exception_reporter_filter()
    
  1733.         self.assertEqual(
    
  1734.             reporter_filter.cleanse_setting("database_url", "super_secret"),
    
  1735.             reporter_filter.cleansed_substitute,
    
  1736.         )
    
  1737. 
    
  1738. 
    
  1739. class NonHTMLResponseExceptionReporterFilter(
    
  1740.     ExceptionReportTestMixin, LoggingCaptureMixin, SimpleTestCase
    
  1741. ):
    
  1742.     """
    
  1743.     Sensitive information can be filtered out of error reports.
    
  1744. 
    
  1745.     The plain text 500 debug-only error page is served when it has been
    
  1746.     detected the request doesn't accept HTML content. Don't check for
    
  1747.     (non)existence of frames vars in the traceback information section of the
    
  1748.     response content because they're not included in these error pages.
    
  1749.     Refs #14614.
    
  1750.     """
    
  1751. 
    
  1752.     rf = RequestFactory(HTTP_ACCEPT="application/json")
    
  1753. 
    
  1754.     def test_non_sensitive_request(self):
    
  1755.         """
    
  1756.         Request info can bee seen in the default error reports for
    
  1757.         non-sensitive requests.
    
  1758.         """
    
  1759.         with self.settings(DEBUG=True):
    
  1760.             self.verify_unsafe_response(non_sensitive_view, check_for_vars=False)
    
  1761. 
    
  1762.         with self.settings(DEBUG=False):
    
  1763.             self.verify_unsafe_response(non_sensitive_view, check_for_vars=False)
    
  1764. 
    
  1765.     def test_sensitive_request(self):
    
  1766.         """
    
  1767.         Sensitive POST parameters cannot be seen in the default
    
  1768.         error reports for sensitive requests.
    
  1769.         """
    
  1770.         with self.settings(DEBUG=True):
    
  1771.             self.verify_unsafe_response(sensitive_view, check_for_vars=False)
    
  1772. 
    
  1773.         with self.settings(DEBUG=False):
    
  1774.             self.verify_safe_response(sensitive_view, check_for_vars=False)
    
  1775. 
    
  1776.     def test_paranoid_request(self):
    
  1777.         """
    
  1778.         No POST parameters can be seen in the default error reports
    
  1779.         for "paranoid" requests.
    
  1780.         """
    
  1781.         with self.settings(DEBUG=True):
    
  1782.             self.verify_unsafe_response(paranoid_view, check_for_vars=False)
    
  1783. 
    
  1784.         with self.settings(DEBUG=False):
    
  1785.             self.verify_paranoid_response(paranoid_view, check_for_vars=False)
    
  1786. 
    
  1787.     def test_custom_exception_reporter_filter(self):
    
  1788.         """
    
  1789.         It's possible to assign an exception reporter filter to
    
  1790.         the request to bypass the one set in DEFAULT_EXCEPTION_REPORTER_FILTER.
    
  1791.         """
    
  1792.         with self.settings(DEBUG=True):
    
  1793.             self.verify_unsafe_response(
    
  1794.                 custom_exception_reporter_filter_view, check_for_vars=False
    
  1795.             )
    
  1796. 
    
  1797.         with self.settings(DEBUG=False):
    
  1798.             self.verify_unsafe_response(
    
  1799.                 custom_exception_reporter_filter_view, check_for_vars=False
    
  1800.             )
    
  1801. 
    
  1802.     @override_settings(DEBUG=True, ROOT_URLCONF="view_tests.urls")
    
  1803.     def test_non_html_response_encoding(self):
    
  1804.         response = self.client.get("/raises500/", HTTP_ACCEPT="application/json")
    
  1805.         self.assertEqual(response.headers["Content-Type"], "text/plain; charset=utf-8")
    
  1806. 
    
  1807. 
    
  1808. class DecoratorsTests(SimpleTestCase):
    
  1809.     def test_sensitive_variables_not_called(self):
    
  1810.         msg = (
    
  1811.             "sensitive_variables() must be called to use it as a decorator, "
    
  1812.             "e.g., use @sensitive_variables(), not @sensitive_variables."
    
  1813.         )
    
  1814.         with self.assertRaisesMessage(TypeError, msg):
    
  1815. 
    
  1816.             @sensitive_variables
    
  1817.             def test_func(password):
    
  1818.                 pass
    
  1819. 
    
  1820.     def test_sensitive_post_parameters_not_called(self):
    
  1821.         msg = (
    
  1822.             "sensitive_post_parameters() must be called to use it as a "
    
  1823.             "decorator, e.g., use @sensitive_post_parameters(), not "
    
  1824.             "@sensitive_post_parameters."
    
  1825.         )
    
  1826.         with self.assertRaisesMessage(TypeError, msg):
    
  1827. 
    
  1828.             @sensitive_post_parameters
    
  1829.             def test_func(request):
    
  1830.                 return index_page(request)
    
  1831. 
    
  1832.     def test_sensitive_post_parameters_http_request(self):
    
  1833.         class MyClass:
    
  1834.             @sensitive_post_parameters()
    
  1835.             def a_view(self, request):
    
  1836.                 return HttpResponse()
    
  1837. 
    
  1838.         msg = (
    
  1839.             "sensitive_post_parameters didn't receive an HttpRequest object. "
    
  1840.             "If you are decorating a classmethod, make sure to use "
    
  1841.             "@method_decorator."
    
  1842.         )
    
  1843.         with self.assertRaisesMessage(TypeError, msg):
    
  1844.             MyClass().a_view(HttpRequest())