Content Security Policy (CSP) in the Next.js Supabase Turbo kit
Learn how to secure your Next.js application with Content Security Policy (CSP) using the Next.js Supabase Turbo kit.
The Next.js Supabase Turbo kit provides a secure way to include CSP headers in your application. This functionality is available starting from version 2.8.0 and uses Nosecone to generate the CSP headers.
Steps to configure CSP
Learn how to configure CSP in the Next.js Supabase Turbo kit.
Default security headers
Whether or not strict CSP is enabled, apps/web/proxy.ts sends these headers on every page response:
| Header | Value |
|---|---|
X-Content-Type-Options | nosniff |
X-Frame-Options | SAMEORIGIN |
Referrer-Policy | strict-origin-when-cross-origin |
Permissions-Policy | camera=(), microphone=(), geolocation=() |
Cross-Origin-Opener-Policy | same-origin |
Strict-Transport-Security | max-age=63072000 (production builds only) |
They are defaults: a header already on the response is kept, so the stricter Nosecone values win when ENABLE_STRICT_CSP=true. To change one, edit DEFAULT_SECURITY_HEADERS in apps/web/proxy.ts. API routes (/api/*) are outside the proxy matcher and don't get them.
Browsers remember HSTS for two years. To also cover every subdomain, set HSTS_HEADER in apps/web/proxy.ts to max-age=63072000; includeSubDomains, but only once every subdomain of your domain serves HTTPS: browsers will refuse plain-HTTP subdomains for the whole max-age.
If your app needs the camera, microphone or geolocation, or is embedded in an iframe on another site, change Permissions-Policy or X-Frame-Options accordingly.
How to enable a strict CSP
To enable a strict CSP, you need to set the following environment variable:
ENABLE_STRICT_CSP=trueBy default, this setting is disabled due to the overhead required for development. While this is a bit advanced, it is recommended to enable it before going to production - or at some point in your development process.
Once enabled, the CSP headers will be automatically added to the response headers using the Next.js middleware.
Strict CSP and static rendering
A CSP nonce must be unique for every response, so it can never be part of a page rendered at build time. With strict CSP enabled, the root layout reads the nonce from headers(), which makes every page render per request — including the marketing pages, which are otherwise static and CDN-cacheable.
The kit is configured for static rendering by default, so enabling strict CSP requires two code changes alongside the environment variable:
- Add
export const instant = falseto the root layout, since no route can produce an instant static shell any more:
export const instant = false;- Remove the
ensureStaticexport from the marketing layout. It fails the build when any part of a marketing page renders per request, and the nonce read in the root layout counts:
- export const ensureStatic = 'navigation';If you skip the second step, next build fails on the marketing routes. That is intentional: it stops strict CSP from silently making your marketing site dynamic.
Weigh this before enabling it: you trade CDN-cached marketing pages for a server render on every visit.
Making your application compliant with CSP
Enabling a strict CSP has a few consequences on your application - you will need to make sure your application is compliant with the CSP rules.
- You will need to add the
nonceto any third-party script tags or stylesheets you have in your application. - If you make external HTTP requests, you will need to add the domains to the default list of allowed domains (by default, only requests to your Supabase project are allowed).
Adding a nonce to your application
From a Server Component, you can retrieve a nonce from the headers() object.
import { headers } from "next/headers";const headersStore = await headers();const nonce = headersStore.get("x-nonce");If you want to pass the nonce to a Script tag, you can do so by adding the nonce attribute to the script tag.
<script nonce={nonce} src="https://example.com/script.js"></script>Modifying the default CSP configuration
In the application middleware, modify the apps/web/lib/create-csp-response.ts file to modify the CSP configuration. You may need to do this to allow safe external requests that your application makes.
Please refer to the Nosecone documentation for more information on the available options.
const config: NoseconeOptions = {...noseconeConfig,contentSecurityPolicy: { directives: { ...noseconeConfig.contentSecurityPolicy.directives, connectSrc: [ ...noseconeConfig.contentSecurityPolicy.directives.connectSrc, ...ALLOWED_ORIGINS, ], imgSrc: [ ...noseconeConfig.contentSecurityPolicy.directives.imgSrc, ...IMG_SRC_ORIGINS, ], upgradeInsecureRequests: UPGRADE_INSECURE_REQUESTS, },},crossOriginEmbedderPolicy: CROSS_ORIGIN_EMBEDDER_POLICY,};