Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 988b61c

Browse files
committed
[1.5.x] Prevented arbitrary file inclusion with {% ssi %} tag and relative paths.
Thanks Rainer Koirikivi for the report and draft patch. This is a security fix; disclosure to follow shortly. Backport of 7fe5b65 from master
1 parent 693ebff commit 988b61c

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

django/template/defaulttags.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Default tags used by the template system, available to all templates."""
22
from __future__ import unicode_literals
33

4+
import os
45
import sys
56
import re
67
from datetime import datetime
@@ -312,6 +313,7 @@ def render(self, context):
312313
return ''
313314

314315
def include_is_allowed(filepath):
316+
filepath = os.path.abspath(filepath)
315317
for root in settings.ALLOWED_INCLUDE_ROOTS:
316318
if filepath.startswith(root):
317319
return True

tests/regressiontests/templates/tests.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,3 +1737,34 @@ def test_include_only(self):
17371737
template.Template('{% include "child" only %}').render(ctx),
17381738
'none'
17391739
)
1740+
1741+
1742+
class SSITests(TestCase):
1743+
def setUp(self):
1744+
self.this_dir = os.path.dirname(os.path.abspath(upath(__file__)))
1745+
self.ssi_dir = os.path.join(self.this_dir, "templates", "first")
1746+
1747+
def render_ssi(self, path):
1748+
# the path must exist for the test to be reliable
1749+
self.assertTrue(os.path.exists(path))
1750+
return template.Template('{%% ssi "%s" %%}' % path).render(Context())
1751+
1752+
def test_allowed_paths(self):
1753+
acceptable_path = os.path.join(self.ssi_dir, "..", "first", "test.html")
1754+
with override_settings(ALLOWED_INCLUDE_ROOTS=(self.ssi_dir,)):
1755+
self.assertEqual(self.render_ssi(acceptable_path), 'First template\n')
1756+
1757+
def test_relative_include_exploit(self):
1758+
"""
1759+
May not bypass ALLOWED_INCLUDE_ROOTS with relative paths
1760+
1761+
e.g. if ALLOWED_INCLUDE_ROOTS = ("/var/www",), it should not be
1762+
possible to do {% ssi "/var/www/../../etc/passwd" %}
1763+
"""
1764+
disallowed_paths = [
1765+
os.path.join(self.ssi_dir, "..", "ssi_include.html"),
1766+
os.path.join(self.ssi_dir, "..", "second", "test.html"),
1767+
]
1768+
with override_settings(ALLOWED_INCLUDE_ROOTS=(self.ssi_dir,)):
1769+
for path in disallowed_paths:
1770+
self.assertEqual(self.render_ssi(path), '')

0 commit comments

Comments
 (0)