@@ -43,31 +43,51 @@ a more slightly strict policy and is used to test the policy without breaking th
43
43
44
44
.. code-block :: python
45
45
46
+ from csp.constants import NONE , SELF
47
+
46
48
CONTENT_SECURITY_POLICY = {
47
49
" EXCLUDE_URL_PREFIXES" : [" /excluded-path/" ],
48
50
" DIRECTIVES" : {
49
- " default-src" : [" 'self' " , " cdn.example.net" ],
50
- " frame-ancestors" : [" 'self' " ],
51
- " form-action" : [" 'self' " ],
51
+ " default-src" : [SELF , " cdn.example.net" ],
52
+ " frame-ancestors" : [SELF ],
53
+ " form-action" : [SELF ],
52
54
" report-uri" : " /csp-report/" ,
53
55
},
54
56
}
55
57
56
58
CONTENT_SECURITY_POLICY_REPORT_ONLY = {
57
59
" EXCLUDE_URL_PREFIXES" : [" /excluded-path/" ],
58
60
" DIRECTIVES" : {
59
- " default-src" : [" 'none' " ],
60
- " connect-src" : [" 'self' " ],
61
- " img-src" : [" 'self' " ],
62
- " form-action" : [" 'self' " ],
63
- " frame-ancestors" : [" 'self' " ],
64
- " script-src" : [" 'self' " ],
65
- " style-src" : [" 'self' " ],
61
+ " default-src" : [NONE ],
62
+ " connect-src" : [SELF ],
63
+ " img-src" : [SELF ],
64
+ " form-action" : [SELF ],
65
+ " frame-ancestors" : [SELF ],
66
+ " script-src" : [SELF ],
67
+ " style-src" : [SELF ],
66
68
" upgrade-insecure-requests" : True ,
67
69
" report-uri" : " /csp-report/" ,
68
70
},
69
71
}
70
72
73
+ .. note ::
74
+
75
+ In the above example, the constant ``NONE `` is converted to the CSP keyword ``"'none'" `` and
76
+ is distinct from Python's ``None `` value. The CSP keyword ``'none' `` is a special value that
77
+ signifies that you do not want any sources for this directive. The ``None `` value is a
78
+ Python keyword that represents the absence of a value and when used as the value of a directive,
79
+ it will remove the directive from the policy, e.g. the following will remove the
80
+ ``frame-ancestors `` directive from the policy:
81
+
82
+ .. code-block :: python
83
+
84
+ CONTENT_SECURITY_POLICY = {
85
+ " DIRECTIVES" : {
86
+ # ...
87
+ " frame-ancestors" : None ,
88
+ }
89
+ }
90
+
71
91
72
92
Policy Settings
73
93
===============
@@ -101,8 +121,11 @@ policy.
101
121
102
122
.. note ::
103
123
The "special" source values of ``'self' ``, ``'unsafe-inline' ``, ``'unsafe-eval' ``,
104
- ``'none' `` and hash-source (``'sha256-...' ``) must be quoted!
105
- e.g.: ``"default-src": ["'self'"] ``. Without quotes they will not work as intended.
124
+ ``'strict-dynamic' ``, ``'none' ``, etc. must be quoted! e.g.: ``"default-src": ["'self'"] ``.
125
+ Without quotes they will not work as intended.
126
+
127
+ Consider using the ``csp.constants `` module to get these values to help avoiding quoting
128
+ errors or typos, e.g., ``from csp.constants import SELF, STRICT_DYNAMIC ``.
106
129
107
130
.. note ::
108
131
Deprecated features of CSP in general have been moved to the bottom of this list.
0 commit comments