Skip to content

Fixes #2159 -- Do not HTML-escape traces in the cache and profiling panel #2164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion debug_toolbar/templates/debug_toolbar/panels/cache.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ <h4>{% translate "Calls" %}</h4>
</tr>
<tr class="djUnselected djToggleDetails_{{ forloop.counter }}" id="cacheDetails_{{ forloop.counter }}">
<td colspan="1"></td>
<td colspan="5"><pre class="djdt-stack">{{ call.trace }}</pre></td>
<td colspan="5"><pre class="djdt-stack">{{ call.trace|safe }}</pre></td>
</tr>
{% endfor %}
</tbody>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
{% else %}
<span class="djNoToggleSwitch"></span>
{% endif %}
<span class="djdt-stack">{{ call.func_std_string }}</span>
<span class="djdt-stack">{{ call.func_std_string|safe }}</span>
</div>
</td>
<td>{{ call.cumtime|floatformat:3 }}</td>
Expand Down
4 changes: 4 additions & 0 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ def assertValidHTML(self, content):
msg_parts.append(f" {lines[position[0] - 1]}")
raise self.failureException("\n".join(msg_parts))

def reload_stats(self):
data = self.toolbar.store.panel(self.toolbar.request_id, self.panel_id)
self.panel.load_stats_from_store(data)


class BaseTestCase(BaseMixin, TestCase):
pass
Expand Down
3 changes: 3 additions & 0 deletions tests/panels/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,13 @@ def test_insert_content(self):
# ensure the panel does not have content yet.
self.assertNotIn("café", self.panel.content)
self.panel.generate_stats(self.request, response)
self.reload_stats()
# ensure the panel renders correctly.
content = self.panel.content
self.assertIn("café", content)
self.assertValidHTML(content)
# ensure traces aren't escaped
self.assertIn('<span class="djdt-path">', content)

def test_generate_server_timing(self):
self.assertEqual(len(self.panel.calls), 0)
Expand Down
3 changes: 3 additions & 0 deletions tests/panels/test_profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@ def test_insert_content(self):
# ensure the panel does not have content yet.
self.assertNotIn("regular_view", self.panel.content)
self.panel.generate_stats(self.request, response)
self.reload_stats()
# ensure the panel renders correctly.
content = self.panel.content
self.assertIn("regular_view", content)
self.assertIn("render", content)
self.assertValidHTML(content)
# ensure traces aren't escaped
self.assertIn('<span class="djdt-path">', content)

@override_settings(DEBUG_TOOLBAR_CONFIG={"PROFILER_THRESHOLD_RATIO": 1})
def test_cum_time_threshold(self):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.test import TestCase
from django.test.utils import override_settings
from django.utils.safestring import SafeData, mark_safe

from debug_toolbar import store

Expand Down Expand Up @@ -97,6 +98,18 @@ def test_panel(self):
self.store.save_panel("bar", "bar.panel", {"a": 1})
self.assertEqual(self.store.panel("bar", "bar.panel"), {"a": 1})

def test_serialize_safestring(self):
before = {"string": mark_safe("safe")}

self.store.save_panel("bar", "bar.panel", before)
after = self.store.panel("bar", "bar.panel")

self.assertFalse(type(before["string"]) is str)
self.assertTrue(isinstance(before["string"], SafeData))

self.assertTrue(type(after["string"]) is str)
self.assertFalse(isinstance(after["string"], SafeData))


class StubStore(store.BaseStore):
pass
Expand Down