Skip to content

feat: enable strict Content-Security-Policy with nonce-based script-src#214

Open
cupcakearmy wants to merge 1 commit into
mainfrom
feat/strict-csp
Open

feat: enable strict Content-Security-Policy with nonce-based script-src#214
cupcakearmy wants to merge 1 commit into
mainfrom
feat/strict-csp

Conversation

@cupcakearmy

Copy link
Copy Markdown
Owner

Closes #164

Enables the previously disabled CSP middleware. The old implementation was disabled because SvelteKit's inline bootstrap script would be blocked by a strict policy.

Changes

src/csp.rs — Rewritten to:

  • Generate a cryptographically random nonce per request (ring + bs62)
  • Inject the nonce into the inline <script> tag in index.html
  • Set Content-Security-Policy header with 'nonce-{nonce}' + 'strict-dynamic'
  • Policy: default-src 'self'; script-src 'nonce-{nonce}' 'strict-dynamic'; style-src 'self' 'unsafe-inline'; object-src 'none'; base-uri 'self'; connect-src 'self'

src/main.rs — Replaced ServeFile fallback with csp::spa_fallback handler

How it works

  1. Visitor requests an SPA route (e.g. /note/abc)
  2. Server generates a random nonce, injects it into the inline bootstrap script
  3. Script loads modules via import()strict-dynamic trusts dynamically loaded scripts
  4. Static assets (JS chunks, CSS) are served via ServeDir without being blocked

Replaces the disabled CSP middleware with a working implementation:
- Generates a per-request nonce for script-src
- Injects nonce into the inline SvelteKit bootstrap script
- Uses 'strict-dynamic' so dynamically imported modules are trusted
- SPA fallback serves index.html with CSP header
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strict Content Security Policy

1 participant