Skip to content

Commit 053cc95

Browse files
apollo13carltongibson
authored andcommitted
[2.2.x] Fixed CVE-2021-33203 -- Fixed potential path-traversal via admindocs' TemplateDetailView.
1 parent 6229d87 commit 053cc95

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

django/contrib/admindocs/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from django.http import Http404
1616
from django.template.engine import Engine
1717
from django.urls import get_mod_func, get_resolver, get_urlconf
18+
from django.utils._os import safe_join
1819
from django.utils.decorators import method_decorator
1920
from django.utils.inspect import (
2021
func_accepts_kwargs, func_accepts_var_args, get_func_full_args,
@@ -328,7 +329,7 @@ def get_context_data(self, **kwargs):
328329
else:
329330
# This doesn't account for template loaders (#24128).
330331
for index, directory in enumerate(default_engine.dirs):
331-
template_file = Path(directory) / template
332+
template_file = Path(safe_join(directory, template))
332333
if template_file.exists():
333334
with template_file.open() as f:
334335
template_contents = f.read()

docs/releases/2.2.24.txt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,14 @@ Django 2.2.24 release notes
66

77
Django 2.2.24 fixes two security issues in 2.2.23.
88

9-
...
9+
CVE-2021-33203: Potential directory traversal via ``admindocs``
10+
===============================================================
11+
12+
Staff members could use the :mod:`~django.contrib.admindocs`
13+
``TemplateDetailView`` view to check the existence of arbitrary files.
14+
Additionally, if (and only if) the default admindocs templates have been
15+
customized by the developers to also expose the file contents, then not only
16+
the existence but also the file contents would have been exposed.
17+
18+
As a mitigation, path sanitation is now applied and only files within the
19+
template root directories can be loaded.

tests/admin_docs/test_views.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ def test_no_sites_framework(self):
134134
self.assertContains(response, 'View documentation')
135135

136136

137+
@unittest.skipUnless(utils.docutils_is_available, 'no docutils installed.')
138+
class AdminDocViewDefaultEngineOnly(TestDataMixin, AdminDocsTestCase):
139+
140+
def setUp(self):
141+
self.client.force_login(self.superuser)
142+
143+
def test_template_detail_path_traversal(self):
144+
cases = ['/etc/passwd', '../passwd']
145+
for fpath in cases:
146+
with self.subTest(path=fpath):
147+
response = self.client.get(
148+
reverse('django-admindocs-templates', args=[fpath]),
149+
)
150+
self.assertEqual(response.status_code, 400)
151+
152+
137153
@override_settings(TEMPLATES=[{
138154
'NAME': 'ONE',
139155
'BACKEND': 'django.template.backends.django.DjangoTemplates',

0 commit comments

Comments
 (0)