Skip to content

Commit 3040252

Browse files
author
Rob Hudson
committed
Update migration guide to include decorators
1 parent a2fe375 commit 3040252

File tree

1 file changed

+82
-23
lines changed

1 file changed

+82
-23
lines changed

docs/migration-guide.rst

Lines changed: 82 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Overview
99

1010
In the latest version of `django-csp`, the format for configuring Content Security Policy (CSP)
1111
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``.
1515

1616
Migrating from the Old Settings Format
1717
======================================
1818

19-
Step 1: Update `django-csp` Version
20-
-----------------------------------
19+
Update `django-csp`
20+
-------------------
2121

2222
First, update the `django-csp` package to the latest version that supports the new settings format.
2323
You can do this by running:
@@ -26,8 +26,8 @@ You can do this by running:
2626
2727
pip install -U django-csp
2828
29-
Step 2: Add the `csp` app to `INSTALLED_APPS`
30-
---------------------------------------------
29+
Add the `csp` app to `INSTALLED_APPS`
30+
-------------------------------------
3131

3232
In your Django project's `settings.py` file, add the `csp` app to the ``INSTALLED_APPS`` setting:
3333

@@ -39,10 +39,11 @@ In your Django project's `settings.py` file, add the `csp` app to the ``INSTALLE
3939
...
4040
]
4141
42-
Step 3: Run the Django check command
43-
------------------------------------
42+
Run the Django check command
43+
----------------------------
4444

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:
4647

4748
.. code-block:: bash
4849
@@ -51,22 +52,22 @@ Run the Django check command to get a settings config based on your existing CSP
5152
This can help you identify the existing CSP settings in your project and provide a starting point
5253
for migrating to the new format.
5354

54-
Step 4: Identify Existing CSP Settings
55-
--------------------------------------
55+
Identify Existing CSP Settings
56+
------------------------------
5657

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
5859
``CSP_`` prefix, such as ``CSP_DEFAULT_SRC``, ``CSP_SCRIPT_SRC``, ``CSP_IMG_SRC``, etc.
5960

60-
Step 5: Create the New Settings Dictionary
61-
------------------------------------------
61+
Create the New Settings Dictionary
62+
----------------------------------
6263

6364
In your Django project's `settings.py` file, create a new dictionary called
6465
``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.
6768

68-
Step 6: Migrate Existing Settings
69-
---------------------------------
69+
Migrate Existing Settings
70+
-------------------------
7071

7172
Migrate your existing CSP settings to the new format by populating the ``DIRECTIVES`` dictionary
7273
inside the ``CONTENT_SECURITY_POLICY`` setting. The keys of the ``DIRECTIVES`` dictionary should be the
@@ -95,15 +96,73 @@ The new settings would be:
9596
},
9697
}
9798
98-
Note that the directive names in the ``DIRECTIVES`` dictionary are in lowercase and use dashes instead
99-
of underscores.
99+
.. note::
100100

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+
-------------------
103122

104123
After migrating to the new settings format, remove all the old ``CSP_`` prefixed settings from your
105124
`settings.py` file.
106125

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+
107166
Conclusion
108167
==========
109168

0 commit comments

Comments
 (0)