Skip to content

feat(nextjs): Add CSP in middleware #5493

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 36 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
76e237b
feat(nextjs): Add CSP in middleware
jacekradko Apr 1, 2025
ecc5dd8
wip
jacekradko Apr 1, 2025
4cddb2d
wip
jacekradko Apr 1, 2025
3d4c081
refactor to accept directives
jacekradko Apr 1, 2025
66e0b64
wip
jacekradko Apr 1, 2025
581dac6
wip
jacekradko Apr 1, 2025
5488b01
wip
jacekradko Apr 1, 2025
f060cf6
wip
jacekradko Apr 1, 2025
a5ac231
small refactors
jacekradko Apr 1, 2025
8b82204
add type safety and additional valid directives
jacekradko Apr 1, 2025
e0dbeef
wip
jacekradko Apr 1, 2025
fe4d8e8
wip
jacekradko Apr 1, 2025
7c674e1
wip
jacekradko Apr 1, 2025
6dd3dec
Merge branch 'main' into feat/nextjs-inject-csp-in-middleware
jacekradko Apr 2, 2025
13449bb
Update typings of directives argument
jacekradko Apr 2, 2025
c647397
Update packages/nextjs/src/server/clerkMiddleware.ts
jacekradko Apr 3, 2025
ce872cf
Condense comments
jacekradko Apr 3, 2025
0697722
Update changeset
jacekradko Apr 3, 2025
b05416f
remove sorting
jacekradko Apr 3, 2025
6b1ff48
Added Stripe values to CSP
jacekradko Apr 3, 2025
a340c6b
remove quoted directives from test
jacekradko Apr 3, 2025
852488f
Parse host from PK
jacekradko Apr 3, 2025
4d7e57b
small refactor
jacekradko Apr 3, 2025
7b0ee20
wip
jacekradko Apr 3, 2025
a7fc477
renamed function to be more descriptive of current functionality
jacekradko Apr 3, 2025
3971996
Copy all headers
jacekradko Apr 3, 2025
3755c15
added clerk-telemetry.com to connect-src
jacekradko Apr 3, 2025
f234644
Merge branch 'main' into feat/nextjs-inject-csp-in-middleware
jacekradko Apr 3, 2025
3fc526c
Improve typings
jacekradko Apr 3, 2025
46eadd5
wip
jacekradko Apr 3, 2025
e613894
Merge branch 'main' into feat/nextjs-inject-csp-in-middleware
jacekradko Apr 4, 2025
b79f414
Update .changeset/vast-clubs-speak.md
brkalow Apr 4, 2025
c094d6c
use Headers const
jacekradko Apr 4, 2025
3202669
export const
jacekradko Apr 4, 2025
b60616e
changeset
jacekradko Apr 4, 2025
d822452
update fastify test to ignore changes in @clerk/backend
jacekradko Apr 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-singers-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': minor
---

Added constants.Headers.ContentSecurityPolicy and constants.Headers.Nonce
31 changes: 31 additions & 0 deletions .changeset/vast-clubs-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
'@clerk/nextjs': minor
---

Added Content Security Policy (CSP) header generation functionality to `clerkMiddleware` with support for both standard and strict-dynamic modes. Key features:

- Automatic generation of CSP headers with default security policies compatible with Clerk requirements
- Support for both standard and strict-dynamic CSP modes
- Automatic nonce generation for strict-dynamic mode
- Ability to add custom directives to match project requirements

Example

```
export default clerkMiddleware(
async (auth, request) => {
if (!isPublicRoute(request)) {
await auth.protect();
}
},
{
contentSecurityPolicy: {
mode: "strict-dynamic",
directives: {
"connect-src": ["external.api.com"],
"script-src": ["external.scripts.com"]
}
}
}
);
```
32 changes: 17 additions & 15 deletions packages/backend/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,31 @@ const QueryParameters = {
} as const;

const Headers = {
AuthToken: 'x-clerk-auth-token',
Accept: 'accept',
AuthMessage: 'x-clerk-auth-message',
Authorization: 'authorization',
AuthReason: 'x-clerk-auth-reason',
AuthSignature: 'x-clerk-auth-signature',
AuthStatus: 'x-clerk-auth-status',
AuthReason: 'x-clerk-auth-reason',
AuthMessage: 'x-clerk-auth-message',
ClerkUrl: 'x-clerk-clerk-url',
EnableDebug: 'x-clerk-debug',
ClerkRequestData: 'x-clerk-request-data',
AuthToken: 'x-clerk-auth-token',
CacheControl: 'cache-control',
ClerkRedirectTo: 'x-clerk-redirect-to',
ClerkRequestData: 'x-clerk-request-data',
ClerkUrl: 'x-clerk-clerk-url',
CloudFrontForwardedProto: 'cloudfront-forwarded-proto',
Authorization: 'authorization',
ContentType: 'content-type',
ContentSecurityPolicy: 'content-security-policy',
EnableDebug: 'x-clerk-debug',
ForwardedHost: 'x-forwarded-host',
ForwardedPort: 'x-forwarded-port',
ForwardedProto: 'x-forwarded-proto',
ForwardedHost: 'x-forwarded-host',
Accept: 'accept',
Referrer: 'referer',
UserAgent: 'user-agent',
Origin: 'origin',
Host: 'host',
ContentType: 'content-type',
SecFetchDest: 'sec-fetch-dest',
Location: 'location',
CacheControl: 'cache-control',
Nonce: 'x-nonce',
Origin: 'origin',
Referrer: 'referer',
SecFetchDest: 'sec-fetch-dest',
UserAgent: 'user-agent',
} as const;

const ContentTypes = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,6 @@ exports[`constants from environment variables 1`] = `
{
"API_URL": "CLERK_API_URL",
"API_VERSION": "CLERK_API_VERSION",
"Cookies": {
"ClientUat": "__client_uat",
"DevBrowser": "__clerk_db_jwt",
"Handshake": "__clerk_handshake",
"RedirectCount": "__clerk_redirect_count",
"Refresh": "__refresh",
"Session": "__session",
},
"Headers": {
"Accept": "accept",
"AuthMessage": "x-clerk-auth-message",
"AuthReason": "x-clerk-auth-reason",
"AuthSignature": "x-clerk-auth-signature",
"AuthStatus": "x-clerk-auth-status",
"AuthToken": "x-clerk-auth-token",
"Authorization": "authorization",
"CacheControl": "cache-control",
"ClerkRedirectTo": "x-clerk-redirect-to",
"ClerkRequestData": "x-clerk-request-data",
"ClerkUrl": "x-clerk-clerk-url",
"CloudFrontForwardedProto": "cloudfront-forwarded-proto",
"ContentType": "content-type",
"EnableDebug": "x-clerk-debug",
"ForwardedHost": "x-forwarded-host",
"ForwardedPort": "x-forwarded-port",
"ForwardedProto": "x-forwarded-proto",
"Host": "host",
"Location": "location",
"Origin": "origin",
"Referrer": "referer",
"SecFetchDest": "sec-fetch-dest",
"UserAgent": "user-agent",
},
"JWT_KEY": "CLERK_JWT_KEY",
"PUBLISHABLE_KEY": "CLERK_PUBLISHABLE_KEY",
"SDK_METADATA": {
Expand Down
9 changes: 8 additions & 1 deletion packages/fastify/src/__tests__/constants.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ describe('constants', () => {

test('from environment variables', () => {
jest.resetModules();
expect(constants).toMatchSnapshot();
const { Headers, Cookies, ...localConstants } = constants;

// Verify imported constants exist but don't snapshot them
expect(Headers).toBeDefined();
expect(Cookies).toBeDefined();

// Only snapshot our local constants
expect(localConstants).toMatchSnapshot();
});
});
13 changes: 9 additions & 4 deletions packages/nextjs/src/app-router/server/ClerkProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ const getDynamicClerkState = React.cache(async function getDynamicClerkState() {
return data;
});

const getNonceFromCSPHeader = React.cache(async function getNonceFromCSPHeader() {
return getScriptNonceFromHeader((await headers()).get('Content-Security-Policy') || '') || '';
const getNonceHeaders = React.cache(async function getNonceHeaders() {
const headersList = await headers();
const nonce = headersList.get('X-Nonce');
return nonce
? nonce
: // Fallback to extracting from CSP header
getScriptNonceFromHeader(headersList.get('Content-Security-Policy') || '') || '';
});

export async function ClerkProvider(
Expand Down Expand Up @@ -51,9 +56,9 @@ export async function ClerkProvider(
* For some reason, Next 13 requires that functions which call `headers()` are awaited where they are invoked.
* Without the await here, Next will throw a DynamicServerError during build.
*/
return Promise.resolve(await getNonceFromCSPHeader());
return Promise.resolve(await getNonceHeaders());
}
return getNonceFromCSPHeader();
return getNonceHeaders();
}

const propsWithEnvs = mergeNextClerkPropsWithEnv({
Expand Down
Loading