2016-05-13 02:45:00 +02:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2016-07-17 05:13:27 +02:00
|
|
|
"crypto/subtle"
|
2016-05-13 17:18:00 +02:00
|
|
|
"errors"
|
2016-05-13 02:45:00 +02:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/labstack/echo"
|
2016-07-19 08:24:50 +02:00
|
|
|
"github.com/labstack/gommon/random"
|
2016-05-13 02:45:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// CSRFConfig defines the config for CSRF middleware.
|
|
|
|
CSRFConfig struct {
|
2016-07-27 18:34:44 +02:00
|
|
|
// Skipper defines a function to skip middleware.
|
|
|
|
Skipper Skipper
|
|
|
|
|
2016-07-17 05:13:27 +02:00
|
|
|
// TokenLength is the length of the generated token.
|
2017-12-28 21:24:34 +02:00
|
|
|
TokenLength uint8 `yaml:"token_length"`
|
2016-07-17 05:13:27 +02:00
|
|
|
// Optional. Default value 32.
|
2016-05-13 02:45:00 +02:00
|
|
|
|
2016-05-27 04:23:46 +02:00
|
|
|
// TokenLookup is a string in the form of "<source>:<key>" that is used
|
|
|
|
// to extract token from the request.
|
2016-05-26 23:06:30 +02:00
|
|
|
// Optional. Default value "header:X-CSRF-Token".
|
|
|
|
// Possible values:
|
|
|
|
// - "header:<name>"
|
|
|
|
// - "form:<name>"
|
2016-06-05 19:29:23 +02:00
|
|
|
// - "query:<name>"
|
2017-12-28 21:24:34 +02:00
|
|
|
TokenLookup string `yaml:"token_lookup"`
|
2016-05-26 23:06:30 +02:00
|
|
|
|
2016-05-13 17:18:00 +02:00
|
|
|
// Context key to store generated CSRF token into context.
|
|
|
|
// Optional. Default value "csrf".
|
2017-12-28 21:24:34 +02:00
|
|
|
ContextKey string `yaml:"context_key"`
|
2016-05-13 17:18:00 +02:00
|
|
|
|
2016-05-13 02:45:00 +02:00
|
|
|
// Name of the CSRF cookie. This cookie will store CSRF token.
|
|
|
|
// Optional. Default value "csrf".
|
2017-12-28 21:24:34 +02:00
|
|
|
CookieName string `yaml:"cookie_name"`
|
2016-05-13 02:45:00 +02:00
|
|
|
|
|
|
|
// Domain of the CSRF cookie.
|
|
|
|
// Optional. Default value none.
|
2017-12-28 21:24:34 +02:00
|
|
|
CookieDomain string `yaml:"cookie_domain"`
|
2016-05-13 02:45:00 +02:00
|
|
|
|
2016-05-13 03:14:00 +02:00
|
|
|
// Path of the CSRF cookie.
|
2016-05-13 02:45:00 +02:00
|
|
|
// Optional. Default value none.
|
2017-12-28 21:24:34 +02:00
|
|
|
CookiePath string `yaml:"cookie_path"`
|
2016-05-13 02:45:00 +02:00
|
|
|
|
2016-06-29 02:38:12 +02:00
|
|
|
// Max age (in seconds) of the CSRF cookie.
|
|
|
|
// Optional. Default value 86400 (24hr).
|
2017-12-28 21:24:34 +02:00
|
|
|
CookieMaxAge int `yaml:"cookie_max_age"`
|
2016-05-13 02:45:00 +02:00
|
|
|
|
|
|
|
// Indicates if CSRF cookie is secure.
|
2016-05-13 03:14:00 +02:00
|
|
|
// Optional. Default value false.
|
2017-12-28 21:24:34 +02:00
|
|
|
CookieSecure bool `yaml:"cookie_secure"`
|
2016-07-17 05:13:27 +02:00
|
|
|
|
|
|
|
// Indicates if CSRF cookie is HTTP only.
|
|
|
|
// Optional. Default value false.
|
2017-12-28 21:24:34 +02:00
|
|
|
CookieHTTPOnly bool `yaml:"cookie_http_only"`
|
2016-05-13 02:45:00 +02:00
|
|
|
}
|
2016-05-13 17:18:00 +02:00
|
|
|
|
2016-05-26 23:06:30 +02:00
|
|
|
// csrfTokenExtractor defines a function that takes `echo.Context` and returns
|
2016-05-13 17:18:00 +02:00
|
|
|
// either a token or an error.
|
2016-05-26 23:06:30 +02:00
|
|
|
csrfTokenExtractor func(echo.Context) (string, error)
|
2016-05-13 02:45:00 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// DefaultCSRFConfig is the default CSRF middleware config.
|
|
|
|
DefaultCSRFConfig = CSRFConfig{
|
2017-01-28 21:43:56 +02:00
|
|
|
Skipper: DefaultSkipper,
|
2016-07-17 05:13:27 +02:00
|
|
|
TokenLength: 32,
|
2016-06-29 02:38:12 +02:00
|
|
|
TokenLookup: "header:" + echo.HeaderXCSRFToken,
|
|
|
|
ContextKey: "csrf",
|
2016-07-14 01:55:46 +02:00
|
|
|
CookieName: "_csrf",
|
2016-06-29 02:38:12 +02:00
|
|
|
CookieMaxAge: 86400,
|
2016-05-13 02:45:00 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// CSRF returns a Cross-Site Request Forgery (CSRF) middleware.
|
|
|
|
// See: https://en.wikipedia.org/wiki/Cross-site_request_forgery
|
2016-07-17 05:13:27 +02:00
|
|
|
func CSRF() echo.MiddlewareFunc {
|
2016-05-13 02:45:00 +02:00
|
|
|
c := DefaultCSRFConfig
|
|
|
|
return CSRFWithConfig(c)
|
|
|
|
}
|
|
|
|
|
2016-09-01 05:10:14 +02:00
|
|
|
// CSRFWithConfig returns a CSRF middleware with config.
|
2016-05-13 02:45:00 +02:00
|
|
|
// See `CSRF()`.
|
|
|
|
func CSRFWithConfig(config CSRFConfig) echo.MiddlewareFunc {
|
|
|
|
// Defaults
|
2016-07-27 18:34:44 +02:00
|
|
|
if config.Skipper == nil {
|
|
|
|
config.Skipper = DefaultCSRFConfig.Skipper
|
|
|
|
}
|
2016-07-17 05:13:27 +02:00
|
|
|
if config.TokenLength == 0 {
|
|
|
|
config.TokenLength = DefaultCSRFConfig.TokenLength
|
2016-05-13 02:45:00 +02:00
|
|
|
}
|
2016-05-27 04:23:46 +02:00
|
|
|
if config.TokenLookup == "" {
|
|
|
|
config.TokenLookup = DefaultCSRFConfig.TokenLookup
|
2016-05-26 23:06:30 +02:00
|
|
|
}
|
2016-05-13 17:18:00 +02:00
|
|
|
if config.ContextKey == "" {
|
|
|
|
config.ContextKey = DefaultCSRFConfig.ContextKey
|
|
|
|
}
|
2016-05-13 02:45:00 +02:00
|
|
|
if config.CookieName == "" {
|
|
|
|
config.CookieName = DefaultCSRFConfig.CookieName
|
|
|
|
}
|
2016-06-29 02:38:12 +02:00
|
|
|
if config.CookieMaxAge == 0 {
|
|
|
|
config.CookieMaxAge = DefaultCSRFConfig.CookieMaxAge
|
2016-05-13 02:45:00 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 23:06:30 +02:00
|
|
|
// Initialize
|
2016-05-27 04:23:46 +02:00
|
|
|
parts := strings.Split(config.TokenLookup, ":")
|
2016-05-26 23:06:30 +02:00
|
|
|
extractor := csrfTokenFromHeader(parts[1])
|
|
|
|
switch parts[0] {
|
|
|
|
case "form":
|
|
|
|
extractor = csrfTokenFromForm(parts[1])
|
|
|
|
case "query":
|
|
|
|
extractor = csrfTokenFromQuery(parts[1])
|
|
|
|
}
|
|
|
|
|
2016-05-13 02:45:00 +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-13 02:45:00 +02:00
|
|
|
req := c.Request()
|
2016-07-17 05:13:27 +02:00
|
|
|
k, err := c.Cookie(config.CookieName)
|
2016-07-16 19:22:24 +02:00
|
|
|
token := ""
|
2016-05-13 02:45:00 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2016-07-17 05:13:27 +02:00
|
|
|
// Generate token
|
2016-07-19 08:24:50 +02:00
|
|
|
token = random.String(config.TokenLength)
|
2016-07-16 19:22:24 +02:00
|
|
|
} else {
|
|
|
|
// Reuse token
|
2016-09-23 07:53:44 +02:00
|
|
|
token = k.Value
|
2016-07-16 19:22:24 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 07:53:44 +02:00
|
|
|
switch req.Method {
|
2016-07-16 19:22:24 +02:00
|
|
|
case echo.GET, echo.HEAD, echo.OPTIONS, echo.TRACE:
|
2016-05-13 02:45:00 +02:00
|
|
|
default:
|
2016-07-17 05:13:27 +02:00
|
|
|
// Validate token only for requests which are not defined as 'safe' by RFC7231
|
2016-07-14 01:55:46 +02:00
|
|
|
clientToken, err := extractor(c)
|
2016-05-13 17:18:00 +02:00
|
|
|
if err != nil {
|
2017-01-05 22:35:09 +02:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
2016-05-13 17:18:00 +02:00
|
|
|
}
|
2016-07-17 05:13:27 +02:00
|
|
|
if !validateCSRFToken(token, clientToken) {
|
2017-01-05 22:35:09 +02:00
|
|
|
return echo.NewHTTPError(http.StatusForbidden, "Invalid csrf token")
|
2016-05-13 02:45:00 +02:00
|
|
|
}
|
|
|
|
}
|
2016-07-17 05:13:27 +02:00
|
|
|
|
|
|
|
// Set CSRF cookie
|
2016-09-23 07:53:44 +02:00
|
|
|
cookie := new(http.Cookie)
|
|
|
|
cookie.Name = config.CookieName
|
|
|
|
cookie.Value = token
|
2016-07-17 05:13:27 +02:00
|
|
|
if config.CookiePath != "" {
|
2016-09-23 07:53:44 +02:00
|
|
|
cookie.Path = config.CookiePath
|
2016-07-17 05:13:27 +02:00
|
|
|
}
|
|
|
|
if config.CookieDomain != "" {
|
2016-09-23 07:53:44 +02:00
|
|
|
cookie.Domain = config.CookieDomain
|
2016-07-17 05:13:27 +02:00
|
|
|
}
|
2016-09-23 07:53:44 +02:00
|
|
|
cookie.Expires = time.Now().Add(time.Duration(config.CookieMaxAge) * time.Second)
|
|
|
|
cookie.Secure = config.CookieSecure
|
|
|
|
cookie.HttpOnly = config.CookieHTTPOnly
|
2016-07-17 05:13:27 +02:00
|
|
|
c.SetCookie(cookie)
|
|
|
|
|
|
|
|
// Store token in the context
|
|
|
|
c.Set(config.ContextKey, token)
|
|
|
|
|
|
|
|
// Protect clients from caching the response
|
|
|
|
c.Response().Header().Add(echo.HeaderVary, echo.HeaderCookie)
|
|
|
|
|
2016-05-13 02:45:00 +02:00
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 23:06:30 +02:00
|
|
|
// csrfTokenFromForm returns a `csrfTokenExtractor` that extracts token from the
|
2016-05-13 17:18:00 +02:00
|
|
|
// provided request header.
|
2016-05-26 23:06:30 +02:00
|
|
|
func csrfTokenFromHeader(header string) csrfTokenExtractor {
|
2016-05-13 17:18:00 +02:00
|
|
|
return func(c echo.Context) (string, error) {
|
2016-09-23 07:53:44 +02:00
|
|
|
return c.Request().Header.Get(header), nil
|
2016-05-13 17:18:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 23:06:30 +02:00
|
|
|
// csrfTokenFromForm returns a `csrfTokenExtractor` that extracts token from the
|
2016-05-13 17:18:00 +02:00
|
|
|
// provided form parameter.
|
2016-05-26 23:06:30 +02:00
|
|
|
func csrfTokenFromForm(param string) csrfTokenExtractor {
|
2016-05-13 17:18:00 +02:00
|
|
|
return func(c echo.Context) (string, error) {
|
|
|
|
token := c.FormValue(param)
|
|
|
|
if token == "" {
|
2017-01-05 22:35:09 +02:00
|
|
|
return "", errors.New("Missing csrf token in the form parameter")
|
2016-05-13 17:18:00 +02:00
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 23:06:30 +02:00
|
|
|
// csrfTokenFromQuery returns a `csrfTokenExtractor` that extracts token from the
|
2016-05-13 17:18:00 +02:00
|
|
|
// provided query parameter.
|
2016-05-26 23:06:30 +02:00
|
|
|
func csrfTokenFromQuery(param string) csrfTokenExtractor {
|
2016-05-13 17:18:00 +02:00
|
|
|
return func(c echo.Context) (string, error) {
|
|
|
|
token := c.QueryParam(param)
|
|
|
|
if token == "" {
|
2017-01-05 22:35:09 +02:00
|
|
|
return "", errors.New("Missing csrf token in the query string")
|
2016-05-13 17:18:00 +02:00
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-17 05:13:27 +02:00
|
|
|
func validateCSRFToken(token, clientToken string) bool {
|
|
|
|
return subtle.ConstantTimeCompare([]byte(token), []byte(clientToken)) == 1
|
2016-05-13 02:45:00 +02:00
|
|
|
}
|