@@ -7,29 +7,35 @@ Modifying the Policy with Decorators
7
7
Content Security Policies should be restricted and paranoid by default. You may, on some views,
8
8
need to expand or change the policy. django-csp includes four decorators to help.
9
9
10
+ All decorators take an optional keyword argument, ``REPORT_ONLY ``, which defaults to ``False ``. If
11
+ set to ``True ``, the decorator will update the report-only policy instead of the enforced policy.
10
12
11
13
``@csp_exempt ``
12
14
===============
13
15
14
16
Using the ``@csp_exempt `` decorator disables the CSP header on a given
15
17
view.
16
18
17
- ::
19
+ .. code-block :: python
18
20
19
21
from csp.decorators import csp_exempt
20
22
23
+
21
24
# Will not have a CSP header.
22
25
@csp_exempt ()
23
26
def myview (request ):
24
27
return render(... )
25
28
29
+
26
30
# Will not have a CSP report-only header.
27
31
@csp_exempt (REPORT_ONLY = True )
28
32
def myview (request ):
29
33
return render(... )
30
34
31
35
You can manually set this on a per-response basis by setting the ``_csp_exempt ``
32
- or ``_csp_exempt_ro `` attribute on the response to ``True ``::
36
+ or ``_csp_exempt_ro `` attribute on the response to ``True ``:
37
+
38
+ .. code-block :: python
33
39
34
40
# Also will not have a CSP header.
35
41
def myview (request ):
@@ -45,6 +51,7 @@ The ``@csp_update`` header allows you to **append** values to the source lists s
45
51
settings. If there is no setting, the value passed to the decorator will be used verbatim.
46
52
47
53
.. note ::
54
+
48
55
To quote the CSP spec: "There's no inheritance; ... the default list is not used for that
49
56
resource type" if it is set. E.g., the following will not allow images from 'self'::
50
57
@@ -55,15 +62,17 @@ decorator excpects a single dictionary argument, where the keys are the directiv
55
62
are either strings, lists or tuples. An optional argument, ``REPORT_ONLY ``, can be set to ``True ``
56
63
to update the report-only policy instead of the enforced policy.
57
64
58
- ::
65
+ .. code-block :: python
59
66
60
67
from csp.decorators import csp_update
61
68
69
+
62
70
# Will append imgsrv.com to the list of values for `img-src` in the enforced policy.
63
71
@csp_update ({" img-src" : " imgsrv.com" })
64
72
def myview (request ):
65
73
return render(... )
66
74
75
+
67
76
# Will append cdn-img.com to the list of values for `img-src` in the report-only policy.
68
77
@csp_update ({" img-src" : " cdn-img.com" }, REPORT_ONLY = True )
69
78
def myview (request ):
@@ -77,41 +86,66 @@ The ``@csp_replace`` decorator allows you to **replace** a source list specified
77
86
there is no setting, the value passed to the decorator will be used verbatim. (See the note under
78
87
``@csp_update ``.) If the specified value is None, the corresponding key will not be included.
79
88
80
- The arguments and values are the same as ``@csp_update ``::
89
+ The arguments and values are the same as ``@csp_update ``:
90
+
91
+ .. code-block :: python
81
92
82
93
from csp.decorators import csp_replace
83
94
95
+
84
96
# Will allow images only from imgsrv2.com in the enforced policy.
85
97
@csp_replace ({" img-src" : " imgsrv2.com" })
86
98
def myview (request ):
87
99
return render(... )
88
100
101
+
89
102
# Will allow images only from cdn-img2.com in the report-only policy.
90
103
@csp_replace ({" img-src" : " imgsrv2.com" })
91
104
def myview (request ):
92
105
return render(... )
93
106
107
+ The ``csp_replace `` decorator can also be used to remove a directive from the policy by setting the
108
+ value to ``None ``. For example, if the ``frame-ancestors `` directive is set in the Django settings
109
+ and you want to remove the ``frame-ancestors `` directive from the policy for this view:
110
+
111
+ .. code-block :: python
112
+
113
+ from csp.decorators import csp_replace
114
+
115
+
116
+ @csp_replace ({" frame-ancestors" : None })
117
+ def myview (request ):
118
+ return render(... )
119
+
94
120
95
121
``@csp ``
96
122
========
97
123
98
124
If you need to set the entire policy on a view, ignoring all the settings, you can use the ``@csp ``
99
- decorator. This, and the other decorators, can be stacked to update both policies if both are in
100
- use, as shown below. The arguments and values are as above::
125
+ decorator. This can be stacked to update both the enforced policy and the report-only policy if both
126
+ are in use, as shown below.
101
127
128
+ .. code-block :: python
129
+
130
+ from csp.constants import SELF , UNSAFE_INLINE
102
131
from csp.decorators import csp
103
132
104
- @csp({
105
- "default_src": ["'self'"],
106
- "img-src": ["imgsrv.com"],
107
- "script-src": ["scriptsrv.com", "googleanalytics.com", "'unsafe-inline'"]}
108
- })
109
- @csp({
110
- "default_src": ["'self'"],
111
- "img-src": ["imgsrv.com"],
112
- "script-src": ["scriptsrv.com", "googleanalytics.com"]},
113
- "frame-src": ["'self'"],
114
- REPORT_ONLY=True
115
- })
133
+
134
+ @csp (
135
+ {
136
+ " default_src" : [SELF ],
137
+ " img-src" : [" imgsrv.com" ],
138
+ " script-src" : [" scriptsrv.com" , " googleanalytics.com" , UNSAFE_INLINE ],
139
+ }
140
+ )
141
+ @csp (
142
+ {
143
+ " default_src" : [SELF ],
144
+ " img-src" : [" imgsrv.com" ],
145
+ " script-src" : [" scriptsrv.com" , " googleanalytics.com" ],
146
+ " frame-src" : [SELF ],
147
+ },
148
+ REPORT_ONLY = True ,
149
+ )
116
150
def myview (request ):
117
151
return render(... )
0 commit comments