Set security headers on all HTTP responses: Content-Security-Policy, X-Content-Type-Options, X-Frame-Options, Strict-Transport-Security. Missing headers leave your app vulnerable to XSS, clickjacking, MIME sniffing, and protocol downgrade attacks.
Missing security headers leave your application exposed to well-known attack vectors. Without Content-Security-Policy, XSS attacks execute freely. Without X-Frame-Options, your site can be embedded in an attacker's iframe for clickjacking. Without HSTS, connections can be downgraded from HTTPS to HTTP for man-in-the-middle attacks.
BeforeMerge scans your pull requests against this rule and 4+ others. Get actionable feedback before code ships.
HTTP security headers are a defense-in-depth layer that mitigates common web attacks even when other defenses fail. They are set once in your server configuration and protect every page and API response.
Without these headers:
Set these security headers on all HTTP responses:
Content-Security-Policy — restrict script/style/image sourcesStrict-Transport-Security — enforce HTTPSX-Content-Type-Options: nosniff — prevent MIME sniffingX-Frame-Options: DENY (or SAMEORIGIN) — prevent clickjackingReferrer-Policy: strict-origin-when-cross-origin — limit referrer leakagePermissions-Policy — restrict browser features (camera, microphone, geolocation)// next.config.ts — no security headers configured
const nextConfig = {
// No headers configured
};
export default nextConfig;// next.config.ts — security headers on all responses
const securityHeaders = [
{
key: "Content-Security-Policy",
value: "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline';",
},
{
key: "Strict-Transport-Security",
value: "max-age=63072000; includeSubDomains; preload",
},
{
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=()",
},
];
const nextConfig = {
async headers() {
return [
{
source: "/(.*)",
headers: securityHeaders,
},
];
},
};
export default nextConfig;Check your application's response headers:
curl -I https://your-app.comLook for missing security headers. Use securityheaders.com for a comprehensive scan.
Content-Security-Policy-Report-Only