A static site removes PHP, the database and the login form from the public surface. It does not remove the browser’s attack surface. A script injected into a page still runs, a page can still be framed, and a referrer can still leak a URL. Response headers are how you tell the browser what you allow.
On WordPress these headers come from a plugin or the web server. On a static host they come from the host, which is both simpler and less flexible: the better hosts let you set most headers, and GitHub Pages lets you set nothing.
The headers that matter
- Content-Security-Policy. An allowlist for scripts, styles, images, fonts, frames and form targets. The main defence against injected script.
- Strict-Transport-Security. Tells the browser to use HTTPS for this host for a period, even if a link says HTTP.
- X-Content-Type-Options: nosniff. Stops the browser guessing a file’s type and running an image as a script.
- Referrer-Policy. Controls how much of the current URL is sent to other sites.
- Permissions-Policy. Turns off browser features the site does not need, such as the camera and location.
- X-Frame-Options or CSP
frame-ancestors. Stops another site framing yours.frame-ancestorsis the modern control.
Where to set them
Netlify and Cloudflare Pages read a _headers file from the published site. The GitHub add-on keeps that file across publishes, so a header block added once survives every deploy. On Cloudflare Pages, a Worker with static assets may not read _headers the same way; if the headers do not appear, use a Worker or a Transform Rule.
GitHub Pages cannot set custom response headers. The practical fix is a Cloudflare proxy in front with a Response Header Transform Rule, or a meta tag for the few policies that work in meta. S3 needs a CloudFront response headers policy, because object metadata does not carry arbitrary headers. Your own server sets them in nginx or Apache.
A starting set
# _headers
/*
Content-Security-Policy: default-src 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'none'; form-action 'self' https://api.web3forms.com; img-src 'self' data:; script-src 'self' https://plausible.io; style-src 'self' 'unsafe-inline'; font-src 'self'; connect-src 'self' https://api.web3forms.com https://plausible.io
Strict-Transport-Security: max-age=300
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
X-Frame-Options: DENY
Read that CSP as a list of decisions, not boilerplate. default-src 'self' denies everything not listed. form-action must name the form provider because it does not fall back to default-src. connect-src covers the form script’s fetch and the analytics request. img-src data: is there for inline icons and small embedded images.
style-src 'unsafe-inline' is doing real work in that block, and it is the compromise most WordPress sites end up making. It is covered below.
The same headers in nginx
add_header Content-Security-Policy "default-src 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'none'; form-action 'self' https://api.web3forms.com; img-src 'self' data:; script-src 'self' https://plausible.io; style-src 'self' 'unsafe-inline'; connect-src 'self' https://api.web3forms.com https://plausible.io" always;
add_header Strict-Transport-Security "max-age=300" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
always makes nginx send the header on error responses too. Watch inheritance: once a location block declares its own add_header, it drops the ones from the parent. Put the set in a snippet and include it where you need it.
CSP and page builders
The block editor and most page builders emit inline styles and inline scripts. A strict script-src 'self' breaks them. A strict style-src 'self' breaks the layout. This is the main reason a WordPress static site ends up with a weaker CSP than a hand-written one.
Styles are the easier call. Allow 'unsafe-inline' for style-src, or split it into style-src-elem and style-src-attr and see which one the builder needs. Inline style attributes are far less dangerous than inline script.
Scripts are the hard call. Hashes work only while the inline script is byte-identical; a change to that script, from a plugin update or an editor setting, changes the hash and the script stops. Nonces need a server to mint a new value per response, and a static file cannot. A nonce baked in at build time is shared by every visitor and every cached copy, which removes most of the point.
So you have three options. Move the inline script into a file and allow 'self'. Allow 'unsafe-inline' for scripts and accept that the CSP no longer stops most XSS. Or keep a strict policy and fix the theme and plugins until the console is clean, which can require significant work on a site you did not write.
Whichever you pick, list the third parties explicitly. Analytics, comment embeds, YouTube, Google Fonts, maps and form endpoints each need a directive. A missing one fails in the browser console, not in the build, so test on the published site with the console open.
Deploy the policy as Content-Security-Policy-Report-Only first. It reports violations without blocking, which is how you find the inline script you forgot. Then enforce it. Reports contain URLs, so choose where they go rather than sending them nowhere.
HSTS
Start with max-age=300 and raise it once you are sure every name on the domain works over HTTPS. includeSubDomains applies to webmail, staging and anything else served over HTTPS on a subdomain; if one of those has a broken certificate, it becomes unreachable. preload is a commitment to a browser list that is hard to reverse, so leave it until the domain has been stable for months.
HSTS does not fix mixed content. An http:// image on an HTTPS page is still blocked; rebuilding for the right address is the fix.
What a meta tag cannot do
A CSP in a <meta http-equiv> tag is a partial policy. frame-ancestors, report-uri and sandbox are ignored there. HSTS and X-Content-Type-Options cannot be set by meta at all. A meta referrer tag does work, and so does a meta CSP for the directives it supports. That is the GitHub Pages fallback, and it is a shadow of real headers.
Test what is sent
curl -sI https://example.com/ | grep -iE 'content-security|strict-transport|x-content-type|referrer-policy|permissions-policy|x-frame'
Check the live domain, a deep page and the 404 page. Then open the browser console and look for violations. Two CSP headers on the same response are combined, and the result is the intersection: a proxy that adds its own policy can break a site that was working. If you see a header twice, find the second source before you change the first.
Security headers are worth doing properly, and they are not a substitute for keeping plugins out of the static build. A strong header policy does not help if the page ships a secret in JavaScript.