Configuring django-csp
Content-Security-Policy is a complicated header. There are many values you may need to tweak here.
It’s worth reading the latest CSP spec and making sure you understand it before configuring django-csp.
Note
Many settings require a tuple or list. You may get very strange
policies and even errors when mistakenly configuring them as a string.
Policy Settings
These settings affect the policy in the header. The defaults are in italics.
Note
Deprecated features of CSP in general have been moved to the bottom of this list.
Warning
The “special” source values of 'self', 'unsafe-inline',
'unsafe-eval', 'none' and hash-source ('sha256-...') must be
quoted! e.g.: CSP_DEFAULT_SRC = ("'self'",). Without quotes they will
not work as intended.
CSP_DEFAULT_SRCSet the
default-srcdirective. Atupleorlistof values, e.g.:("'self'", 'cdn.example.net'). [“‘self’”]CSP_SCRIPT_SRCSet the
script-srcdirective. Atupleorlist. NoneCSP_SCRIPT_SRC_ATTRSet the
script-src-attrdirective. Atupleorlist. NoneCSP_SCRIPT_SRC_ELEMSet the
script-src-elemdirective. Atupleorlist. NoneCSP_IMG_SRCSet the
img-srcdirective. Atupleorlist. NoneCSP_OBJECT_SRCSet the
object-srcdirective. Atupleorlist. NoneCSP_MEDIA_SRCSet the
media-srcdirective. Atupleorlist. NoneCSP_FRAME_SRCSet the
frame-srcdirective. Atupleorlist. NoneCSP_FONT_SRCSet the
font-srcdirective. Atupleorlist. NoneCSP_CONNECT_SRCSet the
connect-srcdirective. Atupleorlist. NoneCSP_STYLE_SRCSet the
style-srcdirective. Atupleorlist. NoneCSP_STYLE_SRC_ATTRSet the
style-src-attrdirective. Atupleorlist. NoneCSP_STYLE_SRC_ELEMSet the
style-src-elemdirective. Atupleorlist. NoneCSP_BASE_URISet the
base-uridirective. Atupleorlist. NoneNote: This doesn’t use
default-srcas a fall-back.CSP_CHILD_SRCSet the
child-srcdirective. Atupleorlist. NoneCSP_FRAME_ANCESTORSSet the
frame-ancestorsdirective. Atupleorlist. NoneNote: This doesn’t use
default-srcas a fall-back.CSP_NAVIGATE_TOSet the
navigate-todirective. Atupleorlist. NoneNote: This doesn’t use
default-srcas a fall-back.CSP_FORM_ACTIONSet the
FORM_ACTIONdirective. Atupleorlist. NoneNote: This doesn’t use
default-srcas a fall-back.CSP_SANDBOXSet the
sandboxdirective. Atupleorlist. NoneNote: This doesn’t use
default-srcas a fall-back.CSP_REPORT_URISet the
report-uridirective. Atupleorlistof URIs. Each URI can be a full or relative URI. NoneNote: This doesn’t use
default-srcas a fall-back.CSP_REPORT_TOSet the
report-todirective. Astringdescribing a reporting group. NoneSee Section 1.2: https://w3c.github.io/reporting/#group
Also see this MDN note on
report-uriandreport-to.CSP_MANIFEST_SRCSet the
manifest-srcdirective. Atupleorlist. NoneCSP_WORKER_SRCSet the
worker-srcdirective. Atupleorlist. NoneCSP_REQUIRE_SRI_FORSet the
require-sri-fordirective. Atupleorlist. NoneValid values: a
listcontaining'script','style', or both.CSP_UPGRADE_INSECURE_REQUESTSInclude
upgrade-insecure-requestsdirective. Aboolean. FalseCSP_REQUIRE_TRUSTED_TYPES_FORInclude
require-trusted-types-fordirective. Atupleorlist. NoneValid values:
["'script'"]CSP_TRUSTED_TYPESInclude
trusted-typesdirective. Atupleorlist. NoneValid values: a
listof allowed policy names that may includedefaultand/or'allow-duplicates'CSP_INCLUDE_NONCE_INInclude dynamically generated nonce in all listed directives. A
tupleorlist, e.g.:CSP_INCLUDE_NONCE_IN = ['script-src']will add'nonce-<b64-value>'to thescript-srcdirective. [‘default-src’]Note: The nonce value will only be generated if
request.csp_nonceis accessed during the request/response cycle.
Deprecated CSP settings
The following settings are still configurable, but are considered deprecated in terms of the latest implementation of the relevant spec.
CSP_BLOCK_ALL_MIXED_CONTENTInclude
block-all-mixed-contentdirective. Aboolean. FalseRelated note on MDN.
Spec: block-all-mixed-content
CSP_PLUGIN_TYPESSet the
plugin-typesdirective. Atupleorlist. NoneNote: This doesn’t use
default-srcas a fall-back.Related note on MDN.
CSP_PREFETCH_SRCSet the
prefetch-srcdirective. Atupleorlist. NoneRelated note on MDN.
Changing the Policy
The policy can be changed on a per-view (or even per-request) basis. See the decorator documentation for more details.
Other Settings
These settings control the behavior of django-csp. Defaults are in italics.
CSP_REPORT_ONLYSend “report-only” headers instead of real headers. A
boolean. FalseCSP_EXCLUDE_URL_PREFIXESA
tuple(not alist) of URL prefixes. URLs beginning with any of these will not get the CSP headers. ()
Warning
Excluding any path on your site will eliminate the benefits of CSP
everywhere on your site. The typical browser security model for
JavaScript considers all paths alike. A Cross-Site Scripting flaw
on, e.g., excluded-page/ can therefore be leveraged to access
everything on the same origin.