Debian Codegraph @

tests/utils_tests/test_html.py

raw
1 import math
2 import os
3 import sys
4 from datetime import datetime
5
6 from django.core.exceptions import SuspiciousOperation
7 from django.core.serializers.json import DjangoJSONEncoder
8 from django.test import SimpleTestCase
9 from django.utils.deprecation import RemovedInDjango60Warning
10 from django.utils.functional import lazystr
11 from django.utils.html import (
12 conditional_escape,
13 escape,
14 escapejs,
15 format_html,
16 format_html_join,
17 html_safe,
18 json_script,
19 linebreaks,
20 smart_urlquote,
21 strip_spaces_between_tags,
22 strip_tags,
23 urlize,
24 )
25 from django.utils.safestring import mark_safe
26
27
28 class TestUtilsHtml(SimpleTestCase):
29 def check_output(self, function, value, output=None):
30 """
31 function(value) equals output. If output is None, function(value)
32 equals value.
33 """
34 if output is None:
35 output = value
36 self.assertEqual(function(value), output)
37
38 def test_escape(self):
39 items = (
40 ("&", "&"),
41 ("<", "&lt;"),
42 (">", "&gt;"),
43 ('"', "&quot;"),
44 ("'", "&#x27;"),
45 )
46 # Substitution patterns for testing the above items.
47 patterns = ("%s", "asdf%sfdsa", "%s1", "1%sb")
48 for value, output in items:
49 with self.subTest(value=value, output=output):
50 for pattern in patterns:
51 with self.subTest(value=value, output=output, pattern=pattern):
52 self.check_output(escape, pattern % value, pattern % output)
53 self.check_output(
54 escape, lazystr(pattern % value), pattern % output
55 )
56 # Check repeated values.
57 self.check_output(escape, value * 2, output * 2)
58 # Verify it doesn't double replace &.
59 self.check_output(escape, "<&", "&lt;&amp;")
60
61 def test_format_html(self):
62 self.assertEqual(
63 format_html(
64 "{} {} {third} {fourth}",
65 "< Dangerous >",
66 mark_safe("<b>safe</b>"),
67 third="< dangerous again",
68 fourth=mark_safe("<i>safe again</i>"),
69 ),
70 "&lt; Dangerous &gt; <b>safe</b> &lt; dangerous again <i>safe again</i>",
71 )
72
73 def test_format_html_no_params(self):
74 msg = "Calling format_html() without passing args or kwargs is deprecated."
75 # RemovedInDjango60Warning: when the deprecation ends, replace with:
76 # msg = "args or kwargs must be provided."
77 # with self.assertRaisesMessage(TypeError, msg):
78 with self.assertWarnsMessage(RemovedInDjango60Warning, msg) as ctx:
79 name = "Adam"
80 self.assertEqual(format_html(f"<i>{name}</i>"), "<i>Adam</i>")
81 self.assertEqual(ctx.filename, __file__)
82
83 def test_format_html_join_with_positional_arguments(self):
84 self.assertEqual(
85 format_html_join(
86 "\n",
87 "<li>{}) {}</li>",
88 [(1, "Emma"), (2, "Matilda")],
89 ),
90 "<li>1) Emma</li>\n<li>2) Matilda</li>",
91 )
92
93 def test_format_html_join_with_keyword_arguments(self):
94 self.assertEqual(
95 format_html_join(
96 "\n",
97 "<li>{id}) {text}</li>",
98 [{"id": 1, "text": "Emma"}, {"id": 2, "text": "Matilda"}],
99 ),
100 "<li>1) Emma</li>\n<li>2) Matilda</li>",
101 )
102
103 def test_linebreaks(self):
104 items = (
105 ("para1\n\npara2\r\rpara3", "<p>para1</p>\n\n<p>para2</p>\n\n<p>para3</p>"),
106 (
107 "para1\nsub1\rsub2\n\npara2",
108 "<p>para1<br>sub1<br>sub2</p>\n\n<p>para2</p>",
109 ),
110 (
111 "para1\r\n\r\npara2\rsub1\r\rpara4",
112 "<p>para1</p>\n\n<p>para2<br>sub1</p>\n\n<p>para4</p>",
113 ),
114 ("para1\tmore\n\npara2", "<p>para1\tmore</p>\n\n<p>para2</p>"),
115 )
116 for value, output in items:
117 with self.subTest(value=value, output=output):
118 self.check_output(linebreaks, value, output)
119 self.check_output(linebreaks, lazystr(value), output)
120
121 def test_strip_tags(self):
122 # Python fixed a quadratic-time issue in HTMLParser in 3.13.6, 3.12.12,
123 # 3.11.14, 3.10.19, and 3.9.24. The fix slightly changes HTMLParser's
124 # output, so tests for particularly malformed input must handle both
125 # old and new results. The check below is temporary until all supported
126 # Python versions and CI workers include the fix. See:
127 # https://github.com/python/cpython/commit/6eb6c5db
128 min_fixed_security = {
129 (3, 14): (3, 14),
130 (3, 13): (3, 13, 6),
131 (3, 12): (3, 12, 12),
132 (3, 11): (3, 11, 14),
133 (3, 10): (3, 10, 19),
134 (3, 9): (3, 9, 24),
135 }
136 htmlparser_fixed_security = (
137 sys.version_info >= min_fixed_security[sys.version_info[:2]]
138 )
139 # Similarly, there was a fix for terminating incomplete entities. See:
140 # https://github.com/python/cpython/commit/95296a9d
141 min_fixed_incomplete_entities = {
142 (3, 14): (3, 14, 1),
143 (3, 13): (3, 13, 10),
144 # Not fixed in the following versions.
145 (3, 12): (3, 12, math.inf),
146 (3, 11): (3, 11, math.inf),
147 (3, 10): (3, 10, math.inf),
148 (3, 9): (3, 9, math.inf),
149 }
150 major_version = sys.version_info[:2]
151 htmlparser_fixed_security = sys.version_info >= min_fixed_security.get(
152 major_version, major_version
153 )
154 htmlparser_fixed_incomplete_entities = (
155 sys.version_info
156 >= min_fixed_incomplete_entities.get(major_version, major_version)
157 )
158 items = (
159 (
160 "<p>See: &#39;&eacute; is an apostrophe followed by e acute</p>",
161 "See: &#39;&eacute; is an apostrophe followed by e acute",
162 ),
163 (
164 "<p>See: &#x27;&eacute; is an apostrophe followed by e acute</p>",
165 "See: &#x27;&eacute; is an apostrophe followed by e acute",
166 ),
167 ("<adf>a", "a"),
168 ("</adf>a", "a"),
169 ("<asdf><asdf>e", "e"),
170 ("hi, <f x", "hi, <f x"),
171 ("234<235, right?", "234<235, right?"),
172 ("a4<a5 right?", "a4<a5 right?"),
173 ("b7>b2!", "b7>b2!"),
174 ("</fe", "</fe"),
175 ("<x>b<y>", "b"),
176 ("a<p onclick=\"alert('<test>')\">b</p>c", "abc"),
177 ("a<p a >b</p>c", "abc"),
178 ("d<a:b c:d>e</p>f", "def"),
179 ('<strong>foo</strong><a href="http://example.com">bar</a>', "foobar"),
180 # caused infinite loop on Pythons not patched with
181 # https://bugs.python.org/issue20288
182 ("&gotcha&#;<>", "&gotcha&#;<>"),
183 ("<sc<!-- -->ript>test<<!-- -->/script>", "ript>test"),
184 (
185 "<script>alert()</script>&h",
186 "alert()&h;" if htmlparser_fixed_incomplete_entities else "alert()h",
187 ),
188 (
189 "><!" + ("&" * 16000) + "D",
190 ">" if htmlparser_fixed_security else "><!" + ("&" * 16000) + "D",
191 ),
192 ("X<<<<br>br>br>br>X", "XX"),
193 ("<" * 50 + "a>" * 50, ""),
194 (
195 ">" + "<a" * 500 + "a",
196 ">" if htmlparser_fixed_security else ">" + "<a" * 500 + "a",
197 ),
198 ("<a" * 49 + "a" * 951, "<a" * 49 + "a" * 951),
199 ("<" + "a" * 1_002, "<" + "a" * 1_002),
200 )
201 for value, output in items:
202 with self.subTest(value=value, output=output):
203 self.check_output(strip_tags, value, output)
204 self.check_output(strip_tags, lazystr(value), output)
205
206 def test_strip_tags_suspicious_operation_max_depth(self):
207 value = "<" * 51 + "a>" * 51, "<a>"
208 with self.assertRaises(SuspiciousOperation):
209 strip_tags(value)
210
211 def test_strip_tags_suspicious_operation_large_open_tags(self):
212 items = [
213 ">" + "<a" * 501,
214 "<a" * 50 + "a" * 950,
215 ]
216 for value in items:
217 with self.subTest(value=value):
218 with self.assertRaises(SuspiciousOperation):
219 strip_tags(value)
220
221 def test_strip_tags_files(self):
222 # Test with more lengthy content (also catching performance regressions)
223 for filename in ("strip_tags1.html", "strip_tags2.txt"):
224 with self.subTest(filename=filename):
225 path = os.path.join(os.path.dirname(__file__), "files", filename)
226 with open(path) as fp:
227 content = fp.read()
228 start = datetime.now()
229 stripped = strip_tags(content)
230 elapsed = datetime.now() - start
231 self.assertEqual(elapsed.seconds, 0)
232 self.assertIn("Test string that has not been stripped.", stripped)
233 self.assertNotIn("<", stripped)
234
235 def test_strip_spaces_between_tags(self):
236 # Strings that should come out untouched.
237 items = (" <adf>", "<adf> ", " </adf> ", " <f> x</f>")
238 for value in items:
239 with self.subTest(value=value):
240 self.check_output(strip_spaces_between_tags, value)
241 self.check_output(strip_spaces_between_tags, lazystr(value))
242
243 # Strings that have spaces to strip.
244 items = (
245 ("<d> </d>", "<d></d>"),
246 ("<p>hello </p>\n<p> world</p>", "<p>hello </p><p> world</p>"),
247 ("\n<p>\t</p>\n<p> </p>\n", "\n<p></p><p></p>\n"),
248 )
249 for value, output in items:
250 with self.subTest(value=value, output=output):
251 self.check_output(strip_spaces_between_tags, value, output)
252 self.check_output(strip_spaces_between_tags, lazystr(value), output)
253
254 def test_escapejs(self):
255 items = (
256 (
257 "\"double quotes\" and 'single quotes'",
258 "\\u0022double quotes\\u0022 and \\u0027single quotes\\u0027",
259 ),
260 (r"\ : backslashes, too", "\\u005C : backslashes, too"),
261 (
262 "and lots of whitespace: \r\n\t\v\f\b",
263 "and lots of whitespace: \\u000D\\u000A\\u0009\\u000B\\u000C\\u0008",
264 ),
265 (
266 r"<script>and this</script>",
267 "\\u003Cscript\\u003Eand this\\u003C/script\\u003E",
268 ),
269 (
270 "paragraph separator:\u2029and line separator:\u2028",
271 "paragraph separator:\\u2029and line separator:\\u2028",
272 ),
273 ("`", "\\u0060"),
274 )
275 for value, output in items:
276 with self.subTest(value=value, output=output):
277 self.check_output(escapejs, value, output)
278 self.check_output(escapejs, lazystr(value), output)
279
280 def test_json_script(self):
281 tests = (
282 # "<", ">" and "&" are quoted inside JSON strings
283 (
284 (
285 "&<>",
286 '<script id="test_id" type="application/json">'
287 '"\\u0026\\u003C\\u003E"</script>',
288 )
289 ),
290 # "<", ">" and "&" are quoted inside JSON objects
291 (
292 {"a": "<script>test&ing</script>"},
293 '<script id="test_id" type="application/json">'
294 '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}'
295 "</script>",
296 ),
297 # Lazy strings are quoted
298 (
299 lazystr("&<>"),
300 '<script id="test_id" type="application/json">"\\u0026\\u003C\\u003E"'
301 "</script>",
302 ),
303 (
304 {"a": lazystr("<script>test&ing</script>")},
305 '<script id="test_id" type="application/json">'
306 '{"a": "\\u003Cscript\\u003Etest\\u0026ing\\u003C/script\\u003E"}'
307 "</script>",
308 ),
309 )
310 for arg, expected in tests:
311 with self.subTest(arg=arg):
312 self.assertEqual(json_script(arg, "test_id"), expected)
313
314 def test_json_script_custom_encoder(self):
315 class CustomDjangoJSONEncoder(DjangoJSONEncoder):
316 def encode(self, o):
317 return '{"hello": "world"}'
318
319 self.assertHTMLEqual(
320 json_script({}, encoder=CustomDjangoJSONEncoder),
321 '<script type="application/json">{"hello": "world"}</script>',
322 )
323
324 def test_json_script_without_id(self):
325 self.assertHTMLEqual(
326 json_script({"key": "value"}),
327 '<script type="application/json">{"key": "value"}</script>',
328 )
329
330 def test_smart_urlquote(self):
331 items = (
332 # IDN is encoded as percent-encoded ("quoted") UTF-8 (#36013).
333 ("http://öäü.com/", "http://%C3%B6%C3%A4%C3%BC.com/"),
334 ("https://faß.example.com", "https://fa%C3%9F.example.com"),
335 (
336 "http://öäü.com/öäü/",
337 "http://%C3%B6%C3%A4%C3%BC.com/%C3%B6%C3%A4%C3%BC/",
338 ),
339 (
340 # Valid under IDNA 2008, but was invalid in IDNA 2003.
341 "https://މިހާރު.com",
342 "https://%DE%89%DE%A8%DE%80%DE%A7%DE%83%DE%AA.com",
343 ),
344 (
345 # Valid under WHATWG URL Specification but not IDNA 2008.
346 "http://👓.ws",
347 "http://%F0%9F%91%93.ws",
348 ),
349 # Pre-encoded IDNA is left unchanged.
350 ("http://xn--iny-zx5a.com/idna2003", "http://xn--iny-zx5a.com/idna2003"),
351 ("http://xn--fa-hia.com/idna2008", "http://xn--fa-hia.com/idna2008"),
352 # Everything unsafe is quoted, !*'();:@&=+$,/?#[]~ is considered
353 # safe as per RFC.
354 (
355 "http://example.com/path/öäü/",
356 "http://example.com/path/%C3%B6%C3%A4%C3%BC/",
357 ),
358 ("http://example.com/%C3%B6/ä/", "http://example.com/%C3%B6/%C3%A4/"),
359 ("http://example.com/?x=1&y=2+3&z=", "http://example.com/?x=1&y=2+3&z="),
360 ("http://example.com/?x=<>\"'", "http://example.com/?x=%3C%3E%22%27"),
361 (
362 "http://example.com/?q=http://example.com/?x=1%26q=django",
363 "http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3D"
364 "django",
365 ),
366 (
367 "http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3D"
368 "django",
369 "http://example.com/?q=http%3A%2F%2Fexample.com%2F%3Fx%3D1%26q%3D"
370 "django",
371 ),
372 ("http://.www.f oo.bar/", "http://.www.f%20oo.bar/"),
373 ('http://example.com">', "http://example.com%22%3E"),
374 ("http://10.22.1.1/", "http://10.22.1.1/"),
375 ("http://[fd00::1]/", "http://[fd00::1]/"),
376 )
377 for value, output in items:
378 with self.subTest(value=value, output=output):
379 self.assertEqual(smart_urlquote(value), output)
380
381 def test_conditional_escape(self):
382 s = "<h1>interop</h1>"
383 self.assertEqual(conditional_escape(s), "&lt;h1&gt;interop&lt;/h1&gt;")
384 self.assertEqual(conditional_escape(mark_safe(s)), s)
385 self.assertEqual(conditional_escape(lazystr(mark_safe(s))), s)
386
387 def test_html_safe(self):
388 @html_safe
389 class HtmlClass:
390 def __str__(self):
391 return "<h1>I'm a html class!</h1>"
392
393 html_obj = HtmlClass()
394 self.assertTrue(hasattr(HtmlClass, "__html__"))
395 self.assertTrue(hasattr(html_obj, "__html__"))
396 self.assertEqual(str(html_obj), html_obj.__html__())
397
398 def test_html_safe_subclass(self):
399 class BaseClass:
400 def __html__(self):
401 # defines __html__ on its own
402 return "some html content"
403
404 def __str__(self):
405 return "some non html content"
406
407 @html_safe
408 class Subclass(BaseClass):
409 def __str__(self):
410 # overrides __str__ and is marked as html_safe
411 return "some html safe content"
412
413 subclass_obj = Subclass()
414 self.assertEqual(str(subclass_obj), subclass_obj.__html__())
415
416 def test_html_safe_defines_html_error(self):
417 msg = "can't apply @html_safe to HtmlClass because it defines __html__()."
418 with self.assertRaisesMessage(ValueError, msg):
419
420 @html_safe
421 class HtmlClass:
422 def __html__(self):
423 return "<h1>I'm a html class!</h1>"
424
425 def test_html_safe_doesnt_define_str(self):
426 msg = "can't apply @html_safe to HtmlClass because it doesn't define __str__()."
427 with self.assertRaisesMessage(ValueError, msg):
428
429 @html_safe
430 class HtmlClass:
431 pass
432
433 def test_urlize(self):
434 tests = (
435 (
436 "Search for google.com/?q=! and see.",
437 'Search for <a href="http://google.com/?q=">google.com/?q=</a>! and '
438 "see.",
439 ),
440 (
441 "Search for google.com/?q=1&lt! and see.",
442 'Search for <a href="http://google.com/?q=1%3C">google.com/?q=1&lt'
443 "</a>! and see.",
444 ),
445 (
446 lazystr("Search for google.com/?q=!"),
447 'Search for <a href="http://google.com/?q=">google.com/?q=</a>!',
448 ),
449 (
450 "http://www.foo.bar/",
451 '<a href="http://www.foo.bar/">http://www.foo.bar/</a>',
452 ),
453 (
454 "Look on www.نامه‌ای.com.",
455 "Look on <a "
456 'href="http://www.%D9%86%D8%A7%D9%85%D9%87%E2%80%8C%D8%A7%DB%8C.com"'
457 ">www.نامه‌ای.com</a>.",
458 ),
459 ("[email protected]", '<a href="mailto:[email protected]">[email protected]</a>'),
460 (
461 "test@" + "한.글." * 15 + "aaa",
462 '<a href="mailto:test@'
463 + "%ED%95%9C.%EA%B8%80." * 15
464 + 'aaa">'
465 + "test@"
466 + "한.글." * 15
467 + "aaa</a>",
468 ),
469 (
470 # RFC 6068 requires a mailto URI to percent-encode a number of
471 # characters that can appear in <addr-spec>.
472 "yes+this=is&a%[email protected]",
473 '<a href="mailto:yes%2Bthis%3Dis%26a%25valid%[email protected]"'
474 ">yes+this=is&a%[email protected]</a>",
475 ),
476 (
477 "foo@faß.example.com",
478 '<a href="mailto:foo@fa%C3%9F.example.com">foo@faß.example.com</a>',
479 ),
480 (
481 "idna-2008@މިހާރު.example.mv",
482 '<a href="mailto:idna-2008@%DE%89%DE%A8%DE%80%DE%A7%DE%83%DE%AA.ex'
483 'ample.mv">idna-2008@މިހާރު.example.mv</a>',
484 ),
485 )
486 for value, output in tests:
487 with self.subTest(value=value):
488 self.assertEqual(urlize(value), output)
489
490 def test_urlize_unchanged_inputs(self):
491 tests = (
492 ("a" + "@a" * 50000) + "a", # simple_email_re catastrophic test
493 # Unicode domain catastrophic tests.
494 "a@" + "한.글." * 1_000_000 + "a",
495 "http://" + "한.글." * 1_000_000 + "com",
496 "www." + "한.글." * 1_000_000 + "com",
497 ("a" + "." * 1000000) + "a", # trailing_punctuation catastrophic test
498 "foo@",
499 "@foo.com",
500 "[email protected]",
501 "foo@localhost",
502 "foo@localhost.",
503 "test@example?;+!.com",
504 "email [email protected],then I'll respond",
505 # trim_punctuation catastrophic tests
506 "(" * 100_000 + ":" + ")" * 100_000,
507 "(" * 100_000 + "&:" + ")" * 100_000,
508 "([" * 100_000 + ":" + "])" * 100_000,
509 "[(" * 100_000 + ":" + ")]" * 100_000,
510 "([[" * 100_000 + ":" + "]])" * 100_000,
511 "&:" + ";" * 100_000,
512 "&.;" * 100_000,
513 ".;" * 100_000,
514 "&" + ";:" * 100_000,
515 )
516 for value in tests:
517 with self.subTest(value=value):
518 self.assertEqual(urlize(value), value)