The security policy
Every page carries a Content-Security-Policy: a list of where the page may load scripts, styles, fonts, frames and connections from, and where its forms may send. If a script or a link ever gets into a listing or a page (a submission you didn’t spot, a pasted snippet), the browser refuses to run it. The site’s own scripts are all files on the site, so the policy allows those, the services you name, and nothing else. It’s on by default, and you don’t need to do anything for the starter’s own features.
Turned on by themselves. The policy reads your config and content, and allows what the site’s own features need:
- Your analytics service, the one chosen in the editor’s Settings → Analytics (Cloudflare Web Analytics, Plausible, Fathom or Umami), and no other. See “Analytics” below.
- Your form services (
formsindirectory.config.json): Formspree, Web3Forms, or a custom endpoint’s address. - Your newsletter service, the one chosen in the editor’s Settings → Newsletter (Kit, Mailchimp, Buttondown or beehiiv), and only its form’s address: as a form target, or for beehiiv’s own form, a frame. With Kit, the page sends the sign-up itself and shows Kit’s answer on your site, so Kit’s address is allowed for connections too (
src/lib/newsletter-client.ts). The owner pastes the service’s whole embed code; the editor keeps only what the site needs from it (for Kit, the form’s number), so nothing else from the code reaches the page. - Turnstile, once
submissions.turnstileSiteKeyis set. - Stripe Checkout, while the booking form is on (
advertise.checkout.provider: "stripe"). - Videos, maps, booking pages, players and sign-up forms pasted into a page, a post or a listing (YouTube, Vimeo, Google Maps, Calendly, Cal.com, Spotify, beehiiv, Substack:
EMBED_SERVICESinsrc/lib/csp-presets.ts, each with what its embed code looks like), unless the editor’s Settings → Services says Never for it (src/config/settings/services.json; Always allows it with no embed found). Owners paste the code into the editor’s Embed block (Insert → Embed), which saves it as written, leaving out HTML comments and inline scripts. - Your own pages in frames, such as one of your embeddable lists placed on one of your pages.
Other addresses, from the editor. Settings → Services → Advanced → Other addresses: an address and what it’s for (frames, scripts, stylesheets, fonts, connections, form targets), the same as an { "origin", "for" } entry here (security.addresses).
What the build says. At the end of each build, every page is read against its own policy (src/lib/setup/blocked.ts). Anything it loads that its policy blocks becomes a warning in the build log and a note in the editor, naming the setting that would allow it: the service on Settings → Services, Settings → Analytics, or an address to add under Other addresses. So do an inline script in a page, and embed code saved as text (pasted into the text itself, not the Embed block). An .mdx page that MDX can’t read stops the build before it starts, naming the page as the editor does and the line.
Adding a service. Anything else, such as a chat widget, or a newsletter form you add to a layout by hand instead of through Settings → Newsletter, goes in directory.config.json → security.allow. Use a preset’s name where there is one:
"security": { "allow": ["mailchimp", "youtube"]}| Kind | Presets |
|---|---|
| Analytics | cloudflare-web-analytics, plausible, fathom, umami (Umami Cloud): each is added by itself for the service chosen in Settings → Analytics, so you rarely need them. google-analytics (gtag.js and Tag Manager) is for adding Google’s snippet by hand (below) |
| Embeds | youtube, vimeo, google-maps, calendly, cal-com, spotify (found by themselves in your content, so you rarely need them) |
| Forms | formspree, web3forms (added by themselves for the forms you set up) |
| Newsletters | mailchimp, buttondown, kit (ConvertKit), mailerlite, beehiiv, substack |
| Payments | stripe (Stripe.js, the Buy Button and the Pricing Table; the booking form needs only part of it, which it adds itself) |
| Other | turnstile, google-fonts (only if you link fonts from Google; the theme keeps its own on the site) |
Each preset lists the addresses the service’s standard snippet or embed uses (src/lib/csp-presets.ts).
A service with no preset. Add its origin, the https:// and host only, with no path:
"https://widget.example.com"allows it for everything: scripts, styles, fonts, connections, frames and forms.{ "origin": "https://widget.example.com", "for": ["script", "connect"] }allows it only for what it needs. Choose fromscript,style,font,img,connect,frame,mediaandform."https://*.example.com"covers its subdomains.
Names, origins and objects can be mixed in one list. A name that isn’t a preset stops the build and lists the presets. Images from any https address, and audio or video files, are always allowed.
The setting in full (src/config/directory.config.json; every key is optional):
"security": { "csp": true, "mode": "enforce", "allow": [], "reportUri": ""}allow: what to let in, beyond the site’s own features: preset names, origins, and{ "origin", "for" }entries (below).mode:"enforce"(the default) blocks what isn’t allowed;"report-only"only reports it (below).reportUri: where report-only mode also sends its reports.csp:falseleaves the policy out.
Analytics
Choose the service in the editor: Settings → Analytics (src/config/settings/analytics.json: service and id). The site then does three things itself, with no snippet in a layout and nothing in security.allow:
- every built page loads that service’s script from the service’s own address (
src/components/SiteAnalytics.astro, in Base.astro’s<head>); - the policy allows that service and no other analytics;
- the privacy page’s Analytics block names it.
The ID field takes the ID alone or the service’s whole snippet, and reads the ID from it. Nothing is written into the page itself: Plausible’s two start-up lines are a file on the site (public/plausible-init.js).
- Cloudflare Web Analytics, turned on in Cloudflare’s dashboard. Cloudflare then adds its script to your pages by itself. Choose Cloudflare Web Analytics and leave the ID empty: the site allows Cloudflare’s script and adds none of its own. With another service chosen, or none, the policy blocks Cloudflare’s script and no visits are counted; the editor’s note and
npm run doctor -- --liveboth say so when they see it. - Umami on your own server, or Plausible on your own server. The setting covers Umami Cloud and plausible.io. For your own instance, leave Settings → Analytics on None, add the script tag to Base.astro’s
<head>, and allow your instance:"allow": [{ "origin": "https://umami.yourdomain.com", "for": ["script", "connect"] }]. - Where it’s left out:
npm run dev, a preview build (a branch other than the live one), and any page whose layout passesanalytics={false}to Base (/tell-them/, which opens with a submitter’s details in its address). - An analytics preset left in
security.allowfrom before the setting still works. The build notes it, and warns when it would let a second analytics service load beside the one chosen in the editor.
Google Analytics isn’t in Settings → Analytics yet: it sets cookies, so it needs a cookie notice, and both come in a later update. To add it by hand until then (the cookie notice is yours to add too): Google’s snippet has two parts: a script tag, which the google-analytics preset allows, and a few lines written straight into the page, which the policy blocks wherever they come from. Put those lines in a <script> in an .astro component instead. Astro then writes them to a file, which the policy allows. For example, src/components/GoogleAnalytics.astro (a file of your own: SiteAnalytics.astro is the starter’s), placed in Base.astro’s <head>, keeps the id in an attribute (not define:vars, which writes an inline script):
---const id = 'G-XXXXXXXXXX';---<script is:inline async src={`https://www.googletagmanager.com/gtag/js?id=${id}`} data-ga={id}></script><script> const id = document.querySelector<HTMLScriptElement>('script[data-ga]')?.dataset.ga; const w = window as unknown as { dataLayer: unknown[]; gtag: (...a: unknown[]) => void }; w.dataLayer = w.dataLayer || []; w.gtag = function () { w.dataLayer.push(arguments); }; w.gtag('js', new Date()); if (id) w.gtag('config', id);</script>"allow": ["google-analytics"]Other examples
For a service you add yourself, put its snippet in the site’s <head>: in src/layouts/Base.astro, or in a small component of your own that Base includes. Then allow the service.
A YouTube or Vimeo video. Nothing to do when the embed is in a page, post or listing file (<iframe src="https://www.youtube-nocookie.com/embed/…">): the build finds it and allows the player. Add "youtube" or "vimeo" only if the embed’s address is put together somewhere the build can’t see, such as a script.
Other embeds without a preset, as { "origin", "for" } entries:
"allow": [ { "origin": "https://www.google.com", "for": ["frame"] }, { "origin": "https://open.spotify.com", "for": ["frame"] }, { "origin": "https://calendly.com", "for": ["frame"] }, { "origin": "https://assets.calendly.com", "for": ["script", "style"] }]These are, in order: a Google frame other than a map, a Spotify player, and Calendly’s inline widget. Maps, Spotify and Calendly now have presets found in the content by themselves, so you need these only where an embed is put together somewhere the build can’t see.
A newsletter form and a widget together. Presets and origins mix in one list:
"security": { "allow": ["mailchimp", { "origin": "https://widget.example.com", "for": ["script", "frame"] }]}If something still doesn’t load, npm run dev names exactly what to add (below).
Trying a change without breaking the site: report-only mode. Set "mode": "report-only", deploy, and use the site. The browser then lets everything load, and lists what the policy would block in its console (“[Report Only] Refused to load…”). Add what’s missing, then set "mode": "enforce" again (the default).
- Where it works: report-only mode needs a header, which a
<meta>tag can’t send, so it works on hosts that readdist/_headers(Cloudflare and Netlify). On Vercel, put theContent-Security-Policy-Report-Onlyline fromdist/_headersintovercel.json. - Collecting reports:
"reportUri": "https://…"also sends the reports to that address, such as a report-uri.com or Sentry endpoint. - The CMS: in this mode,
/admin/lists reports in its console too. Ignore those: the CMS has its own policy.
While you work: npm run dev. The dev server applies the policy as report-only, so nothing breaks, and shows a notice in the page’s corner for anything that would be blocked once built. The notice names what was blocked and says exactly what to add, such as Add "mailchimp" to …security.allow, or the { "origin", "for" } entry for an address with no preset. The notice is never in a build. "csp": false turns the policy off entirely (the other headers stay), but adding the service is better.
“My script or embed doesn’t work.”
- Check the console. Run
npm run devand look for the notice, or open the browser’s console on the live site. Chrome and Edge say “Refused to load … because it violates the following Content Security Policy directive: “script-src …”“, and Firefox says “Content-Security-Policy: The page’s settings blocked…”. - Find the directive. It tells you the kind:
script-src→script,connect-src→connect,frame-src→frame,form-action→form,style-src→style,font-src→font. - Add the preset or the origin to
security.allow, rebuild, and reload. - Still blocked, as “inline”: the snippet runs code written straight into the page (
<script>…</script>, oronclick="…"). Allowing its address isn’t enough. Put the code in a<script>in an.astrocomponent, which Astro writes to a file, and allow the service’s own script address. Most snippets are just a script tag plus a few lines of setup, which can go in the component. - “eval” or “unsafe-eval”: the library builds code from text, which the policy doesn’t allow. Look for its build without
eval. - To rule the policy out quickly, switch to report-only mode: if it works then, it’s the policy.
The other headers. dist/_headers adds, for every file:
- frames from other sites refused (
frame-ancestors 'self'), except the embeddable lists’ pages (/embed/*), which any site may frame (see “Embeddable lists”); nosniff;- a referrer policy;
- no camera, microphone or location (
Permissions-Policy).
Cloudflare and Netlify read that file. They add up every rule that matches a path, so a looser rule for /embed/* can’t undo a strict one for /*. With embeds on, the build therefore writes the framing rule per top-level path instead (/, /listings, /listings/*, …: one pair per folder it built), and frame-ancestors * for /embed/* alone. That stays well under Cloudflare’s limit of 100 rules, and the build warns if it gets close. On Vercel and on a Render static site, which don’t read _headers, npm run setup:host-files writes the same rules into vercel.json or render.yaml after a build (Hosting). Neither host documents what happens when two matching rules set the same header, so the generated file never relies on it: the site-wide rule keeps every header, and each narrower rule carries only its full security policy. npm run doctor says when the file no longer matches the build.
To keep your own rules, put a _headers file in public/. The build keeps its rules after the site’s.
npm run qa:security checks the build, and qa:quick and qa:full run it every time. It checks that:
- every page has the policy (or none in report-only mode);
- no page has an inline script or a
javascript:link; dist/_headersis in order;- the presets and the features’ own sources resolve as described here;
- the development notice isn’t in the build.