File CVE-2022-22818.patch of Package python-Django1
From 625e2352a6f9da2fe2a1cf683e0cdc1888e81a19 Mon Sep 17 00:00:00 2001
From: Markus Holtermann <info@markusholtermann.eu>
Date: Sun, 2 Jan 2022 00:37:40 +0100
Subject: [PATCH 1/2] [2.2.x] Fixed CVE-2022-22818 -- Fixed possible XSS via {%
debug %} template tag.
Thanks Keryn Knight for the report.
Co-authored-by: Adam Johnson <me@adamj.eu>
---
django/template/defaulttags.py | 9 ++--
docs/ref/templates/builtins.txt | 8 +++-
docs/releases/2.2.27.txt | 10 +++-
.../template_tests/syntax_tests/test_debug.py | 46 +++++++++++++++++++
tests/template_tests/tests.py | 10 ----
5 files changed, 68 insertions(+), 15 deletions(-)
create mode 100644 tests/template_tests/syntax_tests/test_debug.py
diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py
index c4a37c25dd..31fa279ca0 100644
--- a/django/template/defaulttags.py
+++ b/django/template/defaulttags.py
@@ -11,7 +11,7 @@ from itertools import cycle as itertools_cycle, groupby
from django.conf import settings
from django.utils import six, timezone
from django.utils.encoding import force_text
-from django.utils.html import conditional_escape, format_html
+from django.utils.html import conditional_escape, escape, format_html
from django.utils.lorem_ipsum import paragraphs, words
from django.utils.safestring import mark_safe
@@ -97,10 +97,13 @@ class CycleNode(Node):
class DebugNode(Node):
def render(self, context):
+ if not settings.DEBUG:
+ return ''
+
from pprint import pformat
- output = [force_text(pformat(val)) for val in context]
+ output = [force_text(escape(pformat(val))) for val in context]
output.append('\n\n')
- output.append(force_text(pformat(sys.modules)))
+ output.append(force_text(escape(pformat(sys.modules))))
return ''.join(output)
diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt
index bc24308ba4..c4b0fa3987 100644
--- a/docs/ref/templates/builtins.txt
+++ b/docs/ref/templates/builtins.txt
@@ -194,7 +194,13 @@ from its first value when it's next encountered.
---------
Outputs a whole load of debugging information, including the current context
-and imported modules.
+and imported modules. ``{% debug %}`` outputs nothing when the :setting:`DEBUG`
+setting is ``False``.
+
+.. versionchanged:: 2.2.27
+
+ In older versions, debugging information was displayed when the
+ :setting:`DEBUG` setting was ``False``.
.. templatetag:: extends
diff --git a/tests/template_tests/syntax_tests/test_debug.py b/tests/template_tests/syntax_tests/test_debug.py
new file mode 100644
index 0000000000..17fea44b68
--- /dev/null
+++ b/tests/template_tests/syntax_tests/test_debug.py
@@ -0,0 +1,46 @@
+from django.contrib.auth.models import Group
+from django.test import SimpleTestCase, override_settings
+
+from ..utils import setup
+
+
+@override_settings(DEBUG=True)
+class DebugTests(SimpleTestCase):
+
+ @override_settings(DEBUG=False)
+ @setup({'non_debug': '{% debug %}'})
+ def test_non_debug(self):
+ output = self.engine.render_to_string('non_debug', {})
+ self.assertEqual(output, '')
+
+ @setup({'modules': '{% debug %}'})
+ def test_modules(self):
+ output = self.engine.render_to_string('modules', {})
+ self.assertIn(
+ ''django': <module 'django' ',
+ output,
+ )
+
+ @setup({'plain': '{% debug %}'})
+ def test_plain(self):
+ output = self.engine.render_to_string('plain', {'a': 1})
+ self.assertTrue(output.startswith(
+ '{'a': 1}'
+ '{'False': False, 'None': None, '
+ ''True': True}\n\n{'
+ ))
+
+ @setup({'non_ascii': '{% debug %}'})
+ def test_non_ascii(self):
+ group = Group(name="清風")
+ output = self.engine.render_to_string('non_ascii', {'group': group})
+ self.assertTrue(output.startswith(
+ '{'group': <Group: 清風>}'
+ ))
+
+ @setup({'script': '{% debug %}'})
+ def test_script(self):
+ output = self.engine.render_to_string('script', {'frag': '<script>'})
+ self.assertTrue(output.startswith(
+ '{'frag': '<script>'}'
+ ))
diff --git a/tests/template_tests/tests.py b/tests/template_tests/tests.py
index a8f089c351..946daa4570 100644
--- a/tests/template_tests/tests.py
+++ b/tests/template_tests/tests.py
@@ -3,7 +3,6 @@
import sys
-from django.contrib.auth.models import Group
from django.template import Context, Engine, TemplateSyntaxError
from django.template.base import UNKNOWN_SOURCE
from django.test import SimpleTestCase, override_settings
@@ -146,15 +145,6 @@ class TemplateTests(SimpleTestCase):
with self.assertRaises(NoReverseMatch):
t.render(Context())
- def test_debug_tag_non_ascii(self):
- """
- #23060 -- Test non-ASCII model representation in debug output.
- """
- group = Group(name="清風")
- c1 = Context({"objs": [group]})
- t1 = Engine().from_string('{% debug %}')
- self.assertIn("清風", t1.render(c1))
-
def test_extends_generic_template(self):
"""
#24338 -- Allow extending django.template.backends.django.Template
--
2.25.1