@@ -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 ):
@@ -55,15 +61,17 @@ decorator excpects a single dictionary argument, where the keys are the directiv
55
61
are either strings, lists or tuples. An optional argument, ``REPORT_ONLY ``, can be set to ``True ``
56
62
to update the report-only policy instead of the enforced policy.
57
63
58
- ::
64
+ .. code-block :: python
59
65
60
66
from csp.decorators import csp_update
61
67
68
+
62
69
# Will append imgsrv.com to the list of values for `img-src` in the enforced policy.
63
70
@csp_update ({" img-src" : " imgsrv.com" })
64
71
def myview (request ):
65
72
return render(... )
66
73
74
+
67
75
# Will append cdn-img.com to the list of values for `img-src` in the report-only policy.
68
76
@csp_update ({" img-src" : " cdn-img.com" }, REPORT_ONLY = True )
69
77
def myview (request ):
@@ -77,41 +85,66 @@ The ``@csp_replace`` decorator allows you to **replace** a source list specified
77
85
there is no setting, the value passed to the decorator will be used verbatim. (See the note under
78
86
``@csp_update ``.) If the specified value is None, the corresponding key will not be included.
79
87
80
- The arguments and values are the same as ``@csp_update ``::
88
+ The arguments and values are the same as ``@csp_update ``:
89
+
90
+ .. code-block :: python
81
91
82
92
from csp.decorators import csp_replace
83
93
94
+
84
95
# Will allow images only from imgsrv2.com in the enforced policy.
85
96
@csp_replace ({" img-src" : " imgsrv2.com" })
86
97
def myview (request ):
87
98
return render(... )
88
99
100
+
89
101
# Will allow images only from cdn-img2.com in the report-only policy.
90
102
@csp_replace ({" img-src" : " imgsrv2.com" })
91
103
def myview (request ):
92
104
return render(... )
93
105
106
+ The ``csp_replace `` decorator can also be used to remove a directive from the policy by setting the
107
+ value to ``None ``. For example, if the ``frame-ancestors `` directive is set in the Django settings
108
+ and you want to remove the ``frame-ancestors `` directive from the policy for this view:
109
+
110
+ .. code-block :: python
111
+
112
+ from csp.decorators import csp_replace
113
+
114
+
115
+ @csp_replace ({" frame-ancestors" : None })
116
+ def myview (request ):
117
+ return render(... )
118
+
94
119
95
120
``@csp ``
96
121
========
97
122
98
123
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::
124
+ decorator. This can be stacked to update both the enforced policy and the report-only policy if both
125
+ are in use, as shown below.
101
126
127
+ .. code-block :: python
128
+
129
+ from csp.constants import SELF , UNSAFE_INLINE
102
130
from csp.decorators import csp
103
131
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
- })
132
+
133
+ @csp (
134
+ {
135
+ " default_src" : [SELF ],
136
+ " img-src" : [" imgsrv.com" ],
137
+ " script-src" : [" scriptsrv.com" , " googleanalytics.com" , UNSAFE_INLINE ],
138
+ }
139
+ )
140
+ @csp (
141
+ {
142
+ " default_src" : [SELF ],
143
+ " img-src" : [" imgsrv.com" ],
144
+ " script-src" : [" scriptsrv.com" , " googleanalytics.com" ],
145
+ " frame-src" : [SELF ],
146
+ },
147
+ REPORT_ONLY = True ,
148
+ )
116
149
def myview (request ):
117
150
return render(... )
0 commit comments