Skip to content

Commit

Permalink
Fix #1621: Do not crash when encountering unexpected data in the request
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiask committed May 18, 2022
1 parent 99d4884 commit ba505ae
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
8 changes: 5 additions & 3 deletions debug_toolbar/panels/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ def nav_subtitle(self):
def generate_stats(self, request, response):
self.record_stats(
{
"get": get_sorted_request_variable(request.GET),
"post": get_sorted_request_variable(request.POST),
"cookies": get_sorted_request_variable(request.COOKIES),
"get": get_sorted_request_variable(request.GET, source="GET"),
"post": get_sorted_request_variable(request.POST, source="POST"),
"cookies": get_sorted_request_variable(
request.COOKIES, source="COOKIES"
),
}
)

Expand Down
13 changes: 8 additions & 5 deletions debug_toolbar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,17 @@ def getframeinfo(frame, context=1):
return (filename, lineno, frame.f_code.co_name, lines, index)


def get_sorted_request_variable(variable):
def get_sorted_request_variable(variable, *, source):
"""
Get a sorted list of variables from the request data.
"""
if isinstance(variable, dict):
return [(k, variable.get(k)) for k in sorted(variable)]
else:
return [(k, variable.getlist(k)) for k in sorted(variable)]
try:
if isinstance(variable, dict):
return [(k, variable.get(k)) for k in sorted(variable)]
else:
return [(k, variable.getlist(k)) for k in sorted(variable)]
except TypeError:
return [(f"<{source}>", variable)]


def get_stack(context=1):
Expand Down
14 changes: 14 additions & 0 deletions tests/panels/test_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ def test_dict_for_request_in_method_post(self):
self.assertIn("foo", content)
self.assertIn("bar", content)

def test_list_for_request_in_method_post(self):
"""
Verify that the toolbar doesn't crash if request.POST contains unexpected data.
See https://github.com/jazzband/django-debug-toolbar/issues/1621
"""
self.request.POST = [{"a": 1}, {"b": 2}]
response = self.panel.process_request(self.request)
self.panel.generate_stats(self.request, response)
# ensure the panel POST request data is processed correctly.
content = self.panel.content
self.assertIn("&lt;POST&gt;", content)
self.assertIn("[{&#x27;a&#x27;: 1}, {&#x27;b&#x27;: 2}]", content)

def test_namespaced_url(self):
self.request.path = "/admin/login/"
response = self.panel.process_request(self.request)
Expand Down

0 comments on commit ba505ae

Please sign in to comment.