-
Notifications
You must be signed in to change notification settings - Fork 12
/
middleware_builtin.go
469 lines (409 loc) · 11.2 KB
/
middleware_builtin.go
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package ucon
import (
"context"
"crypto/rand"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"strings"
"github.com/favclip/golidator"
)
var httpReqType = reflect.TypeOf((*http.Request)(nil))
var httpRespType = reflect.TypeOf((*http.ResponseWriter)(nil)).Elem()
var contextType = reflect.TypeOf((*context.Context)(nil)).Elem()
var errorType = reflect.TypeOf((*error)(nil)).Elem()
var stringParserType = reflect.TypeOf((*StringParser)(nil)).Elem()
// PathParameterKey is context key of path parameter. context returns map[string]string.
var PathParameterKey = &struct{ temp string }{}
// ErrInvalidPathParameterType is the error that context with PathParameterKey key returns not map[string]string type.
var ErrInvalidPathParameterType = newBadRequestf("path parameter type should be map[string]string")
// ErrPathParameterFieldMissing is the path parameter mapping error.
var ErrPathParameterFieldMissing = newBadRequestf("can't find path parameter in struct")
// ErrCSRFBadToken is the error returns CSRF token verify failure.
var ErrCSRFBadToken = newBadRequestf("invalid CSRF token")
// HTTPErrorResponse is a response to represent http errors.
type HTTPErrorResponse interface {
// StatusCode returns http response status code.
StatusCode() int
// ErrorMessage returns an error object.
// Returned object will be converted by json.Marshal and written as http response body.
ErrorMessage() interface{}
}
// HTTPResponseModifier is an interface to hook on each responses and modify those.
// The hook will hijack ResponseMapper, so it makes possible to do something in place of ResponseMapper.
// e.g. You can convert a response object to xml and write it as response body.
type HTTPResponseModifier interface {
Handle(b *Bubble) error
}
type httpError struct {
Code int `json:"code"`
Message interface{} `json:"message"`
}
func (he *httpError) StatusCode() int {
return he.Code
}
func (he *httpError) ErrorMessage() interface{} {
return he
}
func (he *httpError) Error() string {
return fmt.Sprintf("status code %d: %s", he.StatusCode(), he.ErrorMessage())
}
func newBadRequestf(format string, a ...interface{}) *httpError {
return &httpError{
Code: http.StatusBadRequest,
Message: fmt.Sprintf(format, a...),
}
}
// StringParser is a parser for string-to-object custom conversion.
type StringParser interface {
ParseString(value string) (interface{}, error)
}
// HTTPRWDI injects Bubble.R and Bubble.W into the bubble.Arguments.
func HTTPRWDI() MiddlewareFunc {
return func(b *Bubble) error {
for idx, argT := range b.ArgumentTypes {
if httpReqType.AssignableTo(argT) {
b.Arguments[idx] = reflect.ValueOf(b.R)
continue
}
if httpRespType.AssignableTo(argT) {
b.Arguments[idx] = reflect.ValueOf(b.W)
continue
}
}
return b.Next()
}
}
// NetContextDI injects Bubble.Context into the bubble.Arguments.
// deprecated. use ContextDI instead of NetContextDI.
func NetContextDI() MiddlewareFunc {
return ContextDI()
}
// ContextDI injects Bubble.Context into the bubble.Arguments.
func ContextDI() MiddlewareFunc {
return func(b *Bubble) error {
for idx, argT := range b.ArgumentTypes {
if contextType.AssignableTo(argT) {
b.Arguments[idx] = reflect.ValueOf(b.Context)
continue
}
}
return b.Next()
}
}
// RequestObjectMapper converts a request to object and injects it into the bubble.Arguments.
func RequestObjectMapper() MiddlewareFunc {
return func(b *Bubble) error {
argIdx := -1
var argT reflect.Type
for idx, arg := range b.Arguments {
if arg.IsValid() {
// already injected
continue
}
if b.ArgumentTypes[idx].Kind() != reflect.Ptr || b.ArgumentTypes[idx].Elem().Kind() != reflect.Struct {
// only support for struct
continue
}
argT = b.ArgumentTypes[idx]
argIdx = idx
break
}
if argT == nil {
return b.Next()
}
reqV := reflect.New(argT.Elem())
req := reqV.Interface()
// NOTE value will be overwritten by below process
// url path extract
if v := b.Context.Value(PathParameterKey); v != nil {
params, ok := v.(map[string]string)
if !ok {
return ErrInvalidPathParameterType
}
for key, value := range params {
found, _ := valueStringMapper(reqV, key, value)
if !found {
return ErrPathParameterFieldMissing
}
}
}
// url get parameter
for key, ss := range b.R.URL.Query() {
_, err := valueStringSliceMapper(reqV, key, ss)
if err != nil {
return err
}
}
// request body as JSON
{
// where is the spec???
ct := strings.Split(b.R.Header.Get("Content-Type"), ";")
// TODO check charset
if ct[0] == "application/json" {
var body []byte
var err error
if b.R.Body != nil {
// this case occured in unit test
defer b.R.Body.Close()
body, err = ioutil.ReadAll(b.R.Body)
}
if err != nil {
return err
}
if len(body) == 2 {
// dirty hack. {} map to []interface or [] map to normal struct.
} else if len(body) != 0 {
err := json.Unmarshal(body, req)
if err != nil {
return newBadRequestf(err.Error())
}
}
} else if ct[0] == "application/x-www-form-urlencoded" {
err := b.R.ParseForm()
if err != nil {
return err
}
for key, ss := range b.R.Form {
_, err := valueStringSliceMapper(reqV, key, ss)
if err != nil {
return err
}
}
}
}
// NOTE need request body as a=b&c=d style parsing?
b.Arguments[argIdx] = reqV
return b.Next()
}
}
// ResponseMapper converts a response object to JSON and writes it as response body.
func ResponseMapper() MiddlewareFunc {
return func(b *Bubble) error {
err := b.Next()
// first, error handling
if err != nil {
return b.writeErrorObject(err)
}
// second, error from handlers
for idx := len(b.Returns) - 1; 0 <= idx; idx-- {
rv := b.Returns[idx]
if rv.Type().AssignableTo(errorType) && !rv.IsNil() {
err := rv.Interface().(error)
return b.writeErrorObject(err)
}
}
// last, write payload
for _, rv := range b.Returns {
if rv.Type().AssignableTo(errorType) {
continue
}
v := rv.Interface()
if m, ok := v.(HTTPResponseModifier); ok {
return m.Handle(b)
} else if !rv.IsNil() {
var resp []byte
var err error
if b.Debug {
resp, err = json.MarshalIndent(v, "", " ")
} else {
resp, err = json.Marshal(v)
}
if err != nil {
http.Error(b.W, err.Error(), http.StatusInternalServerError)
return err
}
b.W.Header().Set("Content-Type", "application/json; charset=UTF-8")
b.W.WriteHeader(http.StatusOK)
b.W.Write(resp)
return nil
} else {
b.W.Header().Set("Content-Type", "application/json; charset=UTF-8")
b.W.WriteHeader(http.StatusOK)
if rv.Type().Kind() == reflect.Slice {
b.W.Write([]byte("[]"))
} else {
b.W.Write([]byte("{}"))
}
return nil
}
}
return nil
}
}
func (b *Bubble) writeErrorObject(err error) error {
he, ok := err.(HTTPErrorResponse)
if !ok {
he = &httpError{
Code: http.StatusInternalServerError,
Message: err.Error(),
}
}
msgObj := he.ErrorMessage()
if msgObj == nil {
msgObj = he
}
var resp []byte
if b.Debug {
resp, err = json.MarshalIndent(msgObj, "", " ")
} else {
resp, err = json.Marshal(msgObj)
}
if err != nil {
http.Error(b.W, err.Error(), http.StatusInternalServerError)
return err
}
b.W.Header().Set("Content-Type", "application/json; charset=UTF-8")
b.W.WriteHeader(he.StatusCode())
b.W.Write(resp)
return nil
}
var _ HTTPErrorResponse = &validateError{}
// Validator is an interface of request object validation.
type Validator interface {
Validate(v interface{}) error
}
type validateError struct {
Code int `json:"code"`
Origin error `json:"-"`
}
func (ve *validateError) StatusCode() int {
if her, ok := ve.Origin.(HTTPErrorResponse); ok {
return her.StatusCode()
}
return ve.Code
}
func (ve *validateError) ErrorMessage() interface{} {
if her, ok := ve.Origin.(HTTPErrorResponse); ok {
return her.ErrorMessage()
}
return ve.Origin
}
func (ve *validateError) Error() string {
if ve.Origin != nil {
return ve.Origin.Error()
}
return fmt.Sprintf("status code %d: %v", ve.StatusCode(), ve.ErrorMessage())
}
// RequestValidator checks request object validity.
func RequestValidator(validator Validator) MiddlewareFunc {
if validator == nil {
v := golidator.NewValidator()
v.SetTag("ucon")
validator = v
}
return func(b *Bubble) error {
for idx, argT := range b.ArgumentTypes {
if httpReqType.AssignableTo(argT) {
continue
} else if httpRespType.AssignableTo(argT) {
continue
} else if contextType.AssignableTo(argT) {
continue
}
rv := b.Arguments[idx]
if !rv.IsValid() || rv.IsNil() {
continue
}
v := rv.Interface()
err := validator.Validate(v)
if herr, ok := err.(HTTPErrorResponse); ok && herr != nil {
return err
} else if gerr, ok := err.(*golidator.ErrorReport); ok && gerr != nil {
return &validateError{Code: http.StatusBadRequest, Origin: gerr}
} else if err != nil {
return err
}
}
return b.Next()
}
}
// CSRFOption is options for CSRFProtect.
type CSRFOption struct {
Salt []byte
SafeMethods []string
CookieName string
RequestHeaderName string
GenerateCookie func(r *http.Request) (*http.Cookie, error)
}
// CSRFProtect is a CSRF (Cross Site Request Forgery) prevention middleware.
func CSRFProtect(opts *CSRFOption) (MiddlewareFunc, error) {
// default target is AngularJS
// https://angular.io/docs/ts/latest/guide/security.html#!#http
if opts == nil {
return nil, errors.New("opts is required")
}
if len(opts.SafeMethods) == 0 {
// from https://tools.ietf.org/html/rfc7231#section-4.2.2
// Idempotent Methods
opts.SafeMethods = []string{"GET", "HEAD", "OPTIONS", "TRACE"}
}
if opts.CookieName == "" {
opts.CookieName = "XSRF-TOKEN"
}
if opts.RequestHeaderName == "" {
opts.RequestHeaderName = "X-XSRF-TOKEN"
}
if opts.GenerateCookie == nil {
if len(opts.Salt) == 0 {
return nil, errors.New("opts.Salt is required")
}
opts.GenerateCookie = func(r *http.Request) (*http.Cookie, error) {
b := make([]byte, 32)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
b = append(opts.Salt, b...)
cookie := &http.Cookie{
Name: opts.CookieName,
Value: fmt.Sprintf("%x", sha256.Sum256([]byte(b))),
MaxAge: 0,
HttpOnly: false,
Secure: true,
}
return cookie, nil
}
}
contains := func(strs []string, target string) bool {
for _, str := range strs {
if str == target {
return true
}
}
return false
}
middleware := func(b *Bubble) error {
if contains(opts.SafeMethods, b.R.Method) {
_, err := b.R.Cookie(opts.CookieName)
if err == http.ErrNoCookie {
cookie, err := opts.GenerateCookie(b.R)
if err != nil {
return err
}
http.SetCookie(b.W, cookie)
} else if err != nil {
return err
}
} else {
csrfTokenRequest := b.R.Header.Get(opts.RequestHeaderName)
csrfTokenCookie, _ := b.R.Cookie(opts.CookieName)
if csrfTokenRequest == "" || csrfTokenCookie == nil {
return ErrCSRFBadToken
}
if csrfTokenRequest != csrfTokenCookie.Value {
return ErrCSRFBadToken
}
}
err := b.Next()
if err != nil {
return err
}
return nil
}
return middleware, nil
}