Skip to content
Astro Eclipse
Esc
↑↓navigate↵open⌘Jpreview
On this page

Content security policy

What a strict Content-Security-Policy needs to run the Astro Eclipse head script, tested with Astro's CSP support and with a hand-written policy.

The head script is inline, so a script-src without 'unsafe-inline' needs to allow it. The library adds no inline <style>: the reveal and the CSS guard use constructed stylesheets, which style-src does not block.

Astro’s built-in CSP

With security: { csp: true }, Astro hashes the inline scripts it knows about and puts the hashes in a <meta http-equiv="content-security-policy"> policy. The head script is among them: the packed-tarball test builds a fresh site with the option and checks that the SHA-256 of the head script is in the policy’s script-src. It runs for every Astro version in the CI matrix that has the option, and is skipped for 5.0.0, which does not.

export default defineConfig({
  security: { csp: true },
  integrations: [eclipse()],
})

A hand-written policy

A policy you send as a header needs the hash of the head script, next to the hashes of your other inline scripts:

Content-Security-Policy: script-src 'self' 'sha256-<hash of the head script>'

The script is static for a given config, so its hash changes only when you change the Astro Eclipse config or upgrade the package. To get it, build the site, copy the content of the <script> that contains your storageKey from the built HTML, and hash that text:

printf %s "$SCRIPT_TEXT" | openssl dgst -sha256 -binary | openssl base64 -A

An e2e test in the demo serves the pages with script-src 'self' plus the hashes of every inline script and checks that the theme is applied with no script-src violation.

When the script is blocked

With script-src 'self' alone, the browser refuses the head script and logs a violation. Nothing sets the theme before first paint, so the page has no theme attribute and shows your default CSS until the client API runs. That is a flash of the default theme. Add the hash, or 'unsafe-inline' for scripts if you accept it.

Styles

A strict style-src 'self' does not affect theme changes: an e2e test changes the theme under that policy in Chromium and WebKit and sees no violation. The colorScheme option sets a property through element.style, which CSP does not block either.

Not checked

A nonce per request is not supported: injectScript has no per-request hook, so use a hash. The browser checks ran in Chromium and WebKit; Firefox runs the same tests in CI.

Was this page helpful?