Content-Security-Policy checker
Paste a Content-Security-Policy: the header line, its value, or the meta element. The page reads it as CSP Level 3 does, shows which directive each kind of resource falls back to, flags what lets injected scripts run and what is left open, and writes the lines for Apache, nginx and a meta element.
| Directive | Value | What it does |
|---|
| Kind of resource | Uses | Allows |
|---|
Apache (mod_headers), in the virtual host or .htaccess
nginx, in the server block
A meta element, early in the head of the page; without frame-ancestors, report-uri and sandbox, which a meta element ignores
Sending the header
Apache: with always, the header also goes out with error pages and with redirects that Apache makes itself; without it, only with successful responses. nginx: without always, add_header adds the header only to responses such as 200, 301 and 304, not to error pages; with it, to every response. In nginx, an add_header line in a location block means that none of the add_header lines of the server block apply there, so the policy must be repeated (nginx 1.29.3 and later can merge them with add_header_inherit merge;); a $ in the value starts a variable. To try a policy first, send it as Content-Security-Policy-Report-Only: the browser blocks nothing, and with report-to or report-uri it reports what it would have blocked.
A nonce must be new in every response and should be at least 128 bits from a secure random generator; each script element carries the same value in its nonce attribute. That is the work of the application, not of a static header line.
Which directive each kind of resource uses
The browser takes the first of these directives that the policy has. A kind with none of them is not restricted. base-uri, form-action and frame-ancestors have no fallback: default-src does not cover them.
| Kind of resource | Directives, in the order used |
|---|---|
| Scripts: script elements and files | script-src-elem, then script-src, then default-src |
| Inline event handlers, such as onclick | script-src-attr, then script-src, then default-src |
| Style sheets and style elements | style-src-elem, then style-src, then default-src |
| Style attributes | style-src-attr, then style-src, then default-src |
| Workers | worker-src, then child-src, then script-src, then default-src |
| Connections from scripts | connect-src, then default-src |
| App manifests | manifest-src, then default-src |
| Plugins: object and embed | object-src, then default-src |
| Frames | frame-src, then child-src, then default-src |
| Audio, video and text tracks | media-src, then default-src |
| Fonts | font-src, then default-src |
| Images | img-src, then default-src |
| The base element | base-uri |
| Where forms are sent | form-action |
| Pages that may frame this one | frame-ancestors |
Directives
| Directive | What it controls |
|---|---|
default-src | The fallback for the fetch directives that are not set (see the order below). |
child-src | Frames and workers: the fallback for frame-src and worker-src. |
connect-src | Where scripts may connect: fetch(), XMLHttpRequest, EventSource, sendBeacon() and the ping of a link, and WebSocket connections. |
font-src | Where fonts may load from. |
frame-src | What may load in the page's frames. |
img-src | Where images may load from. |
manifest-src | Where app manifests may load from. |
media-src | Where video, audio and their text tracks may load from. |
object-src | Where plugin content (object and embed) may load from. Set it to 'none'. |
script-src | Where scripts may load from and which inline scripts and event handlers may run; also whether eval() and the like may run. |
script-src-elem | Script elements and script files; event handlers are left to script-src-attr. |
script-src-attr | Inline event handlers, such as onclick. |
style-src | Where style sheets may load from and which inline styles may apply. |
style-src-elem | Style sheets and style elements: every style except style attributes. |
style-src-attr | Style attributes. |
worker-src | Workers, shared workers and service workers. |
webrtc | Whether WebRTC connections may be made: 'allow' or 'block'. |
base-uri | What the page's base element may set as its base address. default-src does not cover it. |
sandbox | Puts the page in an HTML sandbox, as if it were in a sandboxed iframe. Ignored in a meta element. |
form-action | Where the page's forms may be sent. default-src does not cover it. |
frame-ancestors | Which pages may show this page in a frame, iframe, object or embed. default-src does not cover it, a meta element cannot set it, and it takes the place of X-Frame-Options. |
report-uri | Where to send violation reports. Deprecated: use report-to. Ignored where report-to is present, and in a meta element. |
report-to | The name of the reporting endpoint that gets the violation reports. |
upgrade-insecure-requests | The browser loads the page's http addresses over https instead. |
block-all-mixed-content | Mixed content that cannot be upgraded to https is now always blocked, so this directive does nothing. |
require-trusted-types-for | With 'script': the DOM XSS injection sinks take only Trusted Types values, not plain strings. |
trusted-types | Which Trusted Types policies the page may create, by name; 'none' for none, 'allow-duplicates' to allow one name twice. |
Keywords
Keywords go in single quotes: self without them is a host named self. Besides keywords, a source can be a scheme (https:), a host (cdn.example.com, *.example.com, https://example.com/js/), * (any http or https address), a nonce ('nonce-...') or a hash ('sha256-...'). A host with letters outside ASCII is written in Punycode, and of IP addresses only 127.0.0.1 ever matches.
| Keyword | Meaning |
|---|---|
'self' | The page's own origin: its scheme, host and port. |
'unsafe-inline' | Allows inline scripts or styles and inline event handlers. Ignored when a nonce or a hash is listed with it, and for scripts when 'strict-dynamic' is. |
'unsafe-eval' | Lets scripts turn strings into code: eval(), Function(), and setTimeout() or setInterval() given a string. |
'wasm-unsafe-eval' | Lets the page compile and run WebAssembly. |
'strict-dynamic' | For scripts: host and scheme sources, 'self' and 'unsafe-inline' are ignored, and scripts that a nonced or hashed script adds are allowed. |
'unsafe-hashes' | Lets the listed hashes match event handlers, style attributes and javascript: links too; safer than 'unsafe-inline' for a page that cannot drop its event handlers. |
'report-sample' | Violation reports for inline content carry a sample of it. |
'trusted-types-eval' | Allows eval() and the like only while Trusted Types are required for scripts. |
'unsafe-allow-redirects' | In the grammar of CSP Level 3, which does not describe it yet. |
'report-sha256' | For each script the page loads, a report with its SHA-256 hash goes to the reporting endpoint; 'report-sha384' and 'report-sha512' do the same with those hashes. |
'report-sha384' | As 'report-sha256', with SHA-384. |
'report-sha512' | As 'report-sha256', with SHA-512. |
'unsafe-webtransport-hashes' | Allows WebTransport connections made with a list of certificate hashes, which are blocked without it. |
Sources
- W3C, Content Security Policy Level 3 (webappsec-csp at commit e3461f7): the parsing of a policy, the source list grammar, the directives, the fallback lists, the matching of sources, the meta element, the Report-Only header, and the sections on nonce reuse, nonce retargeting, multiple policies, 'strict-dynamic', 'unsafe-hashes' and Strict CSP.
- W3C, Upgrade Insecure Requests (at commit 87d0b9d); Mixed Content (at commit a4eacec), for block-all-mixed-content; Trusted Types (at commit afcdd46), for require-trusted-types-for and trusted-types. The W3C specifications are used under the W3C Software and Document License.
- Apache: the mod_headers reference (Header, always and onsuccess, set and add), httpd at commit 3b46369, Apache License 2.0. nginx: the ngx_http_headers_module reference (add_header), nginx.org at commit 6e62624.
More Web tools
-
SAML response decoder
A SAMLResponse or SAMLRequest decoded in your browser: issuer, status, NameID, audience, times and attributes.
-
TLS cipher suite lookup
0xC02F, its IANA name and its OpenSSL name, IANA's Recommended mark, and Windows' default order.
-
Redirect rule generator
Apache and nginx rules for HTTP to HTTPS, www to the bare domain and old paths to new, with 301 or 308.
-
robots.txt tester
Paste a robots.txt and a path: which group a crawler follows, which rule wins, and why, by RFC 9309.