Skip to content

Commit 93b7c09

Browse files
committed
Make sure djangosaml2 works in csp-enabled applications too (fix #391)
1 parent df5c201 commit 93b7c09

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

djangosaml2/views.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,21 @@
7575
validate_referral_url,
7676
)
7777

78+
# Update Content-Security-Policy headers for POST-Bindings
79+
try:
80+
from csp.decorators import csp_update
81+
except ModuleNotFoundError:
82+
# If csp is not installed, do not update fields as Content-Security-Policy
83+
# is not used
84+
def saml2_csp_update(view):
85+
return view
86+
else:
87+
# script-src 'unsafe-inline' to autosubmit forms,
88+
# form-action https: to send data to IdPs
89+
saml2_csp_update = csp_update(
90+
SCRIPT_SRC=["'unsafe-inline'"], FORM_ACTION=["https:"]
91+
)
92+
7893
logger = logging.getLogger("djangosaml2")
7994

8095

@@ -109,6 +124,7 @@ def get_state_client(self, request: HttpRequest):
109124
return state, client
110125

111126

127+
@method_decorator(saml2_csp_update, name='dispatch')
112128
class LoginView(SPConfigMixin, View):
113129
"""SAML Authorization Request initiator.
114130
@@ -636,6 +652,7 @@ def get(self, request, *args, **kwargs):
636652
)
637653

638654

655+
@method_decorator(saml2_csp_update, name='dispatch')
639656
class LogoutInitView(LoginRequiredMixin, SPConfigMixin, View):
640657
"""SAML Logout Request initiator
641658
@@ -714,7 +731,7 @@ def handle_unsupported_slo_exception(self, request, exception, *args, **kwargs):
714731
return HttpResponseRedirect(getattr(settings, "LOGOUT_REDIRECT_URL", "/"))
715732

716733

717-
@method_decorator(csrf_exempt, name="dispatch")
734+
@method_decorator([saml2_csp_update, csrf_exempt], name="dispatch")
718735
class LogoutView(SPConfigMixin, View):
719736
"""SAML Logout Response endpoint
720737

0 commit comments

Comments
 (0)