21
21
MIDDLEWARE_CSP_LAST = settings .MIDDLEWARE + ["csp.middleware.CSPMiddleware" ]
22
22
23
23
24
- def get_namespaces (element : Element ) -> dict [ str , str ] :
24
+ def get_namespaces (element ) :
25
25
"""
26
26
Return the default `xmlns`. See
27
27
https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces
@@ -39,9 +39,8 @@ def setUp(self):
39
39
super ().setUp ()
40
40
self .parser = HTMLParser ()
41
41
42
- def _fail_if_missing (
43
- self , root : Element , path : str , namespaces : dict [str , str ], nonce : str
44
- ):
42
+ def _fail_if_missing (self , root , path , namespaces , nonce ):
43
+
45
44
"""
46
45
Search elements, fail if a `nonce` attribute is missing on them.
47
46
"""
@@ -50,7 +49,7 @@ def _fail_if_missing(
50
49
if item .attrib .get ("nonce" ) != nonce :
51
50
raise self .failureException (f"{ item } has no nonce attribute." )
52
51
53
- def _fail_if_found (self , root : Element , path : str , namespaces : dict [ str , str ] ):
52
+ def _fail_if_found (self , root , path , namespaces ):
54
53
"""
55
54
Search elements, fail if a `nonce` attribute is found on them.
56
55
"""
@@ -59,7 +58,7 @@ def _fail_if_found(self, root: Element, path: str, namespaces: dict[str, str]):
59
58
if "nonce" in item .attrib :
60
59
raise self .failureException (f"{ item } has a nonce attribute." )
61
60
62
- def _fail_on_invalid_html (self , content : bytes , parser : HTMLParser ):
61
+ def _fail_on_invalid_html (self , content , parser ):
63
62
"""Fail if the passed HTML is invalid."""
64
63
if parser .errors :
65
64
default_msg = ["Content is invalid HTML:" ]
@@ -74,10 +73,10 @@ def test_exists(self):
74
73
"""A `nonce` should exist when using the `CSPMiddleware`."""
75
74
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
76
75
with self .settings (MIDDLEWARE = middleware ):
77
- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
76
+ response = self .client .get (path = "/csp_view/" )
78
77
self .assertEqual (response .status_code , 200 )
79
78
80
- html_root : Element = self .parser .parse (stream = response .content )
79
+ html_root = self .parser .parse (stream = response .content )
81
80
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
82
81
self .assertContains (response , "djDebug" )
83
82
@@ -98,10 +97,10 @@ def test_does_not_exist_nonce_wasnt_used(self):
98
97
"""
99
98
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
100
99
with self .settings (MIDDLEWARE = middleware ):
101
- response = cast ( HttpResponse , self .client .get (path = "/regular/basic/" ) )
100
+ response = self .client .get (path = "/regular/basic/" )
102
101
self .assertEqual (response .status_code , 200 )
103
102
104
- html_root : Element = self .parser .parse (stream = response .content )
103
+ html_root = self .parser .parse (stream = response .content )
105
104
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
106
105
self .assertContains (response , "djDebug" )
107
106
@@ -119,15 +118,15 @@ def test_does_not_exist_nonce_wasnt_used(self):
119
118
def test_redirects_exists (self ):
120
119
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
121
120
with self .settings (MIDDLEWARE = middleware ):
122
- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
121
+ response = self .client .get (path = "/csp_view/" )
123
122
self .assertEqual (response .status_code , 200 )
124
123
125
- html_root : Element = self .parser .parse (stream = response .content )
124
+ html_root = self .parser .parse (stream = response .content )
126
125
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
127
126
self .assertContains (response , "djDebug" )
128
127
129
128
namespaces = get_namespaces (element = html_root )
130
- context : ContextList = response .context # pyright: ignore[reportAttributeAccessIssue]
129
+ context = response .context
131
130
nonce = str (context ["toolbar" ].csp_nonce )
132
131
self ._fail_if_missing (
133
132
root = html_root , path = ".//link" , namespaces = namespaces , nonce = nonce
@@ -139,14 +138,14 @@ def test_redirects_exists(self):
139
138
def test_panel_content_nonce_exists (self ):
140
139
for middleware in [MIDDLEWARE_CSP_BEFORE , MIDDLEWARE_CSP_LAST ]:
141
140
with self .settings (MIDDLEWARE = middleware ):
142
- response = cast ( HttpResponse , self .client .get (path = "/csp_view/" ) )
141
+ response = self .client .get (path = "/csp_view/" )
143
142
self .assertEqual (response .status_code , 200 )
144
143
145
144
toolbar = list (DebugToolbar ._store .values ())[- 1 ]
146
145
panels_to_check = ["HistoryPanel" , "TimerPanel" ]
147
146
for panel in panels_to_check :
148
147
content = toolbar .get_panel_by_id (panel ).content
149
- html_root : Element = self .parser .parse (stream = content )
148
+ html_root = self .parser .parse (stream = content )
150
149
namespaces = get_namespaces (element = html_root )
151
150
nonce = str (toolbar .csp_nonce )
152
151
self ._fail_if_missing (
@@ -164,10 +163,10 @@ def test_panel_content_nonce_exists(self):
164
163
165
164
def test_missing (self ):
166
165
"""A `nonce` should not exist when not using the `CSPMiddleware`."""
167
- response = cast ( HttpResponse , self .client .get (path = "/regular/basic/" ) )
166
+ response = self .client .get (path = "/regular/basic/" )
168
167
self .assertEqual (response .status_code , 200 )
169
168
170
- html_root : Element = self .parser .parse (stream = response .content )
169
+ html_root = self .parser .parse (stream = response .content )
171
170
self ._fail_on_invalid_html (content = response .content , parser = self .parser )
172
171
self .assertContains (response , "djDebug" )
173
172
0 commit comments