| 1 |
package csrf |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/http" |
| 5 |
) |
| 6 |
|
| 7 |
// Option describes a functional option for configuring the CSRF handler. |
| 8 |
type Option func(*csrf) |
| 9 |
|
| 10 |
// MaxAge sets the maximum age (in seconds) of a CSRF token's underlying cookie. |
| 11 |
// Defaults to 12 hours. Call csrf.MaxAge(0) to explicitly set session-only |
| 12 |
// cookies. |
| 13 |
func MaxAge(age int) Option { |
| 14 |
return func(cs *csrf) { |
| 15 |
cs.opts.MaxAge = age |
| 16 |
} |
| 17 |
} |
| 18 |
|
| 19 |
// Domain sets the cookie domain. Defaults to the current domain of the request |
| 20 |
// only (recommended). |
| 21 |
// |
| 22 |
// This should be a hostname and not a URL. If set, the domain is treated as |
| 23 |
// being prefixed with a '.' - e.g. "example.com" becomes ".example.com" and |
| 24 |
// matches "www.example.com" and "secure.example.com". |
| 25 |
func Domain(domain string) Option { |
| 26 |
return func(cs *csrf) { |
| 27 |
cs.opts.Domain = domain |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// Path sets the cookie path. Defaults to the path the cookie was issued from |
| 32 |
// (recommended). |
| 33 |
// |
| 34 |
// This instructs clients to only respond with cookie for that path and its |
| 35 |
// subpaths - i.e. a cookie issued from "/register" would be included in requests |
| 36 |
// to "/register/step2" and "/register/submit". |
| 37 |
func Path(p string) Option { |
| 38 |
return func(cs *csrf) { |
| 39 |
cs.opts.Path = p |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
// Secure sets the 'Secure' flag on the cookie. Defaults to true (recommended). |
| 44 |
// Set this to 'false' in your development environment otherwise the cookie won't |
| 45 |
// be sent over an insecure channel. Setting this via the presence of a 'DEV' |
| 46 |
// environmental variable is a good way of making sure this won't make it to a |
| 47 |
// production environment. |
| 48 |
func Secure(s bool) Option { |
| 49 |
return func(cs *csrf) { |
| 50 |
cs.opts.Secure = s |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
// HttpOnly sets the 'HttpOnly' flag on the cookie. Defaults to true (recommended). |
| 55 |
func HttpOnly(h bool) Option { |
| 56 |
return func(cs *csrf) { |
| 57 |
// Note that the function and field names match the case of the |
| 58 |
// related http.Cookie field instead of the "correct" HTTPOnly name |
| 59 |
// that golint suggests. |
| 60 |
cs.opts.HttpOnly = h |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
// SameSite sets the cookie SameSite attribute. Defaults to blank to maintain |
| 65 |
// backwards compatibility, however, Strict is recommended. |
| 66 |
// |
| 67 |
// SameSite(SameSiteStrictMode) will prevent the cookie from being sent by the |
| 68 |
// browser to the target site in all cross-site browsing context, even when |
| 69 |
// following a regular link (GET request). |
| 70 |
// |
| 71 |
// SameSite(SameSiteLaxMode) provides a reasonable balance between security and |
| 72 |
// usability for websites that want to maintain user's logged-in session after |
| 73 |
// the user arrives from an external link. The session cookie would be allowed |
| 74 |
// when following a regular link from an external website while blocking it in |
| 75 |
// CSRF-prone request methods (e.g. POST). |
| 76 |
// |
| 77 |
// This option is only available for go 1.11+. |
| 78 |
func SameSite(s SameSiteMode) Option { |
| 79 |
return func(cs *csrf) { |
| 80 |
cs.opts.SameSite = s |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
// ErrorHandler allows you to change the handler called when CSRF request |
| 85 |
// processing encounters an invalid token or request. A typical use would be to |
| 86 |
// provide a handler that returns a static HTML file with a HTTP 403 status. By |
| 87 |
// default a HTTP 403 status and a plain text CSRF failure reason are served. |
| 88 |
// |
| 89 |
// Note that a custom error handler can also access the csrf.FailureReason(r) |
| 90 |
// function to retrieve the CSRF validation reason from the request context. |
| 91 |
func ErrorHandler(h http.Handler) Option { |
| 92 |
return func(cs *csrf) { |
| 93 |
cs.opts.ErrorHandler = h |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// RequestHeader allows you to change the request header the CSRF middleware |
| 98 |
// inspects. The default is X-CSRF-Token. |
| 99 |
func RequestHeader(header string) Option { |
| 100 |
return func(cs *csrf) { |
| 101 |
cs.opts.RequestHeader = header |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// FieldName allows you to change the name attribute of the hidden <input> field |
| 106 |
// inspected by this package. The default is 'gorilla.csrf.Token'. |
| 107 |
func FieldName(name string) Option { |
| 108 |
return func(cs *csrf) { |
| 109 |
cs.opts.FieldName = name |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// CookieName changes the name of the CSRF cookie issued to clients. |
| 114 |
// |
| 115 |
// Note that cookie names should not contain whitespace, commas, semicolons, |
| 116 |
// backslashes or control characters as per RFC6265. |
| 117 |
func CookieName(name string) Option { |
| 118 |
return func(cs *csrf) { |
| 119 |
cs.opts.CookieName = name |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
// TrustedOrigins configures a set of origins (Referers) that are considered as trusted. |
| 124 |
// This will allow cross-domain CSRF use-cases - e.g. where the front-end is served |
| 125 |
// from a different domain than the API server - to correctly pass a CSRF check. |
| 126 |
// |
| 127 |
// You should only provide origins you own or have full control over. |
| 128 |
func TrustedOrigins(origins []string) Option { |
| 129 |
return func(cs *csrf) { |
| 130 |
cs.opts.TrustedOrigins = origins |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
// setStore sets the store used by the CSRF middleware. |
| 135 |
// Note: this is private (for now) to allow for internal API changes. |
| 136 |
func setStore(s store) Option { |
| 137 |
return func(cs *csrf) { |
| 138 |
cs.st = s |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
// parseOptions parses the supplied options functions and returns a configured |
| 143 |
// csrf handler. |
| 144 |
func parseOptions(h http.Handler, opts ...Option) *csrf { |
| 145 |
// Set the handler to call after processing. |
| 146 |
cs := &csrf{ |
| 147 |
h: h, |
| 148 |
} |
| 149 |
|
| 150 |
// Default to true. See Secure & HttpOnly function comments for rationale. |
| 151 |
// Set here to allow package users to override the default. |
| 152 |
cs.opts.Secure = true |
| 153 |
cs.opts.HttpOnly = true |
| 154 |
|
| 155 |
// Set SameSite=Lax by default, allowing the CSRF cookie to only be sent on |
| 156 |
// top-level navigations. |
| 157 |
cs.opts.SameSite = SameSiteLaxMode |
| 158 |
|
| 159 |
// Default; only override this if the package user explicitly calls MaxAge(0) |
| 160 |
cs.opts.MaxAge = defaultAge |
| 161 |
|
| 162 |
// Range over each options function and apply it |
| 163 |
// to our csrf type to configure it. Options functions are |
| 164 |
// applied in order, with any conflicting options overriding |
| 165 |
// earlier calls. |
| 166 |
for _, option := range opts { |
| 167 |
option(cs) |
| 168 |
} |
| 169 |
|
| 170 |
return cs |
| 171 |
} |