security: add CSP and security headers to middleware - #3
Conversation
Adds the following HTTP security headers to every response:
- Content-Security-Policy: restricts script/style/img/font/connect
sources to self, prevents clickjacking (frame-ancestors none),
prevents form submission to external sites (form-action self)
- X-Frame-Options: DENY (clickjacking defense, legacy browser support)
- X-Content-Type-Options: nosniff (prevents MIME sniffing)
- Referrer-Policy: strict-origin-when-cross-origin
- X-XSS-Protection: 1; mode=block (legacy XSS filter)
CSP allows unsafe-inline for scripts and styles because Next.js
hydration + styled-jsx require it. A future improvement would be to
use nonce-based CSP with Next.js 16 built-in nonce support.
|
Warning Review limit reached
More reviews will be available in 28 minutes and 44 seconds. Learn how PR review limits work. Your organization has used up its prepaid credits, and credit purchases are no longer available. Enable the review add-on in the billing tab to keep reviews running — you're only billed for reviews past your plan's rate limits ($0.25/file). ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based credits. 🚦 How do rate limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please see our Fair Usage Limits Policy for further information. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
What this fixes
The app had no Content-Security-Policy header, meaning an XSS vulnerability anywhere in the app (now or in the future) could execute arbitrary scripts, steal cookies, or exfiltrate data.
Headers added
Content-Security-Policydefault-src 'self'; script-src 'self' 'unsafe-inline'; ...X-Frame-OptionsDENYX-Content-Type-OptionsnosniffReferrer-Policystrict-origin-when-cross-originX-XSS-Protection1; mode=blockCSP policy details
script-src 'unsafe-inline'— required for Next.js hydration. Future improvement: use nonce-based CSP.style-src 'unsafe-inline'— required for Next.js + styled-jsx.img-src data: blob:— allows data-URI avatars and blob uploads.connect-src 'self'— blocks external API calls (no exfiltration).frame-ancestors 'none'— prevents the app from being iframed.Files changed
src/middleware.ts— addedapplySecurityHeaders()helper, applied to all response pathsVerification
npx tsc --noEmit— cleannpx eslint— cleanVercel env vars
None needed.
Deployment risk
Low. Security headers are additive — they don't change app behavior, only add headers to responses. If the CSP is too strict, the browser console will show CSP violations (check DevTools → Console after deploy). The policy is permissive enough for Next.js's needs.
Testing after merge