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

Skip to content

Commit 0142204

Browse files
MarkusHadamchainz
authored andcommitted
[4.0.x] Fixed CVE-2022-22818 -- Fixed possible XSS via {% debug %} template tag.
Thanks Keryn Knight for the report. Backport of 394517f from main. Co-authored-by: Adam Johnson <[email protected]>
1 parent 6928227 commit 0142204

File tree

7 files changed

+87
-16
lines changed

7 files changed

+87
-16
lines changed

django/template/defaulttags.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from django.conf import settings
1010
from django.utils import timezone
11-
from django.utils.html import conditional_escape, format_html
11+
from django.utils.html import conditional_escape, escape, format_html
1212
from django.utils.lorem_ipsum import paragraphs, words
1313
from django.utils.safestring import mark_safe
1414

@@ -99,10 +99,13 @@ def reset(self, context):
9999

100100
class DebugNode(Node):
101101
def render(self, context):
102+
if not settings.DEBUG:
103+
return ''
104+
102105
from pprint import pformat
103-
output = [pformat(val) for val in context]
106+
output = [escape(pformat(val)) for val in context]
104107
output.append('\n\n')
105-
output.append(pformat(sys.modules))
108+
output.append(escape(pformat(sys.modules)))
106109
return ''.join(output)
107110

108111

docs/ref/templates/builtins.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,13 @@ from its first value when it's next encountered.
194194
---------
195195

196196
Outputs a whole load of debugging information, including the current context
197-
and imported modules.
197+
and imported modules. ``{% debug %}`` outputs nothing when the :setting:`DEBUG`
198+
setting is ``False``.
199+
200+
.. versionchanged:: 2.2.27
201+
202+
In older versions, debugging information was displayed when the
203+
:setting:`DEBUG` setting was ``False``.
198204

199205
.. templatetag:: extends
200206

docs/releases/2.2.27.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@ Django 2.2.27 release notes
66

77
Django 2.2.27 fixes two security issues with severity "medium" in 2.2.26.
88

9-
...
9+
CVE-2022-22818: Possible XSS via ``{% debug %}`` template tag
10+
=============================================================
11+
12+
The ``{% debug %}`` template tag didn't properly encode the current context,
13+
posing an XSS attack vector.
14+
15+
In order to avoid this vulnerability, ``{% debug %}`` no longer outputs an
16+
information when the ``DEBUG`` setting is ``False``, and it ensures all context
17+
variables are correctly escaped when the ``DEBUG`` setting is ``True``.

docs/releases/3.2.12.txt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,12 @@ Django 3.2.12 release notes
66

77
Django 3.2.12 fixes two security issues with severity "medium" in 3.2.11.
88

9-
...
9+
CVE-2022-22818: Possible XSS via ``{% debug %}`` template tag
10+
=============================================================
11+
12+
The ``{% debug %}`` template tag didn't properly encode the current context,
13+
posing an XSS attack vector.
14+
15+
In order to avoid this vulnerability, ``{% debug %}`` no longer outputs an
16+
information when the ``DEBUG`` setting is ``False``, and it ensures all context
17+
variables are correctly escaped when the ``DEBUG`` setting is ``True``.

docs/releases/4.0.2.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ Django 4.0.2 fixes two security issues with severity "medium" and several bugs
88
in 4.0.1. Also, the latest string translations from Transifex are incorporated,
99
with a special mention for Bulgarian (fully translated).
1010

11+
CVE-2022-22818: Possible XSS via ``{% debug %}`` template tag
12+
=============================================================
13+
14+
The ``{% debug %}`` template tag didn't properly encode the current context,
15+
posing an XSS attack vector.
16+
17+
In order to avoid this vulnerability, ``{% debug %}`` no longer outputs an
18+
information when the ``DEBUG`` setting is ``False``, and it ensures all context
19+
variables are correctly escaped when the ``DEBUG`` setting is ``True``.
20+
1121
Bugfixes
1222
========
1323

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from django.contrib.auth.models import Group
2+
from django.test import SimpleTestCase, override_settings
3+
4+
from ..utils import setup
5+
6+
7+
@override_settings(DEBUG=True)
8+
class DebugTests(SimpleTestCase):
9+
10+
@override_settings(DEBUG=False)
11+
@setup({'non_debug': '{% debug %}'})
12+
def test_non_debug(self):
13+
output = self.engine.render_to_string('non_debug', {})
14+
self.assertEqual(output, '')
15+
16+
@setup({'modules': '{% debug %}'})
17+
def test_modules(self):
18+
output = self.engine.render_to_string('modules', {})
19+
self.assertIn(
20+
'&#x27;django&#x27;: &lt;module &#x27;django&#x27; ',
21+
output,
22+
)
23+
24+
@setup({'plain': '{% debug %}'})
25+
def test_plain(self):
26+
output = self.engine.render_to_string('plain', {'a': 1})
27+
self.assertTrue(output.startswith(
28+
'{&#x27;a&#x27;: 1}'
29+
'{&#x27;False&#x27;: False, &#x27;None&#x27;: None, '
30+
'&#x27;True&#x27;: True}\n\n{'
31+
))
32+
33+
@setup({'non_ascii': '{% debug %}'})
34+
def test_non_ascii(self):
35+
group = Group(name="清風")
36+
output = self.engine.render_to_string('non_ascii', {'group': group})
37+
self.assertTrue(output.startswith(
38+
'{&#x27;group&#x27;: &lt;Group: 清風&gt;}'
39+
))
40+
41+
@setup({'script': '{% debug %}'})
42+
def test_script(self):
43+
output = self.engine.render_to_string('script', {'frag': '<script>'})
44+
self.assertTrue(output.startswith(
45+
'{&#x27;frag&#x27;: &#x27;&lt;script&gt;&#x27;}'
46+
))

tests/template_tests/tests.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import sys
22

3-
from django.contrib.auth.models import Group
43
from django.template import (
54
Context, Engine, TemplateDoesNotExist, TemplateSyntaxError,
65
)
@@ -163,15 +162,6 @@ def test_super_errors(self):
163162
with self.assertRaises(NoReverseMatch):
164163
t.render(Context())
165164

166-
def test_debug_tag_non_ascii(self):
167-
"""
168-
#23060 -- Test non-ASCII model representation in debug output.
169-
"""
170-
group = Group(name="清風")
171-
c1 = Context({"objs": [group]})
172-
t1 = self._engine().from_string('{% debug %}')
173-
self.assertIn("清風", t1.render(c1))
174-
175165
def test_extends_generic_template(self):
176166
"""
177167
#24338 -- Allow extending django.template.backends.django.Template

0 commit comments

Comments
 (0)