-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcsrf.go
192 lines (164 loc) · 5.34 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
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
package csrf
import (
"crypto/rand"
"crypto/subtle"
"encoding/hex"
"html/template"
"io"
"math"
"net/url"
"strings"
"github.com/revel/revel"
)
// allowMethods are HTTP methods that do NOT require a token.
var allowedMethods = map[string]bool{
"GET": true,
"HEAD": true,
"OPTIONS": true,
"TRACE": true,
}
func RandomString(length int) (string, error) {
buffer := make([]byte, int(math.Ceil(float64(length)/2)))
if _, err := io.ReadFull(rand.Reader, buffer); err != nil {
return "", err
}
str := hex.EncodeToString(buffer)
return str[:length], nil
}
func RefreshToken(c *revel.Controller) string {
token, err := RandomString(64)
if err != nil {
panic(err)
}
c.Session["csrf_token"] = token
return token
}
// CsrfFilter enables CSRF request token creation and verification.
//
// Usage:
// 1) Add `csrf.CsrfFilter` to the app's filters (it must come after the revel.SessionFilter).
// 2) Add CSRF fields to a form with the template tag `{{ csrftoken . }}`.
// The filter adds a function closure to the `ViewArgs` that can pull out the secret and make the token as-needed,
// caching the value in the request. Ajax support provided through the `X-CSRFToken` header.
func CsrfFilter(c *revel.Controller, fc []revel.Filter) {
t, foundToken := c.Session["csrf_token"]
var token string
if !foundToken {
token = RefreshToken(c)
} else {
token = t.(string)
}
referer, refErr := url.Parse(c.Request.Referer())
if refErr != nil {
c.Result = c.Forbidden("REVEL CSRF: Unable to fetch referer")
return
}
requestURL := getFullRequestURL(c)
isSameOrigin := sameOrigin(requestURL, referer)
// If the Request method isn't in the white listed methods
if !allowedMethods[c.Request.Method] && !IsExempt(c) {
validToken := validToken(token, isSameOrigin, foundToken, c)
c.Log.Info("Validating route for token", "token", token, "wasfound", foundToken, "isvalid", validToken)
if !validToken {
c.Log.Warn("Invalid CSRF token", "token", token, "wasfound", foundToken)
return
}
}
fc[0](c, fc[1:])
// Only add token to ViewArgs if the request is: not AJAX, not missing referer header, and (is same origin, or is an empty referer).
if c.Request.GetHttpHeader("X-CSRFToken") == "" && (referer.String() == "" || isSameOrigin) {
c.ViewArgs["_csrftoken"] = token
}
}
// If this call should be checked validate token.
func validToken(token string, isSameOrigin, foundToken bool, c *revel.Controller) (result bool) {
// Token wasn't present at all
if !foundToken {
c.Result = c.Forbidden("REVEL CSRF: Session token missing.")
return
}
// Same origin
if !isSameOrigin {
c.Result = c.Forbidden("REVEL CSRF: Same origin mismatch.")
return
}
var requestToken string
// First check for token in post data
if c.Request.Method == "POST" {
requestToken = c.Params.Get("csrftoken")
}
// Then check for token in custom headers, as with AJAX
if requestToken == "" {
requestToken = c.Request.GetHttpHeader("X-CSRFToken")
}
if requestToken == "" || !compareToken(requestToken, token) {
c.Result = c.Forbidden("REVEL CSRF: Invalid token.")
return
}
return true
}
// Helper function to fix the full URL in the request.
func getFullRequestURL(c *revel.Controller) (requestURL *url.URL) {
requestURL = c.Request.URL
c.Log.Debug("Using ", "request url host", requestURL.Host, "request host", c.Request.Host, "cookie domain", revel.CookieDomain)
// Update any of the information based on the headers
if host := c.Request.GetHttpHeader("X-Forwarded-Host"); host != "" {
requestURL.Host = strings.ToLower(host)
}
if scheme := c.Request.GetHttpHeader("X-Forwarded-Proto"); scheme != "" {
requestURL.Scheme = strings.ToLower(scheme)
}
if scheme := c.Request.GetHttpHeader("X-Forwarded-Scheme"); scheme != "" {
requestURL.Scheme = strings.ToLower(scheme)
}
// Use the revel.CookieDomain for the hostname, or the c.Request.Host
if requestURL.Host == "" {
host := revel.CookieDomain
if host == "" && c.Request.Host != "" {
host = c.Request.Host
// Slice off any port information.
if i := strings.Index(host, ":"); i != -1 {
host = host[:i]
}
}
requestURL.Host = host
}
// If no scheme found in headers use the revel server settings
if requestURL.Scheme == "" {
// Fix the Request.URL, it is missing information, go http server does this
if revel.HTTPSsl {
requestURL.Scheme = "https"
} else {
requestURL.Scheme = "http"
}
fixedURL := requestURL.Scheme + "://" + c.Request.Host + c.Request.URL.Path
if purl, err := url.Parse(fixedURL); err == nil {
requestURL = purl
}
}
c.Log.Debug("getFullRequestURL ", "requestURL", requestURL.String())
return
}
// Compare the two tokens.
func compareToken(requestToken, token string) bool {
// ConstantTimeCompare will panic if the []byte aren't the same length
if len(requestToken) != len(token) {
return false
}
return subtle.ConstantTimeCompare([]byte(requestToken), []byte(token)) == 1
}
// Validates same origin policy.
func sameOrigin(u1, u2 *url.URL) bool {
return u1.Scheme == u2.Scheme && u1.Hostname() == u2.Hostname()
}
// Add a function to the template functions map.
func init() {
revel.TemplateFuncs["csrftoken"] = func(viewArgs map[string]interface{}) template.HTML {
if tokenFunc, ok := viewArgs["_csrftoken"]; !ok {
panic("REVEL CSRF: _csrftoken missing from ViewArgs.")
} else {
//nolint:gosec
return template.HTML(tokenFunc.(string))
}
}
}