26 lines
1.0 KiB
Go
26 lines
1.0 KiB
Go
package middleware
|
|
|
|
import "net/http"
|
|
|
|
// SecurityHeaders returns middleware that sets common security headers on
|
|
// every response. HSTS is intentionally omitted here because Caddy handles
|
|
// it at the reverse-proxy layer.
|
|
func SecurityHeaders(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("X-Content-Type-Options", "nosniff")
|
|
w.Header().Set("X-Frame-Options", "DENY")
|
|
w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
|
|
w.Header().Set("Content-Security-Policy",
|
|
"default-src 'self'; "+
|
|
"script-src 'self' 'unsafe-inline' 'unsafe-eval' 'wasm-unsafe-eval' blob: https://cdn.jsdelivr.net https://unpkg.com; "+
|
|
"worker-src 'self' blob:; "+
|
|
"child-src 'self' blob:; "+
|
|
"style-src 'self' 'unsafe-inline'; "+
|
|
"img-src 'self' https: data: blob:; "+
|
|
"connect-src 'self' wss: ws: https:; "+
|
|
"font-src 'self';")
|
|
w.Header().Set("Permissions-Policy", "camera=(self), microphone=(self), geolocation=()")
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|