Skip to content

Commit 252f96a

Browse files
committed
Fix #1621: Do not crash when encountering unexpected data in the request
1 parent 4d5df40 commit 252f96a

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

debug_toolbar/panels/request.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,11 @@ def generate_stats(self, request, response):
6161
if hasattr(request, "session"):
6262
self.record_stats(
6363
{
64-
"session": [
65-
(k, request.session.get(k))
66-
for k in sorted(request.session.keys())
67-
]
64+
"session": {
65+
"list": [
66+
(k, request.session.get(k))
67+
for k in sorted(request.session.keys())
68+
]
69+
}
6870
}
6971
)

debug_toolbar/templates/debug_toolbar/panels/request_variables.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% load i18n %}
22

3+
{% if variables.list %}
34
<table>
45
<colgroup>
56
<col class="djdt-width-20">
@@ -12,11 +13,14 @@
1213
</tr>
1314
</thead>
1415
<tbody>
15-
{% for key, value in variables %}
16+
{% for key, value in variables.list %}
1617
<tr>
1718
<td><code>{{ key|pprint }}</code></td>
1819
<td><code>{{ value|pprint }}</code></td>
1920
</tr>
2021
{% endfor %}
2122
</tbody>
2223
</table>
24+
{% elif variables.raw %}
25+
<code>{{ variables.raw }}</code>
26+
{% endif %}

debug_toolbar/utils.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,12 +231,16 @@ def getframeinfo(frame, context=1):
231231

232232
def get_sorted_request_variable(variable):
233233
"""
234-
Get a sorted list of variables from the request data.
234+
Get a data structure for showing a sorted list of variables from the
235+
request data.
235236
"""
236-
if isinstance(variable, dict):
237-
return [(k, variable.get(k)) for k in sorted(variable)]
238-
else:
239-
return [(k, variable.getlist(k)) for k in sorted(variable)]
237+
try:
238+
if isinstance(variable, dict):
239+
return {"list": [(k, variable.get(k)) for k in sorted(variable)]}
240+
else:
241+
return {"list": [(k, variable.getlist(k)) for k in sorted(variable)]}
242+
except TypeError:
243+
return {"raw": variable}
240244

241245

242246
def get_stack(context=1):

docs/changes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Change log
33

44
* Removed third party panels which have been archived on GitHub.
55
* Added Django 4.1a1 to the CI matrix.
6+
* Stopped crashing when ``request.GET`` and ``request.POST`` are neither
7+
dictionaries nor ``QueryDict`` instances. Using anything but ``QueryDict``
8+
instances isn't a valid use of Django but, again, django-debug-toolbar
9+
shouldn't crash.
610

711
3.4.0 (2022-05-03)
812
------------------

tests/panels/test_request.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ def test_dict_for_request_in_method_post(self):
8585
self.assertIn("foo", content)
8686
self.assertIn("bar", content)
8787

88+
def test_list_for_request_in_method_post(self):
89+
"""
90+
Verify that the toolbar doesn't crash if request.POST contains unexpected data.
91+
92+
See https://github.com/jazzband/django-debug-toolbar/issues/1621
93+
"""
94+
self.request.POST = [{"a": 1}, {"b": 2}]
95+
response = self.panel.process_request(self.request)
96+
self.panel.generate_stats(self.request, response)
97+
# ensure the panel POST request data is processed correctly.
98+
content = self.panel.content
99+
self.assertIn("[{&#x27;a&#x27;: 1}, {&#x27;b&#x27;: 2}]", content)
100+
88101
def test_namespaced_url(self):
89102
self.request.path = "/admin/login/"
90103
response = self.panel.process_request(self.request)

0 commit comments

Comments
 (0)