From d63241ebc7067fdebbaf704989b34fcd8f26bbe9 Mon Sep 17 00:00:00 2001 From: Rob Hudson Date: Sat, 3 May 2025 10:01:58 -0700 Subject: [PATCH] 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> --- django/conf/global_settings.py | 6 + django/core/checks/security/base.py | 21 ++ django/middleware/csp.py | 36 +++ django/template/context_processors.py | 8 + django/utils/csp.py | 110 +++++++++ docs/howto/csp.txt | 100 +++++++++ docs/howto/index.txt | 1 + docs/index.txt | 1 + docs/ref/checks.txt | 2 + docs/ref/csp.txt | 210 ++++++++++++++++++ docs/ref/index.txt | 1 + docs/ref/middleware.txt | 26 +++ docs/ref/settings.txt | 90 ++++++++ docs/ref/templates/api.txt | 12 + docs/releases/6.0.txt | 37 +++ docs/spelling_wordlist | 1 + docs/topics/security.txt | 55 +++++ tests/check_framework/test_security.py | 53 +++++ .../context_processors/csp_nonce.html | 17 ++ tests/context_processors/tests.py | 65 +++++- tests/context_processors/urls.py | 1 + tests/context_processors/views.py | 4 + tests/middleware/test_csp.py | 135 +++++++++++ tests/middleware/urls.py | 7 + tests/middleware/views.py | 28 +++ tests/utils_tests/test_csp.py | 166 ++++++++++++++ 26 files changed, 1192 insertions(+), 1 deletion(-) create mode 100644 django/middleware/csp.py create mode 100644 django/utils/csp.py create mode 100644 docs/howto/csp.txt create mode 100644 docs/ref/csp.txt create mode 100644 tests/context_processors/templates/context_processors/csp_nonce.html create mode 100644 tests/middleware/test_csp.py create mode 100644 tests/utils_tests/test_csp.py diff --git a/django/conf/global_settings.py b/django/conf/global_settings.py index 672c46b88f..a414d1428c 100644 --- a/django/conf/global_settings.py +++ b/django/conf/global_settings.py @@ -663,6 +663,12 @@ SECURE_REFERRER_POLICY = "same-origin" SECURE_SSL_HOST = None SECURE_SSL_REDIRECT = False +################## +# CSP MIDDLEWARE # +################## +SECURE_CSP = {} +SECURE_CSP_REPORT_ONLY = {} + # RemovedInDjango70Warning: A transitional setting helpful in early adoption of # HTTPS as the default protocol in urlize and urlizetrunc when no protocol is # provided. Set to True to assume HTTPS during the Django 6.x release cycle. 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) + ] diff --git a/django/middleware/csp.py b/django/middleware/csp.py new file mode 100644 index 0000000000..e1c66ada5a --- /dev/null +++ b/django/middleware/csp.py @@ -0,0 +1,36 @@ +from http import HTTPStatus + +from django.conf import settings +from django.utils.csp import CSP, LazyNonce, build_policy +from django.utils.deprecation import MiddlewareMixin + + +def get_nonce(request): + return getattr(request, "_csp_nonce", None) + + +class ContentSecurityPolicyMiddleware(MiddlewareMixin): + def process_request(self, request): + request._csp_nonce = LazyNonce() + + def process_response(self, request, response): + # In DEBUG mode, exclude CSP headers for specific status codes that + # trigger the debug view. + exempted_status_codes = { + HTTPStatus.NOT_FOUND, + HTTPStatus.INTERNAL_SERVER_ERROR, + } + if settings.DEBUG and response.status_code in exempted_status_codes: + return response + + nonce = get_nonce(request) + for header, config in [ + (CSP.HEADER_ENFORCE, settings.SECURE_CSP), + (CSP.HEADER_REPORT_ONLY, settings.SECURE_CSP_REPORT_ONLY), + ]: + # If headers are already set on the response, don't overwrite them. + # This allows for views to set their own CSP headers as needed. + if config and header not in response: + response.headers[str(header)] = build_policy(config, nonce) + + return response diff --git a/django/template/context_processors.py b/django/template/context_processors.py index 32753032fc..f9e5f218e4 100644 --- a/django/template/context_processors.py +++ b/django/template/context_processors.py @@ -10,6 +10,7 @@ of a DjangoTemplates backend and used by RequestContext. import itertools from django.conf import settings +from django.middleware.csp import get_nonce from django.middleware.csrf import get_token from django.utils.functional import SimpleLazyObject, lazy @@ -87,3 +88,10 @@ def media(request): def request(request): return {"request": request} + + +def csp(request): + """ + Add the CSP nonce to the context. + """ + return {"csp_nonce": get_nonce(request)} diff --git a/django/utils/csp.py b/django/utils/csp.py new file mode 100644 index 0000000000..b989a47c23 --- /dev/null +++ b/django/utils/csp.py @@ -0,0 +1,110 @@ +import secrets +from enum import StrEnum + +from django.utils.functional import SimpleLazyObject, empty + + +class CSP(StrEnum): + """ + Content Security Policy constants for directive values and special tokens. + + These constants represent: + 1. Standard quoted string values from the CSP spec (e.g., 'self', 'unsafe-inline') + 2. Special placeholder tokens (NONCE) that get replaced by the middleware + + Using this enum instead of raw strings provides better type checking, + autocompletion, and protection against common mistakes like: + + - Typos (e.g., 'noone' instead of 'none') + - Missing quotes (e.g., ["self"] instead of ["'self'"]) + - Inconsistent quote styles (e.g., ["'self'", "\"unsafe-inline\""]) + + Example usage in Django settings: + + SECURE_CSP = { + "default-src": [CSP.NONE], + "script-src": [CSP.SELF, CSP.NONCE], + } + + """ + + # HTTP Headers. + HEADER_ENFORCE = "Content-Security-Policy" + HEADER_REPORT_ONLY = "Content-Security-Policy-Report-Only" + + # Standard CSP directive values. + NONE = "'none'" + REPORT_SAMPLE = "'report-sample'" + SELF = "'self'" + STRICT_DYNAMIC = "'strict-dynamic'" + UNSAFE_EVAL = "'unsafe-eval'" + UNSAFE_HASHES = "'unsafe-hashes'" + UNSAFE_INLINE = "'unsafe-inline'" + WASM_UNSAFE_EVAL = "'wasm-unsafe-eval'" + + # Special placeholder that gets replaced by the middleware. + # The value itself is arbitrary and should not be mistaken for a real nonce. + NONCE = "" + + +class LazyNonce(SimpleLazyObject): + """ + Lazily generates a cryptographically secure nonce string, for use in CSP headers. + + The nonce is only generated when first accessed (e.g., via string + interpolation or inside a template). + + The nonce will evaluate as `True` if it has been generated, and `False` if + it has not. This is useful for third-party Django libraries that want to + support CSP without requiring it. + + Example Django template usage with context processors enabled: + + + + The `{% if %}` block will only render if the nonce has been evaluated elsewhere. + + """ + + def __init__(self): + super().__init__(self._generate) + + def _generate(self): + return secrets.token_urlsafe(16) + + def __bool__(self): + return self._wrapped is not empty + + +def build_policy(config, nonce=None): + policy = [] + + for directive, values in config.items(): + if values in (None, False): + continue + + if values is True: + rendered_value = "" + else: + if isinstance(values, set): + # Sort values for consistency, preventing cache invalidation + # between requests and ensuring reliable browser caching. + values = sorted(values) + elif not isinstance(values, list | tuple): + values = [values] + + # Replace the nonce sentinel with the actual nonce values, if the + # sentinel is found and a nonce is provided. Otherwise, remove it. + if (has_sentinel := CSP.NONCE in values) and nonce: + values = [f"'nonce-{nonce}'" if v == CSP.NONCE else v for v in values] + elif has_sentinel: + values = [v for v in values if v != CSP.NONCE] + + if not values: + continue + + rendered_value = " ".join(values) + + policy.append(f"{directive} {rendered_value}".rstrip()) + + return "; ".join(policy) diff --git a/docs/howto/csp.txt b/docs/howto/csp.txt new file mode 100644 index 0000000000..756f815bf2 --- /dev/null +++ b/docs/howto/csp.txt @@ -0,0 +1,100 @@ +=========================================== +How to use Django's Content Security Policy +=========================================== + +.. _csp-config: + +Basic config +============ + +To enable Content Security Policy (CSP) in your Django project: + +1. Add the CSP middleware to your :setting:`MIDDLEWARE` setting:: + + MIDDLEWARE = [ + # ... + "django.middleware.csp.ContentSecurityPolicyMiddleware", + # ... + ] + +2. Configure the CSP policies in your ``settings.py`` using either + :setting:`SECURE_CSP` or :setting:`SECURE_CSP_REPORT_ONLY` (or both). The + :ref:`CSP Settings docs ` provide more details about the + differences between these two:: + + from django.utils.csp import CSP + + # To enforce a CSP policy: + SECURE_CSP = { + "default-src": [CSP.SELF], + # Add more directives to be enforced. + } + + # Or for report-only mode: + SECURE_CSP_REPORT_ONLY = { + "default-src": [CSP.SELF], + # Add more directives as needed. + "report-uri": "/path/to/reports-endpoint/", + } + +.. _csp-nonce-config: + +Nonce config +============ + +To use nonces in your CSP policy, beside the basic config, you need to: + +1. Include the :attr:`~django.utils.csp.CSP.NONCE` placeholder value in the CSP + settings. This only applies to ``script-src`` or ``style-src`` directives:: + + from django.utils.csp import CSP + + SECURE_CSP = { + "default-src": [CSP.SELF], + # Allow self-hosted scripts and script tags with matching `nonce` attr. + "script-src": [CSP.SELF, CSP.NONCE], + # Example of the less secure 'unsafe-inline' option. + "style-src": [CSP.SELF, CSP.UNSAFE_INLINE], + } + +2. Add the :func:`~django.template.context_processors.csp` context processor to + your :setting:`TEMPLATES` setting. This makes the generated nonce value + available in the Django templates as the ``csp_nonce`` context variable:: + + TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "OPTIONS": { + "context_processors": [ + # ... + "django.template.context_processors.csp", + ], + }, + }, + ] + +3. In your templates, add the ``nonce`` attribute to the relevant inline + `` + + + +.. admonition:: Caching and Nonce Reuse + + The :class:`~django.middleware.csp.ContentSecurityPolicyMiddleware` + automatically handles generating a unique nonce and inserting the + appropriate ``nonce-`` source expression into the + ``Content-Security-Policy`` (or ``Content-Security-Policy-Report-Only``) + header when the nonce is used in a template. + + To ensure correct behavior, make sure both the HTML and the header are + generated within the same request and not served from cache. See the + reference documentation on :ref:`csp-nonce` for implementation details and + important caching considerations. diff --git a/docs/howto/index.txt b/docs/howto/index.txt index d49a9b1206..00acf5c837 100644 --- a/docs/howto/index.txt +++ b/docs/howto/index.txt @@ -57,6 +57,7 @@ Other guides :maxdepth: 1 auth-remote-user + csp csrf custom-file-storage custom-management-commands diff --git a/docs/index.txt b/docs/index.txt index 358c465df5..330e191e1c 100644 --- a/docs/index.txt +++ b/docs/index.txt @@ -251,6 +251,7 @@ applications and Django provides multiple protection tools and mechanisms: * :doc:`Cross Site Request Forgery protection ` * :doc:`Cryptographic signing ` * :ref:`Security Middleware ` +* :doc:`Content Security Policy ` Internationalization and localization ===================================== diff --git a/docs/ref/checks.txt b/docs/ref/checks.txt index b9cb1d19cf..bb54dbdb98 100644 --- a/docs/ref/checks.txt +++ b/docs/ref/checks.txt @@ -568,6 +568,8 @@ The following checks are run if you use the :option:`check --deploy` option: ``'django-insecure-'`` indicating that it was generated automatically by Django. Please generate a long and random value, otherwise many of Django's security-critical features will be vulnerable to attack. +* **security.E026**: The CSP setting ```` must be a dictionary + (got ```` instead). The following checks verify that your security-related settings are correctly configured: diff --git a/docs/ref/csp.txt b/docs/ref/csp.txt new file mode 100644 index 0000000000..e3666c9129 --- /dev/null +++ b/docs/ref/csp.txt @@ -0,0 +1,210 @@ +======================= +Content Security Policy +======================= + +.. versionadded:: 6.0 + +.. module:: django.middleware.csp + :synopsis: Middleware for Content Security Policy headers + +Content Security Policy (CSP) is a web security standard that helps prevent +content injection attacks by restricting the sources from which content can be +loaded. It plays an important role in a comprehensive :ref:`security strategy +`. + +For configuration instructions in a Django project, see the :ref:`Using CSP +` documentation. For an HTTP guide about CSP, see the `MDN Guide on +CSP `_. + +.. _csp-overview: + +Overview +======== + +The `Content-Security-Policy specification `_ +defines two complementary headers: + +* ``Content-Security-Policy``: Enforces the CSP policy, blocking content that + violates the defined directives. +* ``Content-Security-Policy-Report-Only``: Reports CSP violations without + blocking content, allowing for non-intrusive testing. + +Each policy is composed of one or more directives and their values, which +together instruct the browser on how to handle specific types of content. + +When the :class:`~django.middleware.csp.ContentSecurityPolicyMiddleware` is +enabled, Django automatically builds and attaches the appropriate headers to +each response based on the configured :ref:`settings `, unless +they have already been set by another layer. + +.. _csp-settings: + +Settings +======== + +The :class:`~django.middleware.csp.ContentSecurityPolicyMiddleware` is +configured using the following settings: + +* :setting:`SECURE_CSP`: defines the **enforced Content Security Policy**. +* :setting:`SECURE_CSP_REPORT_ONLY`: defines a **report-only Content Security Policy**. + +.. admonition:: These settings can be used independently or together + + * Use :setting:`SECURE_CSP` alone to enforce a policy that has already been + tested and verified. + * Use :setting:`SECURE_CSP_REPORT_ONLY` on its own to evaluate a new policy + without disrupting site behavior. This mode does not block violations, it + only logs them. It's useful for testing and monitoring, but provides no + protection against active threats. + * Use *both* to maintain an enforced baseline while experimenting with + changes. Even for well-established policies, continuing to collect reports + reports can help detect regressions, unexpected changes in behavior, or + potential tampering in production environments. + +.. _csp-reports: + +Policy violation reports +======================== + +When a CSP violation occurs, browsers typically log details to the developer +console, providing immediate feedback during development. To also receive these +reports programmatically, the policy must include a `reporting directive +`_ +such as ``report-uri`` that specifies where violation data should be sent. + +Django supports configuring these directives via the +:setting:`SECURE_CSP_REPORT_ONLY` settings, but reports will only be issued by +the browser if the policy explicitly includes a valid reporting directive. + +Django does not provide built-in functionality to receive, store, or process +violation reports. To collect and analyze them, you must implement your own +reporting endpoint or integrate with a third-party monitoring service. + +.. _csp-constants: + +CSP constants +============= + +Django provides predefined constants representing common CSP source expression +keywords such as ``'self'``, ``'none'``, and ``'unsafe-inline'``. These +constants are intended for use in the directive values defined in the settings. + +They are available through the :class:`~django.utils.csp.CSP` enum, and using +them is recommended over raw strings. This helps avoid common mistakes such as +typos, improper quoting, or inconsistent formatting, and ensures compliance +with the CSP specification. + +.. module:: django.utils.csp + :synopsis: Constants for Content Security Policy + +.. class:: CSP + + Enum providing standardized constants for common CSP source expressions. + + .. attribute:: NONE + + Represents ``'none'``. Blocks loading resources for the given directive. + + .. attribute:: REPORT_SAMPLE + + Represents ``'report-sample'``. Instructs the browser to include a sample + of the violating code in reports. Note that this may expose sensitive + data. + + .. attribute:: SELF + + Represents ``'self'``. Allows loading resources from the same origin + (same scheme, host, and port). + + .. attribute:: STRICT_DYNAMIC + + Represents ``'strict-dynamic'``. Allows execution of scripts loaded by a + trusted script (e.g., one with a valid nonce or hash), without needing + ``'unsafe-inline'``. + + .. attribute:: UNSAFE_EVAL + + Represents ``'unsafe-eval'``. Allows use of ``eval()`` and similar + JavaScript functions. Strongly discouraged. + + .. attribute:: UNSAFE_HASHES + + Represents ``'unsafe-hashes'``. Allows inline event handlers and some + ``javascript:`` URIs when their content hashes match a policy rule. + Requires CSP Level 3+. + + .. attribute:: UNSAFE_INLINE + + Represents ``'unsafe-inline'``. Allows execution of inline scripts, + styles, and ``javascript:`` URLs. Generally discouraged, especially for + scripts. + + .. attribute:: WASM_UNSAFE_EVAL + + Represents ``'wasm-unsafe-eval'``. Permits compilation and execution of + WebAssembly code without enabling ``'unsafe-eval'`` for scripts. + + .. attribute:: NONCE + + Django-specific placeholder value (``""``) used in + ``script-src`` or ``style-src`` directives to activate nonce-based CSP. + This string is replaced at runtime by the + :class:`~django.middleware.csp.ContentSecurityPolicyMiddleware` with a + secure, random nonce that is generated for each request. See detailed + explanation in :ref:`csp-nonce`. + +.. _csp-nonce: + +Nonce usage +=========== + +A CSP nonce ("number used once") is a unique, random value generated per HTTP +response. Django supports nonces as a secure way to allow specific inline +`` + + + diff --git a/tests/context_processors/tests.py b/tests/context_processors/tests.py index ba92ff8b05..737ff3e1cf 100644 --- a/tests/context_processors/tests.py +++ b/tests/context_processors/tests.py @@ -2,7 +2,8 @@ Tests for Django's bundled context processors. """ -from django.test import SimpleTestCase, TestCase, override_settings +from django.test import SimpleTestCase, TestCase, modify_settings, override_settings +from django.utils.csp import CSP @override_settings( @@ -96,3 +97,65 @@ class DebugContextProcessorTests(TestCase): self.assertContains(response, "Third query list: 2") # Check queries for DB connection 'other' self.assertContains(response, "Fourth query list: 3") + + +@override_settings( + ROOT_URLCONF="context_processors.urls", + TEMPLATES=[ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.csp", + ], + }, + } + ], + MIDDLEWARE=[ + "django.middleware.csp.ContentSecurityPolicyMiddleware", + ], + SECURE_CSP={ + "script-src": [CSP.SELF, CSP.NONCE], + }, +) +class CSPContextProcessorTests(TestCase): + """ + Tests for the django.template.context_processors.csp_nonce processor. + """ + + def test_csp_nonce_in_context(self): + response = self.client.get("/csp_nonce/") + self.assertIn("csp_nonce", response.context) + + @modify_settings( + MIDDLEWARE={"remove": "django.middleware.csp.ContentSecurityPolicyMiddleware"} + ) + def test_csp_nonce_in_context_no_middleware(self): + response = self.client.get("/csp_nonce/") + self.assertIn("csp_nonce", response.context) + + def test_csp_nonce_in_header(self): + response = self.client.get("/csp_nonce/") + self.assertIn(CSP.HEADER_ENFORCE, response.headers) + csp_header = response.headers[CSP.HEADER_ENFORCE] + nonce = response.context["csp_nonce"] + self.assertIn(f"'nonce-{nonce}'", csp_header) + + def test_different_nonce_per_request(self): + response1 = self.client.get("/csp_nonce/") + response2 = self.client.get("/csp_nonce/") + self.assertNotEqual( + response1.context["csp_nonce"], + response2.context["csp_nonce"], + ) + + def test_csp_nonce_in_template(self): + response = self.client.get("/csp_nonce/") + nonce = response.context["csp_nonce"] + self.assertIn(f'