@@ -9,15 +9,15 @@ Overview
9
9
10
10
In the latest version of `django-csp `, the format for configuring Content Security Policy (CSP)
11
11
settings has been updated are are backwards-incompatible with prior versions. The previous approach
12
- of using individual settings prefixed with ``CSP_ `` for each directive is no longer valid. Instead,
13
- all CSP settings are now consolidated into one of two dict-based settings: `` CONTENT_SECURITY_POLICY ``
14
- or ``CONTENT_SECURITY_POLICY_REPORT_ONLY ``.
12
+ of using individual settings prefixed with ``CSP_ `` for each directive is no longer supported.
13
+ Instead, all CSP settings are now consolidated into one of two dict-based settings:
14
+ `` CONTENT_SECURITY_POLICY `` or ``CONTENT_SECURITY_POLICY_REPORT_ONLY ``.
15
15
16
16
Migrating from the Old Settings Format
17
17
======================================
18
18
19
- Step 1: Update `django-csp ` Version
20
- -----------------------------------
19
+ Update `django-csp `
20
+ -------------------
21
21
22
22
First, update the `django-csp ` package to the latest version that supports the new settings format.
23
23
You can do this by running:
@@ -26,8 +26,8 @@ You can do this by running:
26
26
27
27
pip install -U django-csp
28
28
29
- Step 2: Add the `csp ` app to `INSTALLED_APPS `
30
- ---------------------------------------------
29
+ Add the `csp ` app to `INSTALLED_APPS `
30
+ -------------------------------------
31
31
32
32
In your Django project's `settings.py ` file, add the `csp ` app to the ``INSTALLED_APPS `` setting:
33
33
@@ -39,10 +39,11 @@ In your Django project's `settings.py` file, add the `csp` app to the ``INSTALLE
39
39
...
40
40
]
41
41
42
- Step 3: Run the Django check command
43
- ------------------------------------
42
+ Run the Django check command
43
+ ----------------------------
44
44
45
- Run the Django check command to get a settings config based on your existing CSP settings:
45
+ This is optional but can help kick start the new settings configuration for you. Run the Django
46
+ check command which will look for old settings and output a configuration in the new format:
46
47
47
48
.. code-block :: bash
48
49
@@ -51,22 +52,22 @@ Run the Django check command to get a settings config based on your existing CSP
51
52
This can help you identify the existing CSP settings in your project and provide a starting point
52
53
for migrating to the new format.
53
54
54
- Step 4: Identify Existing CSP Settings
55
- --------------------------------------
55
+ Identify Existing CSP Settings
56
+ ------------------------------
56
57
57
- Locate all the existing CSP settings in your Django project. These settings typically start with the
58
+ Locate all the existing CSP settings in your Django project. These settings start with the
58
59
``CSP_ `` prefix, such as ``CSP_DEFAULT_SRC ``, ``CSP_SCRIPT_SRC ``, ``CSP_IMG_SRC ``, etc.
59
60
60
- Step 5: Create the New Settings Dictionary
61
- ------------------------------------------
61
+ Create the New Settings Dictionary
62
+ ----------------------------------
62
63
63
64
In your Django project's `settings.py ` file, create a new dictionary called
64
65
``CONTENT_SECURITY_POLICY `` or ``CONTENT_SECURITY_POLICY_REPORT_ONLY ``, depending on whether you want to
65
- enforce the policy or only report violations. Use the output from the Django check command as a
66
- starting point to populate this dictionary.
66
+ enforce the policy or only report violations, or both . Use the output from the Django check command
67
+ as a starting point to populate this dictionary.
67
68
68
- Step 6: Migrate Existing Settings
69
- ---------------------------------
69
+ Migrate Existing Settings
70
+ -------------------------
70
71
71
72
Migrate your existing CSP settings to the new format by populating the ``DIRECTIVES `` dictionary
72
73
inside the ``CONTENT_SECURITY_POLICY `` setting. The keys of the ``DIRECTIVES `` dictionary should be the
@@ -95,15 +96,73 @@ The new settings would be:
95
96
},
96
97
}
97
98
98
- Note that the directive names in the ``DIRECTIVES `` dictionary are in lowercase and use dashes instead
99
- of underscores.
99
+ .. note ::
100
100
101
- Step 7: Remove Old Settings
102
- ---------------------------
101
+ The keys in the ``DIRECTIVES `` dictionary, the directive names, are in lowercase and use dashes
102
+ instead of underscores to match the CSP specification.
103
+
104
+ .. note ::
105
+
106
+ If you were using the ``CSP_REPORT_PERCENTAGE `` setting, this should be updated to be an integer
107
+ percentage and not a decimal value in the new settings format. For example, if you had
108
+ ``CSP_REPORT_PERCENTAGE = 0.1 ``, this should be updated to:
109
+
110
+ .. code-block :: python
111
+
112
+ CONTENT_SECURITY_POLICY = {
113
+ " REPORT_PERCENTAGE" : 10 ,
114
+ " DIRECTIVES" : {
115
+ " report-uri" : " /csp-report/" ,
116
+ ...
117
+ },
118
+ }
119
+
120
+ Remove Old Settings
121
+ -------------------
103
122
104
123
After migrating to the new settings format, remove all the old ``CSP_ `` prefixed settings from your
105
124
`settings.py ` file.
106
125
126
+ Update the CSP decorators
127
+ -------------------------
128
+
129
+ If you are using the CSP decorators in your views, those will need to be updated as well. The
130
+ decorators now accept a dictionary containing the CSP directives as an argument. For example:
131
+
132
+ .. code-block :: python
133
+
134
+ from csp.decorators import csp_update
135
+
136
+ @csp_update ({" default-src" : [" another-url.com" ]})
137
+ def my_view (request ):
138
+ ...
139
+
140
+ Additionally, each decorator now takes an optional ``report_only `` argument to specify whether the
141
+ policy should be enforced or only report violations. For example:
142
+
143
+ .. code-block :: python
144
+
145
+ from csp.decorators import csp
146
+
147
+ @csp ({" default-src" : [" 'self'" ]}, report_only = True )
148
+ def my_view (request ):
149
+ ...
150
+
151
+ Due to the addition of the ``report_only `` argument and for consistency, the ``csp_exempt ``
152
+ decorator now requires parentheses when used with and without arguments. For example:
153
+
154
+ .. code-block :: python
155
+
156
+ from csp.decorators import csp_exempt
157
+
158
+ @csp_exempt ()
159
+ @csp_exempt (report_only = True )
160
+ def my_view (request ):
161
+ ...
162
+
163
+ Look for uses of the following decorators in your code: ``@csp ``, ``@csp_update ``, ``@csp_replace ``,
164
+ and ``@csp_exempt ``.
165
+
107
166
Conclusion
108
167
==========
109
168
0 commit comments