traefik/pkg/responsemodifiers/headers.go

55 lines
1.7 KiB
Go
Raw Normal View History

2018-11-14 09:18:03 +00:00
package responsemodifiers
2017-06-13 00:48:21 +00:00
import (
2018-11-14 09:18:03 +00:00
"net/http"
2019-08-03 01:58:23 +00:00
"github.com/containous/traefik/v2/pkg/config/dynamic"
"github.com/containous/traefik/v2/pkg/middlewares/headers"
2017-06-13 00:48:21 +00:00
"github.com/unrolled/secure"
)
func buildHeaders(hdrs *dynamic.Headers, contextKey string) func(*http.Response) error {
2017-06-13 00:48:21 +00:00
opt := secure.Options{
2019-04-02 08:40:04 +00:00
BrowserXssFilter: hdrs.BrowserXSSFilter,
ContentTypeNosniff: hdrs.ContentTypeNosniff,
ForceSTSHeader: hdrs.ForceSTSHeader,
FrameDeny: hdrs.FrameDeny,
IsDevelopment: hdrs.IsDevelopment,
SSLRedirect: hdrs.SSLRedirect,
SSLForceHost: hdrs.SSLForceHost,
SSLTemporaryRedirect: hdrs.SSLTemporaryRedirect,
STSIncludeSubdomains: hdrs.STSIncludeSubdomains,
STSPreload: hdrs.STSPreload,
ContentSecurityPolicy: hdrs.ContentSecurityPolicy,
CustomBrowserXssValue: hdrs.CustomBrowserXSSValue,
CustomFrameOptionsValue: hdrs.CustomFrameOptionsValue,
PublicKey: hdrs.PublicKey,
ReferrerPolicy: hdrs.ReferrerPolicy,
SSLHost: hdrs.SSLHost,
AllowedHosts: hdrs.AllowedHosts,
HostsProxyHeaders: hdrs.HostsProxyHeaders,
SSLProxyHeaders: hdrs.SSLProxyHeaders,
STSSeconds: hdrs.STSSeconds,
2019-07-29 14:12:05 +00:00
FeaturePolicy: hdrs.FeaturePolicy,
SecureContextKey: contextKey,
2018-11-14 09:18:03 +00:00
}
return func(resp *http.Response) error {
2019-04-02 08:40:04 +00:00
if hdrs.HasCustomHeadersDefined() || hdrs.HasCorsHeadersDefined() {
2019-07-12 09:46:04 +00:00
err := headers.NewHeader(nil, *hdrs).PostRequestModifyResponseHeaders(resp)
2019-04-02 08:40:04 +00:00
if err != nil {
return err
2018-11-14 09:18:03 +00:00
}
}
2019-04-02 08:40:04 +00:00
if hdrs.HasSecureHeadersDefined() {
2018-11-14 09:18:03 +00:00
err := secure.New(opt).ModifyResponseHeaders(resp)
if err != nil {
return err
}
}
return nil
2017-06-13 00:48:21 +00:00
}
}