@@ -212,32 +212,18 @@ def add_idp_hinting(request, http_response) -> bool:
212
212
213
213
@cache
214
214
def get_csp_handler ():
215
+ """Returns a view decorator for CSP."""
216
+
215
217
def empty_view_decorator (view ):
216
218
return view
217
219
218
220
csp_handler_string = get_custom_setting ("SAML_CSP_HANDLER" , None )
219
221
220
222
if csp_handler_string is None :
221
223
# No CSP handler configured, attempt to use django-csp
222
- try :
223
- from csp .decorators import csp_update
224
- except ModuleNotFoundError :
225
- # If csp is not installed, do not update fields as Content-Security-Policy
226
- # is not used
227
- logger .warning (
228
- "django-csp could not be found, not updating Content-Security-Policy. Please "
229
- "make sure CSP is configured. This can be done by your reverse proxy, "
230
- "django-csp or a custom CSP handler via SAML_CSP_HANDLER. See "
231
- "https://djangosaml2.readthedocs.io/contents/security.html#content-security-policy"
232
- " for more information. "
233
- "This warning can be disabled by setting `SAML_CSP_HANDLER=''` in your settings."
234
- )
235
- return empty_view_decorator
236
- else :
237
- # script-src 'unsafe-inline' to autosubmit forms,
238
- # form-action https: to send data to IdPs
239
- return csp_update (SCRIPT_SRC = ["'unsafe-inline'" ], FORM_ACTION = ["https:" ])
240
- elif csp_handler_string .strip () != "" :
224
+ return _django_csp_update_decorator () or empty_view_decorator
225
+
226
+ if csp_handler_string .strip () != "" :
241
227
# Non empty string is configured, attempt to import it
242
228
csp_handler = import_string (csp_handler_string )
243
229
@@ -249,6 +235,28 @@ def wrapper(*args, **kwargs):
249
235
return wrapper
250
236
251
237
return custom_csp_updater
238
+
239
+ # Fall back to empty decorator when csp_handler_string is empty
240
+ return empty_view_decorator
241
+
242
+
243
+ def _django_csp_update_decorator ():
244
+ """Returns a view CSP decorator if django-csp is available, otherwise None."""
245
+ try :
246
+ from csp .decorators import csp_update
247
+ except ModuleNotFoundError :
248
+ # If csp is not installed, do not update fields as Content-Security-Policy
249
+ # is not used
250
+ logger .warning (
251
+ "django-csp could not be found, not updating Content-Security-Policy. Please "
252
+ "make sure CSP is configured. This can be done by your reverse proxy, "
253
+ "django-csp or a custom CSP handler via SAML_CSP_HANDLER. See "
254
+ "https://djangosaml2.readthedocs.io/contents/security.html#content-security-policy"
255
+ " for more information. "
256
+ "This warning can be disabled by setting `SAML_CSP_HANDLER=''` in your settings."
257
+ )
258
+ return
252
259
else :
253
- # Fall back to empty decorator when csp_handler_string is empty
254
- return empty_view_decorator
260
+ # script-src 'unsafe-inline' to autosubmit forms,
261
+ # form-action https: to send data to IdPs
262
+ return csp_update (SCRIPT_SRC = ["'unsafe-inline'" ], FORM_ACTION = ["https:" ])
0 commit comments