lockettering, uk
grid142 gCO₂/kWh
solar3.8 kW export
last deploy695181c

hardening a static site that has nothing to steal

default-src none, no inline styles, a CI check that fails the build if that changes, and a critical Astro advisory that turned out to be a major-version bump.

A static blog has no login, no database, no forms. The threat model is mostly “someone injects a script into a page people trust”, plus supply chain. Headers and a lockfile handle most of it.

The CSP

Before:

default-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; ...

After:

default-src 'none'; script-src 'self'; style-src 'self'; style-src-attr 'unsafe-inline';
font-src 'self'; img-src 'self' data:; connect-src 'self'; manifest-src 'self';
base-uri 'none'; form-action 'none'; frame-ancestors 'none'; object-src 'none';
upgrade-insecure-requests
  • default-src 'none' means anything I didn’t list is blocked, rather than anything same-origin being allowed.
  • img-src lost https:. No post loads a remote image, so there was no reason to let one.
  • form-action 'none' and base-uri 'none': there are no forms, and nothing needs <base>.
  • 'unsafe-inline' is gone from style-src. Astro was inlining small stylesheets as <style> blocks (inlineStylesheets: 'auto'); that’s now 'never', and five style="..." attributes in page templates became classes.

The one concession is style-src-attr 'unsafe-inline'. Shiki colours code tokens with style="color:#..." on each <span>. Attribute styles can’t load resources or run script, so this is a much smaller hole than inline <style>. Browsers without CSP3 style-src-attr fall back to style-src and render code blocks uncoloured, which is an acceptable failure.

The rest of _headers

  • Cross-Origin-Opener-Policy: same-origin and Cross-Origin-Resource-Policy: same-origin. /rss.xml detaches CORP (! Cross-Origin-Resource-Policy — Cloudflare comma-joins headers from overlapping rules, so overriding produces same-origin, cross-origin) and adds Access-Control-Allow-Origin: *, because web-based feed readers fetch it from their own origin.
  • Permissions-Policy turns off every powerful feature by name. interest-cohort is gone; FLoC died in 2022 and browsing-topics replaced it.
  • <meta name="generator"> is removed. Advertising the exact framework version is free reconnaissance.
  • /.well-known/security.txt (RFC 9116) with an Expires a year out, because the RFC requires one and because I will otherwise forget it exists.

A CI check, so this stays true

Headers rot quietly. One component with a <style> block and the CSP breaks styling in production while the build stays green. scripts/check-csp.mjs walks dist/**/*.html and fails on inline <script>, <style> elements, on*= handlers, or a src/href load from another origin. It runs in CI after npm run build.

The supply chain

npm audit on the old lockfile: 12 advisories, 1 critical, 9 high. Most were build-time only — postcss, svgo, js-yaml, smol-toml parsing files I wrote. npm audit fix got it to 4. The last four needed astro 6 → 7, because the critical one (GHSA-jrpj-wcv7-9fh9, unescaped attribute names in spread props) wasn’t backported.

For a site whose only input is my own MDX, that XSS is close to unexploitable. I upgraded anyway, because “close to” is not a property I want to re-argue every time Dependabot opens a PR. astro check: 0 errors. Build: 11 pages. Audit: found 0 vulnerabilities.

CI now runs npm audit --omit=dev --audit-level=high, checks out with persist-credentials: false, and Dependabot opens weekly npm and monthly Actions PRs.

Nothing here makes the site more secure than a folder of HTML. It makes it as secure as one, on purpose, and makes CI say so if it stops being true.