Security Headers in Caddy

A strict Content Security Policy, HSTS and no-referrer in a Caddy site block, checked with Webbkoll, securityheaders.com and the Mozilla Observatory.

Article Created Updated Also in German: Security-Header in Caddy

Reviewed by adrian on

Once the site was live, I ran it through three scanners. Zero cookies, zero third-party requests, HTTPS by default. But no Content Security Policy, no HSTS and no Referrer-Policy. This guide goes through what the scans found, what I changed and what came out.

The scanners

  • Webbkoll by the Swedish digital rights organisation Dataskydd.net. For every check it names the article of the GDPR behind it: Art. 5.1.c (data minimisation) for the Referrer-Policy and third-party requests, Art. 25 (privacy by design and by default) for no cookies and no third-party calls, Art. 32 (security of processing) for HSTS, CSP and the other headers. That makes it my scanner when I want to show that the site was built GDPR-compliant.
  • securityheaders.com for a quick grade from A+ to F.
  • Mozilla HTTP Observatory for the deepest CSP check.

Why a static site needs this

A site without login, forms or user input looks like a dull target. Three headers still matter:

  • Content Security Policy is insurance for the day foreign HTML lands on the site: a compromised dependency, a misconfiguration, a typo in a template. With a strict CSP the browser refuses to run injected JavaScript.
  • HSTS protects against downgrade attacks, for example someone on open Wi-Fi typing the domain without https://.
  • Referrer-Policy keeps the reader's current URL from going out with every click on an external link.

The setup

  • VPS at Hetzner, Debian.
  • Caddy as reverse proxy (set up here) in front of a container with nginx serving the static Astro build on 127.0.0.1:4321.

The header block

Caddy only sends the bare minimum by default. Everything goes into a header block of the site:

adrian-altner.de, www.adrian-altner.de {
    encode zstd gzip

    @www host www.adrian-altner.de
    redir @www https://adrian-altner.de{uri} permanent

    header /fonts/* Cache-Control "public, max-age=31536000, immutable"
    header /_astro/* Cache-Control "public, max-age=31536000, immutable"
    header /images/* Cache-Control "public, max-age=604800"

    header {
        Content-Security-Policy "default-src 'none'; img-src 'self' data:; style-src 'self' 'unsafe-inline'; script-src 'self'; font-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'; object-src 'none'"
        Referrer-Policy "no-referrer"
        Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
        X-Content-Type-Options "nosniff"
        X-Frame-Options "DENY"
        X-XSS-Protection "0"
        Permissions-Policy "geolocation=(), microphone=(), camera=(), interest-cohort=()"
        Cross-Origin-Opener-Policy "same-origin"
        -Server
    }

    reverse_proxy 127.0.0.1:4321
}
sudo caddy validate --config /etc/caddy/Caddyfile
sudo systemctl reload caddy

curl -I against the site then shows the full set. The less obvious lines:

  • default-src 'none' is stricter than 'self': every directive has to be allowed on purpose, nothing falls back to a looser default.
  • X-XSS-Protection: 0 follows current OWASP advice. The old XSS auditor in older browsers opened more holes than it closed, and 0 switches it off.
  • X-Frame-Options: DENY repeats frame-ancestors 'none' for older browsers without CSP Level 2.
  • -Server removes the Server header, one small fingerprinting hint less.
  • interest-cohort=() opts out of Google's FLoC, even though the feature is as good as dead.

Inline scripts under the CSP

The next scan found a new break: the contact page no longer showed the email address. The address is stored as Base64 and decoded in the browser, and the decode snippet sat inline in the .astro file. Astro turned it into an inline <script type="module">, and script-src 'self' blocked it, as it should. Three options:

  1. 'unsafe-inline': makes the CSP close to worthless.
  2. A SHA-256 hash of the script in the CSP: has to be checked after every build, fragile.
  3. Move the script into its own file and import it: Astro bundles module imports as external files, which script-src 'self' allows.

I took the third. The script moved to src/scripts/decode-obf.ts, and the page only imports it:

<script>
    import '~/scripts/decode-obf';
</script>

After the build it is a hashed _astro/….js file, loaded with src and cached with the long Cache-Control header.

style-src 'unsafe-inline' stays. Astro writes scoped <style> tags, the @font-face rules and the view-transition keyframes inline. Without a nonce per request there is no clean way around it on a static site. Injected styles cannot run code, so I accept it.

Referrer-Policy: no-referrer

My first version used strict-origin-when-cross-origin, which only sends the origin. Webbkoll still marked it yellow, because the origin goes out on HTTPS-to-HTTPS requests. The site has no analytics and nobody needs the referrer, so I switched to:

Referrer-Policy "no-referrer"

Browsers now send no Referer at all on outgoing links. Tile services that check the Referer then need an exception on the element; see Leaflet and Stadia Maps behind a strict CSP.

The result

Webbkoll: HTTPS by default, "Good policy" for the CSP, "Referrers are not transmitted", 0 cookies, 0 third-party requests. securityheaders.com: A+. The only red CSP check is style-src 'unsafe-inline'.

What stayed open

External avatar images from webmentions were blocked by img-src 'self' data:. I solved that by caching the avatars locally at build time.

Takeaways

  • Security headers in Caddy are a few lines. There is no good reason to leave them out.
  • default-src 'none' makes you learn what the site really loads.
  • Keep scripts out of the HTML. Once the CSP is strict, they have to move anyway.
  • no-referrer costs a content site without tracking nothing.
  • The scanners are evidence: Webbkoll names the GDPR article for every finding.

Based on

  • Security headers for an Astro site behind Caddy (raw/2026/2026-04-22/security-headers-for-an-astro-site-behind-caddy.md) [post]

More on this

Topic

  • Web Development How this site and others are made and found: static pages, search engines, the IndieWeb and maps in the browser.

Basis for

  • A GDPR-Compliant YouTube Embed An Astro component that loads YouTube only after a click: show once, agree and remember, or just the link. No cookie, no request to Google before consent.
  • Astro Client Islands under a Strict CSP Why Astro islands fail silently under script-src 'self', how I allowed the two hydration inlines by sha256 hash, and a deploy check for hash drift.
  • Caching Webmention Avatars Locally An Astro helper that downloads webmention avatars during the build, dedupes and sweeps them, so the CSP stays strict and readers never hit the avatar host.
  • Leaflet and Stadia Maps Behind a Strict CSP Two fixes that made the photo world map work in production: the tile host in img-src and a Referrer-Policy of origin on the tiles.

Builds on

Uses

  • Astro Tool for building websites from content such as Markdown. Ships plain HTML by default and only loads JavaScript where it is needed.
  • Caddy Web server and reverse proxy that obtains and renews TLS certificates on its own, configured in a short Caddyfile.

Mentioned in

  • A GDPR-Compliant Google Maps Embed An Astro component that loads Google Maps only after consent, stores the decision in localStorage and builds Google and Apple Maps links from coordinates.

Mentioned

Show in the graph view