diff options
| author | Rob Hudson <rob@cogit8.org> | 2025-05-03 10:01:58 -0700 |
|---|---|---|
| committer | nessita <124304+nessita@users.noreply.github.com> | 2025-06-27 15:57:02 -0300 |
| commit | d63241ebc7067fdebbaf704989b34fcd8f26bbe9 (patch) | |
| tree | 07b5a5cb0c70c446f5f0fb9ad2834501fc3d6544 /django/core | |
| parent | 3f59711581bd22ebd0f13fb040b15b69c0eee21f (diff) | |
Fixed #15727 -- Added Content Security Policy (CSP) support.
This initial work adds a pair of settings to configure specific CSP
directives for enforcing or reporting policy violations, a new
`django.middleware.csp.ContentSecurityPolicyMiddleware` to apply the
appropriate headers to responses, and a context processor to support CSP
nonces in templates for safely inlining assets.
Relevant documentation has been added for the 6.0 release notes,
security overview, a new how-to page, and a dedicated reference section.
Thanks to the multiple reviewers for their precise and valuable feedback.
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
Diffstat (limited to 'django/core')
| -rw-r--r-- | django/core/checks/security/base.py | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/django/core/checks/security/base.py b/django/core/checks/security/base.py index f85adabd1a..9506052196 100644 --- a/django/core/checks/security/base.py +++ b/django/core/checks/security/base.py @@ -141,6 +141,11 @@ E024 = Error( W025 = Warning(SECRET_KEY_WARNING_MSG, id="security.W025") +E026 = Error( + "The Content Security Policy setting '%s' must be a dictionary (got %r instead).", + id="security.E026", +) + def _security_middleware(): return "django.middleware.security.SecurityMiddleware" in settings.MIDDLEWARE @@ -281,3 +286,19 @@ def check_cross_origin_opener_policy(app_configs, **kwargs): ): return [E024] return [] + + +@register(Tags.security) +def check_csp_settings(app_configs, **kwargs): + """ + Validate that CSP settings are properly configured when enabled. + + Ensures both SECURE_CSP and SECURE_CSP_REPORT_ONLY are dictionaries. + """ + # CSP settings must be a dictionary or None. + return [ + Error(E026.msg % (name, value), id=E026.id) + for name in ("SECURE_CSP", "SECURE_CSP_REPORT_ONLY") + if (value := getattr(settings, name, None)) is not None + and not isinstance(value, dict) + ] |
