/
csrf.go
70 lines (57 loc) · 1.64 KB
/
csrf.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
package auth
import (
"crypto/subtle"
"mime"
"net/http"
"net/url"
"github.com/EricLagerg/pnwconference/tokens"
"github.com/golang/glog"
"github.com/gorilla/sessions"
)
// getCSRF returns the CSRF from a session or creates a new CSRF token if
// the value does not exist inside the session.
func getCSRF(session *sessions.Session) (csrf []byte, ok bool) {
csrfIf, ok := session.Values[csrfToken]
if !ok {
csrf = tokens.NewCSRFToken(session.ID)
} else {
if csrf, ok = csrfIf.([]byte); !ok {
csrf = tokens.NewCSRFToken(session.ID)
}
}
return
}
// ValidCSRF returns true if the given CSRF token matches the CSRF token
// for a specific session.
func ValidCSRF(r *http.Request, session *sessions.Session, ws bool) bool {
var err error
// MITM check.
if r.URL.Scheme == "https" {
referer, err := url.Parse(r.Header.Get("Referer"))
if err != nil || referer.String() == "" {
glog.Errorln(err, ErrNoReferer)
return false
}
if !sameOrigin(referer, r.URL) {
return false
}
}
ss, err := GetSession(session.ID)
if err != nil {
return false
}
var mac1 []byte
// If we have an input field inside a multipart form, then our CSRF
// token will be found inside Form. Otherwise, it'll be in
// PostForm. We'd like to default to PostFormValue because FormValue
// accepts URL query parameters, and we'll never send CSRF tokens
// that way.
d, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
if d == "multipart/form-data" || ws {
mac1 = []byte(r.FormValue("_csrf"))
} else {
mac1 = []byte(r.PostFormValue("_csrf"))
}
mac2 := ss.CSRFToken
return len(mac1) == len(mac2) && subtle.ConstantTimeCompare(mac1, mac2) == 1
}