d2a16ff8 by astaxie

fix #26 add xsrf function

1 parent f1e50596
...@@ -44,6 +44,7 @@ var ( ...@@ -44,6 +44,7 @@ var (
44 EnbaleHotUpdate bool //enable HotUpdate default is false 44 EnbaleHotUpdate bool //enable HotUpdate default is false
45 HttpServerTimeOut int64 45 HttpServerTimeOut int64
46 ErrorsShow bool 46 ErrorsShow bool
47 XSRFKEY string
47 ) 48 )
48 49
49 func init() { 50 func init() {
...@@ -72,6 +73,7 @@ func init() { ...@@ -72,6 +73,7 @@ func init() {
72 AppConfigPath = path.Join(AppPath, "conf", "app.conf") 73 AppConfigPath = path.Join(AppPath, "conf", "app.conf")
73 HttpServerTimeOut = 0 74 HttpServerTimeOut = 0
74 ErrorsShow = true 75 ErrorsShow = true
76 XSRFKEY = "beegoxsrf"
75 ParseConfig() 77 ParseConfig()
76 } 78 }
77 79
......
...@@ -186,6 +186,9 @@ func ParseConfig() (err error) { ...@@ -186,6 +186,9 @@ func ParseConfig() (err error) {
186 if errorsshow, err := AppConfig.Bool("errorsshow"); err == nil { 186 if errorsshow, err := AppConfig.Bool("errorsshow"); err == nil {
187 ErrorsShow = errorsshow 187 ErrorsShow = errorsshow
188 } 188 }
189 if xsrfkey := AppConfig.String("xsrfkey"); xsrfkey != "" {
190 XSRFKEY = xsrfkey
191 }
189 } 192 }
190 return nil 193 return nil
191 } 194 }
......
...@@ -101,3 +101,11 @@ var cookieValueSanitizer = strings.NewReplacer("\n", " ", "\r", " ", ";", " ") ...@@ -101,3 +101,11 @@ var cookieValueSanitizer = strings.NewReplacer("\n", " ", "\r", " ", ";", " ")
101 func sanitizeValue(v string) string { 101 func sanitizeValue(v string) string {
102 return cookieValueSanitizer.Replace(v) 102 return cookieValueSanitizer.Replace(v)
103 } 103 }
104
105 func (ctx *Context) GetCookie(key string) string {
106 keycookie, err := ctx.Request.Cookie(key)
107 if err != nil {
108 return ""
109 }
110 return keycookie.Value
111 }
......
...@@ -4,9 +4,13 @@ import ( ...@@ -4,9 +4,13 @@ import (
4 "bytes" 4 "bytes"
5 "compress/gzip" 5 "compress/gzip"
6 "compress/zlib" 6 "compress/zlib"
7 "crypto/hmac"
8 "crypto/sha1"
9 "encoding/base64"
7 "encoding/json" 10 "encoding/json"
8 "encoding/xml" 11 "encoding/xml"
9 "errors" 12 "errors"
13 "fmt"
10 "github.com/astaxie/beego/session" 14 "github.com/astaxie/beego/session"
11 "html/template" 15 "html/template"
12 "io" 16 "io"
...@@ -18,16 +22,18 @@ import ( ...@@ -18,16 +22,18 @@ import (
18 "path" 22 "path"
19 "strconv" 23 "strconv"
20 "strings" 24 "strings"
25 "time"
21 ) 26 )
22 27
23 type Controller struct { 28 type Controller struct {
24 Ctx *Context 29 Ctx *Context
25 Data map[interface{}]interface{} 30 Data map[interface{}]interface{}
26 ChildName string 31 ChildName string
27 TplNames string 32 TplNames string
28 Layout string 33 Layout string
29 TplExt string 34 TplExt string
30 CruSession session.SessionStore 35 _xsrf_token string
36 CruSession session.SessionStore
31 } 37 }
32 38
33 type ControllerInterface interface { 39 type ControllerInterface interface {
...@@ -331,3 +337,42 @@ func (c *Controller) DelSession(name interface{}) { ...@@ -331,3 +337,42 @@ func (c *Controller) DelSession(name interface{}) {
331 func (c *Controller) IsAjax() bool { 337 func (c *Controller) IsAjax() bool {
332 return (c.Ctx.Request.Header.Get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest") 338 return (c.Ctx.Request.Header.Get("HTTP_X_REQUESTED_WITH") == "XMLHttpRequest")
333 } 339 }
340
341 func (c *Controller) XsrfToken() string {
342 if c._xsrf_token == "" {
343 token := c.Ctx.GetCookie("_xsrf")
344 if token == "" {
345 h := hmac.New(sha1.New, []byte(XSRFKEY))
346 fmt.Fprintf(h, "%s:%d", c.Ctx.Request.RemoteAddr, time.Now().UnixNano())
347 tok := fmt.Sprintf("%s:%d", h.Sum(nil), time.Now().UnixNano())
348 token := base64.URLEncoding.EncodeToString([]byte(tok))
349 c.Ctx.SetCookie("_xsrf", token)
350 }
351 c._xsrf_token = token
352 }
353 return c._xsrf_token
354 }
355
356 func (c *Controller) CheckXsrfCookie() bool {
357 token := c.GetString("_xsrf")
358
359 if token == "" {
360 token = c.Ctx.Request.Header.Get("X-Xsrftoken")
361 }
362 if token == "" {
363 token = c.Ctx.Request.Header.Get("X-Csrftoken")
364 }
365 if token == "" {
366 c.Ctx.Abort(403, "'_xsrf' argument missing from POST")
367 }
368
369 if c._xsrf_token != token {
370 c.Ctx.Abort(403, "XSRF cookie does not match POST argument")
371 }
372 return true
373 }
374
375 func (c *Controller) XsrfFormHtml() string {
376 return "<input type=\"hidden\" name=\"_xsrf\" value=\"" +
377 c._xsrf_token + "\"/>"
378 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!