Skip to content

security: add CSP and security headers to middleware - #3

Open
isaJrKai wants to merge 1 commit into
mainfrom
fix/csp-header
Open

security: add CSP and security headers to middleware#3
isaJrKai wants to merge 1 commit into
mainfrom
fix/csp-header

Conversation

@isaJrKai

Copy link
Copy Markdown
Owner

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

Header Value Purpose
Content-Security-Policy default-src 'self'; script-src 'self' 'unsafe-inline'; ... Prevents XSS, clickjacking, data exfiltration
X-Frame-Options DENY Clickjacking defense (legacy browsers)
X-Content-Type-Options nosniff Prevents MIME sniffing
Referrer-Policy strict-origin-when-cross-origin Limits referrer leakage
X-XSS-Protection 1; mode=block Legacy XSS filter

CSP policy details

default-src 'self';
script-src 'self' 'unsafe-inline';
style-src 'self' 'unsafe-inline';
img-src 'self' data: blob:;
font-src 'self' data:;
connect-src 'self';
frame-ancestors 'none';
base-uri 'self';
form-action 'self';
  • 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 — added applySecurityHeaders() helper, applied to all response paths

Verification

  • npx tsc --noEmit — clean
  • npx eslint — clean

Vercel 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

  1. Open the deployed app
  2. Open DevTools → Console
  3. Look for red CSP violation errors
  4. If any appear, they'll indicate which directive needs loosening

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.
@coderabbitai

coderabbitai Bot commented Jun 25, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@isaJrKai, we couldn't start this review because you've reached your PR review rate limit.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 501abc9f-bef3-467c-ad4d-c3abfa0914e5

📥 Commits

Reviewing files that changed from the base of the PR and between 7053878 and d978de3.

📒 Files selected for processing (1)
  • src/middleware.ts
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/csp-header

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.

❤️ Share

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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant