From 5cc825ee1778142b4153b976f0d9498549c20a25 Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Mon, 20 Dec 2021 12:28:21 +0000 Subject: [PATCH] Avoid installing middleware if Content-Encoding is set at all MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The previous check for "gzip" ignored the possibility of other `Content-Encoding` values, such as Brotli’s ``br``, which the middleware could also not touch. Instead, do not attempt to alter the content for *any* `Content-Encoding`. I tested by installing django-brotli in the example app below the toolbar middleware and hitting it with: ``` curl http://localhost:8000/ -H 'Accept-Encoding: br' ``` Without the change, it would crash with: ``` File "/.../debug_toolbar/middleware.py", line 87, in __call__ content = response.content.decode(response.charset) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7 in position 1: invalid continuation byte ``` After, it is fixed. --- debug_toolbar/middleware.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py index e1cf32d23..df4516b4f 100644 --- a/debug_toolbar/middleware.py +++ b/debug_toolbar/middleware.py @@ -78,7 +78,7 @@ def __call__(self, request): content_type = response.get("Content-Type", "").split(";")[0] if ( getattr(response, "streaming", False) - or "gzip" in content_encoding + or content_encoding != "" or content_type not in _HTML_TYPES ): return response