test_debug.py 81 KB

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