Releases: mozilla/django-csp
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 withCSP_
prefixes, you now
use dictionaries calledCONTENT_SECURITY_POLICY
and/orCONTENT_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 sentinelNONCE
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 asTrue
, 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 separateCONTENT_SECURITY_POLICY
andCONTENT_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 between0.0
and100.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.
New Contributors
- @robhudson made their first contribution in #218
- @jamesbeith made their first contribution in #233
- @sevdog made their first contribution in #226
- @jcari-dev made their first contribution in #238
- @crbunney made their first contribution in #237
- @janbrasna made their first contribution in #250
- @crgwbr made their first contribution in #251
- @adamchainz made their first contribution in #261
- @titouanc made their first contribution in #266
Full Changelog: 3.8...v4.0
4.0b7
What's Changed
- Remove
CSPMiddlewareAlwaysGenerateNonce
by @robhudson in #274
Full Changelog: 4.0b6...4.0b7
4.0b6
What's Changed
- Fix CSPMiddlewareAlwaysGenerateNonce by @jwhitlock in #272
- Prepare for 4.0b6 release by @jwhitlock in #273
Full Changelog: 4.0b5...4.0b6
4.0b5
What's Changed
- Allow reading nonce if it was included in header by @jwhitlock in #269
- Convert
FalseLazyObject
toCheckableLazyObject
, use to wrap nonce by @jwhitlock in #270 - Upgrade ReadTheDocs environment by @adamchainz in #262
- Prepare for 4.0b5 Release by @jwhitlock in #271
Full Changelog: 4.0b4...4.0b5
4.0b4
4.0b3
What's Changed
- Add Python 3.13, drop EOL Python 3.8 by @robhudson in #245
- docs: Fix trusted_types links by @janbrasna in #250
- Fix #249: Add
EXCLUDE_URL_PREFIXES
check by @robhudson in #252 - fix: support CSP configuration as sets by @crgwbr in #251
- docs: Note that reporting percentage needs rate limiting middleware by @janbrasna in #256
- Update project details by @robhudson in #257
- Fix #229: Document constant NONE vs Python's None by @robhudson in #255
- Fix #247: Raise error when nonce accessed after response by @robhudson in #258
- Test on Django 5.2 by @adamchainz in #261
New Contributors
- @janbrasna made their first contribution in #250
- @crgwbr made their first contribution in #251
- @adamchainz made their first contribution in #261
Full Changelog: 4.0b2...4.0b3
4.0b2
What's Changed
- Add missing report-only from csp replace example by @jamesbeith in #233
- Add type hints, fix mypy issues (#198) by @jwhitlock in #228
- Expand the ruff config to include import sorting and others. by @robhudson in #234
- Use simple logical operator in middleware by @sevdog in #226
- Docs: Fix typos in configuration.rst and decorators.rst by @jcari-dev in #238
- Fix #231: report percentage of 100% should always report by @robhudson in #236
- Fix a couple docs examples by @robhudson in #239
- refactor(middleware): Refactor internals of CSPMiddleware so that it's easier to extend existing logic without copy/pasting it into subclass by @crbunney in #237
- Fix #230: Make REPORT_PERCENTAGE a float by @robhudson in #242
- Add Django 5.1 to the test matrix by @robhudson in #243
- Prepare for 4.0b2 release by @robhudson in #244
New Contributors
- @jamesbeith made their first contribution in #233
- @sevdog made their first contribution in #226
- @jcari-dev made their first contribution in #238
- @crbunney made their first contribution in #237
Full Changelog: 4.0b1...4.0b2
4.0b1
Backwards-Incompatible Release - Beta for Community Testing
This release introduces significant changes that are not backwards compatible. We encourage all users to review the migration guide thoroughly before upgrading.
- This beta version includes substantial updates that may affect the behavior of your existing applications.
- Migration steps are necessary to ensure compatibility.
- We welcome community testing and feedback to identify any issues or improvements needed before a final release.
What's Changed
- Restructure CSP Configuration with Streamlined Settings (backwards incompatible) by @robhudson in #219
- Add constants for CSP keywords by @robhudson in #222
- Fix
report_only
toREPORT_ONLY
in decorator docs by @robhudson in #224 - Move to NONCE sentinel instead of 'include-nonce-in' by @robhudson in #223
New Contributors
- @robhudson made their first contribution in #218
Full Changelog: 3.8...4.0b1
3.8
Please note that 3.8 is Python-code-identical to 3.8rc1, and there were no regressions or problems noted or reported with 3.8rc0 nor 3.8rc1
django-csp
lives!
It's been more than a year since the last release and the project needed some refreshing before we can move forward with it.
This release aims to be functionally equivalent to 3.7, but with formal support for more modern Django and Python versions, all the way up to Django 5 on Python 3.12
Please see https://github.com/mozilla/django-csp/blob/3.8rc/CHANGES for a short summary of changes.
Feedback and bug reports are very welcome. π
What's Changed
- Update installation.rst by @Jesus805 in #163
- Fix 164 migrate ci by @g-k in #165
- Update test configuration to cover up to Py3.9 and Django 3.2 by @mkoistinen in #172
- Remove deprecation warning for child-src by @rik in #154
- add project_urls to setup.py by @pawl in #171
- Drop old Django and Python versions by @g-k in #175
- rename default branch by @g-k in #176
- Update CI badge to CircleCI by @g-k in #177
- fix unwrap script re by @g-k in #178
- Tweak configuration docs by @jaap3 in #146
- docs: add note about nonce value visibility by @g-k in #180
- GH-182 Update docs to clarify when nonce will not be added to headers by @DylanYoung in #185
- Remove outdated docs reference to MIDDLEWARE_CLASSES by @mlazar-endear in #193
- updating csp_replace decorator doc by @chestnutcone in #183
- Wrap the test install with quotes. by @tim-schilling in #200
- Reawaken development by @stevejalim in #204
- Add readthedocs config and slightly update Sphinx config by @stevejalim in #205
- Ensure docs building has access to django_csp itself by @stevejalim in #206
- Add Sphinx RTD theme by @stevejalim in #207
- Improve themeing in RTD by @stevejalim in #208
- Update settings documentation to move deprecated-within-csp settings to their own section, at the bottom by @stevejalim in #210
- MiddlewareMixin is always present in django>=3.2 by @asottile-sentry in #211
- Bring codebase up to modern Python using pyupgrade by @stevejalim in #213
- Update GH actions helpers to use Node 20-based versions by @stevejalim in #214
- Prepare for 3.8rc release by @stevejalim in #215
- Tomlify setup.py by @hmpf in #216
- Prepare for 3.8 final release by @stevejalim in #217
New Contributors
- @Jesus805 made their first contribution in #163
- @mkoistinen made their first contribution in #172
- @pawl made their first contribution in #171
- @DylanYoung made their first contribution in #185
- @mlazar-endear made their first contribution in #193
- @chestnutcone made their first contribution in #183
- @tim-schilling made their first contribution in #200
- @stevejalim made their first contribution in #204
- @asottile-sentry made their first contribution in #211
- @hmpf made their first contribution in #216
Full Changelog: 3.7...3.8
3.8rc1
Release candidate for django-csp 3.8
Please note that 3.8rc1 is almost identical to 3.8rc0, and there were no regressions or problems noted with 3.8rc0
It's been more than a year since the last release and the project needed some refreshing before we can move forward with it.
This release aims to be functionally equivalent to 3.7, but with formal support for more modern Django and Python versions, all the way up to Django 5 on Python 3.12
Please see https://github.com/mozilla/django-csp/blob/3.8rc1/CHANGES for a short summary of changes.
Feedback and bug reports are very welcome.
What's Changed in 3.8rc1
New Contributors to 3.8rc1
What's Changed in 3.8rc0 (based on commit history)
- Update installation.rst by @Jesus805 in #163
- Fix 164 migrate ci by @g-k in #165
- Update test configuration to cover up to Py3.9 and Django 3.2 by @mkoistinen in #172
- Remove deprecation warning for child-src by @rik in #154
- add project_urls to setup.py by @pawl in #171
- Drop old Django and Python versions by @g-k in #175
- rename default branch by @g-k in #176
- Update CI badge to CircleCI by @g-k in #177
- fix unwrap script re by @g-k in #178
- Tweak configuration docs by @jaap3 in #146
- docs: add note about nonce value visibility by @g-k in #180
- GH-182 Update docs to clarify when nonce will not be added to headers by @DylanYoung in #185
- Remove outdated docs reference to MIDDLEWARE_CLASSES by @mlazar-endear in #193
- updating csp_replace decorator doc by @chestnutcone in #183
- Wrap the test install with quotes. by @tim-schilling in #200
- Reawaken development by @stevejalim in #204
- Add readthedocs config and slightly update Sphinx config by @stevejalim in #205
- Ensure docs building has access to django_csp itself by @stevejalim in #206
- Add Sphinx RTD theme by @stevejalim in #207
- Improve themeing in RTD by @stevejalim in #208
- Update settings documentation to move deprecated-within-csp settings to their own section, at the bottom by @stevejalim in #210
- MiddlewareMixin is always present in django>=3.2 by @asottile-sentry in #211
- Bring codebase up to modern Python using pyupgrade by @stevejalim in #213
- Update GH actions helpers to use Node 20-based versions by @stevejalim in #214
- Prepare for 3.8rc release by @stevejalim in #215
New Contributors to 3.8rc0
- @Jesus805 made their first contribution in #163
- @mkoistinen made their first contribution in #172
- @pawl made their first contribution in #171
- @DylanYoung made their first contribution in #185
- @mlazar-endear made their first contribution in #193
- @chestnutcone made their first contribution in #183
- @tim-schilling made their first contribution in #200
- @stevejalim made their first contribution in #204
- @asottile-sentry made their first contribution in #211
Full Changelog: 3.7...3.8rc1