This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
forked from gorilla/csrf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
293 lines (245 loc) · 7.28 KB
/
helpers_test.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
package csrf
import (
"bytes"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"text/template"
)
var testTemplate = `
<html>
<body>
<form action="/" method="POST">
{{ .csrfField }}
</form>
</body>
</html>
`
// Test that our form helpers correctly inject a token into the response body.
func TestFormToken(t *testing.T) {
s := http.NewServeMux()
// Make the token available outside of the handler for comparison.
var token string
s.HandleFunc("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token = Token(r)
t := template.Must((template.New("base").Parse(testTemplate)))
t.Execute(w, map[string]interface{}{
TemplateTag: TemplateField(r),
})
}))
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
p := Protect(testKey, unauthorizedHandler)(s)
p.ServeHTTP(rr, r)
if rr.Code != http.StatusOK {
t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
rr.Code, http.StatusOK)
}
if len(token) != base64.StdEncoding.EncodedLen(tokenLength*2) {
t.Fatalf("token length invalid: got %v want %v", len(token), base64.StdEncoding.EncodedLen(tokenLength*2))
}
if !strings.Contains(rr.Body.String(), token) {
t.Fatalf("token not in response body: got %v want %v", rr.Body.String(), token)
}
}
// Test that we can extract a CSRF token from a multipart form.
func TestMultipartFormToken(t *testing.T) {
s := http.NewServeMux()
// Make the token available outside of the handler for comparison.
var token string
s.HandleFunc("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token = Token(r)
t := template.Must((template.New("base").Parse(testTemplate)))
t.Execute(w, map[string]interface{}{
TemplateTag: TemplateField(r),
})
}))
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
p := Protect(testKey, unauthorizedHandler)(s)
p.ServeHTTP(rr, r)
// Set up our multipart form
var b bytes.Buffer
mp := multipart.NewWriter(&b)
wr, err := mp.CreateFormField(fieldName)
if err != nil {
t.Fatal(err)
}
wr.Write([]byte(token))
mp.Close()
r, err = http.NewRequest("POST", "http://www.gorillatoolkit.org/", &b)
if err != nil {
t.Fatal(err)
}
// Add the multipart header.
r.Header.Set("Content-Type", mp.FormDataContentType())
// Send back the issued cookie.
setCookie(rr, r)
rr = httptest.NewRecorder()
p.ServeHTTP(rr, r)
}
// TestMaskUnmaskTokens tests that a token traversing the mask -> unmask process
// is correctly unmasked to the original 'real' token.
func TestMaskUnmaskTokens(t *testing.T) {
realToken, err := generateRandomBytes(tokenLength)
if err != nil {
t.Fatal(err)
}
issued := mask(realToken, nil)
decoded, err := base64.StdEncoding.DecodeString(issued)
if err != nil {
t.Fatal(err)
}
unmasked := unmask(decoded)
if !compareTokens(unmasked, realToken) {
t.Fatalf("tokens do not match: got %x want %x", unmasked, realToken)
}
}
// Tests domains that should (or should not) return true for a
// same-origin check.
func TestSameOrigin(t *testing.T) {
var originTests = []struct {
o1 string
o2 string
expected bool
}{
{"https://gorillatoolkit.org/", "https://gorillatoolkit.org", true},
{"http://golang.org/", "http://golang.org/pkg/net/http", true},
{"https://gorillatoolkit.org/", "http://gorillatoolkit.org", false},
{"https://gorillatoolkit.org:3333/", "http://gorillatoolkit.org:4444", false},
}
for _, origins := range originTests {
a, err := url.Parse(origins.o1)
if err != nil {
t.Fatal(err)
}
b, err := url.Parse(origins.o2)
if err != nil {
t.Fatal(err)
}
if sameOrigin(a, b) != origins.expected {
t.Fatalf("origin checking failed: %v and %v, expected %v",
origins.o1, origins.o2, origins.expected)
}
}
}
func TestXOR(t *testing.T) {
testTokens := []struct {
a []byte
b []byte
expected []byte
}{
{[]byte("goodbye"), []byte("hello"), []byte{15, 10, 3, 8, 13}},
{[]byte("gophers"), []byte("clojure"), []byte{4, 3, 31, 2, 16, 0, 22}},
{nil, []byte("requestToken"), nil},
}
for _, token := range testTokens {
if res := xorToken(token.a, token.b); res != nil {
if bytes.Compare(res, token.expected) != 0 {
t.Fatalf("xorBytes failed to return the expected result: got %v want %v",
res, token.expected)
}
}
}
}
// shortReader provides a broken implementation of io.Reader for testing.
type shortReader struct{}
func (sr shortReader) Read(p []byte) (int, error) {
return len(p) % 2, io.ErrUnexpectedEOF
}
// TestGenerateRandomBytes tests the (extremely rare) case that crypto/rand does
// not return the expected number of bytes.
func TestGenerateRandomBytes(t *testing.T) {
// Pioneered from https://github.com/justinas/nosurf
original := rand.Reader
rand.Reader = shortReader{}
defer func() {
rand.Reader = original
}()
b, err := generateRandomBytes(tokenLength)
if err == nil {
t.Fatalf("generateRandomBytes did not report a short read: only read %d bytes", len(b))
}
}
func TestTemplateField(t *testing.T) {
s := http.NewServeMux()
// Make the token & template field available outside of the handler.
var token string
var templateField string
s.HandleFunc("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token = Token(r)
templateField = string(TemplateField(r))
t := template.Must((template.New("base").Parse(testTemplate)))
t.Execute(w, map[string]interface{}{
TemplateTag: TemplateField(r),
})
}))
testFieldName := "custom_field_name"
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
p := Protect(testKey, unauthorizedHandler, FieldName(testFieldName))(s)
p.ServeHTTP(rr, r)
expectedField := fmt.Sprintf(`<input type="hidden" name="%s" value="%s">`,
testFieldName, token)
if rr.Code != http.StatusOK {
t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
rr.Code, http.StatusOK)
}
if templateField != expectedField {
t.Fatalf("custom FieldName was not set correctly: got %v want %v",
templateField, expectedField)
}
}
func TestCompareTokens(t *testing.T) {
// Go's subtle.ConstantTimeCompare prior to 1.3 did not check for matching
// lengths.
a := []byte("")
b := []byte("an-actual-token")
if v := compareTokens(a, b); v == true {
t.Fatalf("compareTokens failed on different tokens: got %v want %v", v, !v)
}
}
func TestUnsafeSkipCSRFCheck(t *testing.T) {
s := http.NewServeMux()
skipCheck := func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
r = UnsafeSkipCheck(r)
h.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
var teapot = 418
// Issue a POST request without a CSRF token in the request.
s.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set a non-200 header to make the test explicit.
w.WriteHeader(teapot)
}))
r, err := http.NewRequest("POST", "/", nil)
if err != nil {
t.Fatal(err)
}
// Must be used prior to the CSRF handler being invoked.
p := skipCheck(Protect(testKey, unauthorizedHandler)(s))
rr := httptest.NewRecorder()
p.ServeHTTP(rr, r)
if status := rr.Code; status != teapot {
t.Fatalf("middleware failed to skip this request: got %v want %v",
status, teapot)
}
}