ShipBastion / Security guides / Next.js and Vercel security headers: copy-paste config
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
Subresource Integrity (SRI): protect third-party scripts
How to add Subresource Integrity hashes to CDN scripts and stylesheets, generate sha384 hashes with openssl, and why Mozilla Observatory ded...
ShipBastion vs Mozilla HTTP Observatory
ShipBastion runs all 12 Mozilla Observatory tests plus TLS 1.0/1.1 probing, 221 secret rules, Supabase RLS checks and daily monitoring. Obse...
Security checklist for apps built with Cursor, Lovable, Bolt and v0
A pre-launch security checklist for AI-built apps: leaked keys in bundles, Supabase RLS, exposed .env and source maps, security headers, TLS...