Skip to content

Commit 08f6e43

Browse files
committed
Refs django-commons#984: Disallow loading arbitrary files using the template_source debugging view
1 parent 898553c commit 08f6e43

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

debug_toolbar/panels/templates/panel.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from django import http
99
from django.conf.urls import url
10+
from django.core import signing
1011
from django.db.models.query import QuerySet, RawQuerySet
1112
from django.template import RequestContext, Template
1213
from django.test.signals import template_rendered
@@ -192,8 +193,10 @@ def generate_stats(self, request, response):
192193
template = template_data.get('template', None)
193194
if hasattr(template, 'origin') and template.origin and template.origin.name:
194195
template.origin_name = template.origin.name
196+
template.origin_hash = signing.dumps(template.origin.name)
195197
else:
196198
template.origin_name = _('No origin')
199+
template.origin_hash = ''
197200
info['template'] = template
198201
# Clean up context for better readability
199202
if self.toolbar.config['SHOW_TEMPLATE_CONTEXT']:

debug_toolbar/panels/templates/views.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import absolute_import, unicode_literals
22

3+
from django.core import signing
34
from django.http import HttpResponseBadRequest
45
from django.template import TemplateDoesNotExist
56
from django.template.engine import Engine
@@ -23,6 +24,10 @@ def template_source(request):
2324
template_origin_name = request.GET.get('template_origin', None)
2425
if template_origin_name is None:
2526
return HttpResponseBadRequest('"template_origin" key is required')
27+
try:
28+
template_origin_name = signing.loads(template_origin_name)
29+
except Exception:
30+
return HttpResponseBadRequest('"template_origin" is invalid')
2631
template_name = request.GET.get('template', template_origin_name)
2732

2833
final_loaders = []

debug_toolbar/templates/debug_toolbar/panels/templates.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ <h4>{% blocktrans count templates|length as template_count %}Template{% plural %
1414
{% if templates %}
1515
<dl>
1616
{% for template in templates %}
17-
<dt><strong><a class="remoteCall toggleTemplate" href="{% url 'djdt:template_source' %}?template={{ template.template.name }}&amp;template_origin={{ template.template.origin_name }}">{{ template.template.name|addslashes }}</a></strong></dt>
17+
<dt><strong><a class="remoteCall toggleTemplate" href="{% url 'djdt:template_source' %}?template={{ template.template.name }}&amp;template_origin={{ template.template.origin_hash }}">{{ template.template.name|addslashes }}</a></strong></dt>
1818
<dd><samp>{{ template.template.origin_name|addslashes }}</samp></dd>
1919
{% if template.context %}
2020
<dd>

0 commit comments

Comments
 (0)