|
13 | 13 |
|
14 | 14 | from .base import IntegrationTestCase |
15 | 15 |
|
| 16 | +MIDDLEWARE_CSP_BEFORE = settings.MIDDLEWARE.copy() |
| 17 | +MIDDLEWARE_CSP_BEFORE.insert( |
| 18 | + MIDDLEWARE_CSP_BEFORE.index("debug_toolbar.middleware.DebugToolbarMiddleware"), |
| 19 | + "csp.middleware.CSPMiddleware", |
| 20 | +) |
| 21 | +MIDDLEWARE_CSP_LAST = settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"] |
| 22 | + |
16 | 23 |
|
17 | 24 | def get_namespaces(element: Element) -> dict[str, str]: |
18 | 25 | """ |
@@ -63,70 +70,97 @@ def _fail_on_invalid_html(self, content: bytes, parser: HTMLParser): |
63 | 70 | msg = self._formatMessage(None, "\n".join(default_msg)) |
64 | 71 | raise self.failureException(msg) |
65 | 72 |
|
66 | | - @override_settings( |
67 | | - MIDDLEWARE=settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"] |
68 | | - ) |
69 | 73 | def test_exists(self): |
70 | 74 | """A `nonce` should exist when using the `CSPMiddleware`.""" |
71 | | - response = cast(HttpResponse, self.client.get(path="/regular/basic/")) |
72 | | - self.assertEqual(response.status_code, 200) |
73 | | - |
74 | | - html_root: Element = self.parser.parse(stream=response.content) |
75 | | - self._fail_on_invalid_html(content=response.content, parser=self.parser) |
76 | | - self.assertContains(response, "djDebug") |
77 | | - |
78 | | - namespaces = get_namespaces(element=html_root) |
79 | | - toolbar = list(DebugToolbar._store.values())[0] |
80 | | - nonce = str(toolbar.request.csp_nonce) |
81 | | - self._fail_if_missing( |
82 | | - root=html_root, path=".//link", namespaces=namespaces, nonce=nonce |
83 | | - ) |
84 | | - self._fail_if_missing( |
85 | | - root=html_root, path=".//script", namespaces=namespaces, nonce=nonce |
86 | | - ) |
| 75 | + for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]: |
| 76 | + with self.settings(MIDDLEWARE=middleware): |
| 77 | + response = cast(HttpResponse, self.client.get(path="/csp_view/")) |
| 78 | + self.assertEqual(response.status_code, 200) |
| 79 | + |
| 80 | + html_root: Element = self.parser.parse(stream=response.content) |
| 81 | + self._fail_on_invalid_html(content=response.content, parser=self.parser) |
| 82 | + self.assertContains(response, "djDebug") |
| 83 | + |
| 84 | + namespaces = get_namespaces(element=html_root) |
| 85 | + toolbar = list(DebugToolbar._store.values())[-1] |
| 86 | + nonce = str(toolbar.csp_nonce) |
| 87 | + self._fail_if_missing( |
| 88 | + root=html_root, path=".//link", namespaces=namespaces, nonce=nonce |
| 89 | + ) |
| 90 | + self._fail_if_missing( |
| 91 | + root=html_root, path=".//script", namespaces=namespaces, nonce=nonce |
| 92 | + ) |
| 93 | + |
| 94 | + def test_does_not_exist_nonce_wasnt_used(self): |
| 95 | + """ |
| 96 | + A `nonce` should not exist even when using the `CSPMiddleware` |
| 97 | + if the view didn't access the request.csp_nonce attribute. |
| 98 | + """ |
| 99 | + for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]: |
| 100 | + with self.settings(MIDDLEWARE=middleware): |
| 101 | + response = cast(HttpResponse, self.client.get(path="/regular/basic/")) |
| 102 | + self.assertEqual(response.status_code, 200) |
| 103 | + |
| 104 | + html_root: Element = self.parser.parse(stream=response.content) |
| 105 | + self._fail_on_invalid_html(content=response.content, parser=self.parser) |
| 106 | + self.assertContains(response, "djDebug") |
| 107 | + |
| 108 | + namespaces = get_namespaces(element=html_root) |
| 109 | + self._fail_if_found( |
| 110 | + root=html_root, path=".//link", namespaces=namespaces |
| 111 | + ) |
| 112 | + self._fail_if_found( |
| 113 | + root=html_root, path=".//script", namespaces=namespaces |
| 114 | + ) |
87 | 115 |
|
88 | 116 | @override_settings( |
89 | 117 | DEBUG_TOOLBAR_CONFIG={"DISABLE_PANELS": set()}, |
90 | | - MIDDLEWARE=settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"], |
91 | 118 | ) |
92 | 119 | def test_redirects_exists(self): |
93 | | - response = cast(HttpResponse, self.client.get(path="/regular/basic/")) |
94 | | - self.assertEqual(response.status_code, 200) |
| 120 | + for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]: |
| 121 | + with self.settings(MIDDLEWARE=middleware): |
| 122 | + response = cast(HttpResponse, self.client.get(path="/csp_view/")) |
| 123 | + self.assertEqual(response.status_code, 200) |
| 124 | + |
| 125 | + html_root: Element = self.parser.parse(stream=response.content) |
| 126 | + self._fail_on_invalid_html(content=response.content, parser=self.parser) |
| 127 | + self.assertContains(response, "djDebug") |
| 128 | + |
| 129 | + namespaces = get_namespaces(element=html_root) |
| 130 | + context: ContextList = response.context # pyright: ignore[reportAttributeAccessIssue] |
| 131 | + nonce = str(context["toolbar"].csp_nonce) |
| 132 | + self._fail_if_missing( |
| 133 | + root=html_root, path=".//link", namespaces=namespaces, nonce=nonce |
| 134 | + ) |
| 135 | + self._fail_if_missing( |
| 136 | + root=html_root, path=".//script", namespaces=namespaces, nonce=nonce |
| 137 | + ) |
95 | 138 |
|
96 | | - html_root: Element = self.parser.parse(stream=response.content) |
97 | | - self._fail_on_invalid_html(content=response.content, parser=self.parser) |
98 | | - self.assertContains(response, "djDebug") |
99 | | - |
100 | | - namespaces = get_namespaces(element=html_root) |
101 | | - context: ContextList = response.context # pyright: ignore[reportAttributeAccessIssue] |
102 | | - nonce = str(context["toolbar"].request.csp_nonce) |
103 | | - self._fail_if_missing( |
104 | | - root=html_root, path=".//link", namespaces=namespaces, nonce=nonce |
105 | | - ) |
106 | | - self._fail_if_missing( |
107 | | - root=html_root, path=".//script", namespaces=namespaces, nonce=nonce |
108 | | - ) |
109 | | - |
110 | | - @override_settings( |
111 | | - MIDDLEWARE=settings.MIDDLEWARE + ["csp.middleware.CSPMiddleware"] |
112 | | - ) |
113 | 139 | def test_panel_content_nonce_exists(self): |
114 | | - response = cast(HttpResponse, self.client.get(path="/regular/basic/")) |
115 | | - self.assertEqual(response.status_code, 200) |
116 | | - |
117 | | - toolbar = list(DebugToolbar._store.values())[0] |
118 | | - panels_to_check = ["HistoryPanel", "TimerPanel"] |
119 | | - for panel in panels_to_check: |
120 | | - content = toolbar.get_panel_by_id(panel).content |
121 | | - html_root: Element = self.parser.parse(stream=content) |
122 | | - namespaces = get_namespaces(element=html_root) |
123 | | - nonce = str(toolbar.request.csp_nonce) |
124 | | - self._fail_if_missing( |
125 | | - root=html_root, path=".//link", namespaces=namespaces, nonce=nonce |
126 | | - ) |
127 | | - self._fail_if_missing( |
128 | | - root=html_root, path=".//script", namespaces=namespaces, nonce=nonce |
129 | | - ) |
| 140 | + for middleware in [MIDDLEWARE_CSP_BEFORE, MIDDLEWARE_CSP_LAST]: |
| 141 | + with self.settings(MIDDLEWARE=middleware): |
| 142 | + response = cast(HttpResponse, self.client.get(path="/csp_view/")) |
| 143 | + self.assertEqual(response.status_code, 200) |
| 144 | + |
| 145 | + toolbar = list(DebugToolbar._store.values())[-1] |
| 146 | + panels_to_check = ["HistoryPanel", "TimerPanel"] |
| 147 | + for panel in panels_to_check: |
| 148 | + content = toolbar.get_panel_by_id(panel).content |
| 149 | + html_root: Element = self.parser.parse(stream=content) |
| 150 | + namespaces = get_namespaces(element=html_root) |
| 151 | + nonce = str(toolbar.csp_nonce) |
| 152 | + self._fail_if_missing( |
| 153 | + root=html_root, |
| 154 | + path=".//link", |
| 155 | + namespaces=namespaces, |
| 156 | + nonce=nonce, |
| 157 | + ) |
| 158 | + self._fail_if_missing( |
| 159 | + root=html_root, |
| 160 | + path=".//script", |
| 161 | + namespaces=namespaces, |
| 162 | + nonce=nonce, |
| 163 | + ) |
130 | 164 |
|
131 | 165 | def test_missing(self): |
132 | 166 | """A `nonce` should not exist when not using the `CSPMiddleware`.""" |
|
0 commit comments