Skip to content

Bug in insertion code, upper and lower string may have different lengths #531

@aaugustin

Description

@aaugustin

Sertaç Ö. Yıldız sent the following email with two attachements:

Subject: django-debug-toolbar issue and patch

I noticed an annoying bug when the page contains “İ” (capital I with dot above) character. I didn’t check with different versions but with python-3.3.3 on MacOSX attached test fails on git master.

0001-Toolbar-insertion-test-for-middleware.patch

From 5f8cedafd456c1ebc913ee649a789c4307961d62 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Serta=C3=A7=20=C3=96=2E=20Y=C4=B1ld=C4=B1z?=
 <sertacyildiz@gmail.com>
Date: Sun, 19 Jan 2014 03:39:27 +0200
Subject: [PATCH 1/2] Toolbar insertion test for middleware
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Byte counts of "İ" and "İ".lower() are different.  If the page html
contains "İ", process_response() method cannot insert the toolbar
content at the correct index, and breaks the page content.

---
 tests/test_integration.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tests/test_integration.py b/tests/test_integration.py
index 56a2d9c..e7a4fe0 100644
--- a/tests/test_integration.py
+++ b/tests/test_integration.py
@@ -80,6 +80,12 @@ class DebugToolbarTestCase(BaseTestCase):
     def test_middleware_response_only(self):
         DebugToolbarMiddleware().process_response(self.request, self.response)

+    def test_middleware_response_insertion(self):
+        resp = regular_view(self.request, "İ")
+        DebugToolbarMiddleware().process_response(self.request, resp)
+        # check toolbar insertion before "</body>"
+        self.assertContains(resp, '</div>\n</body>')
+

 @override_settings(DEBUG=True)
 class DebugToolbarIntegrationTestCase(TestCase):
--
1.8.5.3

0002-Insert-toolbar-with-re.sub.patch

From 9c4b107f4acf267cd7ccd19d83cae31c0350ed59 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Serta=C3=A7=20=C3=96=2E=20Y=C4=B1ld=C4=B1z?=
 <sertacyildiz@gmail.com>
Date: Sun, 19 Jan 2014 03:51:46 +0200
Subject: [PATCH 2/2] Insert toolbar with re.sub()

---
 debug_toolbar/middleware.py | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/debug_toolbar/middleware.py b/debug_toolbar/middleware.py
index b6099d3..d359a6e 100644
--- a/debug_toolbar/middleware.py
+++ b/debug_toolbar/middleware.py
@@ -4,6 +4,7 @@ Debug Toolbar middleware

 from __future__ import absolute_import, unicode_literals

+import re
 import threading

 from django.conf import settings
@@ -106,13 +107,15 @@ class DebugToolbarMiddleware(object):

         # Insert the toolbar in the response.
         content = force_text(response.content, encoding=settings.DEFAULT_CHARSET)
-        try:
-            insert_at = content.lower().rindex(dt_settings.CONFIG['INSERT_BEFORE'].lower())
-        except ValueError:
-            pass
-        else:
+
+        insert_before_pat = "(%s.*)" % dt_settings.CONFIG['INSERT_BEFORE']
+        match = re.search(insert_before_pat, content, flags=re.I)
+        if match:
             toolbar_content = toolbar.render_toolbar()
-            response.content = content[:insert_at] + toolbar_content + content[insert_at:]
+            response.content = re.sub(insert_before_pat,
+                                      toolbar_content + match.group(),
+                                      content,
+                                      flags=re.I)
             if response.get('Content-Length', None):
                 response['Content-Length'] = len(response.content)
         return response
-- 
1.8.5.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions