-
Notifications
You must be signed in to change notification settings - Fork 4
/
csrf_test.go
288 lines (226 loc) · 7.21 KB
/
csrf_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
package csrf
import (
"net/http"
"net/http/httptest"
"strings"
"testing"
"goji.io/pat"
"golang.org/x/net/context"
"goji.io"
)
var testKey = []byte("keep-it-secret-keep-it-safe-----")
var testHandler = goji.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {})
// TestProtect is a high-level test to make sure the middleware returns the
// wrapped handler with a 200 OK status.
func TestProtect(t *testing.T) {
m := goji.NewMux()
m.UseC(Protect(testKey))
m.HandleFuncC(pat.Get("/"), testHandler)
r, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
m.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 rr.Header().Get("Set-Cookie") == "" {
t.Fatalf("cookie not set: got %q", rr.Header().Get("Set-Cookie"))
}
}
// Test that idempotent methods return a 200 OK status and that non-idempotent
// methods return a 403 Forbidden status when a CSRF cookie is not present.
func TestMethods(t *testing.T) {
m := goji.NewMux()
m.UseC(Protect(testKey))
m.HandleFuncC(pat.New("/"), testHandler)
// Test idempontent ("safe") methods
for _, method := range safeMethods {
r, err := http.NewRequest(method, "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
m.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 rr.Header().Get("Set-Cookie") == "" {
t.Fatalf("cookie not set: got %q", rr.Header().Get("Set-Cookie"))
}
}
// Test non-idempotent methods (should return a 403 without a cookie set)
nonIdempotent := []string{"POST", "PUT", "DELETE", "PATCH"}
for _, method := range nonIdempotent {
r, err := http.NewRequest(method, "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
m.ServeHTTP(rr, r)
if rr.Code != http.StatusForbidden {
t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
rr.Code, http.StatusForbidden)
}
if rr.Header().Get("Set-Cookie") == "" {
t.Fatalf("cookie not set: got %q", rr.Header().Get("Set-Cookie"))
}
}
}
// TestBadCookie tests for failure when a cookie header is modified (malformed).
func TestBadCookie(t *testing.T) {
m := goji.NewMux()
m.UseC(Protect(testKey))
var token string
m.HandleFuncC(pat.New("/"), func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
token = Token(ctx, r)
})
// Obtain a CSRF cookie via a GET request.
r, err := http.NewRequest("GET", "http://www.gorillatoolkit.org/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
m.ServeHTTP(rr, r)
// POST the token back in the header.
r, err = http.NewRequest("POST", "http://www.gorillatoolkit.org/", nil)
if err != nil {
t.Fatal(err)
}
// Replace the cookie prefix
badHeader := strings.Replace("_csrfToken=", rr.Header().Get("Set-Cookie"), "_badCookie", -1)
r.Header.Set("Cookie", badHeader)
r.Header.Set("X-CSRF-Token", token)
r.Header.Set("Referer", "http://www.gorillatoolkit.org/")
rr = httptest.NewRecorder()
m.ServeHTTP(rr, r)
if rr.Code != http.StatusForbidden {
t.Fatalf("middleware failed to reject a bad cookie: got %v want %v",
rr.Code, http.StatusForbidden)
}
}
func TestErrorHandler(t *testing.T) {
m := goji.NewMux()
errorHandler := goji.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
if ctx.Value(errorKey) == nil {
t.Errorf("should have access to a goji error")
}
http.Error(w, "", http.StatusTeapot)
})
m.UseC(Protect(testKey, ErrorHandler(errorHandler)))
r, _ := http.NewRequest("POST", "/", nil)
rr := httptest.NewRecorder()
m.ServeHTTPC(context.Background(), rr, r)
if rr.Code != http.StatusTeapot {
t.Fatalf("custom error handler was not called: got %v want %v",
rr.Code, http.StatusTeapot)
}
}
// Responses should set a "Vary: Cookie" header to protect client/proxy caching.
func TestVaryHeader(t *testing.T) {
m := goji.NewMux()
m.UseC(Protect(testKey))
m.HandleFuncC(pat.Get("/"), testHandler)
r, err := http.NewRequest("HEAD", "https://www.golang.org/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
m.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 rr.Header().Get("Vary") != "Cookie" {
t.Fatalf("vary header not set: got %q want %q", rr.Header().Get("Vary"), "Cookie")
}
}
// Requests with no Referer header should fail.
func TestNoReferer(t *testing.T) {
m := goji.NewMux()
m.UseC(Protect(testKey))
m.HandleFuncC(pat.Get("/"), testHandler)
r, err := http.NewRequest("POST", "https://golang.org/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
m.ServeHTTP(rr, r)
if rr.Code != http.StatusForbidden {
t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
rr.Code, http.StatusForbidden)
}
}
// TestBadReferer checks that HTTPS requests with a Referer that does not
// match the request URL correctly fail CSRF validation.
func TestBadReferer(t *testing.T) {
m := goji.NewMux()
m.UseC(Protect(testKey))
var token string
m.HandleFuncC(pat.New("/"), func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
token = Token(ctx, r)
})
// Obtain a CSRF cookie via a GET request.
r, err := http.NewRequest("GET", "https://www.gorillatoolkit.org/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
m.ServeHTTP(rr, r)
// POST the token back in the header.
r, err = http.NewRequest("POST", "https://www.gorillatoolkit.org/", nil)
if err != nil {
t.Fatal(err)
}
setCookie(rr, r)
r.Header.Set("X-CSRF-Token", token)
// Set a non-matching Referer header.
r.Header.Set("Referer", "http://goji.io")
rr = httptest.NewRecorder()
m.ServeHTTP(rr, r)
if rr.Code != http.StatusForbidden {
t.Fatalf("middleware failed to pass to the next handler: got %v want %v",
rr.Code, http.StatusForbidden)
}
}
// Requests with a valid Referer should pass.
func TestWithReferer(t *testing.T) {
m := goji.NewMux()
m.UseC(Protect(testKey))
var token string
m.HandleFuncC(pat.New("/"), func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
token = Token(ctx, r)
})
// Obtain a CSRF cookie via a GET request.
r, err := http.NewRequest("GET", "http://www.gorillatoolkit.org/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
m.ServeHTTP(rr, r)
// POST the token back in the header.
r, err = http.NewRequest("POST", "http://www.gorillatoolkit.org/", nil)
if err != nil {
t.Fatal(err)
}
setCookie(rr, r)
r.Header.Set("X-CSRF-Token", token)
r.Header.Set("Referer", "http://www.gorillatoolkit.org/")
rr = httptest.NewRecorder()
m.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)
}
}
// TestFormField tests that a token in the form field takes precedence over a
// token in the HTTP header.
// TODO(matt): Finish this test.
func TestFormField(t *testing.T) {
}
func setCookie(rr *httptest.ResponseRecorder, r *http.Request) {
r.Header.Set("Cookie", rr.Header().Get("Set-Cookie"))
}