2016-04-08 01:16:58 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2020-08-18 03:39:54 +02:00
|
|
|
"regexp"
|
2016-04-08 01:16:58 +02:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2019-01-30 12:56:56 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
2016-04-08 01:16:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// CORSConfig defines the config for CORS middleware.
|
|
|
|
CORSConfig struct {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
2016-04-08 01:16:58 +02:00
|
|
|
// AllowOrigin defines a list of origins that may access the resource.
|
2016-11-22 00:42:13 +02:00
|
|
|
// Optional. Default value []string{"*"}.
|
2017-12-28 21:24:34 +02:00
|
|
|
AllowOrigins []string `yaml:"allow_origins"`
|
2016-04-08 01:16:58 +02:00
|
|
|
|
|
|
|
// AllowMethods defines a list methods allowed when accessing the resource.
|
|
|
|
// This is used in response to a preflight request.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value DefaultCORSConfig.AllowMethods.
|
2017-12-28 21:24:34 +02:00
|
|
|
AllowMethods []string `yaml:"allow_methods"`
|
2016-04-08 01:16:58 +02:00
|
|
|
|
|
|
|
// AllowHeaders defines a list of request headers that can be used when
|
2019-01-10 01:15:33 +02:00
|
|
|
// making the actual request. This is in response to a preflight request.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value []string{}.
|
2017-12-28 21:24:34 +02:00
|
|
|
AllowHeaders []string `yaml:"allow_headers"`
|
2016-04-08 01:16:58 +02:00
|
|
|
|
|
|
|
// AllowCredentials indicates whether or not the response to the request
|
|
|
|
// can be exposed when the credentials flag is true. When used as part of
|
|
|
|
// a response to a preflight request, this indicates whether or not the
|
|
|
|
// actual request can be made using credentials.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value false.
|
2017-12-28 21:24:34 +02:00
|
|
|
AllowCredentials bool `yaml:"allow_credentials"`
|
2016-04-08 01:16:58 +02:00
|
|
|
|
|
|
|
// ExposeHeaders defines a whitelist headers that clients are allowed to
|
|
|
|
// access.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value []string{}.
|
2017-12-28 21:24:34 +02:00
|
|
|
ExposeHeaders []string `yaml:"expose_headers"`
|
2016-04-08 01:16:58 +02:00
|
|
|
|
|
|
|
// MaxAge indicates how long (in seconds) the results of a preflight request
|
|
|
|
// can be cached.
|
2016-05-10 20:52:04 +02:00
|
|
|
// Optional. Default value 0.
|
2017-12-28 21:24:34 +02:00
|
|
|
MaxAge int `yaml:"max_age"`
|
2016-04-08 01:16:58 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultCORSConfig is the default CORS middleware config.
|
|
|
|
DefaultCORSConfig = CORSConfig{
|
2017-01-28 21:43:56 +02:00
|
|
|
Skipper: DefaultSkipper,
|
2016-11-22 00:42:13 +02:00
|
|
|
AllowOrigins: []string{"*"},
|
2018-10-14 17:16:58 +02:00
|
|
|
AllowMethods: []string{http.MethodGet, http.MethodHead, http.MethodPut, http.MethodPatch, http.MethodPost, http.MethodDelete},
|
2016-04-08 01:16:58 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-04-08 01:57:57 +02:00
|
|
|
// CORS returns a Cross-Origin Resource Sharing (CORS) middleware.
|
2016-05-13 02:45:00 +02:00
|
|
|
// See: https://developer.mozilla.org/en/docs/Web/HTTP/Access_control_CORS
|
2016-04-08 01:16:58 +02:00
|
|
|
func CORS() echo.MiddlewareFunc {
|
2016-04-08 06:20:50 +02:00
|
|
|
return CORSWithConfig(DefaultCORSConfig)
|
2016-04-08 01:16:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-01 05:10:14 +02:00
|
|
|
// CORSWithConfig returns a CORS middleware with config.
|
2016-05-13 02:45:00 +02:00
|
|
|
// See: `CORS()`.
|
2016-04-08 06:20:50 +02:00
|
|
|
func CORSWithConfig(config CORSConfig) echo.MiddlewareFunc {
|
2016-04-08 01:16:58 +02:00
|
|
|
// Defaults
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultCORSConfig.Skipper
|
|
|
|
}
|
2016-11-22 00:42:13 +02:00
|
|
|
if len(config.AllowOrigins) == 0 {
|
|
|
|
config.AllowOrigins = DefaultCORSConfig.AllowOrigins
|
|
|
|
}
|
2016-04-08 01:16:58 +02:00
|
|
|
if len(config.AllowMethods) == 0 {
|
|
|
|
config.AllowMethods = DefaultCORSConfig.AllowMethods
|
|
|
|
}
|
2016-11-13 06:24:53 +02:00
|
|
|
|
2020-08-18 03:39:54 +02:00
|
|
|
allowOriginPatterns := []string{}
|
|
|
|
for _, origin := range config.AllowOrigins {
|
|
|
|
pattern := regexp.QuoteMeta(origin)
|
|
|
|
pattern = strings.Replace(pattern, "\\*", ".*", -1)
|
|
|
|
pattern = strings.Replace(pattern, "\\?", ".", -1)
|
|
|
|
pattern = "^" + pattern + "$"
|
|
|
|
allowOriginPatterns = append(allowOriginPatterns, pattern)
|
|
|
|
}
|
|
|
|
|
2016-04-08 01:16:58 +02:00
|
|
|
allowMethods := strings.Join(config.AllowMethods, ",")
|
|
|
|
allowHeaders := strings.Join(config.AllowHeaders, ",")
|
|
|
|
exposeHeaders := strings.Join(config.ExposeHeaders, ",")
|
|
|
|
maxAge := strconv.Itoa(config.MaxAge)
|
|
|
|
|
|
|
|
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-04-24 19:21:23 +02:00
|
|
|
req := c.Request()
|
2016-05-03 17:32:28 +02:00
|
|
|
res := c.Response()
|
2016-11-13 06:24:53 +02:00
|
|
|
origin := req.Header.Get(echo.HeaderOrigin)
|
2016-11-22 00:42:13 +02:00
|
|
|
allowOrigin := ""
|
2016-11-13 06:24:53 +02:00
|
|
|
|
2016-11-22 00:42:13 +02:00
|
|
|
// Check allowed origins
|
|
|
|
for _, o := range config.AllowOrigins {
|
2018-06-19 15:27:36 +02:00
|
|
|
if o == "*" && config.AllowCredentials {
|
|
|
|
allowOrigin = origin
|
|
|
|
break
|
|
|
|
}
|
2016-11-22 00:42:13 +02:00
|
|
|
if o == "*" || o == origin {
|
|
|
|
allowOrigin = o
|
|
|
|
break
|
2016-11-13 06:24:53 +02:00
|
|
|
}
|
2019-03-09 20:32:49 +02:00
|
|
|
if matchSubdomain(origin, o) {
|
|
|
|
allowOrigin = origin
|
|
|
|
break
|
|
|
|
}
|
2016-11-13 06:24:53 +02:00
|
|
|
}
|
2016-04-08 01:16:58 +02:00
|
|
|
|
2020-08-18 03:39:54 +02:00
|
|
|
// Check allowed origin patterns
|
|
|
|
for _, re := range allowOriginPatterns {
|
|
|
|
if allowOrigin == "" {
|
|
|
|
didx := strings.Index(origin, "://")
|
|
|
|
if didx == -1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
domAuth := origin[didx+3:]
|
|
|
|
// to avoid regex cost by invalid long domain
|
|
|
|
if len(domAuth) > 253 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if match, _ := regexp.MatchString(re, origin); match {
|
|
|
|
allowOrigin = origin
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-08 01:16:58 +02:00
|
|
|
// Simple request
|
2018-10-14 17:16:58 +02:00
|
|
|
if req.Method != http.MethodOptions {
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Add(echo.HeaderVary, echo.HeaderOrigin)
|
2016-11-22 00:42:13 +02:00
|
|
|
res.Header().Set(echo.HeaderAccessControlAllowOrigin, allowOrigin)
|
2016-04-08 01:16:58 +02:00
|
|
|
if config.AllowCredentials {
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Set(echo.HeaderAccessControlAllowCredentials, "true")
|
2016-04-08 01:16:58 +02:00
|
|
|
}
|
|
|
|
if exposeHeaders != "" {
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Set(echo.HeaderAccessControlExposeHeaders, exposeHeaders)
|
2016-04-08 01:16:58 +02:00
|
|
|
}
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Preflight request
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Add(echo.HeaderVary, echo.HeaderOrigin)
|
|
|
|
res.Header().Add(echo.HeaderVary, echo.HeaderAccessControlRequestMethod)
|
|
|
|
res.Header().Add(echo.HeaderVary, echo.HeaderAccessControlRequestHeaders)
|
2016-11-22 00:42:13 +02:00
|
|
|
res.Header().Set(echo.HeaderAccessControlAllowOrigin, allowOrigin)
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Set(echo.HeaderAccessControlAllowMethods, allowMethods)
|
2016-04-08 01:16:58 +02:00
|
|
|
if config.AllowCredentials {
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Set(echo.HeaderAccessControlAllowCredentials, "true")
|
2016-04-08 01:16:58 +02:00
|
|
|
}
|
|
|
|
if allowHeaders != "" {
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Set(echo.HeaderAccessControlAllowHeaders, allowHeaders)
|
2016-04-08 01:16:58 +02:00
|
|
|
} else {
|
2016-09-23 07:53:44 +02:00
|
|
|
h := req.Header.Get(echo.HeaderAccessControlRequestHeaders)
|
2016-04-08 01:16:58 +02:00
|
|
|
if h != "" {
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Set(echo.HeaderAccessControlAllowHeaders, h)
|
2016-04-08 01:16:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if config.MaxAge > 0 {
|
2016-05-03 17:32:28 +02:00
|
|
|
res.Header().Set(echo.HeaderAccessControlMaxAge, maxAge)
|
2016-04-08 01:16:58 +02:00
|
|
|
}
|
|
|
|
return c.NoContent(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|