Files
mira/backend/internal/security/headers.go
Cauê Faleiros 3314c2e863
Some checks failed
CI / frontend (push) Has been cancelled
CI / docker (push) Has been cancelled
CI / backend (push) Has been cancelled
Reapply "Build post chat and attachment workflows"
This reverts commit 5bc4a551af.
2026-06-10 09:24:44 -03:00

44 lines
1.1 KiB
Go

package security
import (
"net/http"
"strings"
"github.com/gin-gonic/gin"
)
func Headers() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Content-Security-Policy", "default-src 'self'; base-uri 'self'; frame-ancestors 'none'; form-action 'self'")
c.Header("X-Content-Type-Options", "nosniff")
c.Header("X-Frame-Options", "DENY")
c.Header("Referrer-Policy", "strict-origin-when-cross-origin")
c.Header("Permissions-Policy", "camera=(), microphone=(), geolocation=()")
c.Next()
}
}
func CORS(allowedOrigins []string) gin.HandlerFunc {
allowed := map[string]bool{}
for _, origin := range allowedOrigins {
allowed[strings.TrimSpace(origin)] = true
}
return func(c *gin.Context) {
origin := c.GetHeader("Origin")
if allowed[origin] {
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Authorization, Content-Type")
c.Header("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE, OPTIONS")
}
if c.Request.Method == http.MethodOptions {
c.AbortWithStatus(http.StatusNoContent)
return
}
c.Next()
}
}