fix(backend): relax CSP for Swagger UI docs
- Allow 'unsafe-inline' and 'unsafe-eval' scripts on /docs routes - Swagger UI requires inline scripts to function properly - Keep strict CSP for all other API routes
This commit is contained in:
parent
dc2142499b
commit
15fe5db50e
1 changed files with 17 additions and 5 deletions
|
|
@ -1,13 +1,20 @@
|
||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import "net/http"
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
// SecurityHeadersMiddleware adds essential security headers to all responses
|
// SecurityHeadersMiddleware adds essential security headers to all responses
|
||||||
// Based on OWASP guidelines
|
// Based on OWASP guidelines
|
||||||
func SecurityHeadersMiddleware(next http.Handler) http.Handler {
|
func SecurityHeadersMiddleware(next http.Handler) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// Prevent clickjacking
|
// Prevent clickjacking (allow framing for Swagger UI)
|
||||||
|
if strings.HasPrefix(r.URL.Path, "/docs") {
|
||||||
|
w.Header().Set("X-Frame-Options", "SAMEORIGIN")
|
||||||
|
} else {
|
||||||
w.Header().Set("X-Frame-Options", "DENY")
|
w.Header().Set("X-Frame-Options", "DENY")
|
||||||
|
}
|
||||||
|
|
||||||
// Prevent MIME sniffing
|
// Prevent MIME sniffing
|
||||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||||
|
|
@ -21,8 +28,13 @@ func SecurityHeadersMiddleware(next http.Handler) http.Handler {
|
||||||
// Permissions policy (disable potentially dangerous features)
|
// Permissions policy (disable potentially dangerous features)
|
||||||
w.Header().Set("Permissions-Policy", "geolocation=(), microphone=(), camera=()")
|
w.Header().Set("Permissions-Policy", "geolocation=(), microphone=(), camera=()")
|
||||||
|
|
||||||
// Content Security Policy - adjust as needed for your frontend
|
// Content Security Policy - relaxed for Swagger UI docs
|
||||||
|
if strings.HasPrefix(r.URL.Path, "/docs") {
|
||||||
|
// Swagger UI needs unsafe-inline and unsafe-eval for its scripts
|
||||||
|
w.Header().Set("Content-Security-Policy", "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:")
|
||||||
|
} else {
|
||||||
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'")
|
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
|
// HSTS - uncomment in production with HTTPS
|
||||||
// w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains")
|
// w.Header().Set("Strict-Transport-Security", "max-age=31536000; includeSubDomains")
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue