VibeSecCheck
Home/Security Guides/nextjs-security-headers
Medium RiskHeaders, Auth & APIsNext.js 15VercelCSPHSTS

Production Next.js 15 Security Headers Setup

A fresh Next.js project ships with zero Content-Security-Policy, no X-Frame-Options, no nosniff, and an X-Powered-By: Next.js header. Adding 25 lines to next.config.ts instantly hardens your browser attack surface.

Why AI Builders Introduce This

AI scaffolding almost always leaves next.config.ts empty (`const nextConfig = {}`), skipping browser-level XSS, clickjacking, and MIME-sniffing defenses.

Real-World Exploit Impact
  • Clickjacking allows malicious sites to overlay invisible iframes over your billing/delete buttons
  • Missing CSP lets injected scripts exfiltrate auth cookies or localStorage tokens to attacker domains
  • Missing HSTS leaves first-visit HTTP users vulnerable to downgrade attacks on public Wi-Fi

Vulnerable Pattern vs. Production Fix

Default Unhardened next.config.tstypescript
// next.config.ts — Emits X-Powered-By: Next.js and zero browser security headers
const nextConfig = {};
export default nextConfig;
Hardened next.config.ts Security Headers
import type { NextConfig } from "next";

const securityHeaders = [
  { key: "Strict-Transport-Security", value: "max-age=31536000; includeSubDomains" },
  { key: "X-Frame-Options", value: "DENY" },
  { key: "X-Content-Type-Options", value: "nosniff" },
  { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
  { key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=()" },
  {
    key: "Content-Security-Policy",
    value:
      "default-src 'self'; img-src 'self' data: https:; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; connect-src 'self' https://*.supabase.co https://api.stripe.com; frame-ancestors 'none'; object-src 'none';",
  },
];

const nextConfig: NextConfig = {
  poweredByHeader: false,
  productionBrowserSourceMaps: false,
  async headers() {
    return [{ source: "/(.*)", headers: securityHeaders }];
  },
};

export default nextConfig;

Copy-Paste AI Fix Prompt (Cursor · Lovable · Bolt · Claude Code)

Add production HTTP security headers in next.config.ts:
1. Set poweredByHeader: false and productionBrowserSourceMaps: false.
2. Add async headers() applying Strict-Transport-Security, X-Frame-Options: DENY, X-Content-Type-Options: nosniff, Referrer-Policy: strict-origin-when-cross-origin, Permissions-Policy, and a Content-Security-Policy tailored to the external domains our app actually uses (inspect our code for Supabase, Stripe, analytics, or font domains so CSP doesn't break anything).
3. Verify the app builds cleanly with npm run build.

How to Verify Your Fix Worked

  1. 1Run `curl -I https://your-app.com` and verify all 6 security headers appear and X-Powered-By is gone.
  2. 2Open browser DevTools Console and navigate all pages to ensure zero CSP violation errors.
  3. 3Re-run VibeSecCheck and watch your Vibe Security Score jump into the A range.

Frequently Asked Questions

Will Content-Security-Policy break my Supabase or Stripe integration?

Only if you forget to allowlist their domains in connect-src / script-src / frame-src. Start by inspecting your Network tab or using Content-Security-Policy-Report-Only first.

Why set both X-Frame-Options: DENY and CSP frame-ancestors 'none'?

CSP frame-ancestors is the modern standard, while X-Frame-Options provides backward compatibility for older browsers and compliance scanners.

Should I enable HSTS preload immediately?

Only add `preload` once you are 100% certain your apex domain and every subdomain will permanently support HTTPS.

Related Security Playbooks