Astro security headers are set by whatever serves your site, not by astro build. For a static site, add a _headers file to public/ on Cloudflare Pages or Netlify, or a headers block in vercel.json on Vercel. For on-demand (SSR) routes, set them in middleware. Astro’s built-in security.csp option adds the Content Security Policy script and style hashes for you.
Key Takeaways
- Static HTML cannot send HTTP headers; your host or server adds them.
- Start with five headers:
Strict-Transport-Security,X-Content-Type-Options,Referrer-Policy,Permissions-Policyand a frame policy.security.cspinastro.config.mjsemits a CSP<meta>tag with hashes for Astro’s scripts and styles.- Some directives, such as
frame-ancestors, only work as a real HTTP header, not in a meta tag.
The baseline set
These headers are safe for almost every Astro site:
| Header | Value | What it does |
|---|---|---|
Strict-Transport-Security |
max-age=31536000; includeSubDomains |
Forces HTTPS on return visits |
X-Content-Type-Options |
nosniff |
Stops browsers guessing file types |
Referrer-Policy |
strict-origin-when-cross-origin |
Limits the URL data sent to other sites |
Permissions-Policy |
camera=(), microphone=(), geolocation=() |
Disables browser features you do not use |
X-Frame-Options |
DENY |
Stops other sites framing yours |
Only add preload to HSTS once you are sure every subdomain serves HTTPS, because removal from browser preload lists is slow.
Cloudflare Pages and Netlify: a _headers file
Both hosts read a plain-text _headers file from the build output. Put it in public/ and Astro copies it to dist/ unchanged:
# public/_headers
/*
Strict-Transport-Security: max-age=31536000; includeSubDomains
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin
Permissions-Policy: camera=(), microphone=(), geolocation=()
X-Frame-Options: DENY
Content-Security-Policy: frame-ancestors 'none'
/* applies the block to every path. Our _headers file tip covers path patterns and how the two hosts differ.
Vercel: vercel.json
Vercel reads headers from vercel.json in the project root:
{
"headers": [
{
"source": "/(.*)",
"headers": [
{ "key": "Strict-Transport-Security", "value": "max-age=31536000; includeSubDomains" },
{ "key": "X-Content-Type-Options", "value": "nosniff" },
{ "key": "Referrer-Policy", "value": "strict-origin-when-cross-origin" },
{ "key": "X-Frame-Options", "value": "DENY" }
]
}
]
}
SSR routes: middleware
If you use an adapter and on-demand rendering, set headers on every response in src/middleware.ts:
// src/middleware.ts
import { defineMiddleware } from 'astro:middleware';
export const onRequest = defineMiddleware(async (_context, next) => {
const response = await next();
response.headers.set('X-Content-Type-Options', 'nosniff');
response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
response.headers.set('X-Frame-Options', 'DENY');
return response;
});
Middleware also runs while prerendering, but headers set there are discarded for static pages because static files have no response to attach them to. Use the host’s header config for prerendered pages and middleware for on-demand ones. See our middleware guide.
Content Security Policy with security.csp
A hand-written CSP breaks easily, because Astro inlines small scripts and styles. Astro’s security.csp option computes hashes for everything it bundles and writes a CSP <meta> tag into each page’s <head>:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
site: 'https://www.example.com',
security: {
csp: true,
},
});
It does not run in astro dev, so test with astro build and astro preview. The installed type docs list current limits: external scripts and styles need hashes you supply, <ClientRouter /> view transitions are not supported, and Shiki’s inline styles conflict with it. A meta-tag CSP also cannot carry frame-ancestors, which is why the _headers example above sends it as a header. Our Content Security Policy guide covers directives and hashes in detail.
Check the result
After deploying, inspect the live headers:
# Terminal
curl -sI https://www.example.com/ | grep -iE 'strict|nosniff|referrer|permissions|frame|content-security'
Run the same check on a deep page and on an asset under /_astro/. Our Astro security checklist lists what else to review before launch, and the Cloudflare deploy docs show where the file goes for our Astro themes.
FAQ
Does Astro set security headers by default?
No. A static build is just files, and files do not carry headers. Your host adds them from _headers, vercel.json or server config. For SSR routes, middleware can set them.
Where do I put the _headers file in an Astro project?
In public/_headers. Astro copies everything in public/ to the root of dist/, which is where Cloudflare Pages and Netlify look for it.
Is X-Frame-Options still needed?
frame-ancestors in a Content Security Policy header replaces it in modern browsers. Sending both is harmless and covers older browsers.
Can I set a CSP in Astro without breaking inline scripts?
Yes. Enable security.csp in astro.config.mjs. Astro hashes the scripts and styles it generates and adds them to a CSP meta tag, so bundled and inline Astro scripts keep working.