|  | 
| 78 | 78 | 
 | 
| 79 | 79 | logger = logging.getLogger("djangosaml2") | 
| 80 | 80 | 
 | 
|  | 81 | +# Update Content-Security-Policy headers for POST-Bindings | 
|  | 82 | +try: | 
|  | 83 | +    from csp.decorators import csp_update | 
|  | 84 | +except ModuleNotFoundError: | 
|  | 85 | +    # If csp is not installed, do not update fields as Content-Security-Policy | 
|  | 86 | +    # is not used | 
|  | 87 | +    def saml2_csp_update(view): | 
|  | 88 | +        return view | 
|  | 89 | + | 
|  | 90 | +    logger.warning("django-csp could not be found, not updating Content-Security-Policy. Please " | 
|  | 91 | +                   "make sure CSP is configured at least by httpd or setup django-csp. See " | 
|  | 92 | +                   "https://djangosaml2.readthedocs.io/contents/security.html#content-security-policy" | 
|  | 93 | +                   " for more information") | 
|  | 94 | +else: | 
|  | 95 | +    # script-src 'unsafe-inline' to autosubmit forms, | 
|  | 96 | +    # form-action https: to send data to IdPs | 
|  | 97 | +    saml2_csp_update = csp_update( | 
|  | 98 | +        SCRIPT_SRC=["'unsafe-inline'"], FORM_ACTION=["https:"] | 
|  | 99 | +    ) | 
|  | 100 | + | 
| 81 | 101 | 
 | 
| 82 | 102 | def _set_subject_id(session, subject_id): | 
| 83 | 103 |     session["_saml2_subject_id"] = code(subject_id) | 
| @@ -123,6 +143,7 @@ def get_state_client(self, request: HttpRequest): | 
| 123 | 143 |         return state, client | 
| 124 | 144 | 
 | 
| 125 | 145 | 
 | 
|  | 146 | +@method_decorator(saml2_csp_update, name='dispatch') | 
| 126 | 147 | class LoginView(SPConfigMixin, View): | 
| 127 | 148 |     """SAML Authorization Request initiator. | 
| 128 | 149 | 
 | 
| @@ -671,6 +692,7 @@ def get(self, request, *args, **kwargs): | 
| 671 | 692 |         ) | 
| 672 | 693 | 
 | 
| 673 | 694 | 
 | 
|  | 695 | +@method_decorator(saml2_csp_update, name='dispatch') | 
| 674 | 696 | class LogoutInitView(LoginRequiredMixin, SPConfigMixin, View): | 
| 675 | 697 |     """SAML Logout Request initiator | 
| 676 | 698 | 
 | 
| @@ -749,7 +771,7 @@ def handle_unsupported_slo_exception(self, request, exception, *args, **kwargs): | 
| 749 | 771 |         return HttpResponseRedirect(getattr(settings, "LOGOUT_REDIRECT_URL", "/")) | 
| 750 | 772 | 
 | 
| 751 | 773 | 
 | 
| 752 |  | -@method_decorator(csrf_exempt, name="dispatch") | 
|  | 774 | +@method_decorator([saml2_csp_update, csrf_exempt], name="dispatch") | 
| 753 | 775 | class LogoutView(SPConfigMixin, View): | 
| 754 | 776 |     """SAML Logout Response endpoint | 
| 755 | 777 | 
 | 
|  | 
0 commit comments