2020-09-01 16:16:04 +00:00
|
|
|
package headers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-02-03 14:24:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/config/dynamic"
|
2024-01-15 15:14:05 +00:00
|
|
|
"github.com/traefik/traefik/v3/pkg/middlewares"
|
2020-09-01 16:16:04 +00:00
|
|
|
"github.com/unrolled/secure"
|
|
|
|
)
|
|
|
|
|
|
|
|
type secureHeader struct {
|
|
|
|
next http.Handler
|
|
|
|
secure *secure.Secure
|
|
|
|
cfg dynamic.Headers
|
|
|
|
}
|
|
|
|
|
|
|
|
// newSecure constructs a new secure instance with supplied options.
|
|
|
|
func newSecure(next http.Handler, cfg dynamic.Headers, contextKey string) *secureHeader {
|
|
|
|
opt := secure.Options{
|
|
|
|
BrowserXssFilter: cfg.BrowserXSSFilter,
|
|
|
|
ContentTypeNosniff: cfg.ContentTypeNosniff,
|
|
|
|
ForceSTSHeader: cfg.ForceSTSHeader,
|
|
|
|
FrameDeny: cfg.FrameDeny,
|
|
|
|
IsDevelopment: cfg.IsDevelopment,
|
|
|
|
STSIncludeSubdomains: cfg.STSIncludeSubdomains,
|
|
|
|
STSPreload: cfg.STSPreload,
|
|
|
|
ContentSecurityPolicy: cfg.ContentSecurityPolicy,
|
|
|
|
CustomBrowserXssValue: cfg.CustomBrowserXSSValue,
|
|
|
|
CustomFrameOptionsValue: cfg.CustomFrameOptionsValue,
|
|
|
|
PublicKey: cfg.PublicKey,
|
|
|
|
ReferrerPolicy: cfg.ReferrerPolicy,
|
|
|
|
AllowedHosts: cfg.AllowedHosts,
|
|
|
|
HostsProxyHeaders: cfg.HostsProxyHeaders,
|
|
|
|
SSLProxyHeaders: cfg.SSLProxyHeaders,
|
|
|
|
STSSeconds: cfg.STSSeconds,
|
2021-06-21 13:16:13 +00:00
|
|
|
PermissionsPolicy: cfg.PermissionsPolicy,
|
2020-09-01 16:16:04 +00:00
|
|
|
SecureContextKey: contextKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &secureHeader{
|
|
|
|
next: next,
|
|
|
|
secure: secure.New(opt),
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s secureHeader) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
s.secure.HandlerFuncWithNextForRequestOnly(rw, req, func(writer http.ResponseWriter, request *http.Request) {
|
2024-01-15 15:14:05 +00:00
|
|
|
s.next.ServeHTTP(middlewares.NewResponseModifier(writer, request, s.secure.ModifyResponseHeaders), request)
|
2020-09-01 16:16:04 +00:00
|
|
|
})
|
|
|
|
}
|