1
1
from __future__ import annotations
2
2
3
- from typing import cast
4
- from xml .etree .ElementTree import Element
5
-
6
3
from django .conf import settings
7
- from django .http .response import HttpResponse
8
4
from django .test .utils import override_settings
9
5
from html5lib .constants import E
10
6
from html5lib .html5parser import HTMLParser
22
18
MIDDLEWARE_CSP_LAST = settings .MIDDLEWARE + ["csp.middleware.CSPMiddleware" ]
23
19
24
20
25
- def get_namespaces (element : Element ) -> dict [ str , str ] :
21
+ def get_namespaces (element ) :
26
22
"""
27
23
Return the default `xmlns`. See
28
24
https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
@@ -40,9 +36,7 @@ def setUp(self):
40
36
super ().setUp ()
41
37
self .parser = HTMLParser ()
42
38
43
- def _fail_if_missing (
44
- self , root : Element , path : str , namespaces : dict [str , str ], nonce : str
45
- ):
39
+ def _fail_if_missing (self , root , path , namespaces , nonce ):
46
40
"""
47
41
Search elements, fail if a `nonce` attribute is missing on them.
48
42
"""
@@ -51,7 +45,7 @@ def _fail_if_missing(
51
45
if item .attrib .get ("nonce" ) != nonce :
52
46
raise self .failureException (f"{ item } has no nonce attribute." )
53
47
54
- def _fail_if_found (self , root : Element , path : str , namespaces : dict [ str , str ] ):
48
+ def _fail_if_found (self , root , path , namespaces ):
55
49
"""
56
50
Search elements, fail if a `nonce` attribute is found on them.
57
51
"""
@@ -60,7 +54,7 @@ def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]):
60
54
if "nonce" in item .attrib :
61
55
raise self .failureException (f"{ item } has a nonce attribute." )
62
56
63
- def _fail_on_invalid_html (self , content : bytes , parser : HTMLParser ):
57
+ def _fail_on_invalid_html (self , content , parser ):
64
58
"""Fail if the passed HTML is invalid."""
65
59
if parser .errors :
66
60
default_msg = ["Content is invalid HTML:" ]
@@ -75,10 +69,10 @@ def test_exists(self):
75
69
"""A `nonce` should exist when using the `CSPMiddleware`."""
76
70
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
77
71
with self .settings (MIDDLEWARE = middleware ):
78
- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
72
+ response = self .client .get (path = "/csp_view/" )
79
73
self .assertEqual (response .status_code , 200 )
80
74
81
- html_root : Element = self .parser .parse (stream = response .content )
75
+ html_root = self .parser .parse (stream = response .content )
82
76
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
83
77
self .assertContains (response , "djDebug" )
84
78
@@ -98,10 +92,10 @@ def test_does_not_exist_nonce_wasnt_used(self):
98
92
"""
99
93
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
100
94
with self .settings (MIDDLEWARE = middleware ):
101
- response = cast ( HttpResponse , self .client .get (path = "/regular/basic/" ) )
95
+ response = self .client .get (path = "/regular/basic/" )
102
96
self .assertEqual (response .status_code , 200 )
103
97
104
- html_root : Element = self .parser .parse (stream = response .content )
98
+ html_root = self .parser .parse (stream = response .content )
105
99
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
106
100
self .assertContains (response , "djDebug" )
107
101
@@ -119,15 +113,16 @@ def test_does_not_exist_nonce_wasnt_used(self):
119
113
def test_redirects_exists (self ):
120
114
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
121
115
with self .settings (MIDDLEWARE = middleware ):
122
- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
116
+ response = self .client .get (path = "/csp_view/" )
123
117
self .assertEqual (response .status_code , 200 )
124
118
125
- html_root : Element = self .parser .parse (stream = response .content )
119
+ html_root = self .parser .parse (stream = response .content )
126
120
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
127
121
self .assertContains (response , "djDebug" )
128
122
129
123
namespaces = get_namespaces (element = html_root )
130
- nonce = response .context ["request" ].csp_nonce
124
+ context = response .context
125
+ nonce = str (context ["toolbar" ].csp_nonce )
131
126
self ._fail_if_missing (
132
127
root = html_root , path = ".//link" , namespaces = namespaces , nonce = nonce
133
128
)
@@ -139,15 +134,15 @@ def test_panel_content_nonce_exists(self):
139
134
store = get_store ()
140
135
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
141
136
with self .settings (MIDDLEWARE = middleware ):
142
- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
137
+ response = self .client .get (path = "/csp_view/" )
143
138
self .assertEqual (response .status_code , 200 )
144
139
145
140
request_ids = list (store .request_ids ())
146
141
toolbar = DebugToolbar .fetch (request_ids [- 1 ])
147
142
panels_to_check = ["HistoryPanel" , "TimerPanel" ]
148
143
for panel in panels_to_check :
149
144
content = toolbar .get_panel_by_id (panel ).content
150
- html_root : Element = self .parser .parse (stream = content )
145
+ html_root = self .parser .parse (stream = content )
151
146
namespaces = get_namespaces (element = html_root )
152
147
nonce = str (toolbar .csp_nonce )
153
148
self ._fail_if_missing (
@@ -165,10 +160,10 @@ def test_panel_content_nonce_exists(self):
165
160
166
161
def test_missing (self ):
167
162
"""A `nonce` should not exist when not using the `CSPMiddleware`."""
168
- response = cast ( HttpResponse , self .client .get (path = "/regular/basic/" ) )
163
+ response = self .client .get (path = "/regular/basic/" )
169
164
self .assertEqual (response .status_code , 200 )
170
165
171
- html_root : Element = self .parser .parse (stream = response .content )
166
+ html_root = self .parser .parse (stream = response .content )
172
167
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
173
168
self .assertContains (response , "djDebug" )
174
169
0 commit comments