backend-service/internal/middleware/security.go

31 lines
1.0 KiB
Go
Raw Normal View History

package middleware
import (
"github.com/gin-gonic/gin"
)
// SecurityMiddleware adds security headers to responses
func SecurityMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
// Security headers
c.Header("X-Content-Type-Options", "nosniff")
c.Header("X-Frame-Options", "DENY")
c.Header("X-XSS-Protection", "1; mode=block")
c.Header("Referrer-Policy", "strict-origin-when-cross-origin")
c.Header("Content-Security-Policy", "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self'; media-src 'self'; object-src 'none'; child-src 'none'; worker-src 'none'; frame-ancestors 'none'; form-action 'self'; base-uri 'self'; manifest-src 'self'")
// Remove server information
c.Header("Server", "")
c.Next()
}
}
// NoIndexMiddleware adds no-index header for non-production environments
func NoIndexMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("X-Robots-Tag", "noindex, nofollow, noarchive, nosnippet")
c.Next()
}
}