ShipBastion AI Watch — $19/mo

ShipBastion / Security guides / Next.js and Vercel security headers: copy-paste config

Guide · VibeShield · Updated 2026-11-30 · 6 min read

Next.js and Vercel security headers: copy-paste config

Next.js sends almost no security headers by default. Add them once in next.config.js and every route, including static assets, gets them.

Free scan. Sign in with Google to see the full report; paid plans start with a 7-day free trial.

next.config.js

Start with a CSP in report-only mode if you are unsure what your app loads; switch the header name once the console is clean.

const securityHeaders = [
  { key: 'Strict-Transport-Security', value: 'max-age=63072000; includeSubDomains; preload' },
  { 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:; object-src 'none'; base-uri 'none'; frame-ancestors 'none'" },
];

module.exports = {
  async headers() {
    return [{ source: '/:path*', headers: securityHeaders }];
  },
};

Nonce-based CSP for scripts

Inline scripts from Next.js need either a nonce or unsafe-inline. Generate a nonce per request in middleware and pass it to the CSP; Next.js applies it to its own scripts when it finds the header.

// middleware.ts
import { NextResponse } from 'next/server';
export function middleware(request) {
  const nonce = Buffer.from(crypto.randomUUID()).toString('base64');
  const csp = `script-src 'self' 'nonce-${nonce}' 'strict-dynamic'; object-src 'none'; base-uri 'none'`;
  const headers = new Headers(request.headers);
  headers.set('x-nonce', nonce);
  headers.set('Content-Security-Policy', csp);
  const res = NextResponse.next({ request: { headers } });
  res.headers.set('Content-Security-Policy', csp);
  return res;
}

Verify

Check the live response, not your config file: CDNs and rewrites can drop headers.

curl -sI https://yourapp.com | grep -iE 'strict-transport|content-security|x-content-type|referrer-policy|permissions-policy'

Try it on your own domain

Free scan, no card. Pro is $19/mo after a 7-day free trial and covers all four scanners: VibeShield, CiteReady, InboxShield and BrandShield.

FAQ

Does Vercel add security headers automatically?

Vercel serves HTTPS and HSTS on its domains, but CSP, Referrer-Policy and the other headers are up to you.

Related