package helmet
import (
"fmt"
"github.com/gofiber/fiber/v2"
)
type Config struct {
Filter func (*fiber .Ctx ) bool
XSSProtection string
ContentTypeNosniff string
XFrameOptions string
HSTSMaxAge int
HSTSExcludeSubdomains bool
ContentSecurityPolicy string
CSPReportOnly bool
HSTSPreloadEnabled bool
ReferrerPolicy string
PermissionPolicy string
CrossOriginEmbedderPolicy string
CrossOriginOpenerPolicy string
CrossOriginResourcePolicy string
OriginAgentCluster string
XDNSPrefetchControl string
XDownloadOptions string
XPermittedCrossDomain string
}
func New (config ...Config ) fiber .Handler {
var cfg Config
if len (config ) > 0 {
cfg = config [0 ]
}
if cfg .XSSProtection == "" {
cfg .XSSProtection = "0"
}
if cfg .ContentTypeNosniff == "" {
cfg .ContentTypeNosniff = "nosniff"
}
if cfg .XFrameOptions == "" {
cfg .XFrameOptions = "SAMEORIGIN"
}
if cfg .ReferrerPolicy == "" {
cfg .ReferrerPolicy = "no-referrer"
}
if cfg .CrossOriginEmbedderPolicy == "" {
cfg .CrossOriginEmbedderPolicy = "require-corp"
}
if cfg .CrossOriginOpenerPolicy == "" {
cfg .CrossOriginOpenerPolicy = "same-origin"
}
if cfg .CrossOriginResourcePolicy == "" {
cfg .CrossOriginResourcePolicy = "same-origin"
}
if cfg .OriginAgentCluster == "" {
cfg .OriginAgentCluster = "?1"
}
if cfg .XDNSPrefetchControl == "" {
cfg .XDNSPrefetchControl = "off"
}
if cfg .XDownloadOptions == "" {
cfg .XDownloadOptions = "noopen"
}
if cfg .XPermittedCrossDomain == "" {
cfg .XPermittedCrossDomain = "none"
}
return func (c *fiber .Ctx ) error {
if cfg .Filter != nil && cfg .Filter (c ) {
return c .Next ()
}
if cfg .XSSProtection != "" {
c .Set (fiber .HeaderXXSSProtection , cfg .XSSProtection )
}
if cfg .ContentTypeNosniff != "" {
c .Set (fiber .HeaderXContentTypeOptions , cfg .ContentTypeNosniff )
}
if cfg .XFrameOptions != "" {
c .Set (fiber .HeaderXFrameOptions , cfg .XFrameOptions )
}
if cfg .CrossOriginEmbedderPolicy != "" {
c .Set ("Cross-Origin-Embedder-Policy" , cfg .CrossOriginEmbedderPolicy )
}
if cfg .CrossOriginOpenerPolicy != "" {
c .Set ("Cross-Origin-Opener-Policy" , cfg .CrossOriginOpenerPolicy )
}
if cfg .CrossOriginResourcePolicy != "" {
c .Set ("Cross-Origin-Resource-Policy" , cfg .CrossOriginResourcePolicy )
}
if cfg .OriginAgentCluster != "" {
c .Set ("Origin-Agent-Cluster" , cfg .OriginAgentCluster )
}
if cfg .ReferrerPolicy != "" {
c .Set ("Referrer-Policy" , cfg .ReferrerPolicy )
}
if cfg .XDNSPrefetchControl != "" {
c .Set ("X-DNS-Prefetch-Control" , cfg .XDNSPrefetchControl )
}
if cfg .XDownloadOptions != "" {
c .Set ("X-Download-Options" , cfg .XDownloadOptions )
}
if cfg .XPermittedCrossDomain != "" {
c .Set ("X-Permitted-Cross-Domain-Policies" , cfg .XPermittedCrossDomain )
}
if c .Protocol () == "https" && cfg .HSTSMaxAge != 0 {
subdomains := ""
if !cfg .HSTSExcludeSubdomains {
subdomains = "; includeSubDomains"
}
if cfg .HSTSPreloadEnabled {
subdomains = fmt .Sprintf ("%s; preload" , subdomains )
}
c .Set (fiber .HeaderStrictTransportSecurity , fmt .Sprintf ("max-age=%d%s" , cfg .HSTSMaxAge , subdomains ))
}
if cfg .ContentSecurityPolicy != "" {
if cfg .CSPReportOnly {
c .Set (fiber .HeaderContentSecurityPolicyReportOnly , cfg .ContentSecurityPolicy )
} else {
c .Set (fiber .HeaderContentSecurityPolicy , cfg .ContentSecurityPolicy )
}
}
if cfg .PermissionPolicy != "" {
c .Set (fiber .HeaderPermissionsPolicy , cfg .PermissionPolicy )
}
return c .Next ()
}
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .