VibeCodeHealth

csp

Security headers explained for vibe coders (with copy-paste Next.js/Vercel config)

Plain-English guide to CSP, HSTS, framing protection, nosniff, and Referrer-Policy—plus copy-paste next.config.ts and vercel.json you can deploy today.

Paste your production URL into the free Vibe Code Health grader for a Semrush-style score on secrets, headers, TLS, and common leaks—then come back to this guide for the fixes.

Security headers are short instructions your server sends with every HTML response. Browsers read them before deciding whether a script can run, whether your app can be iframed, or whether HTTP is even allowed next time.

If you ship from Lovable, Bolt, v0, or a quick Next.js deploy, you can have a polished UI and still send zero useful headers. That is one reason so many vibe-coded apps score poorly on automated graders even when they do not leak API keys.

This guide explains the headers that matter for beginners, shows you how to inspect them, and gives copy-paste Next.js / Vercel config. When you want a second opinion on a live URL, use Vibe Code Health—missing CSP and clickjacking protection currently cap typical unhardened apps around a C on rubric v2.1.

The five-minute inspection

  1. Open your production URL (the one you share, not only localhost).
  2. DevTools → Network → reload → click the first document request.
  3. Read Response Headers.

You are looking for names like Content-Security-Policy, Strict-Transport-Security, X-Frame-Options, X-Content-Type-Options, Referrer-Policy, and Permissions-Policy.

Chrome and Edge show them clearly; Firefox does too. You can also use:

curl -sI https://www.your-app.com | grep -iE 'content-security|strict-transport|x-frame|x-content|referrer|permissions|access-control'

Header-by-header, in plain English

Content-Security-Policy (CSP) — start here

What it does: Allow-lists where scripts, styles, images, and frames may load from. A good CSP turns many XSS bugs into failed script loads instead of account takeovers.

Beginner starting point:

Content-Security-Policy: default-src 'self'; script-src 'self' 'unsafe-inline' https://va.vercel-scripts.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: blob: https:; frame-ancestors 'none'; base-uri 'self'; form-action 'self'

Notes:

  • 'unsafe-inline' for scripts is common for vibe apps and design tools. It weakens XSS protection but is still better than no CSP. Plan to move toward nonces/hashes later (MDN CSP).
  • Add hosts you truly need (https://*.supabase.co in connect-src, analytics hosts, font CDNs).
  • frame-ancestors 'none' doubles as clickjacking protection.

Strict-Transport-Security (HSTS)

What it does: Tells browsers “for this domain, use HTTPS only for a long time.”

Strict-Transport-Security: max-age=63072000; includeSubDomains; preload

Only enable this after HTTPS works for every hostname users might hit. Reference: MDN HSTS. Vercel often adds HSTS for you on *.vercel.app and custom domains.

X-Frame-Options or CSP frame-ancestors

What it does: Stops other sites from embedding your UI in an iframe (clickjacking). Prefer CSP frame-ancestors, but X-Frame-Options: DENY is widely understood.

X-Frame-Options: DENY

Background: OWASP Clickjacking.

X-Content-Type-Options

X-Content-Type-Options: nosniff

Blocks browsers from “guessing” MIME types. Tiny change, real edge-case wins. MDN nosniff.

Referrer-Policy

Referrer-Policy: strict-origin-when-cross-origin

Avoids sending full paths and query strings (sometimes containing tokens) to third-party origins. MDN Referrer-Policy.

Permissions-Policy

Permissions-Policy: camera=(), microphone=(), geolocation=(), interest-cohort=()

Explicitly turns off powerful device APIs you are not using. MDN Permissions-Policy.

Access-Control-Allow-Origin — usually should not be * on HTML

Access-Control-Allow-Origin: * means any website may read that response from JavaScript. On a public marketing page the risk is often low; on APIs or authenticated JSON it is serious. Never combine * with Access-Control-Allow-Credentials: true (browsers reject it, and it signals a confused CORS setup).

For a same-origin Next app, omit ACAO on document responses entirely. See MDN CORS.

Copy-paste: Next.js App Router (next.config.ts)

This pattern matches what we use in production for Vibe Code Health—adjust script-src / connect-src for your own vendors.

import type { NextConfig } from "next";

const securityHeaders = [
  { key: "X-Content-Type-Options", value: "nosniff" },
  { key: "X-Frame-Options", value: "DENY" },
  { key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
  {
    key: "Permissions-Policy",
    value: "camera=(), microphone=(), geolocation=(), interest-cohort=()",
  },
  {
    key: "Strict-Transport-Security",
    value: "max-age=63072000; includeSubDomains; preload",
  },
  {
    key: "Content-Security-Policy",
    value: [
      "default-src 'self'",
      "script-src 'self' 'unsafe-inline' https://va.vercel-scripts.com",
      "style-src 'self' 'unsafe-inline'",
      "img-src 'self' data: blob: https:",
      "font-src 'self' data:",
      "connect-src 'self' https://va.vercel-scripts.com https://*.supabase.co",
      "frame-ancestors 'none'",
      "base-uri 'self'",
      "form-action 'self'",
    ].join("; "),
  },
];

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

export default nextConfig;

Docs: Next.js headers.

Redeploy, then re-check with curl -sI or the grader.

Copy-paste: vercel.json (static or non-Next)

{
  "headers": [
    {
      "source": "/(.*)",
      "headers": [
        { "key": "X-Content-Type-Options", "value": "nosniff" },
        { "key": "X-Frame-Options", "value": "DENY" },
        { "key": "Referrer-Policy", "value": "strict-origin-when-cross-origin" },
        {
          "key": "Permissions-Policy",
          "value": "camera=(), microphone=(), geolocation=()"
        },
        {
          "key": "Content-Security-Policy",
          "value": "default-src 'self'; frame-ancestors 'none'; base-uri 'self'"
        }
      ]
    }
  ]
}

Vercel’s own header docs: Project headers / security headers.

Lovable, Bolt, and v0

Not every AI host exposes a full next.config. Practical options:

  1. Ask the builder for host-level headers — “Add CSP, X-Frame-Options DENY, nosniff, Referrer-Policy, and HSTS in the deployment config.”
  2. Put a tiny Edge middleware on platforms that support it, setting response headers on HTML routes.
  3. Front the app with a host that supports response headers (Cloudflare Transform Rules, Netlify _headers, etc.).

If the platform truly cannot set headers, document that limitation—and avoid putting sensitive actions in pages that can be iframed by anyone.

Netlify _headers format: Netlify custom headers.

A sane rollout order (so you do not break prod)

  1. Deploy headers on a preview URL first.
  2. Click through login, payments, embeds, and admin.
  3. Watch the browser console for CSP violations; add specific hosts, do not jump straight to *.
  4. Promote to production.
  5. Re-scan with Vibe Code Health and keep the shareable report for your “we tried” folder.

If a CSP breaks an embed you need (e.g. a Stripe Element or a map iframe), widen only that directive (script-src, frame-src, connect-src)—do not delete the whole policy.

Common mistakes (and the fix)

“I added headers in next.config, but production still shows the old ones.”
Hard-refresh, wait for the new deployment, and confirm you are curling the production hostname—not an old preview. On CDNs, document responses can be cached; a new deploy or cache purge may be required.

“CSP broke my CSS.”
You likely blocked a font or style CDN. Prefer self-hosting fonts (as Next next/font does) or add that host to style-src / font-src only.

“I set Access-Control-Allow-Origin: * to fix an API error.”
That “fix” on an HTML document is usually unrelated to your API bug. For APIs, allowlist the exact frontend origin(s). For pages, omit ACAO.

“Lovable/Bolt says it cannot set headers.”
Put Cloudflare (or similar) in front, or migrate the production deploy to Vercel/Netlify where response headers are first-class. Shipping without CSP forever is choosing to stay soft against XSS.

“Is X-XSS-Protection still required?”
Modern guidance is to rely on CSP; the old X-XSS-Protection header is deprecated in current browsers. Prefer CSP. See MDN X-XSS-Protection.

Minimal vs stronger CSP (when you are ready)

Day-one (works with many vibe UIs):

default-src 'self'; script-src 'self' 'unsafe-inline' https://va.vercel-scripts.com; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; connect-src 'self' https://*.supabase.co; frame-ancestors 'none'

Harder (after you remove inline scripts):

Use nonces with Next.js middleware or framework helpers so each response allows only that page’s scripts. This takes more engineering but is the gold standard for XSS containment. Next and React docs continue to evolve here—follow current framework guidance rather than copying year-old Stack Overflow snippets.

You do not need the harder policy to beat “no policy at all.” Ship day-one, measure, tighten.

How graders think about headers

On Vibe Code Health’s rubric v2.1:

  • Missing CSP is a high-impact finding.
  • Missing CSP and framing protection caps the letter grade at C, because that pair is the baseline browser defense most vibe apps skip.
  • HTML responses with Access-Control-Allow-Origin: * are usually informational; * plus credentials is critical.

Pair header work with the Lovable security checklist and Supabase key hygiene. Headers will not save you if service_role is in the bundle—but they will raise the cost of the next XSS.

Sources

Ship the UI. Then spend fifteen minutes on headers—it is one of the highest leverage hardening passes you can do without rewriting the app.

Grade your app next

Passive scan only—no login, no crawl of your whole site.

Open the scanner

Platform checkers

Free A–F grades written for the host you ship on.