Skip to content

Refs #984: Disallow loading arbitrary files using the template_source… #1008

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 2 commits into from
Nov 15, 2017
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
3 changes: 3 additions & 0 deletions debug_toolbar/panels/templates/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from django import http
from django.conf.urls import url
from django.core import signing
from django.db.models.query import QuerySet, RawQuerySet
from django.template import RequestContext, Template
from django.test.signals import template_rendered
Expand Down Expand Up @@ -192,8 +193,10 @@ def generate_stats(self, request, response):
template = template_data.get('template', None)
if hasattr(template, 'origin') and template.origin and template.origin.name:
template.origin_name = template.origin.name
template.origin_hash = signing.dumps(template.origin.name)
else:
template.origin_name = _('No origin')
template.origin_hash = ''
info['template'] = template
# Clean up context for better readability
if self.toolbar.config['SHOW_TEMPLATE_CONTEXT']:
Expand Down
5 changes: 5 additions & 0 deletions debug_toolbar/panels/templates/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import absolute_import, unicode_literals

from django.core import signing
from django.http import HttpResponseBadRequest
from django.template import TemplateDoesNotExist
from django.template.engine import Engine
Expand All @@ -23,6 +24,10 @@ def template_source(request):
template_origin_name = request.GET.get('template_origin', None)
if template_origin_name is None:
return HttpResponseBadRequest('"template_origin" key is required')
try:
template_origin_name = signing.loads(template_origin_name)
except Exception:
return HttpResponseBadRequest('"template_origin" is invalid')
template_name = request.GET.get('template', template_origin_name)

final_loaders = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h4>{% blocktrans count templates|length as template_count %}Template{% plural %
{% if templates %}
<dl>
{% for template in templates %}
<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>
<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>
<dd><samp>{{ template.template.origin_name|addslashes }}</samp></dd>
{% if template.context %}
<dd>
Expand Down
3 changes: 2 additions & 1 deletion tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import django
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
from django.core import signing
from django.core.checks import Error, run_checks
from django.template.loader import get_template
from django.test import RequestFactory, TestCase
Expand Down Expand Up @@ -143,7 +144,7 @@ def test_template_source_checks_show_toolbar(self):
url = '/__debug__/template_source/'
data = {
'template': template.template.name,
'template_origin': template.template.origin.name
'template_origin': signing.dumps(template.template.origin.name)
}

response = self.client.get(url, data)
Expand Down