package middleware import "net/http" // SecurityHeadersMiddleware adds essential security headers to all responses // Based on OWASP guidelines func SecurityHeadersMiddleware(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { // Prevent clickjacking w.Header().Set("X-Frame-Options", "DENY") // Prevent MIME sniffing w.Header().Set("X-Content-Type-Options", "nosniff") // Enable XSS filter w.Header().Set("X-XSS-Protection", "1; mode=block") // Only send referrer for same-origin w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin") // Permissions policy (disable potentially dangerous features) w.Header().Set("Permissions-Policy", "geolocation=(), microphone=(), camera=()") // Content Security Policy - adjust as needed for your frontend w.Header().Set("Content-Security-Policy", "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'") // HSTS - uncomment in production with HTTPS // w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains") next.ServeHTTP(w, r) }) }