summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Apolloner <florian@apolloner.eu>2021-05-25 11:55:06 +0200
committerCarlton Gibson <carlton.gibson@noumenal.es>2021-06-02 10:26:22 +0200
commit053cc9534d174dc89daba36724ed2dcb36755b90 (patch)
tree5e2ee37b34f60856b7b4a14c023315a3d5b46997
parent6229d8794ff7d3f471e29811857d72e67f24b608 (diff)
[2.2.x] Fixed CVE-2021-33203 -- Fixed potential path-traversal via admindocs' TemplateDetailView.
-rw-r--r--django/contrib/admindocs/views.py3
-rw-r--r--docs/releases/2.2.24.txt12
-rw-r--r--tests/admin_docs/test_views.py16
3 files changed, 29 insertions, 2 deletions
diff --git a/django/contrib/admindocs/views.py b/django/contrib/admindocs/views.py
index 0474c38fd4..5986717d95 100644
--- a/django/contrib/admindocs/views.py
+++ b/django/contrib/admindocs/views.py
@@ -15,6 +15,7 @@ from django.db import models
from django.http import Http404
from django.template.engine import Engine
from django.urls import get_mod_func, get_resolver, get_urlconf
+from django.utils._os import safe_join
from django.utils.decorators import method_decorator
from django.utils.inspect import (
func_accepts_kwargs, func_accepts_var_args, get_func_full_args,
@@ -328,7 +329,7 @@ class TemplateDetailView(BaseAdminDocsView):
else:
# This doesn't account for template loaders (#24128).
for index, directory in enumerate(default_engine.dirs):
- template_file = Path(directory) / template
+ template_file = Path(safe_join(directory, template))
if template_file.exists():
with template_file.open() as f:
template_contents = f.read()
diff --git a/docs/releases/2.2.24.txt b/docs/releases/2.2.24.txt
index 5b71d9939f..9bcf7037c4 100644
--- a/docs/releases/2.2.24.txt
+++ b/docs/releases/2.2.24.txt
@@ -6,4 +6,14 @@ Django 2.2.24 release notes
Django 2.2.24 fixes two security issues in 2.2.23.
-...
+CVE-2021-33203: Potential directory traversal via ``admindocs``
+===============================================================
+
+Staff members could use the :mod:`~django.contrib.admindocs`
+``TemplateDetailView`` view to check the existence of arbitrary files.
+Additionally, if (and only if) the default admindocs templates have been
+customized by the developers to also expose the file contents, then not only
+the existence but also the file contents would have been exposed.
+
+As a mitigation, path sanitation is now applied and only files within the
+template root directories can be loaded.
diff --git a/tests/admin_docs/test_views.py b/tests/admin_docs/test_views.py
index bcadff7d8a..dc6d3c127b 100644
--- a/tests/admin_docs/test_views.py
+++ b/tests/admin_docs/test_views.py
@@ -134,6 +134,22 @@ class AdminDocViewTests(TestDataMixin, AdminDocsTestCase):
self.assertContains(response, 'View documentation')
+@unittest.skipUnless(utils.docutils_is_available, 'no docutils installed.')
+class AdminDocViewDefaultEngineOnly(TestDataMixin, AdminDocsTestCase):
+
+ def setUp(self):
+ self.client.force_login(self.superuser)
+
+ def test_template_detail_path_traversal(self):
+ cases = ['/etc/passwd', '../passwd']
+ for fpath in cases:
+ with self.subTest(path=fpath):
+ response = self.client.get(
+ reverse('django-admindocs-templates', args=[fpath]),
+ )
+ self.assertEqual(response.status_code, 400)
+
+
@override_settings(TEMPLATES=[{
'NAME': 'ONE',
'BACKEND': 'django.template.backends.django.DjangoTemplates',