|
| 1 | + |
| 2 | +# django-csp v4 Migration Guide |
| 3 | + |
| 4 | +## Overview |
| 5 | + |
| 6 | +In the latest version of `django-csp`, the format for configuring Content Security Policy (CSP) |
| 7 | +settings has been updated are are backwards-incompatible with prior versions. The previous approach |
| 8 | +of using individual settings prefixed with `CSP_` for each directive is no longer valid. Instead, |
| 9 | +all CSP settings are now consolidated into one of two dict-based settings: `CONTENT_SECURITY_POLICY` |
| 10 | +or `CONTENT_SECURITY_POLICY_REPORT_ONLY`. |
| 11 | + |
| 12 | +## Migrating from the Old Settings Format |
| 13 | + |
| 14 | +### Step 1: Identify Existing CSP Settings |
| 15 | + |
| 16 | +First, locate all the existing CSP settings in your Django project. These settings typically start |
| 17 | +with the `CSP_` prefix, such as `CSP_DEFAULT_SRC`, `CSP_SCRIPT_SRC`, `CSP_IMG_SRC`, and |
| 18 | +`CSP_EXCLUDE_URL_PREFIXES`. |
| 19 | + |
| 20 | +### Step 2: Create the New Settings Dictionary |
| 21 | + |
| 22 | +In your Django project's `settings.py` file, create a new dictionary called |
| 23 | +`CONTENT_SECURITY_POLICY` or `CONTENT_SECURITY_POLICY_REPORT_ONLY`, depending on whether you want to |
| 24 | +enforce the policy or report violations. |
| 25 | + |
| 26 | +```python |
| 27 | +CONTENT_SECURITY_POLICY = { |
| 28 | + "EXCLUDE_URL_PREFIXES": [], |
| 29 | + "DIRECTIVES": {}, |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +### Step 3: Migrate Existing Settings |
| 34 | + |
| 35 | +Migrate your existing CSP settings to the new format by populating the `DIRECTIVES` dictionary |
| 36 | +inside the `CONTENT_SECURITY_POLICY` setting. The keys of the `DIRECTIVES` dictionary should be the |
| 37 | +CSP directive names in lowercase, and the values should be lists containing the corresponding |
| 38 | +sources. |
| 39 | + |
| 40 | +For example, if you had the following old settings: |
| 41 | + |
| 42 | +```python |
| 43 | +CSP_DEFAULT_SRC = ["'self'", "*.example.com"] |
| 44 | +CSP_SCRIPT_SRC = ["'self'", "js.cdn.com/example/"] |
| 45 | +CSP_IMG_SRC = ["'self'", "data:", "example.com"] |
| 46 | +CSP_EXCLUDE_URL_PREFIXES = ["/admin"] |
| 47 | +``` |
| 48 | + |
| 49 | +The new settings would be: |
| 50 | + |
| 51 | +```python |
| 52 | +CONTENT_SECURITY_POLICY = { |
| 53 | + "EXCLUDE_URL_PREFIXES": ["/admin"], |
| 54 | + "DIRECTIVES": { |
| 55 | + "default-src": ["'self'", "*.example.com"], |
| 56 | + "script-src": ["'self'", "js.cdn.com/example/"], |
| 57 | + "img-src": ["'self'", "data:", "example.com"], |
| 58 | + }, |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +Note that the directive names in the `DIRECTIVES` dictionary are in lowercase and use dashes instead |
| 63 | +of underscores. |
| 64 | + |
| 65 | +### Step 4: Remove Old Settings |
| 66 | + |
| 67 | +After migrating to the new settings format, remove all the old `CSP_` prefixed settings from your |
| 68 | +`settings.py` file. |
| 69 | + |
| 70 | +### Step 5: Update the django-csp Version |
| 71 | + |
| 72 | +Finally, update your `django-csp` version to the latest version that support the new settings |
| 73 | +format. |
| 74 | + |
| 75 | +## Conclusion |
| 76 | + |
| 77 | +By following this migration guide, you should be able to successfully update your Django project to |
| 78 | +use the new dict-based CSP settings format introduced in the latest version of `django-csp`. This |
| 79 | +change aligns the package with the latest CSP specification and provides a more organized and |
| 80 | +flexible way to configure your Content Security Policy. |
0 commit comments