2016-04-24 17:10:45 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2016-07-27 18:34:44 +02:00
|
|
|
// SecureConfig defines the config for Secure middleware.
|
2016-04-24 17:10:45 +02:00
|
|
|
SecureConfig struct {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
2016-05-03 17:32:28 +02:00
|
|
|
// XSSProtection provides protection against cross-site scripting attack (XSS)
|
|
|
|
// by setting the `X-XSS-Protection` header.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value "1; mode=block".
|
2016-05-19 03:53:54 +02:00
|
|
|
XSSProtection string `json:"xss_protection"`
|
2016-05-03 17:32:28 +02:00
|
|
|
|
|
|
|
// ContentTypeNosniff provides protection against overriding Content-Type
|
|
|
|
// header by setting the `X-Content-Type-Options` header.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value "nosniff".
|
2016-05-19 03:53:54 +02:00
|
|
|
ContentTypeNosniff string `json:"content_type_nosniff"`
|
2016-05-03 17:32:28 +02: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.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value "SAMEORIGIN".
|
2016-05-03 17:32:28 +02:00
|
|
|
// Possible values:
|
2016-05-26 23:06:30 +02: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.
|
2016-05-19 03:53:54 +02:00
|
|
|
XFrameOptions string `json:"x_frame_options"`
|
2016-05-03 17:32:28 +02: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.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value 0.
|
2016-05-19 03:53:54 +02:00
|
|
|
HSTSMaxAge int `json:"hsts_max_age"`
|
2016-05-03 17:32:28 +02: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.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value false.
|
2016-05-19 03:53:54 +02:00
|
|
|
HSTSExcludeSubdomains bool `json:"hsts_exclude_subdomains"`
|
2016-05-03 17:32:28 +02: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.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value "".
|
2016-05-19 03:53:54 +02:00
|
|
|
ContentSecurityPolicy string `json:"content_security_policy"`
|
2016-04-24 17:10:45 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-07-27 18:34:44 +02:00
|
|
|
// DefaultSecureConfig is the default Secure middleware config.
|
2016-05-03 07:41:07 +02:00
|
|
|
DefaultSecureConfig = SecureConfig{
|
2017-01-28 21:43:56 +02:00
|
|
|
Skipper: DefaultSkipper,
|
2016-05-03 17:32:28 +02:00
|
|
|
XSSProtection: "1; mode=block",
|
|
|
|
ContentTypeNosniff: "nosniff",
|
|
|
|
XFrameOptions: "SAMEORIGIN",
|
2016-05-03 07:41:07 +02:00
|
|
|
}
|
2016-04-24 17:10:45 +02:00
|
|
|
)
|
|
|
|
|
2016-07-27 18:34:44 +02:00
|
|
|
// Secure returns a Secure middleware.
|
2016-05-03 20:42:42 +02: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 17:10:45 +02:00
|
|
|
func Secure() echo.MiddlewareFunc {
|
|
|
|
return SecureWithConfig(DefaultSecureConfig)
|
|
|
|
}
|
|
|
|
|
2016-09-01 05:10:14 +02:00
|
|
|
// SecureWithConfig returns a Secure middleware with config.
|
2016-05-13 02:45:00 +02:00
|
|
|
// See: `Secure()`.
|
2016-04-24 17:10:45 +02:00
|
|
|
func SecureWithConfig(config SecureConfig) echo.MiddlewareFunc {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Defaults
|
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultSecureConfig.Skipper
|
|
|
|
}
|
|
|
|
|
2016-04-24 17:10:45 +02:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper(c) {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
2016-05-03 17:32:28 +02:00
|
|
|
req := c.Request()
|
|
|
|
res := c.Response()
|
|
|
|
|
|
|
|
if config.XSSProtection != "" {
|
|
|
|
res.Header().Set(echo.HeaderXXSSProtection, config.XSSProtection)
|
2016-05-03 07:41:07 +02:00
|
|
|
}
|
2016-05-03 17:32:28 +02:00
|
|
|
if config.ContentTypeNosniff != "" {
|
|
|
|
res.Header().Set(echo.HeaderXContentTypeOptions, config.ContentTypeNosniff)
|
2016-05-03 07:41:07 +02:00
|
|
|
}
|
|
|
|
if config.XFrameOptions != "" {
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Set(echo.HeaderXFrameOptions, config.XFrameOptions)
|
2016-05-03 07:41:07 +02:00
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
if (c.IsTLS() || (req.Header.Get(echo.HeaderXForwardedProto) == "https")) && config.HSTSMaxAge != 0 {
|
2016-05-03 07:41:07 +02:00
|
|
|
subdomains := ""
|
2016-05-03 17:32:28 +02:00
|
|
|
if !config.HSTSExcludeSubdomains {
|
2016-05-03 07:41:07 +02:00
|
|
|
subdomains = "; includeSubdomains"
|
|
|
|
}
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Set(echo.HeaderStrictTransportSecurity, fmt.Sprintf("max-age=%d%s", config.HSTSMaxAge, subdomains))
|
2016-05-03 07:41:07 +02:00
|
|
|
}
|
|
|
|
if config.ContentSecurityPolicy != "" {
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Set(echo.HeaderContentSecurityPolicy, config.ContentSecurityPolicy)
|
2016-05-03 07:41:07 +02:00
|
|
|
}
|
2016-04-24 17:10:45 +02:00
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|