Configure Clerk Content-Security-Policy headers
Content-Security-Policy (CSP) headers secure your document by preventing resources from being loaded from unexpected sources. This protects your apps from XSS attacks and data injections.
For Clerk to work correctly in your application, you'll need to configure the following CSP directives:
script-src
- This value should include the host application's FAPI hostname, such ashttps://clerk.your-domain.com
, as well as cloudflare's bot protection host,https://challenges.cloudflare.com
.connect-src
- This value should also include the host application's FAPI hostname, such ashttps://clerk.your-domain.com
.img-src
- This value should behttps://img.clerk.com
.worker-src
- Use the'self'
value to indicate that workers can be loaded from first-party scripts. Theblob:
schema value also needs to be included.style-src
- Due to Clerk's usage of runtime CSS-in-JS for styling,'unsafe-inline'
needs to be included.frame-src
- This value should also include cloudflare's bot protection host,https://challenges.cloudflare.com
.
The following example demonstrates a Next.js config file that sets the necessary directives for your application's assets and Clerk to load and function correctly. The values used apply to your currently selected instance, make sure to handle both your development instance and production instance hosts.
Usage of unsafe-eval
and unsafe-inline
directives in Next.js
- Within
script-src
,unsafe-eval
is a requirement for Next.js to run in development mode. For production environment CSPs, it should be removed. - Within
script-src
,unsafe-inline
is a requirement for Next.js in both dev and prod environments if you're using the App Router and not usingstrict-dynamic
. If you are using the Pages Router, it can be removed. - Within
style-src
,unsafe-inline
is a requirement for Clerk's components to inject their styles. Removing this requirement is on our roadmap. If you'd like to see this implemented sooner, contact support.
Implementing a strict-dynamic
CSP
If you'd like to implement a strict-dynamic CSP, Clerk supports this, but with a different type of configuration. As strict-dynamic CSPs require a "nonce" value that should be programmatically generated per-request, the best way to make this work is within middleware that runs on every request. The following example demonstrates how to implement a strict-dynamic CSP with Next.js middleware, but the same approach could be used with any other framework.
With this strict-dynamic
configuration in place, all script tags must be passed with a nonce
value or they will be blocked. This can be done by passing the nonce value as a nonce
parameter to the script tag. For example, <script src="https://example.com/script.js" nonce="<nonce_value>"></script>
. If you are using Next.js, any scripts loaded through next will automatically have the nonce value injected.
With the above middleware, the nonce value is made accessible via the x-nonce
request header. An example is provided below on how to access this value within a Next.js page.
If you're using one of Clerk's React-based SDKs, in order for Clerk to load correctly, the nonce value must also be passed to the <ClerkProvider>
component. This can be done by passing the nonce value as a nonce
prop to the <ClerkProvider>
component. For example:
If you are using Next.js and your layout file is rendered on the server (as is the case with the example above), Clerk's SDK will automatically read the nonce
from the request and pass it into <ClerkProvider>
. If you are rendering the provider on the client, you will need to explicitly pass the nonce
value to the client and into <ClerkProvider>
.
Feedback
Last updated on