# 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.

*Canonical: https://makerkit.dev/docs/react-router-supabase-turbo/security/csp*

---

The Next.js Supabase Turbo kit provides a secure way to include [CSP headers](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/CSP) in your application. This functionality is available starting from version 2.8.0 and uses [Nosecone](https://docs.arcjet.com/nosecone/reference#headers) to generate the CSP headers.

{% sequence title="Steps to configure CSP" description="Learn how to configure CSP in the Next.js Supabase Turbo kit." %}

[Default security headers](#default-security-headers)

[How to enable a strict CSP](#how-to-enable-a-strict-csp)

[Strict CSP and static rendering](#strict-csp-and-static-rendering)

[Making your application compliant with CSP](#making-your-application-compliant-with-csp)

[Adding a nonce to your application](#adding-a-nonce-to-your-application)

[Modifying the default CSP configuration](#modifying-the-default-csp-configuration)

{% /sequence %}

## 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.

{% callout type="warning" title="HSTS and subdomains (opt-in)" %}
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`.
{% /callout %}

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:

```bash
ENABLE_STRICT_CSP=true
```

By 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:

1. Add `export const instant = false` to the root layout, since no route can produce an instant static shell any more:

```tsx {% filename="apps/web/app/[locale]/layout.tsx" %}
export const instant = false;
```

2. Remove the `ensureStatic` export 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:

```diff {% filename="apps/web/app/[locale]/(marketing)/layout.tsx" %}
- 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.

1. You will need to add the `nonce` to any third-party script tags or stylesheets you have in your application.
2. 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.

```tsx
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.

```tsx
<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](https://docs.arcjet.com/nosecone/reference#headers) for more information on the available options.

```ts {% filename="apps/web/lib/create-csp-response.ts" %}
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,
};
```
