Skip to content

Commit e19c1d1

Browse files
committed
Split out django-csp handler logic
1 parent 0387327 commit e19c1d1

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

djangosaml2/utils.py

+29-21
Original file line numberDiff line numberDiff line change
@@ -212,32 +212,18 @@ def add_idp_hinting(request, http_response) -> bool:
212212

213213
@cache
214214
def get_csp_handler():
215+
"""Returns a view decorator for CSP."""
216+
215217
def empty_view_decorator(view):
216218
return view
217219

218220
csp_handler_string = get_custom_setting("SAML_CSP_HANDLER", None)
219221

220222
if csp_handler_string is None:
221223
# 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() != "":
241227
# Non empty string is configured, attempt to import it
242228
csp_handler = import_string(csp_handler_string)
243229

@@ -249,6 +235,28 @@ def wrapper(*args, **kwargs):
249235
return wrapper
250236

251237
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
252259
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

Comments
 (0)