Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[1.5.x] Prevented arbitrary file inclusion with {% ssi %} tag and rel…

…ative paths.

Thanks Rainer Koirikivi for the report and draft patch.

This is a security fix; disclosure to follow shortly.

Backport of 7fe5b65 from master
  • Loading branch information...
commit 988b61c550d798f9a66d17ee0511fb7a9a7f33ca 1 parent 693ebff
@timgraham timgraham authored
View
2  django/template/defaulttags.py
@@ -1,6 +1,7 @@
"""Default tags used by the template system, available to all templates."""
from __future__ import unicode_literals
+import os
import sys
import re
from datetime import datetime
@@ -312,6 +313,7 @@ def render(self, context):
return ''
def include_is_allowed(filepath):
+ filepath = os.path.abspath(filepath)
for root in settings.ALLOWED_INCLUDE_ROOTS:
if filepath.startswith(root):
return True
View
31 tests/regressiontests/templates/tests.py
@@ -1737,3 +1737,34 @@ def test_include_only(self):
template.Template('{% include "child" only %}').render(ctx),
'none'
)
+
+
+class SSITests(TestCase):
+ def setUp(self):
+ self.this_dir = os.path.dirname(os.path.abspath(upath(__file__)))
+ self.ssi_dir = os.path.join(self.this_dir, "templates", "first")
+
+ def render_ssi(self, path):
+ # the path must exist for the test to be reliable
+ self.assertTrue(os.path.exists(path))
+ return template.Template('{%% ssi "%s" %%}' % path).render(Context())
+
+ def test_allowed_paths(self):
+ acceptable_path = os.path.join(self.ssi_dir, "..", "first", "test.html")
+ with override_settings(ALLOWED_INCLUDE_ROOTS=(self.ssi_dir,)):
+ self.assertEqual(self.render_ssi(acceptable_path), 'First template\n')
+
+ def test_relative_include_exploit(self):
+ """
+ May not bypass ALLOWED_INCLUDE_ROOTS with relative paths
+
+ e.g. if ALLOWED_INCLUDE_ROOTS = ("/var/www",), it should not be
+ possible to do {% ssi "/var/www/../../etc/passwd" %}
+ """
+ disallowed_paths = [
+ os.path.join(self.ssi_dir, "..", "ssi_include.html"),
+ os.path.join(self.ssi_dir, "..", "second", "test.html"),
+ ]
+ with override_settings(ALLOWED_INCLUDE_ROOTS=(self.ssi_dir,)):
+ for path in disallowed_paths:
+ self.assertEqual(self.render_ssi(path), '')
Please sign in to comment.
Something went wrong with that request. Please try again.