1
0
mirror of https://github.com/labstack/echo.git synced 2026-06-20 01:18:42 +02:00
Files
echo/middleware/secure.go
T

146 lines
5.5 KiB
Go
Raw Normal View History

2016-04-24 23:10:45 +08:00
package middleware
import (
"fmt"
"github.com/labstack/echo/v4"
2016-04-24 23:10:45 +08:00
)
type (
2016-07-27 09:34:44 -07:00
// SecureConfig defines the config for Secure middleware.
2016-04-24 23:10:45 +08:00
SecureConfig struct {
2016-07-27 09:34:44 -07:00
// Skipper defines a function to skip middleware.
Skipper Skipper
2016-05-03 08:32:28 -07:00
// XSSProtection provides protection against cross-site scripting attack (XSS)
// by setting the `X-XSS-Protection` header.
// Optional. Default value "1; mode=block".
2017-12-28 11:24:34 -08:00
XSSProtection string `yaml:"xss_protection"`
2016-05-03 08:32:28 -07:00
// ContentTypeNosniff provides protection against overriding Content-Type
// header by setting the `X-Content-Type-Options` header.
// Optional. Default value "nosniff".
2017-12-28 11:24:34 -08:00
ContentTypeNosniff string `yaml:"content_type_nosniff"`
2016-05-03 08:32:28 -07:00
// XFrameOptions can be used to indicate whether or not a browser should
// be allowed to render a page in a <frame>, <iframe> or <object> .
// Sites can use this to avoid clickjacking attacks, by ensuring that their
// content is not embedded into other sites.provides protection against
// clickjacking.
// Optional. Default value "SAMEORIGIN".
2016-05-03 08:32:28 -07:00
// Possible values:
2016-05-26 14:06:30 -07:00
// - "SAMEORIGIN" - The page can only be displayed in a frame on the same origin as the page itself.
// - "DENY" - The page cannot be displayed in a frame, regardless of the site attempting to do so.
// - "ALLOW-FROM uri" - The page can only be displayed in a frame on the specified origin.
2017-12-28 11:24:34 -08:00
XFrameOptions string `yaml:"x_frame_options"`
2016-05-03 08:32:28 -07:00
// HSTSMaxAge sets the `Strict-Transport-Security` header to indicate how
// long (in seconds) browsers should remember that this site is only to
// be accessed using HTTPS. This reduces your exposure to some SSL-stripping
// man-in-the-middle (MITM) attacks.
// Optional. Default value 0.
2017-12-28 11:24:34 -08:00
HSTSMaxAge int `yaml:"hsts_max_age"`
2016-05-03 08:32:28 -07:00
// HSTSExcludeSubdomains won't include subdomains tag in the `Strict Transport Security`
// header, excluding all subdomains from security policy. It has no effect
// unless HSTSMaxAge is set to a non-zero value.
// Optional. Default value false.
2017-12-28 11:24:34 -08:00
HSTSExcludeSubdomains bool `yaml:"hsts_exclude_subdomains"`
2016-05-03 08:32:28 -07:00
// ContentSecurityPolicy sets the `Content-Security-Policy` header providing
// security against cross-site scripting (XSS), clickjacking and other code
// injection attacks resulting from execution of malicious content in the
// trusted web page context.
// Optional. Default value "".
2017-12-28 11:24:34 -08:00
ContentSecurityPolicy string `yaml:"content_security_policy"`
// CSPReportOnly would use the `Content-Security-Policy-Report-Only` header instead
// of the `Content-Security-Policy` header. This allows iterative updates of the
// content security policy by only reporting the violations that would
// have occurred instead of blocking the resource.
// Optional. Default value false.
CSPReportOnly bool `yaml:"csp_report_only"`
// HSTSPreloadEnabled will add the preload tag in the `Strict Transport Security`
// header, which enables the domain to be included in the HSTS preload list
// maintained by Chrome (and used by Firefox and Safari): https://hstspreload.org/
// Optional. Default value false.
HSTSPreloadEnabled bool `yaml:"hsts_preload_enabled"`
// ReferrerPolicy sets the `Referrer-Policy` header providing security against
// leaking potentially sensitive request paths to third parties.
// Optional. Default value "".
ReferrerPolicy string `yaml:"referrer_policy"`
2016-04-24 23:10:45 +08:00
}
)
var (
2016-07-27 09:34:44 -07:00
// DefaultSecureConfig is the default Secure middleware config.
2016-05-02 22:41:07 -07:00
DefaultSecureConfig = SecureConfig{
Skipper: DefaultSkipper,
2016-05-03 08:32:28 -07:00
XSSProtection: "1; mode=block",
ContentTypeNosniff: "nosniff",
XFrameOptions: "SAMEORIGIN",
HSTSPreloadEnabled: false,
2016-05-02 22:41:07 -07:00
}
2016-04-24 23:10:45 +08:00
)
2016-07-27 09:34:44 -07:00
// Secure returns a Secure middleware.
2016-05-03 11:42:42 -07:00
// Secure middleware provides protection against cross-site scripting (XSS) attack,
// content type sniffing, clickjacking, insecure connection and other code injection
// attacks.
2016-04-24 23:10:45 +08:00
func Secure() echo.MiddlewareFunc {
return SecureWithConfig(DefaultSecureConfig)
}
2016-08-31 20:10:14 -07:00
// SecureWithConfig returns a Secure middleware with config.
2016-05-12 17:45:00 -07:00
// See: `Secure()`.
2016-04-24 23:10:45 +08:00
func SecureWithConfig(config SecureConfig) echo.MiddlewareFunc {
2016-07-27 09:34:44 -07:00
// Defaults
if config.Skipper == nil {
config.Skipper = DefaultSecureConfig.Skipper
}
2016-04-24 23:10:45 +08:00
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
2016-07-27 09:34:44 -07:00
if config.Skipper(c) {
return next(c)
}
2016-05-03 08:32:28 -07:00
req := c.Request()
res := c.Response()
if config.XSSProtection != "" {
res.Header().Set(echo.HeaderXXSSProtection, config.XSSProtection)
2016-05-02 22:41:07 -07:00
}
2016-05-03 08:32:28 -07:00
if config.ContentTypeNosniff != "" {
res.Header().Set(echo.HeaderXContentTypeOptions, config.ContentTypeNosniff)
2016-05-02 22:41:07 -07:00
}
if config.XFrameOptions != "" {
2016-05-03 08:32:28 -07:00
res.Header().Set(echo.HeaderXFrameOptions, config.XFrameOptions)
2016-05-02 22:41:07 -07:00
}
2016-09-22 22:53:44 -07:00
if (c.IsTLS() || (req.Header.Get(echo.HeaderXForwardedProto) == "https")) && config.HSTSMaxAge != 0 {
2016-05-02 22:41:07 -07:00
subdomains := ""
2016-05-03 08:32:28 -07:00
if !config.HSTSExcludeSubdomains {
2016-05-02 22:41:07 -07:00
subdomains = "; includeSubdomains"
}
if config.HSTSPreloadEnabled {
subdomains = fmt.Sprintf("%s; preload", subdomains)
}
2016-05-03 08:32:28 -07:00
res.Header().Set(echo.HeaderStrictTransportSecurity, fmt.Sprintf("max-age=%d%s", config.HSTSMaxAge, subdomains))
2016-05-02 22:41:07 -07:00
}
if config.ContentSecurityPolicy != "" {
if config.CSPReportOnly {
res.Header().Set(echo.HeaderContentSecurityPolicyReportOnly, config.ContentSecurityPolicy)
} else {
res.Header().Set(echo.HeaderContentSecurityPolicy, config.ContentSecurityPolicy)
}
2016-05-02 22:41:07 -07:00
}
if config.ReferrerPolicy != "" {
res.Header().Set(echo.HeaderReferrerPolicy, config.ReferrerPolicy)
}
2016-04-24 23:10:45 +08:00
return next(c)
}
}
}