Update dependency django-csp to v4 #163

Closed
renovate wants to merge 1 commit from renovate/django-csp-4.x into main
Collaborator

This PR contains the following updates:

Package Change Age Confidence
django-csp (changelog) ==3.8 → ==4.0 age confidence

Release Notes

mozilla/django-csp (django-csp)

v4.0

Compare Source

===

This release contains several breaking changes. For a complete migration guide, see:
https://django-csp.readthedocs.io/en/latest/migration-guide.html

Breaking Changes

  • Configuration Format: Moved to dict-based configuration which allows for setting policies for
    both enforced and report-only. Instead of using individual settings with CSP_ prefixes, you now
    use dictionaries called CONTENT_SECURITY_POLICY and/or CONTENT_SECURITY_POLICY_REPORT_ONLY.
    (#​219)

    You can use Django's check command to automatically identify existing CSP settings and generate a
    template for the new configuration format:

    python manage.py check
    

    This will detect your old CSP_ prefixed settings and output a draft of the new dict-based
    configuration, giving you a starting point for migration.

    Example:

    Change from:

    CSP_DEFAULT_SRC = ["'self'", "*.example.com"]
    CSP_SCRIPT_SRC = ["'self'", "js.cdn.com/example/"]
    CSP_IMG_SRC = ["'self'", "data:", "example.com"]
    CSP_EXCLUDE_URL_PREFIXES = ["/admin"]
    

    to:

    from csp.constants import SELF
    
    CONTENT_SECURITY_POLICY = {
        "DIRECTIVES": {
            "default-src": [SELF, "*.example.com"],
            "script-src": [SELF, "js.cdn.com/example/"],
            "img-src": [SELF, "data:", "example.com"],
        },
        "EXCLUDE_URL_PREFIXES": ["/admin"],
    }
    
  • Nonce Configuration: Switched from specifying directives that should contain nonces as a
    separate list to using a sentinel NONCE value in the directive itself.
    (#​223)

    Example:

    Change from:

    CSP_INCLUDE_NONCE_IN = ['script-src', 'style-src']
    

    to:

    from csp.constants import NONCE, SELF
    
    CONTENT_SECURITY_POLICY = {
        "DIRECTIVES": {
            "script-src": [SELF, NONCE],
            "style-src": [SELF, NONCE],
        }
    }
    
  • Nonce Behavior: Changed how request.csp_nonce works - it is now Falsy
    (bool(request.csp_nonce)) until it is read as a string (e.g., used in a template or with
    str(request.csp_nonce)). Previously, it always tested as True, and testing generated the nonce.
    (#​270)

    Before:

    # The nonce was generated when this was evaluated
    if request.csp_nonce:
        # Do something with nonce
    

    After:

    # This won't generate the nonce, and will evaluate to False until nonce is read as a string
    if request.csp_nonce:
        # This code won't run until nonce is used as a string
    
    # To generate and use the nonce
    nonce_value = str(request.csp_nonce)
    
  • Dropped support for Django ≤3.2.

  • Dropped support for Python 3.8.

New Features and Improvements

  • Dual Policy Support: Added support for enforced and report-only policies simultaneously using
    the separate CONTENT_SECURITY_POLICY and CONTENT_SECURITY_POLICY_REPORT_ONLY settings.

    Example:

    from csp.constants import NONE, SELF
    
    # Enforced policy
    CONTENT_SECURITY_POLICY = {
        "DIRECTIVES": {
            "default-src": [SELF, "cdn.example.net"],
            "frame-ancestors": [SELF],
        },
    }
    
    # Report-only policy (stricter for testing)
    CONTENT_SECURITY_POLICY_REPORT_ONLY = {
        "DIRECTIVES": {
            "default-src": [NONE],
            "script-src": [SELF],
            "style-src": [SELF],
            "report-uri": "https://example.com/csp-report/",
        },
    }
    
  • CSP Constants: Added CSP keyword constants in csp.constants (e.g., SELF instead of
    "'self'") to minimize quoting mistakes and typos.
    (#​222)

    Example:

    Change from:

    CSP_DEFAULT_SRC = ["'self'", "'none'"]
    

    to:

    from csp.constants import SELF, NONE
    
    CONTENT_SECURITY_POLICY = {
        "DIRECTIVES": {
            "default-src": [SELF, NONE],  # No need to worry about quoting
        }
    }
    
  • Added comprehensive type hints. (#​228)

  • Added EXCLUDE_URL_PREFIXES check not a string. (#​252)

  • Added support for CSP configuration as sets. (#​251)

  • Changed REPORT_PERCENTAGE to be a float between 0.0 and 100.0 and improved behavior for 100%
    report percentage to always send CSP reports.

  • Added ability to read the nonce after response if it was included in the header. This will raise
    an error when nonce is accessed after response if not already generated.
    (#​269)

  • Made changes to simplify middleware logic and make CSPMiddleware easier to subclass. The updated
    middleware returns a PolicyParts dataclass that can be modified before the policy is built.
    (#​237)

Other Changes

  • Added Python 3.13 support.
  • Added support for Django 5.1 and 5.2.
  • Documentation improvements including fixed trusted_types links and clarification on NONE vs Python's None.
  • Documentation note that reporting percentage needs rate limiting middleware.
  • Expanded ruff configuration and moved into pyproject.toml.

Configuration

📅 Schedule: Branch creation - Only on Sunday and Saturday ( * * * * 0,6 ) in timezone America/New_York, Automerge - Only on Sunday and Saturday ( * * * * 0,6 ) in timezone America/New_York.

🚦 Automerge: Disabled by config. Please merge this manually once you are satisfied.

♻ Rebasing: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 Ignore: Close this PR and you won't be reminded about this update again.


  • If you want to rebase/retry this PR, check this box

This PR has been generated by Renovate Bot.

This PR contains the following updates: | Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) | |---|---|---|---| | [django-csp](https://github.com/mozilla/django-csp) ([changelog](https://github.com/mozilla/django-csp/blob/main/CHANGES.md)) | `==3.8` → `==4.0` | ![age](https://developer.mend.io/api/mc/badges/age/pypi/django-csp/4.0?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/django-csp/3.8/4.0?slim=true) | --- ### Release Notes <details> <summary>mozilla/django-csp (django-csp)</summary> ### [`v4.0`](https://github.com/mozilla/django-csp/blob/HEAD/CHANGES.md#40) [Compare Source](https://github.com/mozilla/django-csp/compare/3.8...v4.0) \=== This release contains several breaking changes. For a complete migration guide, see: <https://django-csp.readthedocs.io/en/latest/migration-guide.html> #### Breaking Changes - **Configuration Format**: Moved to dict-based configuration which allows for setting policies for both enforced and report-only. Instead of using individual settings with `CSP_` prefixes, you now use dictionaries called `CONTENT_SECURITY_POLICY` and/or `CONTENT_SECURITY_POLICY_REPORT_ONLY`. ([#&#8203;219](https://github.com/mozilla/django-csp/pull/219)) You can use Django's check command to automatically identify existing CSP settings and generate a template for the new configuration format: ``` python manage.py check ``` This will detect your old `CSP_` prefixed settings and output a draft of the new dict-based configuration, giving you a starting point for migration. **Example:** Change from: ```python CSP_DEFAULT_SRC = ["'self'", "*.example.com"] CSP_SCRIPT_SRC = ["'self'", "js.cdn.com/example/"] CSP_IMG_SRC = ["'self'", "data:", "example.com"] CSP_EXCLUDE_URL_PREFIXES = ["/admin"] ``` to: ```python from csp.constants import SELF CONTENT_SECURITY_POLICY = { "DIRECTIVES": { "default-src": [SELF, "*.example.com"], "script-src": [SELF, "js.cdn.com/example/"], "img-src": [SELF, "data:", "example.com"], }, "EXCLUDE_URL_PREFIXES": ["/admin"], } ``` - **Nonce Configuration**: Switched from specifying directives that should contain nonces as a separate list to using a sentinel `NONCE` value in the directive itself. ([#&#8203;223](https://github.com/mozilla/django-csp/pull/223)) **Example:** Change from: ```python CSP_INCLUDE_NONCE_IN = ['script-src', 'style-src'] ``` to: ```python from csp.constants import NONCE, SELF CONTENT_SECURITY_POLICY = { "DIRECTIVES": { "script-src": [SELF, NONCE], "style-src": [SELF, NONCE], } } ``` - **Nonce Behavior**: Changed how `request.csp_nonce` works - it is now Falsy (`bool(request.csp_nonce)`) until it is read as a string (e.g., used in a template or with `str(request.csp_nonce)`). Previously, it always tested as `True`, and testing generated the nonce. ([#&#8203;270](https://github.com/mozilla/django-csp/pull/270)) **Before:** ```python # The nonce was generated when this was evaluated if request.csp_nonce: # Do something with nonce ``` **After:** ```python # This won't generate the nonce, and will evaluate to False until nonce is read as a string if request.csp_nonce: # This code won't run until nonce is used as a string # To generate and use the nonce nonce_value = str(request.csp_nonce) ``` - Dropped support for Django ≤3.2. - Dropped support for Python 3.8. #### New Features and Improvements - **Dual Policy Support**: Added support for enforced and report-only policies simultaneously using the separate `CONTENT_SECURITY_POLICY` and `CONTENT_SECURITY_POLICY_REPORT_ONLY` settings. **Example:** ```python from csp.constants import NONE, SELF # Enforced policy CONTENT_SECURITY_POLICY = { "DIRECTIVES": { "default-src": [SELF, "cdn.example.net"], "frame-ancestors": [SELF], }, } # Report-only policy (stricter for testing) CONTENT_SECURITY_POLICY_REPORT_ONLY = { "DIRECTIVES": { "default-src": [NONE], "script-src": [SELF], "style-src": [SELF], "report-uri": "https://example.com/csp-report/", }, } ``` - **CSP Constants**: Added CSP keyword constants in `csp.constants` (e.g., `SELF` instead of `"'self'"`) to minimize quoting mistakes and typos. ([#&#8203;222](https://github.com/mozilla/django-csp/pull/222)) **Example:** Change from: ```python CSP_DEFAULT_SRC = ["'self'", "'none'"] ``` to: ```python from csp.constants import SELF, NONE CONTENT_SECURITY_POLICY = { "DIRECTIVES": { "default-src": [SELF, NONE], # No need to worry about quoting } } ``` - Added comprehensive type hints. ([#&#8203;228](https://github.com/mozilla/django-csp/pull/228)) - Added `EXCLUDE_URL_PREFIXES` check not a string. ([#&#8203;252](https://github.com/mozilla/django-csp/pull/252)) - Added support for CSP configuration as sets. ([#&#8203;251](https://github.com/mozilla/django-csp/pull/251)) - Changed `REPORT_PERCENTAGE` to be a float between `0.0` and `100.0` and improved behavior for 100% report percentage to always send CSP reports. - Added ability to read the nonce after response if it was included in the header. This will raise an error when nonce is accessed after response if not already generated. ([#&#8203;269](https://github.com/mozilla/django-csp/pull/269)) - Made changes to simplify middleware logic and make `CSPMiddleware` easier to subclass. The updated middleware returns a PolicyParts dataclass that can be modified before the policy is built. ([#&#8203;237](https://github.com/mozilla/django-csp/pull/237)) #### Other Changes - Added Python 3.13 support. - Added support for Django 5.1 and 5.2. - Documentation improvements including fixed trusted\_types links and clarification on NONE vs Python's None. - Documentation note that reporting percentage needs rate limiting middleware. - Expanded ruff configuration and moved into pyproject.toml. </details> --- ### Configuration 📅 **Schedule**: Branch creation - Only on Sunday and Saturday ( * * * * 0,6 ) in timezone America/New_York, Automerge - Only on Sunday and Saturday ( * * * * 0,6 ) in timezone America/New_York. 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My40LjMiLCJ1cGRhdGVkSW5WZXIiOiI0My40LjMiLCJ0YXJnZXRCcmFuY2giOiJtYWluIiwibGFiZWxzIjpbXX0=-->
chrisw-b closed this pull request 2026-02-08 16:21:01 -05:00
Author
Collaborator

Renovate Ignore Notification

Because you closed this PR without merging, Renovate will ignore this update. You will not get PRs for any future 4.x releases. But if you manually upgrade to 4.x then Renovate will re-enable minor and patch updates automatically.

If you accidentally closed this PR, or if you changed your mind: rename this PR to get a fresh replacement PR.

### Renovate Ignore Notification Because you closed this PR without merging, Renovate will ignore this update. You will not get PRs for *any* future `4.x` releases. But if you manually upgrade to `4.x` then Renovate will re-enable `minor` and `patch` updates automatically. If you accidentally closed this PR, or if you changed your mind: rename this PR to get a fresh replacement PR.
Commenting is not possible because the repository is archived.
No reviewers
No labels
No milestone
No assignees
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
chrisw-b/bkwrm-container!163
No description provided.