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 |
Content-Security-Policy: <policy-directive>; <policy-directive>
Fetch directives control locations from which certain resource types may be loaded.
child-src
<frame>
and <iframe>
. Instead of child-src
, authors who wish to regulate nested browsing contexts and workers should use the frame-src
and worker-src
directives, respectively.
connect-src
default-src
font-src
@font-face
.frame-src
<frame>
and <iframe>
.img-src
manifest-src
media-src
<audio>
, <video>
and <track>
elements.object-src
<object>
, <embed>
, and <applet>
elements.prefetch-src
script-src
style-src
webrtc-src
worker-src
Worker
, SharedWorker
, or ServiceWorker
scripts.Document directives govern the properties of a document or worker environment to which a policy applies.
base-uri
<base>
element.plugin-types
sandbox
<iframe>
sandbox
attribute.disown-opener
Navigation directives govern to which location a user can navigate to or submit a form to, for example.
form-action
frame-ancestors
<frame>
, <iframe>
, <object>
, <embed>
, or <applet>
.navigate-to
Reporting directives control the reporting process of CSP violations. See also the Content-Security-Policy-Report-Only
header.
report-uri
POST
request to the specified URI. Though the report-to
directive is intended to replace the deprecated report-uri
directive, report-to
isn’t supported in most browsers yet. So for compatibility with current browsers while also adding forward compatibility when browsers get report-to
support, you can specify both report-uri
and report-to
:
Content-Security-Policy: ...; report-uri https://endpoint.example.com; report-to groupname
In browsers that support report-to
, the report-uri
directive will be ignored.
report-to
SecurityPolicyViolationEvent
.block-all-mixed-content
referrer
Referrer-Policy
header instead.require-sri-for
upgrade-insecure-requests
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.
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 <meta>
element.
You can use the Content-Security-Policy
header more than once like in the example below. Pay special attention to the 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, as the strictest policy, connect-src 'none'
is enforced.
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/
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 occurred:
Content-Security-Policy-Report-Only: default-src https:; report-uri /csp-violation-report-endpoint/
See Mozilla Web Security Guidelines for more examples.
Specification | Status | Comment |
---|---|---|
Content Security Policy Level 3 | Working Draft | Adds disown-opener , manifest-src , navigate-to , report-to , strict-dynamic , worker-src . Undeprecates frame-src . Deprecates report-uri in favor if report-to . |
Mixed Content | Candidate Recommendation | Adds block-all-mixed-content . |
Subresource Integrity | Recommendation | Adds require-sri-for . |
Upgrade Insecure Requests | Candidate Recommendation | Adds upgrade-insecure-requests . |
Content Security Policy Level 2 | Recommendation | Adds base-uri , child-src , form-action , frame-ancestors , plugin-types , referrer , and report-uri . Deprecates frame-src . |
Content Security Policy 1.0 | Obsolete | Defines connect-src , default-src , font-src , frame-src , img-src , media-src , object-src , report-uri, sandbox , script-src, and style-src . |
Desktop | ||||||
---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | |
Basic support | 25
|
14 | 23
|
10
|
15 | 7
|
<meta> element support |
Yes | Yes | 45 | No | Yes | Yes |
Worker support | Yes | ? | 50 | No | ? | No |
Mobile | |||||||
---|---|---|---|---|---|---|---|
Android webview | Chrome for Android | Edge Mobile | Firefox for Android | Opera for Android | iOS Safari | Samsung Internet | |
Basic support | Yes | Yes | Yes | 23 | ? | 7.1
|
Yes |
<meta> element support |
Yes | Yes | Yes | 45 | Yes | Yes | Yes |
Worker support | Yes | Yes | ? | 50 | ? | No | Yes |
Content-Security-Policy-Report-Only
Display security and privacy policies In Firefox Developer Tools
© 2005–2018 Mozilla Developer Network and individual contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy