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 ({{Glossary("XSS")}}).
Header type | {{Glossary("Response header")}} |
---|---|
{{Glossary("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.
- {{CSP("child-src")}}
- Defines the valid sources for web workers and nested browsing contexts loaded using elements such as {{HTMLElement("frame")}} and {{HTMLElement("iframe")}}.
- {{CSP("connect-src")}}
- Restricts the URLs which can be loaded using script interfaces
- {{CSP("default-src")}}
- Serves as a fallback for the other {{Glossary("Fetch directive", "fetch directives")}}.
- {{CSP("font-src")}}
- Specifies valid sources for fonts loaded using {{cssxref("@font-face")}}.
- {{CSP("frame-src")}}
- Specifies valid sources for nested browsing contexts loading using elements such as {{HTMLElement("frame")}} and {{HTMLElement("iframe")}}.
- {{CSP("img-src")}}
- Specifies valid sources of images and favicons.
- {{CSP("manifest-src")}}
- Specifies valid sources of application manifest files.
- {{CSP("media-src")}}
- Specifies valid sources for loading media using the {{HTMLElement("audio")}} and {{HTMLElement("video")}} elements.
- {{CSP("object-src")}}
- Specifies valid sources for the {{HTMLElement("object")}}, {{HTMLElement("embed")}}, and {{HTMLElement("applet")}} elements.
- {{CSP("script-src")}}
- Specifies valid sources for JavaScript.
- {{CSP("style-src")}}
- Specifies valid sources for stylesheets.
- {{CSP("worker-src")}}
- Specifies valid sources for {{domxref("Worker")}}, {{domxref("SharedWorker")}}, or {{domxref("ServiceWorker")}} scripts.
Document directives
Document directives govern the properties of a document or worker environment to which a policy applies.
- {{CSP("base-uri")}}
- Restricts the URLs which can be used in a document's {{HTMLElement("base")}} element.
- {{CSP("plugin-types")}}
- Restricts the set of plugins that can be embedded into a document by limiting the types of resources which can be loaded.
- {{CSP("sandbox")}}
- Enables a sandbox for the requested resource similar to the {{HTMLElement("iframe")}} {{htmlattrxref("sandbox", "iframe")}} attribute.
- {{CSP("disown-opener")}}
- Ensures a resource will disown its opener when navigated to.
Navigation directives
Navigation directives govern to which location a user can navigate to or submit a form to, for example.
- {{CSP("form-action")}}
- Restricts the URLs which can be used as the target of a form submissions from a given context.
- {{CSP("frame-ancestors")}}
- Specifies valid parents that may embed a page using {{HTMLElement("frame")}}, {{HTMLElement("iframe")}}, {{HTMLElement("object")}}, {{HTMLElement("embed")}}, or {{HTMLElement("applet")}}.
- {{CSP("navigation-to")}}
- Restricts the URLs to which a document can navigate by any means (a, form, window.location, window.open, etc.)
Reporting directives
Reporting directives control the reporting process of CSP violations. See also the {{HTTPHeader("Content-Security-Policy-Report-Only")}} header.
- {{CSP("report-uri")}} {{deprecated_inline}}
- Instructs the user agent to report attempts to violate the Content Security Policy. These violation reports consist of {{Glossary("JSON")}} documents sent via an HTTP
POST
request to the specified URI. - {{CSP("report-to")}}
- Fires a
SecurityPolicyViolationEvent
.
Other directives
- {{CSP("block-all-mixed-content")}}
- Prevents loading any assets using HTTP when the page is loaded using HTTPS.
- {{CSP("referrer")}} {{obsolete_inline}}
- Used to specify information in the referer (sic) header for links away from a page. Use the {{HTTPHeader("Referrer-Policy")}} header instead.
- {{CSP("require-sri-for")}}
- Requires the use of {{Glossary("SRI")}} for scripts or styles on the page.
- {{CSP("upgrade-insecure-requests")}}
- 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.
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 delivered 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 than created it.
Multiple content security policies
CSP allows multiple policies being specified for a resource, including via the Content-Security-Policy
header, the {{HTTPHeader("Content-Security-Policy-Report-Only")}} header and a {{HTMLElement("meta")}} element.
You can use the Content-Security-Policy
header more than once like in the example below. Pay special attention to the {{CSP("connect-src")}} directive here. Even though the second policy would allow the connection, the first policy contains connect-src 'none'
. Adding additional policies can only further restrict the capabilities of the protected resource, which means that there will be no connection allowed and connect-src 'none'
is enforced as the strictest policy.
Content-Security-Policy: default-src 'self' http://example.com; connect-src 'none'; Content-Security-Policy: connect-src http://example.com/; script-src http://example.com/
Examples
Example: Disable unsafe inline/eval, only allow loading of resources (images, fonts, scripts, etc.) over https:
// header Content-Security-Policy: default-src https: // meta tag <meta http-equiv="Content-Security-Policy" content="default-src https:">
Example: Pre-existing site that uses too much inline code to fix but wants to ensure resources are loaded only over https and disable plugins:
Content-Security-Policy: default-src https: 'unsafe-eval' 'unsafe-inline'; object-src 'none'
Example: Don't implement the above policy yet; instead just report violations that would have occured
Content-Security-Policy-Report-Only: default-src https:; report-uri /csp-violation-report-endpoint/
See Mozilla Web Security Guidelines for more examples.
Specifications
Specification | Status | Comment |
---|---|---|
{{specName("CSP 3.0")}} | {{Spec2('CSP 3.0')}} | Adds disown-opener , manifest-src , navigation-to , report-uri , strict-dynamic , worker-src . Undeprecates frame-src . Deprecates report-uri in favor if report-to . |
{{specName("Mixed Content")}} | {{Spec2('Mixed Content')}} | Adds block-all-mixed-content . |
{{specName("Subresource Integrity")}} | {{Spec2('Subresource Integrity')}} | Adds require-sri-for . |
{{specName("Upgrade Insecure Requests")}} | {{Spec2('Upgrade Insecure Requests')}} | Adds upgrade-insecure-requests . |
{{specName("CSP 1.1")}} | {{Spec2('CSP 1.1')}} | Adds base-uri , child-src , form-action , frame-ancestors , plugin-types , referrer , reflected-xss , and report-uri . Deprecates frame-src . |
{{specName("CSP 1.0")}} | {{Spec2('CSP 1.0')}} | Defines connect-src , default-src , font-src , frame-src , img-src , media-src , object-src , report-uri, sandbox , script-src, and style-src . |
Browser compatibility
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
{{Compat}}
See also
- {{HTTPHeader("Content-Security-Policy-Report-Only")}}
- Content Security in WebExtensions
-
Display security and privacy policies In Firefox Developer Tools