-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver_csrf.go
More file actions
367 lines (309 loc) · 11.8 KB
/
server_csrf.go
File metadata and controls
367 lines (309 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package diecast
import (
"bytes"
"crypto/rand"
"crypto/subtle"
"fmt"
"io"
"mime"
"net/http"
"path/filepath"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/ghetzel/go-stockutil/httputil"
"github.com/ghetzel/go-stockutil/log"
"github.com/ghetzel/go-stockutil/typeutil"
)
const DefaultCsrfInjectFormFieldSelector = `form[method="post"], form[method="POST"], form[method="Post"]` // if you need more case permutations than this, you may override this default
const DefaultCsrfInjectFieldFormat = `<input type="hidden" name="%s" value="%s">`
const CsrfTokenLength = 32
const ContextCsrfToken = `csrf-token`
const ContextStatusKey = `response-status-code`
const ContextErrorKey = `response-error-message`
const SwitchCaseKey = `switch-case`
var DefaultCsrfHeaderName = `X-CSRF-Token`
var DefaultCsrfFormFieldName = `csrf_token`
var DefaultCsrfCookieName = `csrf_token`
var DefaultCsrfInjectMediaTypes = []string{
`text/html`,
}
type CsrfMethod string
const (
DoubleSubmitCookie CsrfMethod = `cookie`
HMAC CsrfMethod = `hmac`
)
type CSRF struct {
Enable bool `yaml:"enable" json:"enable"` // Whether to enable stateless CSRF protection
Except []string `yaml:"except" json:"except"` // A list of paths and path globs that should not be covered by CSRF protection
Cookie *Cookie `yaml:"cookie" json:"cookie"` // Specify default fields for the CSRF cookie that is set
HeaderName string `yaml:"header" json:"header"` // The name of the HTTP header that CSRF tokens may be present in (default: X-CSRF-Token)
FormFieldName string `yaml:"field" json:"field"` // The name of the HTML form fieldthat CSRF tokens may be present in (default: csrf_token)
InjectFormFields bool `yaml:"injectFormFields" json:"injectFormFields"` // If true, a postprocessor will be added that injects a hidden <input> field into all <form> elements returned from Diecast
InjectFormFieldSelector string `yaml:"injectFormFieldSelector" json:"injectFormFieldSelector"` // A CSS selector used to locate <form> tags that need the CSRF <input> field injected.
InjectFormFieldTemplate string `yaml:"injectFormFieldTemplate" json:"injectFormFieldTemplate"` // Specify the format string that will be used to replace </form> tags with the injected field.
InjectableMediaTypes []string `yaml:"injectableMediaTypes" json:"injectableMediaTypes"` // Specify a list of Media Types (e.g.: MIME or Content-Types) that will have injection attempted on them (if enabled)
server *Server
registered bool
// Method CsrfMethod `yaml:"method" json:"method"` // Specify the method to use for CSRF validation: "cookie" or "hmac". If unspecified, "hmac" is used if private_key is set to a value, otherwise "cookie" is used.
// PrivateKey string `yaml:"private_key" json:"private_key"` // Provide a base64-encoded private key for use with the HMAC method of token validation
}
func (csrf *CSRF) GetHeaderName() string {
if csrf.HeaderName != `` {
return csrf.HeaderName
} else {
return DefaultCsrfHeaderName
}
}
func (csrf *CSRF) GetFormFieldName() string {
if csrf.FormFieldName != `` {
return csrf.FormFieldName
} else {
return DefaultCsrfFormFieldName
}
}
func (csrf *CSRF) GetCookieName() string {
if c := csrf.Cookie; c != nil && c.Name != `` {
return c.Name
} else {
return DefaultCsrfCookieName
}
}
func (csrf *CSRF) Handle(w http.ResponseWriter, req *http.Request) bool {
if csrf.Enable {
log.Debugf("[%s] middleware: check csrf", reqid(req))
csrf.generateTokenForRequest(w, req, false)
switch req.Method {
case http.MethodGet, http.MethodHead, http.MethodOptions, http.MethodTrace:
break
default:
if !csrf.IsExempt(req) {
// if we're validating the request, then we've "consumed" this token and
// should force-regenerate a new one
csrf.generateTokenForRequest(w, req, true)
var creq = req.Clone(req.Context())
if req.Body != nil {
if body, err := io.ReadAll(req.Body); err == nil {
req.Body.Close()
creq.Body = io.NopCloser(bytes.NewBuffer(body))
req.Body = io.NopCloser(bytes.NewBuffer(body))
} else if csrf.server != nil {
csrf.server.respondError(w, req, err, http.StatusBadRequest)
} else {
http.Error(w, err.Error(), http.StatusBadRequest)
return false
}
}
// if the token is missing/invalid, stop here and return an error
if !csrf.Verify(creq) {
if csrf.server != nil {
csrf.server.respondError(w, req, fmt.Errorf("cSRF validation failed"), http.StatusBadRequest)
} else {
http.Error(w, "CSRF validation failed", http.StatusBadRequest)
}
return false
}
} else {
log.Infof("[%s] path %q exempted from CSRF protection", reqid(req), req.URL.Path)
}
}
}
return true
}
// Retrieve the user-submitted token that can be forged.
func (csrf *CSRF) getUserSubmittedToken(req *http.Request) ([]byte, bool) {
// first try to get the token from the header
if token := req.Header.Get(csrf.GetHeaderName()); token != `` {
return b58decode(token), true
}
// then try getting it from a form field
if token := req.PostFormValue(csrf.GetFormFieldName()); token != `` {
return b58decode(token), true
}
// Finally, try a multipart value.
if req.MultipartForm != nil {
if values, ok := req.MultipartForm.Value[csrf.GetFormFieldName()]; ok && len(values) > 0 && values[0] != `` {
return b58decode(values[0]), true
}
}
return nil, false
}
// Retrieve the cookie token that is harder to forge.
func (csrf *CSRF) getCookieToken(req *http.Request) ([]byte, bool) {
if cookie, err := req.Cookie(csrf.GetCookieName()); err == nil {
if cookie.Value != `` {
return b58decode(cookie.Value), true
}
}
return nil, false
}
// Verifies that the token that came in via the CSRF cookie and the one that came in
// as part of the request headers/body are, in fact, the same.
func (csrf *CSRF) Verify(req *http.Request) bool {
if cookieToken, ok := csrf.getCookieToken(req); ok {
if userToken, ok := csrf.getUserSubmittedToken(req); ok {
if subtle.ConstantTimeCompare(cookieToken, userToken) == 1 {
return true
}
}
}
return false
}
func (csrf *CSRF) cookieFor(token string) *http.Cookie {
var cookie = new(http.Cookie)
cookie.Name = csrf.GetCookieName()
cookie.Value = token
cookie.Path = `/`
cookie.MaxAge = 31536000
if c := csrf.Cookie; c != nil {
if c.MaxAge != nil {
cookie.MaxAge = *c.MaxAge
}
if c.Secure != nil {
cookie.Secure = *c.Secure
}
if c.HttpOnly != nil {
cookie.HttpOnly = *c.HttpOnly
}
if c.Path != `` {
cookie.Path = c.Path
}
if c.Domain != `` {
cookie.Domain = c.Domain
}
if c.SameSite != `` {
cookie.SameSite = c.SameSite.SameSite()
}
}
return cookie
}
func (csrf *CSRF) generateTokenForRequest(w http.ResponseWriter, req *http.Request, forceRegen bool) {
var data []byte
if cookieToken, ok := csrf.getCookieToken(req); ok && len(cookieToken) == CsrfTokenLength && !forceRegen {
data = cookieToken
} else {
data = make([]byte, CsrfTokenLength)
if _, err := io.ReadFull(rand.Reader, data); err != nil {
panic(err)
}
}
var token = b58encode(data)
// attach token to the current request context so other things involved in
// generating the response can see it
httputil.RequestSetValue(req, ContextCsrfToken, token)
// set the cookie
w.Header().Set(`Vary`, `Cookie`)
w.Header().Set(csrf.GetHeaderName(), token)
var cookie = csrf.cookieFor(token)
http.SetCookie(w, cookie)
}
func (csrf *CSRF) shouldPostprocessRequest(w http.ResponseWriter, _ *http.Request) bool {
var mediaTypes = DefaultCsrfInjectMediaTypes
var resMediaType = w.Header().Get(`Content-Type`)
if len(csrf.InjectableMediaTypes) > 0 {
mediaTypes = csrf.InjectableMediaTypes
}
for _, ct := range mediaTypes {
if mt, _, err := mime.ParseMediaType(resMediaType); err == nil {
if strings.EqualFold(ct, mt) {
return true
}
}
}
return false
}
func (csrf *CSRF) IsExempt(req *http.Request) bool {
if req != nil {
for _, pattern := range csrf.Except {
if m, err := filepath.Match(pattern, req.URL.Path); err == nil && m {
return true
}
}
}
return false
}
func (server *Server) middlewareCsrf(w http.ResponseWriter, req *http.Request) bool {
// enforce CSRF protection (if configured)
if csrf := server.CSRF; csrf != nil && csrf.Enable {
if !csrf.registered {
csrf.server = server
// Okay, so...
//
// Cross-Site Request Forgery is an insane problem that we have in modern web browsers in which
// an authenticated user that is totally allowed to make requests can be tricked (using trickery)
// into making a valid HTTP request that they did not intend to make. 99% of the time this is done
// by somehow getting them to execute JavaScript in their browser that does Nasty Stuff™.
//
// How do to protect a user from this insane problem that we should have better solutions to by now?
//
// With each request, you include a one-time use token that is set in two places: a cookie and somewhere
// in the content itself (e.g.: a hidden form field or an HTTP header). If the token submitted from the
// form doesn't match the value submitted in the cookie, you're a hacking hacker and its Bad News Time.
//
// "But updating a bunch of forms I may or may not control is annoying and difficult?"
//
// SURE IS!
//
// So, if you set this lunatic feature to "true", here's what Diecast will do for any content that is
// CSRF protected (as defined in the csrf.except setting):
//
// 1. Is csrf.enable set to true?
// 2. YES! Attempt to parse the content as an HTML document.
// 3. COOL! Select all elements from that document that match csrf.injectFormFieldSelector
// 4. RAD! Append the element described in csrf.formTokenTagFormat to those matching elements.
// 5. NEAT! Serve *THAT* HTML instead.
//
// "Yeah we ran out of floorboards so we just painted the dirt. Pretty Clever!"
//
RegisterPostprocessor(`__diecast_csrf`, func(in string, req *http.Request) (string, error) {
if req != nil {
var w = reqres(req)
if csrf.shouldPostprocessRequest(w, req) {
if csrf.InjectFormFields {
if csrf.InjectFormFieldSelector == `` {
csrf.InjectFormFieldSelector = DefaultCsrfInjectFormFieldSelector
}
if csrf.InjectFormFieldTemplate == `` {
csrf.InjectFormFieldTemplate = DefaultCsrfInjectFieldFormat
}
log.Debugf("[%s] injecting form field", reqid(req))
var start = time.Now()
defer func() {
reqtime(req, `csrf-inject`, time.Since(start))
}()
if doc, err := htmldoc(in); err == nil {
doc.Find(csrf.InjectFormFieldSelector).Each(func(i int, form *goquery.Selection) {
if form.Find(fmt.Sprintf("input[name=%q]", csrf.GetFormFieldName())).Length() == 0 {
form.AppendHtml(
fmt.Sprintf(
csrf.InjectFormFieldTemplate,
csrf.GetFormFieldName(),
csrftoken(req),
),
)
}
})
doc.End()
if h, err := doc.Html(); err == nil {
w.Header().Set(`Content-Length`, typeutil.String(len(h)))
return h, nil
} else {
return ``, err
}
}
}
}
}
return in, nil
})
if server.BaseHeader == nil {
server.BaseHeader = new(TemplateHeader)
}
server.BaseHeader.Postprocessors = append([]string{`__diecast_csrf`}, server.BaseHeader.Postprocessors...)
csrf.registered = true
}
return csrf.Handle(w, req)
} else {
return true
}
}