f9a31ea0 by astaxie

EnableXSRF

1 parent 2fa534ff
...@@ -45,7 +45,8 @@ var ( ...@@ -45,7 +45,8 @@ var (
45 HttpServerTimeOut int64 //set httpserver timeout 45 HttpServerTimeOut int64 //set httpserver timeout
46 ErrorsShow bool //set weather show errors 46 ErrorsShow bool //set weather show errors
47 XSRFKEY string //set XSRF 47 XSRFKEY string //set XSRF
48 CopyRequestBody bool //When in raw application, You want to the reqeustbody 48 EnableXSRF bool
49 CopyRequestBody bool //When in raw application, You want to the reqeustbody
49 ) 50 )
50 51
51 func init() { 52 func init() {
......
...@@ -192,6 +192,9 @@ func ParseConfig() (err error) { ...@@ -192,6 +192,9 @@ func ParseConfig() (err error) {
192 if xsrfkey := AppConfig.String("xsrfkey"); xsrfkey != "" { 192 if xsrfkey := AppConfig.String("xsrfkey"); xsrfkey != "" {
193 XSRFKEY = xsrfkey 193 XSRFKEY = xsrfkey
194 } 194 }
195 if enablexsrf, err := AppConfig.Bool("enablexsrf"); err == nil {
196 EnableXSRF = enablexsrf
197 }
195 } 198 }
196 return nil 199 return nil
197 } 200 }
......
...@@ -352,7 +352,7 @@ func (c *Controller) XsrfToken() string { ...@@ -352,7 +352,7 @@ func (c *Controller) XsrfToken() string {
352 h := hmac.New(sha1.New, []byte(XSRFKEY)) 352 h := hmac.New(sha1.New, []byte(XSRFKEY))
353 fmt.Fprintf(h, "%s:%d", c.Ctx.Request.RemoteAddr, time.Now().UnixNano()) 353 fmt.Fprintf(h, "%s:%d", c.Ctx.Request.RemoteAddr, time.Now().UnixNano())
354 tok := fmt.Sprintf("%s:%d", h.Sum(nil), time.Now().UnixNano()) 354 tok := fmt.Sprintf("%s:%d", h.Sum(nil), time.Now().UnixNano())
355 token := base64.URLEncoding.EncodeToString([]byte(tok)) 355 token = base64.URLEncoding.EncodeToString([]byte(tok))
356 c.Ctx.SetCookie("_xsrf", token) 356 c.Ctx.SetCookie("_xsrf", token)
357 } 357 }
358 c._xsrf_token = token 358 c._xsrf_token = token
...@@ -362,7 +362,6 @@ func (c *Controller) XsrfToken() string { ...@@ -362,7 +362,6 @@ func (c *Controller) XsrfToken() string {
362 362
363 func (c *Controller) CheckXsrfCookie() bool { 363 func (c *Controller) CheckXsrfCookie() bool {
364 token := c.GetString("_xsrf") 364 token := c.GetString("_xsrf")
365
366 if token == "" { 365 if token == "" {
367 token = c.Ctx.Request.Header.Get("X-Xsrftoken") 366 token = c.Ctx.Request.Header.Get("X-Xsrftoken")
368 } 367 }
......
...@@ -452,6 +452,17 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -452,6 +452,17 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
452 method := vc.MethodByName("Prepare") 452 method := vc.MethodByName("Prepare")
453 method.Call(in) 453 method.Call(in)
454 454
455 //if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
456 if EnableXSRF {
457 method = vc.MethodByName("XsrfToken")
458 method.Call(in)
459 if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
460 (r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) {
461 method = vc.MethodByName("CheckXsrfCookie")
462 method.Call(in)
463 }
464 }
465
455 //if response has written,yes don't run next 466 //if response has written,yes don't run next
456 if !w.started { 467 if !w.started {
457 if r.Method == "GET" { 468 if r.Method == "GET" {
...@@ -581,6 +592,16 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request) ...@@ -581,6 +592,16 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
581 method.Call(in) 592 method.Call(in)
582 method = vc.MethodByName(mName) 593 method = vc.MethodByName(mName)
583 method.Call(in) 594 method.Call(in)
595 //if XSRF is Enable then check cookie where there has any cookie in the request's cookie _csrf
596 if EnableXSRF {
597 method = vc.MethodByName("XsrfToken")
598 method.Call(in)
599 if r.Method == "POST" || r.Method == "DELETE" || r.Method == "PUT" ||
600 (r.Method == "POST" && (r.Form.Get("_method") == "delete" || r.Form.Get("_method") == "put")) {
601 method = vc.MethodByName("CheckXsrfCookie")
602 method.Call(in)
603 }
604 }
584 if !w.started { 605 if !w.started {
585 if AutoRender { 606 if AutoRender {
586 method = vc.MethodByName("Render") 607 method = vc.MethodByName("Render")
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!