Skip to content

Content security policy

This documentation site ships with a strict Content-Security-Policy (CSP), and the build fails if a page would break it. This page records what the site needs, for anyone who runs or changes it.

Every page carries its own policy in a <meta http-equiv="content-security-policy"> tag. Astro writes it (security.csp in astro.config.mjs), and two build steps complete and check it:

Directive Value Why
default-src 'none' Everything not listed is blocked.
script-src 'self', 'wasm-unsafe-eval' and a SHA-256 hash for each inline script No 'unsafe-inline', no 'unsafe-eval'.
style-src 'self' and a SHA-256 hash for each <style> element No inline styles from elements without a hash.
style-src-attr 'unsafe-inline' See “What Starlight needs” below.
img-src 'self' and data: Local images and small inline icons.
font-src 'self' Geist is self hosted. Nothing loads from Google.
connect-src 'self' Search loads its index from this site.
base-uri, manifest-src 'self'
form-action, object-src 'none' The site has no forms and no plugins.

frame-ancestors cannot be set in a meta tag. It is sent as an HTTP header from public/_headers together with the other security headers.

Starlight is built for a normal web page, so a strict policy takes a few allowances. Each one is narrow and documented here.

  • Inline scripts. Starlight writes three small scripts into every page: one sets the theme before first paint (so there is no flash of the wrong theme), one restores the sidebar scroll position and one shows the search shortcut for your operating system. Astro hashes the scripts it bundles but not these, so scripts/finalise-csp.mjs adds the hash of every inline script to each page’s policy after the build. Hashes are computed from the final HTML, so a change to a script changes its hash automatically. No page allows 'unsafe-inline' for scripts.
  • Style attributes. Starlight sets a few style="..." attributes, for example the icon size. Hashes in style-src cover <style> elements but not attributes, so style-src-attr allows inline attributes. This does not allow <style> elements or scripts.
  • WebAssembly. Search (Pagefind) runs a WebAssembly module. 'wasm-unsafe-eval' allows that and nothing else: eval() and new Function() stay blocked.
  • Syntax highlighting. Code blocks use Expressive Code, which writes its styles as hashed <style> elements and a bundled script. Astro prints a warning that Shiki uses inline styles during the build. It does not apply here, because the check below passes.

pnpm --filter @kytheos/docs build runs, in order: astro build, finalise-csp.mjs and verify-csp.mjs. The last one fails the build when a page has:

  • no policy, or a policy without default-src, script-src or style-src;
  • 'unsafe-inline' or 'unsafe-eval' in script-src;
  • an inline script or <style> element whose hash is not in the page’s policy;
  • an inline event handler such as onclick, or a javascript: link;
  • a script or stylesheet from another origin.

The site is static files, so a policy that lists the hash of each page’s scripts cannot be one shared HTTP header. The meta tag carries the per page policy. The header file adds what a meta tag cannot (frame-ancestors, HSTS, referrer and permissions policies).

A per page policy in a meta tag does not protect against an injected tag placed before it. The content of these docs is written in this repository and rendered at build time, so there is no user input on the page. Moving the policy to HTTP headers is possible later with a small build step that writes a _headers entry for each page.