security: comprehensive hardening pass — CSP, CORS lockdown, CSRF strict-origin, error leak redaction, crypto visitor IDs, rate limiter fix, catalog admin guard, ESM imports, production cookie flags

This commit is contained in:
2026-11-13 11:34:32 +05:30
parent d53c1e071a
commit 56d72d69e6
10 changed files with 157 additions and 33 deletions
+43 -3
View File
@@ -49,13 +49,53 @@ const nextConfig = {
serverComponentsExternalPackages: ['sharp'],
},
async headers() {
const isProd = process.env.NODE_ENV === 'production';
const allowedOrigin = process.env.ALLOWED_ORIGIN || '*';
return [
{
// Global security headers on every response
source: '/:path*',
headers: [
{ key: 'Access-Control-Allow-Origin', value: '*' },
{ key: 'Access-Control-Allow-Methods', value: 'GET,OPTIONS,PATCH,DELETE,POST,PUT' },
{ key: 'Access-Control-Allow-Headers', value: '*' },
// Content Security Policy — tightened; adjust 'self' sources as needed
{
key: 'Content-Security-Policy',
value: [
"default-src 'self'",
"script-src 'self' 'unsafe-inline' 'unsafe-eval' https://apis.google.com",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
"font-src 'self' https://fonts.gstatic.com",
"img-src 'self' data: blob: https:",
"connect-src 'self' https://*.firebaseapp.com https://*.googleapis.com https://identitytoolkit.googleapis.com https://securetoken.googleapis.com",
"frame-src 'self' https://accounts.google.com",
"object-src 'none'",
"base-uri 'self'",
"form-action 'self'",
"upgrade-insecure-requests",
].join('; '),
},
// Block clickjacking
{ key: 'X-Frame-Options', value: 'SAMEORIGIN' },
// Prevent MIME sniffing
{ key: 'X-Content-Type-Options', value: 'nosniff' },
// Control Referer leakage
{ key: 'Referrer-Policy', value: 'strict-origin-when-cross-origin' },
// Disable browser features not in use
{ key: 'Permissions-Policy', value: 'camera=(), microphone=(), geolocation=(), interest-cohort=()' },
// HSTS (only meaningful in production behind HTTPS)
...(isProd ? [{ key: 'Strict-Transport-Security', value: 'max-age=31536000; includeSubDomains; preload' }] : []),
// X-XSS-Protection (legacy browsers)
{ key: 'X-XSS-Protection', value: '1; mode=block' },
],
},
{
// CORS on API routes only — restrict to same origin by default
source: '/api/:path*',
headers: [
{ key: 'Access-Control-Allow-Origin', value: allowedOrigin },
{ key: 'Access-Control-Allow-Methods', value: 'GET,POST,DELETE,OPTIONS' },
{ key: 'Access-Control-Allow-Headers', value: 'Content-Type,Authorization' },
{ key: 'Access-Control-Allow-Credentials', value: 'true' },
],
},
];