From cf01994aae4815a13fbb2118757a887d24cde95a Mon Sep 17 00:00:00 2001 From: Matthias Kestenholz Date: Tue, 1 Dec 2020 13:04:13 +0100 Subject: [PATCH] History panel: Do not crash when receiving invalid JSON Fixes #1403 --- debug_toolbar/panels/history/panel.py | 5 ++++- docs/changes.rst | 6 ++++++ tests/panels/test_history.py | 3 ++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/debug_toolbar/panels/history/panel.py b/debug_toolbar/panels/history/panel.py index 047c7be30..e80d8c93a 100644 --- a/debug_toolbar/panels/history/panel.py +++ b/debug_toolbar/panels/history/panel.py @@ -49,7 +49,10 @@ def generate_stats(self, request, response): and request.body and request.META.get("CONTENT_TYPE") == "application/json" ): - data = json.loads(request.body) + try: + data = json.loads(request.body) + except ValueError: + pass except RawPostDataException: # It is not guaranteed that we may read the request data (again). data = None diff --git a/docs/changes.rst b/docs/changes.rst index e3124d1cf..fcd8fb3b6 100644 --- a/docs/changes.rst +++ b/docs/changes.rst @@ -5,6 +5,12 @@ unreleased ---------- * Moved CI to GitHub Actions: https://github.com/jazzband/django-debug-toolbar/actions +* Stopped crashing when ``request.GET`` and ``request.POST`` are + dictionaries instead of ``QueryDict`` instances. This isn't a valid + use of Django but django-debug-toolbar shouldn't crash anyway. +* Fixed a crash in the history panel when sending a JSON POST request + with invalid JSON. + 3.2a1 (2020-10-19) ------------------ diff --git a/tests/panels/test_history.py b/tests/panels/test_history.py index 6d65b6e9d..6d592bccf 100644 --- a/tests/panels/test_history.py +++ b/tests/panels/test_history.py @@ -30,7 +30,8 @@ def test_post(self): def test_post_json(self): for data, expected_stats_data in ( ({"foo": "bar"}, {"foo": "bar"}), - ("", {}), + ("", {}), # Empty JSON + ("'", {}), # Invalid JSON ): with self.subTest(data=data): self.request = rf.post(