The HTTP Content-Security-Policy
response header allows web site administrators to control resources the user agent is allowed to load for a given page. With a few exceptions, policies mostly involve specifying server origins and script endpoints. This helps guard against cross-site scripting attacks (XSS).
For more information, see also this article on Content Security Policy (CSP).
Header type | Response header |
---|---|
Forbidden header name | no |
Syntax
Content-Security-Policy: <policy-directive>; <policy-directive>
Directives
Fetch directives
Fetch directives control locations from which certain resource types may be loaded.
- and
- Defines the valid sources for
web workers and nested browsing contexts loaded using elements such as
is an HTML element which defines a particular area in which another HTML document can be displayed. A frame should be used within a and
) represents a nested browsing context, effectively embedding another HTML page into the current page.” target=”_blank”><iframe>
. Instead of
child-src
, authors who wish to regulate nested browsing contexts and workers should use the
and and
worker-src
directives, respectively. - Restricts the URLs which can be loaded using script interfaces
- Serves as a fallback for the other
fetch directives. - Specifies valid sources for fonts loaded using
@font-face
. - Specifies valid sources for nested browsing contexts loading using elements such as
is an HTML element which defines a particular area in which another HTML document can be displayed. A frame should be used within a and
) represents a nested browsing context, effectively embedding another HTML page into the current page.” target=”_blank”><iframe>
. - Specifies valid sources of images and favicons.
- Specifies valid sources of application manifest files.
- Specifies valid sources for loading media using the
element is used to embed sound content in documents. It may contain one or more audio sources, represented using the src attribute or the ,
) embeds a media player which supports video playback into the document.” target=”_blank”><video>
and
element is used as a child of the media elements elements. - Specifies valid sources for the
element represents an external resource, which can be treated as an image, a nested browsing context, or a resource to be handled by a plugin.” target=”_blank”><object>
,
element embeds external content at the specified point in the document. This content is provided by an external application or other source of interactive content such as a browser plug-in.” target=”_blank”><embed>
, and
) embeds a Java applet into the document; this element has been deprecated in favor of elements.
Specifies valid sources to be prefetched or prerendered.
- elements, but also things like inline script event handlers (onclick) and XSLT stylesheets which can trigger script execution.” target=”_blank”>
- Specifies valid sources for JavaScript.
- Specifies valid sources for stylesheets.
-
Specifies valid sources for
Worker
,
SharedWorker
, or
ServiceWorker
scripts.
script-src
Document directives
Document directives govern the properties of a document or worker environment to which a policy applies.
-
element. If this value is absent, then any URI is allowed. If this directive is absent, the user agent will use the value in the
-
Restricts the URLs which can be used in a document’s
element specifies the base URL to use for all relative URLs contained within a document. There can be only oneelement in a document.” target=”_blank”> <base>
element. - Restricts the set of plugins that can be embedded into a document by limiting the types of resources which can be loaded.
-
Enables a sandbox for the requested resource similar to the
) represents a nested browsing context, effectively embedding another HTML page into the current page.” target=”_blank”><iframe>
sandbox
attribute. - Ensures a resource will disown its opener when navigated to.
base-uri
Navigation directives
Navigation directives govern to which location a user can navigate to or submit a form to, for example.
- Restricts the URLs which can be used as the target of a form submissions from a given context.
-
Specifies valid parents that may embed a page using
is an HTML element which defines a particular area in which another HTML document can be displayed. A frame should be used within a ,
) represents a nested browsing context, effectively embedding another HTML page into the current page.” target=”_blank”><iframe>
,
element represents an external resource, which can be treated as an image, a nested browsing context, or a resource to be handled by a plugin.” target=”_blank”><object>
,
element embeds external content at the specified point in the document. This content is provided by an external application or other source of interactive content such as a browser plug-in.” target=”_blank”><embed>
, or
) embeds a Java applet into the document; this element has been deprecated in favor of . - Restricts the URLs to which a document can navigate by any means (a, form, window.location, window.open, etc.)
form-action
Reporting directives
Reporting directives control the reporting process of CSP violations. See also the Content-Security-Policy-Report-Only
header.
-
Instructs the user agent to report attempts to violate the Content Security Policy. These violation reports consist of
JSON documents sent via an HTTP
POST
request to the specified URI.Though the
report-to
directive is intended to replace the deprecatedreport-uri
directive,report-to
isn’t supported in most browsers yet. So for compatibility with current browsers while also adding forward compatibility when browsers getreport-to
support, you can specify bothreport-uri
andreport-to
:Content-Security-Policy: ...; report-uri https://endpoint.com; report-to groupname
In browsers that support
report-to
, the
report-uri
directive will be ignored. -
Fires a
SecurityPolicyViolationEvent
.
report-uri
Other directives
- Prevents loading any assets using HTTP when the page is loaded using HTTPS.
-
Used to specify information in the referer (sic) header for links away from a page. Use the
Referrer-Policy
header instead. -
Requires the use of
SRI for scripts or styles on the page. - Instructs user agents to treat all of a site’s insecure URLs (those served over HTTP) as though they have been replaced with secure URLs (those served over HTTPS). This directive is intended for web sites with large numbers of insecure legacy URLs that need to be rewritten.
block-all-mixed-content
CSP in workers
Workers are in general not governed by the content security policy of the document (or parent worker) that created them. To specify a content security policy for the worker, set a Content-Security-Policy
response header for the request which requested the worker script itself.
The exception to this is if the worker script’s origin is a globally unique identifier (for example, if its URL has a scheme of data or blob). In this case, the worker does inherit the content security policy of the document or worker that created it.
Multiple content security policies
CSP allows multiple policies being specified for a resource, including via the Content-Security-Policy
header, the Content-Security-Policy-Report-Only
header and a element represents metadata that cannot be represented by other HTML meta-related elements, like