Skip to content

fix(nextjs): allow Clerk protection origins in generated CSP#9199

Open
mwickett wants to merge 2 commits into
mainfrom
agent/add-protect-csp-defaults
Open

fix(nextjs): allow Clerk protection origins in generated CSP#9199
mwickett wants to merge 2 commits into
mainfrom
agent/add-protect-csp-defaults

Conversation

@mwickett

@mwickett mwickett commented Jul 20, 2026

Copy link
Copy Markdown
Member

Description

Clerk's generated Content Security Policy did not explicitly allow the origins used to protect applications against abuse and fraud. This meant applications relying on clerkMiddleware()'s automatic CSP configuration could still need to add the origins manually.

This adds the Clerk protection origins to the generated script-src, connect-src, and frame-src directives. They are included for every application in both the default and strict CSP modes, regardless of plan or enabled features.

The focused test verifies all three directives, and the existing header assertions cover default, report-only, strict, and custom directive merging.

Paired documentation update: https://github.com/clerk/clerk/pull/2986

Related Accounts CSP update: https://github.com/clerk/cloudflare-workers/pull/2416

Testing

  • pnpm turbo build --filter=@clerk/nextjs
  • pnpm --filter @clerk/nextjs test
  • pnpm --filter @clerk/nextjs lint (passes with 18 existing warnings)
  • pnpm --filter @clerk/nextjs format:check

Checklist

  • pnpm test runs as expected.
  • pnpm build runs as expected.
  • (If applicable) JSDoc comments have been added or updated for any package exports
  • (If applicable) Documentation has been updated

Type of change

  • 🐛 Bug fix
  • 🌟 New feature
  • 🔨 Breaking change
  • 📖 Refactoring / dependency upgrade / documentation
  • other:

@vercel

vercel Bot commented Jul 20, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
swingset Ready Ready Preview, Comment Jul 20, 2026 7:47pm
1 Skipped Deployment
Project Deployment Actions Updated (UTC)
clerk-js-sandbox Skipped Skipped Jul 20, 2026 7:47pm

Request Review

@changeset-bot

changeset-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0c37a56

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@clerk/nextjs Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new

pkg-pr-new Bot commented Jul 20, 2026

Copy link
Copy Markdown

Open in StackBlitz

@clerk/astro

npm i https://pkg.pr.new/@clerk/astro@9199

@clerk/backend

npm i https://pkg.pr.new/@clerk/backend@9199

@clerk/chrome-extension

npm i https://pkg.pr.new/@clerk/chrome-extension@9199

@clerk/clerk-js

npm i https://pkg.pr.new/@clerk/clerk-js@9199

@clerk/electron

npm i https://pkg.pr.new/@clerk/electron@9199

@clerk/electron-passkeys

npm i https://pkg.pr.new/@clerk/electron-passkeys@9199

@clerk/eslint-plugin

npm i https://pkg.pr.new/@clerk/eslint-plugin@9199

@clerk/expo

npm i https://pkg.pr.new/@clerk/expo@9199

@clerk/expo-passkeys

npm i https://pkg.pr.new/@clerk/expo-passkeys@9199

@clerk/express

npm i https://pkg.pr.new/@clerk/express@9199

@clerk/fastify

npm i https://pkg.pr.new/@clerk/fastify@9199

@clerk/hono

npm i https://pkg.pr.new/@clerk/hono@9199

@clerk/localizations

npm i https://pkg.pr.new/@clerk/localizations@9199

@clerk/nextjs

npm i https://pkg.pr.new/@clerk/nextjs@9199

@clerk/nuxt

npm i https://pkg.pr.new/@clerk/nuxt@9199

@clerk/react

npm i https://pkg.pr.new/@clerk/react@9199

@clerk/react-router

npm i https://pkg.pr.new/@clerk/react-router@9199

@clerk/shared

npm i https://pkg.pr.new/@clerk/shared@9199

@clerk/tanstack-react-start

npm i https://pkg.pr.new/@clerk/tanstack-react-start@9199

@clerk/testing

npm i https://pkg.pr.new/@clerk/testing@9199

@clerk/ui

npm i https://pkg.pr.new/@clerk/ui@9199

@clerk/upgrade

npm i https://pkg.pr.new/@clerk/upgrade@9199

@clerk/vue

npm i https://pkg.pr.new/@clerk/vue@9199

commit: 0c37a56

@github-actions

github-actions Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

API Changes Report

Generated by Break Check on 2026-07-20T19:48:28.292Z

Summary

Metric Count
Packages analyzed 19
Packages with changes 0
🔴 Breaking changes 0
🟡 Non-breaking changes 0
🟢 Additions 0

No API Changes Detected

All packages have stable APIs with no detected changes.


Report generated by Break Check

Last ran on 0c37a56.

@coderabbitai

coderabbitai Bot commented Jul 20, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Adds two Clerk protection wildcard origins to the default script-src, connect-src, and frame-src CSP directives generated by clerkMiddleware(). Extends tests across default, report-only, merged-custom, preserved-value, hostname-conversion, and complete-header scenarios. Adds a patch changeset for @clerk/nextjs.

Estimated code review effort: 2 (Simple) | ~10 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly states the main change: allowing Clerk protection origins in generated CSP for nextjs.
Description check ✅ Passed The description matches the change and explains the CSP updates, tests, and documentation.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
packages/nextjs/src/server/__tests__/content-security-policy.test.ts (1)

81-90: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Cover strict CSP in the new origin assertion.

This test only calls createContentSecurityPolicyHeaders with {}. Since strict: true removes http: and https: from script-src, add the same assertions for strict headers to catch regressions in the newly added explicit origins.

As per coding guidelines, unit tests should cover all new functionality and relevant edge cases.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/nextjs/src/server/__tests__/content-security-policy.test.ts` around
lines 81 - 90, Extend the test around createContentSecurityPolicyHeaders to also
generate headers with strict: true, then assert that script-src, connect-src,
and frame-src each retain both Clerk protection origins. Keep the existing
non-strict assertions unchanged and verify the strict policy’s removal of
generic schemes does not remove these explicit origins.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/nextjs/src/server/__tests__/content-security-policy.test.ts`:
- Around line 81-90: Extend the test around createContentSecurityPolicyHeaders
to also generate headers with strict: true, then assert that script-src,
connect-src, and frame-src each retain both Clerk protection origins. Keep the
existing non-strict assertions unchanged and verify the strict policy’s removal
of generic schemes does not remove these explicit origins.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository YAML (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: cb4c01a6-a880-450d-88d3-a35a72c87de6

📥 Commits

Reviewing files that changed from the base of the PR and between a009d91 and f62bc92.

📒 Files selected for processing (3)
  • .changeset/calm-dragons-protect.md
  • packages/nextjs/src/server/__tests__/content-security-policy.test.ts
  • packages/nextjs/src/server/content-security-policy.ts
🔗 Linked repositories identified

CodeRabbit considers these linked repositories for cross-repo context during reviews:

  • clerk/clerk_go (manual)
  • clerk/dashboard (manual)
  • clerk/accounts (manual)
  • clerk/backoffice (manual)
  • clerk/clerk (manual) → reviewed against open PR #2986 agent/update-protect-csp-docs instead of the default branch
  • clerk/clerk-docs (manual)
  • clerk/cloudflare-workers (manual)

@mwickett

Copy link
Copy Markdown
Member Author

Addressed in 0c37a56. The protection-origin test now runs against both default and strict CSP headers, and verifies that strict mode removes the broad HTTP/HTTPS schemes while retaining both explicit Clerk protection origins in script-src, connect-src, and frame-src. The focused CSP suite passes all 38 tests; lint and formatting checks also pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants