-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsrf_test.go
More file actions
265 lines (218 loc) · 7.76 KB
/
csrf_test.go
File metadata and controls
265 lines (218 loc) · 7.76 KB
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
package prefab
import (
"context"
"crypto/rand"
"encoding/hex"
"net/http"
"net/http/httptest"
"strings"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
// Assuming generateCSRFToken and verifyCSRFToken are defined in the package.
func TestGenerateAndVerifyCSRFToken(t *testing.T) {
signingKey := []byte("secret-key")
// Generate a token
token := generateCSRFToken(signingKey)
if token == "" {
t.Fatal("Generated token is empty")
}
// Verify the token
err := verifyCSRFToken(token, signingKey)
if err != nil {
t.Fatalf("Failed to verify the generated token: %v", err)
}
}
func TestVerifyCSRFTokenInvalidFormat(t *testing.T) {
signingKey := []byte("secret-key")
invalidToken := "invalidtokenformat"
err := verifyCSRFToken(invalidToken, signingKey)
if err == nil {
t.Fatal("Expected an error for invalid token format, got nil")
}
st, ok := status.FromError(err)
if !ok || st.Code() != codes.FailedPrecondition {
t.Fatalf("Expected FailedPrecondition error for invalid token format, got: %v", err)
}
}
func TestVerifyCSRFTokenInvalidSignature(t *testing.T) {
signingKey := []byte("secret-key")
// Simulate a token with correct format but invalid signature.
invalidSignature := "ZZZZ_ABCD1234"
err := verifyCSRFToken(invalidSignature, signingKey)
if err == nil {
t.Fatal("Expected an error for invalid signature, got nil")
}
st, ok := status.FromError(err)
if !ok || st.Code() != codes.FailedPrecondition {
t.Fatalf("Expected FailedPrecondition error for invalid data, got: %v", err)
}
}
func TestVerifyCSRFTokenInvalidData(t *testing.T) {
signingKey := []byte("secret-key")
// Simulate a token with correct format but invalid data
invalidData := "ABC123_ZZZZ"
err := verifyCSRFToken(invalidData, signingKey)
if err == nil {
t.Fatal("Expected an error for invalid data, got nil")
}
st, ok := status.FromError(err)
if !ok || st.Code() != codes.FailedPrecondition {
t.Fatalf("Expected FailedPrecondition error for invalid data, got: %v", err)
}
}
func TestVerifyCSRFTokenSignatureMismatch(t *testing.T) {
signingKey := []byte("secret-key")
randomData := make([]byte, 32)
_, _ = rand.Read(randomData) // Ignore error for simplicity in test setup
// Simulate a token with correct format but invalid signature
invalidSignature := hex.EncodeToString([]byte("invalidsignature")) + "_" + hex.EncodeToString(randomData)
err := verifyCSRFToken(invalidSignature, signingKey)
if err == nil {
t.Fatal("Expected an error for signature mismatch, got nil")
}
st, ok := status.FromError(err)
if !ok || st.Code() != codes.FailedPrecondition {
t.Fatalf("Expected FailedPrecondition error for signature mismatch, got: %v", err)
}
}
func TestCsrfMetadataAnnotator(t *testing.T) {
// Define a test CSRF token value
testCsrfToken := "123456789"
// Setup a dummy HTTP request with the CSRF token as a query parameter
req, err := http.NewRequest("GET", "/?csrf-token="+testCsrfToken, nil)
if err != nil {
t.Fatal(err)
}
// Create a ResponseRecorder to act as a dummy ResponseWriter.
rr := httptest.NewRecorder()
// Simulate handling the request, though we're not serving anything real.
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
md := csrfMetadataAnnotator(t.Context(), r)
// Extract the csrf_token value from the metadata
values := md.Get(csrfParam)
if len(values) == 0 {
t.Fatalf("Expected metadata to contain %s, but it was missing", csrfParam)
}
// Check that the value matches our test token
if values[0] != testCsrfToken {
t.Errorf("Expected %s to be %s, got %s", csrfParam, testCsrfToken, values[0])
}
})
// Serve the request to our dummy handler.
handler.ServeHTTP(rr, req)
}
func TestCsrfTokenFromCookie(t *testing.T) {
testToken := "test_csrf_token"
// Mock cookies in the same way that GRPC maps HTTP headers to metadata.
cookie := &http.Cookie{Name: csrfCookie, Value: testToken}
md := metadata.New(map[string]string{"grpcgateway-cookie": cookie.String()})
ctx := metadata.NewIncomingContext(t.Context(), md)
retrievedToken := csrfTokenFromCookie(ctx)
if retrievedToken != testToken {
t.Errorf("Expected CSRF token to be %s, got %s", testToken, retrievedToken)
}
}
func TestCsrfTokenFromCookie_NoCookie(t *testing.T) {
ctx := t.Context() // No cookies embedded
retrievedToken := csrfTokenFromCookie(ctx)
if retrievedToken != "" {
t.Errorf("Expected CSRF token to be empty, got %s", retrievedToken)
}
}
func TestSendCSRFToken(t *testing.T) {
signingKey := []byte("test_signing_key")
mockTransport := &mockServerTransportStream{}
ctx := grpc.NewContextWithServerTransportStream(t.Context(), mockTransport)
generatedToken := SendCSRFToken(ctx, signingKey)
if generatedToken == "" {
t.Errorf("Expected a generated CSRF token, got an empty string")
}
sentCookies := mockTransport.md.Get("grpc-metadata-set-cookie")
if len(sentCookies) == 0 {
t.Fatalf("Expected set-cookie metadata, but it was not found")
}
expectedPrefix := "pf-ct=" + generatedToken + ";"
if !strings.HasPrefix(sentCookies[0], expectedPrefix) {
t.Errorf("Expected cookie to have prefix %s, got %s", expectedPrefix, sentCookies[0])
}
}
func TestVerifyCSRF_EverythingMissing(t *testing.T) {
signingKey := []byte("test_signing_key")
ctx := createContextWithCSRF(t, "", "", "")
if err := VerifyCSRF(ctx, signingKey); err == nil {
t.Error("VerifyCSRF passed when it should have failed")
}
}
func TestVerifyCSRF_MissingCookie(t *testing.T) {
signingKey := []byte("test_signing_key")
ct := generateCSRFToken(signingKey)
ctx := createContextWithCSRF(t, "", ct, "")
if err := VerifyCSRF(ctx, signingKey); err == nil {
t.Error("VerifyCSRF passed when it should have failed")
}
}
func TestVerifyCSRF_CookieParamMismatch(t *testing.T) {
signingKey := []byte("test_signing_key")
ct1 := generateCSRFToken(signingKey)
ct2 := generateCSRFToken([]byte("attacker_key"))
ctx := createContextWithCSRF(t, "", ct1, ct2)
if err := VerifyCSRF(ctx, signingKey); err == nil {
t.Error("VerifyCSRF passed when it should have failed")
}
}
func TestVerifyCSRF_BadKey(t *testing.T) {
ct := generateCSRFToken([]byte("attacker_key"))
ctx := createContextWithCSRF(t, "", ct, ct)
if err := VerifyCSRF(ctx, []byte("real_key")); err == nil {
t.Error("VerifyCSRF passed when it should have failed")
}
}
func TestVerifyCSRF_Success_XHRHeader(t *testing.T) {
signingKey := []byte("test_signing_key")
ctx := createContextWithCSRF(t, "1", "", "")
if err := VerifyCSRF(ctx, signingKey); err != nil {
t.Errorf("VerifyCSRF failed when it should have succeeded: %v", err)
}
}
func TestVerifyCSRF_Success_MatchingParamAndCookie(t *testing.T) {
signingKey := []byte("test_signing_key")
ct := generateCSRFToken(signingKey)
ctx := createContextWithCSRF(t, "", ct, ct)
if err := VerifyCSRF(ctx, signingKey); err != nil {
t.Errorf("VerifyCSRF failed when it should have succeeded: %v", err)
}
}
func createContextWithCSRF(t *testing.T, headerValue, paramValue, cookieValue string) context.Context {
md := metadata.New(map[string]string{})
if headerValue != "" {
md.Set("pf-header-x-csrf-protection", headerValue)
}
if paramValue != "" {
md.Set("csrf-token", paramValue)
}
if cookieValue != "" {
cookie := &http.Cookie{Name: csrfCookie, Value: cookieValue}
md.Set("grpcgateway-cookie", cookie.String())
}
return metadata.NewIncomingContext(t.Context(), md)
}
type mockServerTransportStream struct {
md *metadata.MD
}
func (m *mockServerTransportStream) Method() string {
return "test"
}
func (m *mockServerTransportStream) SetHeader(md metadata.MD) error {
m.md = &md
return nil
}
func (m *mockServerTransportStream) SendHeader(md metadata.MD) error {
panic("Not implemented")
}
func (m *mockServerTransportStream) SetTrailer(md metadata.MD) error {
panic("Not implemented")
}