-
Notifications
You must be signed in to change notification settings - Fork 0
/
csrf_test.go
331 lines (277 loc) · 8.47 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package main
import (
"context"
"crypto/rand"
"crypto/tls"
"encoding/base64"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestValidateCSRFToken(t *testing.T) {
// Setup
r, _ := http.NewRequest("POST", "/", nil)
w := httptest.NewRecorder()
token, err := setCSRFToken(w, r)
if err != nil {
t.Errorf("setCSRFToken() error = %v", err)
}
// Test valid token
r.Header.Set(csrfHeaderName, token)
r.AddCookie(&http.Cookie{Name: csrfCookieName, Value: token})
err = validateCSRFToken(r)
if err != nil {
t.Errorf("validateCSRFToken() error = %v", err)
}
// Test invalid token
r.Header.Set(csrfHeaderName, "invalid_token")
err = validateCSRFToken(r)
if err == nil {
t.Error("validateCSRFToken() did not return error for invalid token")
}
// Test missing cookie
r, _ = http.NewRequest("POST", "/", nil)
r.Header.Set(csrfHeaderName, token)
err = validateCSRFToken(r)
if err == nil {
t.Error("validateCSRFToken() did not return error for missing cookie")
}
// Test missing header
r, _ = http.NewRequest("POST", "/", nil)
r.AddCookie(&http.Cookie{Name: csrfCookieName, Value: token})
err = validateCSRFToken(r)
if err == nil {
t.Error("validateCSRFToken() did not return error for missing header")
}
// Test expired token
r.Header.Set(csrfHeaderName, token)
tokenStoreMu.Lock()
tokenStore[token] = time.Now().Add(-1 * time.Hour)
tokenStoreMu.Unlock()
err = validateCSRFToken(r)
if err == nil {
t.Error("validateCSRFToken() did not return error for expired token")
}
// Test token not in store
delete(tokenStore, token)
err = validateCSRFToken(r)
if err == nil {
t.Error("validateCSRFToken() did not return error for token not in store")
}
}
func TestCSRFMiddleware(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
})
middleware := CSRFMiddleware(handler)
// Test GET request
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
middleware.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("Middleware returned wrong status code for GET: got %v want %v", w.Code, http.StatusOK)
}
token := w.Header().Get(csrfHeaderName)
if token == "" {
t.Error("CSRF token not set in header for GET request")
}
// Get the token from the cookie
var cookieToken string
for _, cookie := range w.Result().Cookies() {
if cookie.Name == csrfCookieName {
cookieToken = cookie.Value
break
}
}
if cookieToken == "" {
t.Error("CSRF token not set in cookie for GET request")
}
// Test POST request with valid token
r, _ = http.NewRequest("POST", "/", strings.NewReader(""))
w = httptest.NewRecorder()
r.Header.Set(csrfHeaderName, cookieToken)
r.AddCookie(&http.Cookie{Name: csrfCookieName, Value: cookieToken})
middleware.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("Middleware returned wrong status code for valid POST: got %v want %v", w.Code, http.StatusOK)
}
// Test POST request with invalid token
r, _ = http.NewRequest("POST", "/", strings.NewReader(""))
w = httptest.NewRecorder()
r.Header.Set(csrfHeaderName, "invalid_token")
r.AddCookie(&http.Cookie{Name: csrfCookieName, Value: cookieToken})
middleware.ServeHTTP(w, r)
if w.Code != http.StatusForbidden {
t.Errorf("Middleware returned wrong status code for invalid POST: got %v want %v", w.Code, http.StatusForbidden)
}
// Test error in setCSRFToken
oldRand := rand.Reader
rand.Reader = strings.NewReader("")
defer func() { rand.Reader = oldRand }()
r, _ = http.NewRequest("GET", "/", nil)
w = httptest.NewRecorder()
middleware.ServeHTTP(w, r)
if w.Code != http.StatusInternalServerError {
t.Errorf("Middleware returned wrong status code for setCSRFToken error: got %v want %v", w.Code, http.StatusInternalServerError)
}
}
// Mock time.Now for testing token expiration
func mockTimeNow(mockTime time.Time) func() {
old := timeNow
timeNow = func() time.Time {
return mockTime
}
return func() {
timeNow = old
}
}
func TestTokenExpiration(t *testing.T) {
originalTimeNow := timeNow
defer func() { timeNow = originalTimeNow }()
// Set current time
now := time.Now()
timeNow = func() time.Time { return now }
// Create a request and set the CSRF token
r, _ := http.NewRequest("GET", "/", nil)
w := httptest.NewRecorder()
token, err := setCSRFToken(w, r)
if err != nil {
t.Errorf("setCSRFToken() error = %v", err)
}
// Prepare a new request for validation
r, _ = http.NewRequest("POST", "/", nil)
r.Header.Set(csrfHeaderName, token)
r.AddCookie(&http.Cookie{Name: csrfCookieName, Value: token})
// Token should be valid initially
err = validateCSRFToken(r)
if err != nil {
t.Errorf("validateCSRFToken() error = %v, token should be valid", err)
}
// Set current time to 11 hours from now
timeNow = func() time.Time { return now.Add(11 * time.Hour) }
// Token should still be valid
err = validateCSRFToken(r)
if err != nil {
t.Errorf("validateCSRFToken() error = %v, token should still be valid", err)
}
// Set current time to 15 hours from now
timeNow = func() time.Time { return now.Add(15 * time.Hour) }
// Token should be expired
err = validateCSRFToken(r)
if err == nil {
t.Error("validateCSRFToken() did not return error for expired token")
}
}
func TestGetCSRFToken(t *testing.T) {
tests := []struct {
name string
context context.Context
expected string
}{
{
name: "Token present in context",
context: context.WithValue(context.Background(), csrfContextKey, "test_token"),
expected: "test_token",
},
{
name: "Token not present in context",
context: context.Background(),
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
r = r.WithContext(tt.context)
token := GetCSRFToken(r)
if token != tt.expected {
t.Errorf("GetCSRFToken() = %v, want %v", token, tt.expected)
}
})
}
}
func TestSetCSRFToken(t *testing.T) {
tests := []struct {
name string
tls *tls.ConnectionState
forwardedProto string
wantSecure bool
}{
{"HTTP request", nil, "", false},
{"HTTPS request", &tls.ConnectionState{}, "", true},
{"HTTP request with X-Forwarded-Proto: https", nil, "https", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
r, _ := http.NewRequest("GET", "/", nil)
r.TLS = tt.tls
if tt.forwardedProto != "" {
r.Header.Set("X-Forwarded-Proto", tt.forwardedProto)
}
w := httptest.NewRecorder()
token, err := setCSRFToken(w, r)
if err != nil {
t.Errorf("setCSRFToken() error = %v", err)
}
cookies := w.Result().Cookies()
var csrfCookie *http.Cookie
for _, cookie := range cookies {
if cookie.Name == csrfCookieName {
csrfCookie = cookie
break
}
}
if csrfCookie == nil {
t.Fatal("CSRF cookie not set")
}
if csrfCookie.Value != token {
t.Errorf("Cookie value (%s) does not match returned token (%s)", csrfCookie.Value, token)
}
if !csrfCookie.HttpOnly {
t.Error("CSRF cookie is not HttpOnly")
}
if csrfCookie.SameSite != http.SameSiteStrictMode {
t.Error("CSRF cookie SameSite is not set to Strict")
}
assert.Equal(t, tt.wantSecure, csrfCookie.Secure, "CSRF cookie Secure flag is %v, want %v", csrfCookie.Secure, tt.wantSecure)
tokenStoreMu.RLock()
expiry, exists := tokenStore[token]
tokenStoreMu.RUnlock()
assert.True(t, exists, "Token not found in token store")
assert.Less(t, time.Now().Add(tokenExpiryTime).Sub(expiry), time.Second)
})
}
}
func TestGenerateCSRFToken(t *testing.T) {
// Test successful token generation
token := generateCSRFToken()
if len(token) == 0 {
t.Error("generateCSRFToken() returned empty token")
}
if len(token) != base64.StdEncoding.EncodedLen(csrfTokenLength) {
t.Errorf("generateCSRFToken() returned token of incorrect length: got %d, want %d", len(token), base64.StdEncoding.EncodedLen(csrfTokenLength))
}
// Test error condition
oldRand := rand.Reader
rand.Reader = strings.NewReader("")
defer func() { rand.Reader = oldRand }()
token = generateCSRFToken()
assert.Equal(t, "", token)
}
func TestInitCSRFLogger(t *testing.T) {
// Create a new logger
logger := slog.New(slog.NewTextHandler(io.Discard, &slog.HandlerOptions{}))
// Initialize the CSRF logger
initCSRFLogger(logger)
// Check if the logger was set correctly
assert.Equal(t, logger, csrfLogger)
// Reset the logger to nil
initCSRFLogger(nil)
// Check if the logger was reset correctly
assert.Nil(t, csrfLogger)
}