Sending a CSP header
A policy has no effect until the server sends it on every response, including error pages and static assets. This page gives the configuration for common web servers, proxies and hosting platforms. The nginx, Apache, Caddy and HAProxy snippets were run against those servers and the response headers checked.
The two headers
Every example sends the same pair. Reporting-Endpoints names where the browser sends violation reports, and report-to in the policy refers to that name.4,3 Replace the example policy with your own.
Reporting-Endpoints: csp-endpoint="https://example.com/csp-reports" Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint
Start new policies with Content-Security-Policy-Report-Only, which reports violations without blocking. Use the same snippets with that header name.2,5
nginx
TESTED · NGINX 1.24.0server block (nginx.conf or sites-available)
server {
# ...
add_header Reporting-Endpoints 'csp-endpoint="https://example.com/csp-reports"' always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint" always;
}- Without
always, nginx adds the header only to 200, 201, 204, 206, 301, 302, 303, 304, 307 and 308 responses. Error pages go out without a policy. add_headeris inherited from the enclosing level only when the current level has noadd_headerof its own. A location that adds any header, such asCache-Control, silently drops the CSP for every response it serves. Tested:/assets/lost both headers.- Fix: keep the two lines in a file and include it in the server block and in every location that sets its own headers. From nginx 1.29.3,
add_header_inherit merge;does the same.
Documentation: 6
nginx: shared include
TESTED · NGINX 1.24.0/etc/nginx/snippets/csp.conf, included where needed
# /etc/nginx/snippets/csp.conf
add_header Reporting-Endpoints 'csp-endpoint="https://example.com/csp-reports"' always;
add_header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint" always;
# server block
server {
include snippets/csp.conf;
location /assets/ {
include snippets/csp.conf; # repeat: this level has its own add_header
add_header Cache-Control "public, max-age=31536000";
}
}- Tested:
/assets/responses carried the CSP, Reporting-Endpoints and Cache-Control headers.
Documentation: 6
Apache HTTP Server
TESTED · APACHE 2.4.58VirtualHost, server config or .htaccess (mod_headers enabled: a2enmod headers)
Header always set Reporting-Endpoints "csp-endpoint=\"https://example.com/csp-reports\"" Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint"
alwayswrites to the table used for every response, including errors and internal redirects such asErrorDocument. Tested: 404 responses carried the policy.- The
onsuccessandalwaystables are separate. If a proxied or CGI backend also sends a CSP, unset it inonsuccessand set it inalways, or the response can carry two policies.
Documentation: 7
Caddy
TESTED · CADDY 2.6.2Caddyfile site block
example.com {
header Reporting-Endpoints `csp-endpoint="https://example.com/csp-reports"`
header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint"
# ...
}- Backticks quote a value that itself contains double quotes.
Documentation: 8
HAProxy
TESTED · HAPROXY 2.8.30frontend (or backend) section
frontend web
bind :443 ssl crt /etc/haproxy/certs/
http-response set-header Reporting-Endpoints 'csp-endpoint="https://example.com/csp-reports"'
http-response set-header Content-Security-Policy "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint"
default_backend appset-headerreplaces any CSP the backend sends, so the proxy becomes the single source of the policy.
Documentation: 9
IIS
FROM DOCUMENTATIONweb.config
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Reporting-Endpoints" value="csp-endpoint="https://example.com/csp-reports"" />
<add name="Content-Security-Policy" value="default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint" />
</customHeaders>
</httpProtocol>
</system.webServer>
</configuration>- Double quotes inside an XML attribute are written as
". Not run here: no Windows host was available.
Documentation: 10
Cloudflare Workers and Pages: _headers
FROM DOCUMENTATION_headers in the build output directory
/* Reporting-Endpoints: csp-endpoint="https://example.com/csp-reports" Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint
- Applies to static assets served by Workers or Pages. This site sends its own CSP this way.
Documentation: 11
Cloudflare: Response Header Transform Rules
FROM DOCUMENTATIONDashboard: Rules > Transform Rules > Modify response header
Operation: Set static Header name: Content-Security-Policy Value: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint
- Works in front of any origin proxied by Cloudflare. Set static replaces the header if the origin already sends one.
- Rules run in order and later rules can overwrite earlier ones. Rules also apply to Cloudflare's own error pages.
Documentation: 12
Netlify
FROM DOCUMENTATION_headers in the publish directory, or netlify.toml
# netlify.toml
[[headers]]
for = "/*"
[headers.values]
Reporting-Endpoints = 'csp-endpoint="https://example.com/csp-reports"'
Content-Security-Policy = "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint"- Custom headers are not applied to proxied content or to responses from functions and edge functions; those must set the header themselves.
Documentation: 13
Vercel
FROM DOCUMENTATIONvercel.json
{
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "Reporting-Endpoints", "value": "csp-endpoint=\"https://example.com/csp-reports\"" },
{ "key": "Content-Security-Policy", "value": "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'self'; report-to csp-endpoint" }
]
}
]
}Documentation: 14
HTML meta element
FALLBACK ONLY<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'self'">
- For hosts that do not allow response headers. The element must appear before any resource it should govern.
- It cannot carry
frame-ancestors,sandboxorreport-uri, and there is no report-only form.1
Report-only alongside enforcement
A response can carry an enforced policy and a report-only policy at the same time. This is how a stricter policy is trialled without loosening the one in force. The pairing was tested with Apache 2.4.58; the policies below are shortened:
Header always set Content-Security-Policy "default-src 'self'; script-src 'self'; ..." Header always set Content-Security-Policy-Report-Only "default-src 'self'; script-src 'self' 'strict-dynamic' 'nonce-...'; report-to csp-endpoint"
If a response carries two enforced Content-Security-Policy headers, the browser enforces both, and a resource must pass each one.1 A CDN, proxy or framework that adds its own policy can therefore block resources the main policy allows. Send the policy from one layer only.
Verify
- Check a normal page.
curl -sI https://example.com/ | grep -i content-security - Check an error page. Request a path that does not exist. A missing header here usually means
alwaysis missing. - Check every location with its own headers. Static asset paths, API routes and proxied applications are where nginx inheritance and platform exceptions drop the policy.
- Count the headers. More than one
Content-Security-Policyline means two layers are sending a policy. - Test the policy itself. Paste it into the policy test.
One policy, many servers
Most organisations send policies from more than one place: a CDN, a load balancer, an application framework. CSP Ledger is planned to generate these snippets from approved records, so each environment receives the same reviewed policy.
References
- [1]Content Security Policy Level 3. W3C, Web Application Security Working Group. Working Draft. The normative definition of directives, source matching and fallback.
- [2]
- [3]Reporting-Endpoints header. MDN Web Docs.
- [4]Reporting API. W3C.
- [5]Content Security Policy Cheat Sheet. OWASP Cheat Sheet Series.
- [6]Module ngx_http_headers_module. nginx documentation. add_header, the always parameter, inheritance between levels, and add_header_inherit (1.29.3+).
- [7]Apache Module mod_headers. Apache HTTP Server 2.4 documentation. Header directive; onsuccess and always tables.
- [8]header (Caddyfile directive). Caddy documentation.
- [9]HAProxy configuration manual: http-response set-header. HAProxy 2.8 documentation.
- [10]Custom Headers <customHeaders>. Microsoft Learn (IIS).
- [11]Headers (Workers static assets). Cloudflare Docs.
- [12]Response Header Transform Rules. Cloudflare Docs.
- [13]Custom headers. Netlify Docs. Not applied to proxied content or responses from functions and edge functions.
- [14]vercel.json: headers. Vercel Docs.