From 06531355bf8fcbfbc35cfd307cb74efd53d028be Mon Sep 17 00:00:00 2001 From: g-but <41178744+g-but@users.noreply.github.com> Date: Tue, 3 Mar 2026 09:51:15 +0100 Subject: [PATCH] feat(security): add security headers via next.config.js Add CSP, HSTS (1 year), X-Content-Type-Options, X-Frame-Options, Referrer-Policy, and Permissions-Policy headers to all routes. CSP allows required external sources (Groq, OpenRouter, Supabase, GitHub, YouTube, Giscus). Co-Authored-By: Claude Opus 4.6 --- next.config.js | 77 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 20 deletions(-) diff --git a/next.config.js b/next.config.js index a87b143..6fc9981 100644 --- a/next.config.js +++ b/next.config.js @@ -45,26 +45,63 @@ const nextConfig = { }, ]; }, - headers: async () => [ - { - source: '/api/:path*', - headers: [ - { - key: 'Cache-Control', - value: 'no-store, max-age=0', - }, - ], - }, - { - source: '/((?!api).*)', - headers: [ - { - key: 'Cache-Control', - value: 'public, max-age=3600, s-maxage=86400, stale-while-revalidate=60', - }, - ], - }, - ], + headers: async () => { + const securityHeaders = [ + { + key: 'Strict-Transport-Security', + value: 'max-age=31536000; includeSubDomains', + }, + { + 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'", + "script-src 'self' 'unsafe-inline' 'unsafe-eval' https://giscus.app https://platform.twitter.com", + "style-src 'self' 'unsafe-inline'", + "img-src 'self' data: blob: https://raw.githubusercontent.com https://images.unsplash.com", + "font-src 'self'", + "connect-src 'self' https://api.groq.com https://openrouter.ai https://api.openai.com https://api.github.com https://raw.githubusercontent.com https://*.supabase.co", + "frame-src 'self' https://www.youtube.com https://giscus.app", + "frame-ancestors 'none'", + ].join('; '), + }, + ]; + + return [ + { + source: '/api/:path*', + headers: [{ key: 'Cache-Control', value: 'no-store, max-age=0' }], + }, + { + source: '/(.*)', + headers: securityHeaders, + }, + { + source: '/((?!api).*)', + headers: [ + { + key: 'Cache-Control', + value: 'public, max-age=3600, s-maxage=86400, stale-while-revalidate=60', + }, + ], + }, + ]; + }, }; module.exports = nextConfig;